diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -2293,6 +2293,7 @@ const CXXScopeSpec &SS, QualType T, TagDecl *OwnedTagDecl = nullptr); + QualType getDecltypeForParenthesizedExpr(Expr *E); QualType BuildTypeofExprType(Expr *E, SourceLocation Loc); /// If AsUnevaluated is false, E is treated as though it were an evaluated /// context, such as when building a type for decltype(auto). diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp --- a/clang/lib/Sema/SemaConcept.cpp +++ b/clang/lib/Sema/SemaConcept.cpp @@ -439,18 +439,19 @@ case concepts::ExprRequirement::SS_ConstraintsNotSatisfied: { ConceptSpecializationExpr *ConstraintExpr = Req->getReturnTypeRequirementSubstitutedConstraintExpr(); - if (ConstraintExpr->getTemplateArgsAsWritten()->NumTemplateArgs == 1) + if (ConstraintExpr->getTemplateArgsAsWritten()->NumTemplateArgs == 1) { // A simple case - expr type is the type being constrained and the concept // was not provided arguments. - S.Diag(ConstraintExpr->getBeginLoc(), + Expr *e = Req->getExpr(); + S.Diag(e->getBeginLoc(), diag::note_expr_requirement_constraints_not_satisfied_simple) - << (int)First << S.BuildDecltypeType(Req->getExpr(), - Req->getExpr()->getBeginLoc()) + << (int)First << S.getDecltypeForParenthesizedExpr(e) << ConstraintExpr->getNamedConcept(); - else + } else { S.Diag(ConstraintExpr->getBeginLoc(), diag::note_expr_requirement_constraints_not_satisfied) << (int)First << ConstraintExpr; + } S.DiagnoseUnsatisfiedConstraint(ConstraintExpr->getSatisfaction()); break; } diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -8663,7 +8663,7 @@ TemplateParameterList *TPL = ReturnTypeRequirement.getTypeConstraintTemplateParameterList(); QualType MatchedType = - BuildDecltypeType(E, E->getBeginLoc()).getCanonicalType(); + getDecltypeForParenthesizedExpr(E).getCanonicalType(); llvm::SmallVector Args; Args.push_back(TemplateArgument(MatchedType)); TemplateArgumentList TAL(TemplateArgumentList::OnStack, Args); diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -8830,6 +8830,29 @@ return Context.getTypeOfExprType(E); } +/// getDecltypeForParenthesizedExpr - Given an expr, will return the type for +/// that expression, as in [dcl.type.simple]p4 but without taking id-expressions +/// and class member access into account. +QualType Sema::getDecltypeForParenthesizedExpr(Expr *E) { + // C++11 [dcl.type.simple]p4: + // [...] + QualType T = E->getType(); + switch (E->getValueKind()) { + // - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the + // type of e; + case VK_XValue: + return Context.getRValueReferenceType(T); + // - otherwise, if e is an lvalue, decltype(e) is T&, where T is the + // type of e; + case VK_LValue: + return Context.getLValueReferenceType(T); + // - otherwise, decltype(e) is the type of e. + case VK_RValue: + return T; + } + llvm_unreachable("Unknown value kind"); +} + /// getDecltypeForExpr - Given an expr, will return the decltype for /// that expression, according to the rules in C++11 /// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18. @@ -8894,22 +8917,7 @@ } } - - // C++11 [dcl.type.simple]p4: - // [...] - QualType T = E->getType(); - switch (E->getValueKind()) { - // - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the - // type of e; - case VK_XValue: T = S.Context.getRValueReferenceType(T); break; - // - otherwise, if e is an lvalue, decltype(e) is T&, where T is the - // type of e; - case VK_LValue: T = S.Context.getLValueReferenceType(T); break; - // - otherwise, decltype(e) is the type of e. - case VK_RValue: break; - } - - return T; + return S.getDecltypeForParenthesizedExpr(E); } QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc, diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.req/compound-requirement.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.req/compound-requirement.cpp --- a/clang/test/CXX/expr/expr.prim/expr.prim.req/compound-requirement.cpp +++ b/clang/test/CXX/expr/expr.prim/expr.prim.req/compound-requirement.cpp @@ -79,19 +79,23 @@ template constexpr bool is_same_v = true; +template struct remove_reference { using type = T; }; +template struct remove_reference { using type = T; }; + template concept Same = is_same_v; template -concept Large = sizeof(T) >= 4; // expected-note{{because 'sizeof(short) >= 4' (2 >= 4) evaluated to false}} +concept Large = sizeof(typename remove_reference::type) >= 4; +// expected-note@-1{{because 'sizeof(typename remove_reference::type) >= 4' (2 >= 4) evaluated to false}} -template requires requires (T t) { { t } -> Large; } // expected-note{{because 'decltype(t)' (aka 'short') does not satisfy 'Large':}} +template requires requires (T t) { { t } -> Large; } // expected-note{{because 'short &' does not satisfy 'Large':}} struct r7 {}; using r7i1 = r7; using r7i2 = r7; // expected-error{{constraints not satisfied for class template 'r7' [with T = short]}} -template requires requires (T t) { { t } -> Same; } +template requires requires (T t) { { t } -> Same; } struct r8 {}; using r8i1 = r8; @@ -99,7 +103,8 @@ // Substitution failure in type constraint -template requires requires (T t) { { t } -> Same; } // expected-note{{because 'Same' would be invalid: type 'int' cannot be used prior to '::' because it has no members}} +template requires requires (T t) { { t } -> Same; } +// expected-note@-1{{because 'Same' would be invalid: type 'int' cannot be used prior to '::' because it has no members}} struct r9 {}; struct M { using type = M; }; @@ -122,6 +127,17 @@ template requires requires (T t) { { t } -> IsEven; } // expected-error{{concept named in type constraint is not a type concept}} struct r11 {}; +// Value categories + +template +requires requires (int b) { + { a } -> Same; + { b } -> Same; + { 0 } -> Same; + { static_cast(a) } -> Same; +} void f1() {} +template void f1<>(); + // C++ [expr.prim.req.compound] Example namespace std_example { template concept C1 = @@ -172,4 +188,4 @@ static_assert(C5); template struct C5_check {}; // expected-note{{because 'short' does not satisfy 'C5'}} using c5 = C5_check; // expected-error{{constraints not satisfied for class template 'C5_check' [with T = short]}} -} \ No newline at end of file +}