Index: clang/lib/Parse/ParseCXXInlineMethods.cpp =================================================================== --- clang/lib/Parse/ParseCXXInlineMethods.cpp +++ clang/lib/Parse/ParseCXXInlineMethods.cpp @@ -452,13 +452,14 @@ CXXMethodDecl *Method; if (FunctionTemplateDecl *FunTmpl = dyn_cast(LM.Method)) - Method = cast(FunTmpl->getTemplatedDecl()); + Method = dyn_cast(FunTmpl->getTemplatedDecl()); else - Method = cast(LM.Method); + Method = dyn_cast(LM.Method); - Sema::CXXThisScopeRAII ThisScope(Actions, Method->getParent(), - Method->getMethodQualifiers(), - getLangOpts().CPlusPlus11); + Sema::CXXThisScopeRAII ThisScope( + Actions, Method ? Method->getParent() : nullptr, + Method ? Method->getMethodQualifiers() : Qualifiers{}, + Method && getLangOpts().CPlusPlus11); // Parse the exception-specification. SourceRange SpecificationRange; Index: clang/test/Parser/cxx1z-class-template-argument-deduction.cpp =================================================================== --- clang/test/Parser/cxx1z-class-template-argument-deduction.cpp +++ clang/test/Parser/cxx1z-class-template-argument-deduction.cpp @@ -230,3 +230,21 @@ template struct Z { Y y; }; } + +namespace PR49735 { +// Ensure that we do not crash when parsing code which looks like an invalid +// deduction guide declaration. +template struct B; // expected-note 2{{template is declared here}} +struct A1 { + B() noexcept(false); // expected-error {{deduction guide must be declared in the same scope as template 'PR49735::B'}} \ + // expected-error {{deduction guide declaration without trailing return type}} +}; + +struct A2 { + template // expected-note {{non-deducible template parameter 'Ty'}} + B() noexcept(false); // expected-error {{deduction guide must be declared in the same scope as template 'PR49735::B'}} \ + // expected-error {{deduction guide template contains a template parameter that cannot be deduced}} \ + // expected-error {{deduction guide declaration without trailing return type}} +}; + +}