<div class="gmail_quote">On Thu, Jun 7, 2012 at 3:47 PM, Douglas Gregor <span dir="ltr"><<a href="mailto:dgregor@apple.com" target="_blank">dgregor@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5"><br>
On May 18, 2012, at 4:42 PM, Kaelyn Uhrain wrote:<br>
<br>
> Author: rikka<br>
> Date: Fri May 18 18:42:49 2012<br>
> New Revision: 157085<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=157085&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=157085&view=rev</a><br>
> Log:<br>
> Suggest adding 'typename' when it would make the compiler<br>
> accept the template argument expression as a type.<br>
><br>
> Modified:<br>
>    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
>    cfe/trunk/lib/Sema/SemaTemplate.cpp<br>
>    cfe/trunk/test/SemaTemplate/typename-specifier.cpp<br>
><br>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=157085&r1=157084&r2=157085&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=157085&r1=157084&r2=157085&view=diff</a><br>

> ==============================================================================<br>
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)<br>
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri May 18 18:42:49 2012<br>
> @@ -2361,6 +2361,8 @@<br>
> def note_member_of_template_here : Note<"member is declared here">;<br>
> def err_template_arg_must_be_type : Error<<br>
>   "template argument for template type parameter must be a type">;<br>
> +def err_template_arg_must_be_type_suggest : Error<<br>
> +  "template argument for template type parameter must be a type; did you forget 'typename'?">;<br>
> def err_template_arg_must_be_expr : Error<<br>
>   "template argument for non-type template parameter must be an expression">;<br>
> def err_template_arg_nontype_ambig : Error<<br>
><br>
> Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=157085&r1=157084&r2=157085&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=157085&r1=157084&r2=157085&view=diff</a><br>

> ==============================================================================<br>
> --- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)<br>
> +++ cfe/trunk/lib/Sema/SemaTemplate.cpp Fri May 18 18:42:49 2012<br>
> @@ -2438,6 +2438,45 @@<br>
><br>
>     return true;<br>
>   }<br>
> +  case TemplateArgument::Expression: {<br>
> +    // We have a template type parameter but the template argument is an<br>
> +    // expression; see if maybe it is missing the "typename" keyword.<br>
> +    CXXScopeSpec SS;<br>
> +    DeclarationNameInfo NameInfo;<br>
> +<br>
> +    if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) {<br>
> +      SS.Adopt(ArgExpr->getQualifierLoc());<br>
> +      NameInfo = ArgExpr->getNameInfo();<br>
<br>
</div></div>Shouldn't we check that there is a nested-name-specifier here?<br></blockquote><div><br></div><div>As far as I'm aware, there is no need. The code below is not dependent on the CXXScopeSpec being valid, and if there isn't a nested-name-specifier then the CXXScopeSpec will be invalid after adopting an empty NestedNameSpecifierLoc. Plus, from what I can tell, any checks for whether the expression needs to have a nested-name-specifier are handled elsewhere (IIRC they're triggered before this code is reached).</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">> +    } else if (DependentScopeDeclRefExpr *ArgExpr =<br>
> +               dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) {<br>
> +      SS.Adopt(ArgExpr->getQualifierLoc());<br>
> +      NameInfo = ArgExpr->getNameInfo();<br>
> +    } else if (CXXDependentScopeMemberExpr *ArgExpr =<br>
> +               dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) {<br>
> +      SS.Adopt(ArgExpr->getQualifierLoc());<br>
> +      NameInfo = ArgExpr->getMemberNameInfo();<br>
<br>
</div>This should check that the base of the dependent-scoped member access expression is actually the implicit 'this'. We currently end up inserting 'typename' where we shouldn't:<br>
<br>
test/SemaTemplate/typename-specifier.cpp:139:10: error: template argument for<br>
<div class="im">      template type parameter must be a type; did you forget 'typename'?<br>
</div>    pair<this->ExampleItemSet::iterator, int> i; // expected-error...<br>
<div class="im">         ^<br></div></blockquote><div><br></div><div>Good point; fixed. The "typename" stuff delves into an area of C++ templates I've never used, and adding the suggestion turned out to be more challenging than I'd expected--it certainly required a lot more knowledge of template syntax and of how Clang deals with them than I would have guessed from the relatively straight-forward and self-contained error and recovery handling I'd encountered working on typo correction (emphasis on "relatively").</div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">> +    }<br>
> +<br>
> +    if (NameInfo.getName()) {<br>
<br>
</div>This should be limited to identifiers, because we don't want to correct here:<br>
<br>
test/SemaTemplate/typename-specifier.cpp:139:10: error: template argument for<br>
<div class="im">      template type parameter must be a type; did you forget 'typename'?<br>
</div>    pair<ExampleItemSet::operator[], int> i;<br>
<div class="im">         ^<br></div></blockquote><div><br></div><div>Also fixed.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">> +      LookupResult Result(*this, NameInfo, LookupOrdinaryName);<br>

> +      LookupParsedName(Result, CurScope, &SS);<br>
> +<br>
> +      bool CouldBeType = Result.getResultKind() ==<br>
> +          LookupResult::NotFoundInCurrentInstantiation;<br>
> +<br>
> +      for (LookupResult::iterator I = Result.begin(), IEnd = Result.end();<br>
> +           !CouldBeType && I != IEnd; ++I) {<br>
> +        CouldBeType = isa<TypeDecl>(*I);<br>
> +      }<br>
<br>
</div>I don't understand why we have this loop here. If the name lookup is able to find a type, we wouldn't be here in this code. It seems like the only interesting case is the NotFoundInCurrentInstantiation case.<br>
</blockquote><div><br></div><div>D'oh. It was left over from when I was trying to figure out how to look up whether an identifier might be a type and even how to get the nested-name-specifier in a form that I could use for the lookups. IIRC at least one of the avenues I'd tried needed the loop to handle lookups that returned multiple decls.</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im"><br>
> +      if (CouldBeType) {<br>
> +        SourceLocation Loc = AL.getSourceRange().getBegin();<br>
> +        Diag(Loc, diag::err_template_arg_must_be_type_suggest);<br>
> +        Diag(Param->getLocation(), diag::note_template_param_here);<br>
> +        return true;<br>
> +      }<br>
<br>
</div>Did you mean for this to be a Fix-It? If so, it needs to recover by changing the template argument itself. If you're not planning on doing that work, it's fine, but please leave a FIXME to say that we'd like to do that work.<br>
</blockquote><div><br></div><div>I've added a FIXME since a Fix-It was beyond what I had able to figure out while muddling through all of the template code...</div><div><br></div><div>The changes mentioned above along with a couple more tests have been committed as r158185.</div>
<div><br></div><div>Thanks!</div><div>Kaelyn</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
        - Doug<br>
<div class="HOEnZb"><div class="h5"><br>
> +    }<br>
> +    // fallthrough<br>
> +  }<br>
>   default: {<br>
>     // We have a template type parameter but the template argument<br>
>     // is not a type.<br>
><br>
> Modified: cfe/trunk/test/SemaTemplate/typename-specifier.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/typename-specifier.cpp?rev=157085&r1=157084&r2=157085&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/typename-specifier.cpp?rev=157085&r1=157084&r2=157085&view=diff</a><br>

> ==============================================================================<br>
> --- cfe/trunk/test/SemaTemplate/typename-specifier.cpp (original)<br>
> +++ cfe/trunk/test/SemaTemplate/typename-specifier.cpp Fri May 18 18:42:49 2012<br>
> @@ -115,3 +115,37 @@<br>
>     using typename BasicGeometry<mydim, int>::operator[]; // expected-error {{typename is allowed for identifiers only}}<br>
>   };<br>
> }<br>
> +<br>
> +<br>
> +namespace missing_typename {<br>
> +template <class T1, class T2> struct pair {}; // expected-note 5 {{template parameter is declared here}}<br>
> +<br>
> +template <class T1, class T2><br>
> +struct map {<br>
> +  typedef T1* iterator;<br>
> +};<br>
> +<br>
> +template <class T><br>
> +class ExampleClass1 {<br>
> +  struct ExampleItem;<br>
> +<br>
> +<br>
> +  struct ExampleItemSet {<br>
> +    typedef ExampleItem* iterator;<br>
> +  };<br>
> +<br>
> +  void foo() {<br>
> +    pair<ExampleItemSet::iterator, int> i; // expected-error {{template argument for template type parameter must be a type; did you forget 'typename'?}}<br>
> +  }<br>
> +  pair<ExampleItemSet::iterator, int> elt; // expected-error {{template argument for template type parameter must be a type; did you forget 'typename'?}}<br>
> +<br>
> +<br>
> +  typedef map<int, ExampleItem*> ExampleItemMap;<br>
> +<br>
> +  static void bar() {<br>
> +    pair<ExampleItemMap::iterator, int> i; // expected-error {{template argument for template type parameter must be a type; did you forget 'typename'?}}<br>
> +  }<br>
> +  pair<ExampleItemMap::iterator, int> entry; // expected-error {{template argument for template type parameter must be a type; did you forget 'typename'?}}<br>
> +  pair<bar, int> foobar; // expected-error {{template argument for template type parameter must be a type}}<br>
> +};<br>
> +} // namespace missing_typename<br>
><br>
><br>
> _______________________________________________<br>
> cfe-commits mailing list<br>
> <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
<br>
</div></div></blockquote></div><br>