llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.48k stars 11.77k forks source link

CTAD: Don't reuse the template parameters of the class in the synthesized deduction guide. #102281

Open hokein opened 2 months ago

hokein commented 2 months ago

https://godbolt.org/z/ea8vzaoeP

template <typename T1> struct B {
  B(T1);
};

B b(1);

The AST is:

|-ClassTemplateDecl 0xd400cf0 <<source>:1:1, line:3:1> line:1:31 B
| |-TemplateTypeParmDecl 0xd400b68 <col:11, col:20> col:20 referenced typename depth 0 index 0 T1
...

-FunctionTemplateDecl 0xd401680 <line:1:1, line:2:7> col:3 implicit <deduction guide for B>
| |-TemplateTypeParmDecl 0xd400b68 <line:1:11, col:20> col:20 referenced typename depth 0 index 0 T1
| |

Note that the TemplateTypeParmDecl is the same in the class template decl and deduction guide. We seem to take a shortcurt and reuse the TemplateTypeParmDecl when synthesizing the deduction guide. Instead, we should clone one to make distinct template parameters for the deduction guide.

llvmbot commented 2 months ago

@llvm/issue-subscribers-clang-frontend

Author: Haojian Wu (hokein)

https://godbolt.org/z/ea8vzaoeP ``` template <typename T1> struct B { B(T1); }; B b(1); ``` The AST is: ``` |-ClassTemplateDecl 0xd400cf0 <<source>:1:1, line:3:1> line:1:31 B | |-TemplateTypeParmDecl 0xd400b68 <col:11, col:20> col:20 referenced typename depth 0 index 0 T1 ... -FunctionTemplateDecl 0xd401680 <line:1:1, line:2:7> col:3 implicit <deduction guide for B> | |-TemplateTypeParmDecl 0xd400b68 <line:1:11, col:20> col:20 referenced typename depth 0 index 0 T1 | | ``` Note that the `TemplateTypeParmDecl` is the same in the class template decl and deduction guide. We seem to take a shortcurt and reuse the `TemplateTypeParmDecl` when synthesizing the deduction guide. Instead, we should clone one to make distinct template parameters for the deduction guide.