llvm-project
1cb47c19 - [clang] Remove written template args from implicit var tpl spec (#156329)

Commit
64 days ago
[clang] Remove written template args from implicit var tpl spec (#156329) `VarTemplateSpecializationDecl::getTemplateArgsAsWritten()` function should return `nullptr` in the case of implicit instantiation, as its `ClassTemplateSpecializationDecl` counterpart does, and not the arguments written in `DeclRefExpr` referencing the specialization in the first place. Otherwise, for such code: ```cpp template <typename> int VarTpl; template <typename T> void tplFn() { (void)VarTpl<T>; // (1) } void fn() { tplFn<char>(); } ``` Clang treats the `char` argument of the `VarTpl` specialization as if it were written in the line marked as (1), which is misleading and hardly makes sense. Moreover, "template args as written" are stored inside `ExplicitInfo` field of `VarTemplateSpecializationDecl`, but it is [documented](https://github.com/llvm/llvm-project/blob/13357e8a12c1a45364a0c4d3137b6d21ee6ac40c/clang/include/clang/AST/DeclTemplate.h#L2653) that it is not for implicit instantiations. Moreover, it is assumed in `TraverseVarTemplateSpecializationDecl` method of `RecursiveASTVisitor` that `getTemplateArgsAsWritten()` returns `nullptr` for implicit instantiations, as it is stated in the comment [there](https://github.com/llvm/llvm-project/blob/13357e8a12c1a45364a0c4d3137b6d21ee6ac40c/clang/include/clang/AST/RecursiveASTVisitor.h#L2196). That said, `setTemplateArgsAsWritten` should be called only for variable template explicit specializations (it is [already done inside `Sema::ActOnVarTemplateSpecialization`](https://github.com/llvm/llvm-project/blob/4c916273041ff5ed7b2af20bec787ffc42871c9f/clang/lib/Sema/SemaTemplate.cpp#L4459)) and explicit instantiations (hence `true` is passed to the new `SetWrittenArgs` parameter of `CheckVarTemplateId` function inside `Sema::ActOnExplicitInstantiation`, but not when handling expressions referencing a variable template specialization). `InstantiateVariableDefinition` function just passes the arguments from the corresponding declaration. I'm not sure about instantiating a class template containing a variable template explicit specialization and thus have tried to leave the logic of the first overload of `TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl` as it was.
Author
Parents
Loading