<div dir="ltr"><div><div style="font-size:12.8000001907349px">I've got a follow-up patch which I think addresses an existing bug that hadn't surfaced.<br></div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px">In the <b>ParseLateTemplatedFuncDef</b> function in <b>lib/Parse/ParseTemplate.cpp</b>, we rebuild the <b>DeclContext</b> starting from the function decl context up to the translation unit context with the following logic:</div></div><div style="font-size:12.8000001907349px"><br></div><div style="font-size:12.8000001907349px"><blockquote style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex" class="gmail_quote">  // Get the list of DeclContexts to reenter.<br>  SmallVector<DeclContext*, 4> DeclContextsToReenter;<br>  DeclContext *DD = FunD;<br>  while (DD && <b>!DD->isTranslationUnit()</b>) {<br>    DeclContextsToReenter.push_back(DD);<br>    DD = DD->getLexicalParent();<br>  }</blockquote></div><div><br></div><div>Then we reenter the sequence of decl contexts in reverse, with <b>PushDeclContext</b>.</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">  // Reenter template scopes from outermost to innermost.<br>  SmallVectorImpl<DeclContext *>::reverse_iterator II =<br>      DeclContextsToReenter.rbegin();<br>  for (; II != DeclContextsToReenter.rend(); ++II) {<br>    TemplateParamScopeStack.push_back(new ParseScope(this,<br>          Scope::TemplateParamScope));<br>    unsigned NumParamLists =<br>      Actions.ActOnReenterTemplateScope(getCurScope(), cast<Decl>(*II));<br>    CurTemplateDepthTracker.addDepth(NumParamLists);<br>    if (*II != FunD) {<br>      TemplateParamScopeStack.push_back(new ParseScope(this, Scope::DeclScope));<br>      <b>Actions.PushDeclContext(Actions.getCurScope(), *II);<br></b>    }<br>  }</blockquote></div><div><br></div><div>This means that <b>Actions</b>'<b> CurContext</b> must be the translation unit context, otherwise the assertion in <b>PushDeclContext</b> fails. I think the intention of the following <b>ContextRAII</b> was to satisfy this pre-condition, but instead we save the <b>CurContext</b> and stay in the <b>CurContext</b>.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">// To restore the context after late parsing.<br>Sema::ContextRAII GlobalSavedContext(Actions, Actions.CurContext);</blockquote><div><br></div><div>The fix is to save the <b>CurContext</b> then jump to the translation unit context in order to satisfy the pre-conditions necessary to rebuild and reenter the decl contexts.</div><div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">// To restore the context after late parsing.<br>Sema::ContextRAII GlobalSavedContext( </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">    Actions, Actions.Context.getTranslationUnitDecl());</blockquote></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 1 May 2015 at 01:12, Michael Park <span dir="ltr"><<a href="mailto:mcypark@gmail.com" target="_blank">mcypark@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I've got a follow-up patch which I think addresses an existing bug that hadn't surfaced.<br></div><div><br></div><div>In the <b>ParseLateTemplatedFuncDef</b> function in <b>lib/Parse/ParseTemplate.cpp</b>, we build up the sequence of DeclContext<br></div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On 29 April 2015 at 11:20, Michael Park <span dir="ltr"><<a href="mailto:mcypark@gmail.com" target="_blank">mcypark@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Ah, that's unfortunate. I'll take a look. Thanks!</div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On 28 April 2015 at 20:33, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>One of our buildbots seems unhappy:</div><div><br></div><a href="http://bb.pgr.jp/builders/ninja-x64-msvc-RA-centos6/builds/11939/steps/test_all/logs/Clang-Unit%20%3A%3A%20ASTMatchers__ASTMatchersTests__HasAncestor.MatchesClosestAncestor" target="_blank">http://bb.pgr.jp/builders/ninja-x64-msvc-RA-centos6/builds/11939/steps/test_all/logs/Clang-Unit%20%3A%3A%20ASTMatchers__ASTMatchersTests__HasAncestor.MatchesClosestAncestor</a><div><br></div><div>Its testcase is this:</div><div><br></div><div><div>      template <typename T> struct C {</div><div>        void f(int) {</div><div>          struct I { void g(T) { int x; } } i; i.g(42);</div><div>        }</div><div>      };</div><div>      template struct C<int>;</div><div><br></div><div>It looks like the problem is some interaction of your patch and delayed template parsing (running the above code through clang with -fdelayed-template-parsing reproduces the issue). Can you take a look?</div><div><div><div><br></div><div class="gmail_extra"><div class="gmail_quote">On Tue, Apr 28, 2015 at 5:23 PM, Michael Park <span dir="ltr"><<a href="mailto:mcypark@gmail.com" target="_blank">mcypark@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Awesome, Thanks Richard!</div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On 28 April 2015 at 20:11, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div>On Tue, Apr 28, 2015 at 4:00 PM, Michael Park <span dir="ltr"><<a href="mailto:mcypark@gmail.com" target="_blank">mcypark@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><div>The answer is that we don't really know. It would be more consistent with the non-template case for it to be ill-formed (it also seems reasonable to say that you can't instantiate a member function until its class is complete), but the point of instantiation rules for constexpr functions aren't settled yet; this is part of DR1581 (still open).</div></span></div></div></div></blockquote><div><br></div></span><div>Agreed. Thanks for the pointer to DR1581!</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">LGTM</blockquote><div><br></div><div>As this is my first patch for Clang, I'm not all that familiar with the required process to get this patch committed. Am I required to do anything further on my end?</div></div></div></div></blockquote><div><br></div></div></div><div>I've committed your patch as r236063. Feel free to go ahead and mark PR20625 as fixed. Thanks!</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Thanks,</div><div><br></div><div>MPark.</div></div></div></div>
</blockquote></div><br></div></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div></div></div></div></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>