Skip to content

Commit

Permalink
[Sema] setInvalidDecl for error deduction declaration
Browse files Browse the repository at this point in the history
Fixed: #62408
`setInvalidDecl` for invalid `CXXDeductionGuideDecl` to
avoid crashes during semantic analysis.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D149516
  • Loading branch information
HerrCai0907 committed May 23, 2023
1 parent 4edaacf commit ca96836
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 25 deletions.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ Bug Fixes in This Version
- Fix crash when attempting to perform parenthesized initialization of an
aggregate with a base class with only non-public constructors.
(`#62296 <https://github.com/llvm/llvm-project/issues/62296>`_)
- Fix crash when handling initialization candidates for invalid deduction guide.
(`#62408 <https://github.com/llvm/llvm-project/issues/62408>`_)
- Fix crash when redefining a variable with an invalid type again with an
invalid type. (`#62447 <https://github.com/llvm/llvm-project/issues/62447>`_)
- Fix a stack overflow issue when evaluating ``consteval`` default arguments.
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -7790,7 +7790,7 @@ class Sema final {
void CheckConversionDeclarator(Declarator &D, QualType &R,
StorageClass& SC);
Decl *ActOnConversionDeclarator(CXXConversionDecl *Conversion);
void CheckDeductionGuideDeclarator(Declarator &D, QualType &R,
bool CheckDeductionGuideDeclarator(Declarator &D, QualType &R,
StorageClass &SC);
void CheckDeductionGuideTemplate(FunctionTemplateDecl *TD);

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9199,8 +9199,8 @@ static FunctionDecl *CreateNewFunctionDecl(Sema &SemaRef, Declarator &D,
SemaRef.Diag(TrailingRequiresClause->getBeginLoc(),
diag::err_trailing_requires_clause_on_deduction_guide)
<< TrailingRequiresClause->getSourceRange();
SemaRef.CheckDeductionGuideDeclarator(D, R, SC);

if (SemaRef.CheckDeductionGuideDeclarator(D, R, SC))
return nullptr;
return CXXDeductionGuideDecl::Create(SemaRef.Context, DC, D.getBeginLoc(),
ExplicitSpecifier, NameInfo, R, TInfo,
D.getEndLoc());
Expand Down
29 changes: 14 additions & 15 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11087,8 +11087,8 @@ struct BadSpecifierDiagnoser {
/// Check the validity of a declarator that we parsed for a deduction-guide.
/// These aren't actually declarators in the grammar, so we need to check that
/// the user didn't specify any pieces that are not part of the deduction-guide
/// grammar.
void Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R,
/// grammar. Return true on invalid deduction-guide.
bool Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R,
StorageClass &SC) {
TemplateName GuidedTemplate = D.getName().TemplateName.get().get();
TemplateDecl *GuidedTemplateDecl = GuidedTemplate.getAsTemplateDecl();
Expand Down Expand Up @@ -11138,7 +11138,7 @@ void Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R,
}

if (D.isInvalidType())
return;
return true;

// Check the declarator is simple enough.
bool FoundFunction = false;
Expand All @@ -11151,11 +11151,9 @@ void Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R,
<< D.getSourceRange();
break;
}
if (!Chunk.Fun.hasTrailingReturnType()) {
Diag(D.getName().getBeginLoc(),
diag::err_deduction_guide_no_trailing_return_type);
break;
}
if (!Chunk.Fun.hasTrailingReturnType())
return Diag(D.getName().getBeginLoc(),
diag::err_deduction_guide_no_trailing_return_type);

// Check that the return type is written as a specialization of
// the template specified as the deduction-guide's name.
Expand Down Expand Up @@ -11190,21 +11188,22 @@ void Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R,
MightInstantiateToSpecialization = true;
}

if (!AcceptableReturnType) {
Diag(TSI->getTypeLoc().getBeginLoc(),
diag::err_deduction_guide_bad_trailing_return_type)
<< GuidedTemplate << TSI->getType()
<< MightInstantiateToSpecialization
<< TSI->getTypeLoc().getSourceRange();
}
if (!AcceptableReturnType)
return Diag(TSI->getTypeLoc().getBeginLoc(),
diag::err_deduction_guide_bad_trailing_return_type)
<< GuidedTemplate << TSI->getType()
<< MightInstantiateToSpecialization
<< TSI->getTypeLoc().getSourceRange();

// Keep going to check that we don't have any inner declarator pieces (we
// could still have a function returning a pointer to a function).
FoundFunction = true;
}

if (D.isFunctionDefinition())
// we can still create a valid deduction guide here.
Diag(D.getIdentifierLoc(), diag::err_deduction_guide_defines_function);
return false;
}

//===----------------------------------------------------------------------===//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ template <class a, class e>
concept g = f<a, e>::h;
template <class a, class e>
concept i = g<e, a>;
template <typename> class j { // expected-note {{candidate template ignored}}
template <typename> class j {
template <typename k>
requires requires { requires i<j, k>; }
j(); // expected-note {{candidate template ignored}}
j();
};
template <> j(); // expected-error {{deduction guide declaration without trailing return type}} // expected-error {{no function template}}
template <> j(); // expected-error {{deduction guide declaration without trailing return type}}
}
3 changes: 1 addition & 2 deletions clang/test/CXX/temp/temp.res/temp.local/p3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ namespace PR6717 {

WebVector(const WebVector<T>& other) { } // expected-error{{undeclared identifier 'T'}} \
precxx17-error{{a type specifier is required}} \
cxx17-error{{deduction guide declaration without trailing return type}} \
cxx17-error{{deduction guide cannot have a function definition}}
cxx17-error{{deduction guide declaration without trailing return type}}

template <typename C>
WebVector<T>& operator=(const C& other) { } // expected-error{{undeclared identifier 'T'}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,8 @@ struct A1 {
};

struct A2 {
template <typename Ty> // expected-note {{non-deducible template parameter 'Ty'}}
template <typename 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}}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s

template <class T> class Foo {}; // expected-note {{candidate template ignored: couldn't infer template argument 'T'}} \
// expected-note {{candidate function template not viable: requires 1 argument, but 0 were provided}}
Foo(); // expected-error {{deduction guide declaration without trailing return type}}
Foo vs; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'Foo'}}

0 comments on commit ca96836

Please sign in to comment.