diff --git a/clang/include/clang/AST/ExprConcepts.h b/clang/include/clang/AST/ExprConcepts.h --- a/clang/include/clang/AST/ExprConcepts.h +++ b/clang/include/clang/AST/ExprConcepts.h @@ -423,12 +423,13 @@ } NestedRequirement(ASTContext &C, Expr *Constraint, - const ConstraintSatisfaction &Satisfaction) : - Requirement(RK_Nested, Constraint->isInstantiationDependent(), - Constraint->containsUnexpandedParameterPack(), - Satisfaction.IsSatisfied), - Value(Constraint), - Satisfaction(ASTConstraintSatisfaction::Create(C, Satisfaction)) {} + const ConstraintSatisfaction &Satisfaction) + : Requirement(RK_Nested, + Constraint && Constraint->isInstantiationDependent(), + Constraint && Constraint->containsUnexpandedParameterPack(), + Satisfaction.IsSatisfied), + Value(Constraint), + Satisfaction(ASTConstraintSatisfaction::Create(C, Satisfaction)) {} bool isSubstitutionFailure() const { return Value.is(); 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 @@ -161,7 +161,7 @@ return ExprError(); bool IsLHSSatisfied = Satisfaction.IsSatisfied; - + if (BO.isOr() && IsLHSSatisfied) // [temp.constr.op] p3 // A disjunction is a constraint taking two operands. To determine if @@ -286,7 +286,8 @@ // bool if this is the operand of an '&&' or '||'. For example, we // might lose an lvalue-to-rvalue conversion here. If so, put it back // before we try to evaluate. - if (!SubstitutedExpression.isInvalid()) + if (SubstitutedExpression.isUsable() && + !SubstitutedExpression.isInvalid()) SubstitutedExpression = S.PerformContextuallyConvertToBool(SubstitutedExpression.get()); if (SubstitutedExpression.isInvalid() || Trap.hasErrorOccurred()) { @@ -316,7 +317,7 @@ Satisfaction.Details.emplace_back( AtomicExpr, new (S.Context) ConstraintSatisfaction::SubstitutionDiagnostic{ - SubstDiag.first, StringRef(Mem, MessageSize)}); + SubstDiag.first, StringRef(Mem, MessageSize)}); Satisfaction.IsSatisfied = false; return ExprEmpty(); } diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -2328,8 +2328,12 @@ Req->getConstraintExpr()->getSourceRange()); if (ConstrInst.isInvalid()) return nullptr; - TransConstraint = TransformExpr(Req->getConstraintExpr()); - if (!TransConstraint.isInvalid()) { + llvm::SmallVector Result; + if (SemaRef.CheckConstraintSatisfaction( + nullptr, {Req->getConstraintExpr()}, Result, TemplateArgs, + Req->getConstraintExpr()->getSourceRange(), Satisfaction)) + TransConstraint = Result[0]; + if (TransConstraint.isUsable() && !TransConstraint.isInvalid()) { bool CheckSucceeded = SemaRef.CheckConstraintExpression(TransConstraint.get()); (void)CheckSucceeded; @@ -2338,7 +2342,7 @@ "did not produce a SFINAE error"); } // Use version of CheckConstraintSatisfaction that does no substitutions. - if (!TransConstraint.isInvalid() && + if (TransConstraint.isUsable() && !TransConstraint.isInvalid() && !TransConstraint.get()->isInstantiationDependent() && !Trap.hasErrorOccurred()) { bool CheckFailed = SemaRef.CheckConstraintSatisfaction( @@ -2355,7 +2359,8 @@ SemaRef.getPrintingPolicy()); })); } - if (TransConstraint.get()->isInstantiationDependent()) + if (TransConstraint.isUsable() && + TransConstraint.get()->isInstantiationDependent()) return new (SemaRef.Context) concepts::NestedRequirement(TransConstraint.get()); return new (SemaRef.Context) concepts::NestedRequirement( diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp --- a/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp +++ b/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp @@ -19,7 +19,8 @@ template struct X { - template requires requires (U u) { requires sizeof(u) == sizeof(T); } // expected-note{{because 'sizeof (u) == sizeof(T)' would be invalid: invalid application of 'sizeof' to an incomplete type 'void'}} + template requires requires (U u) { requires sizeof(u) == sizeof(T); } + // expected-note@-1 {{because substituted constraint expression is ill-formed: invalid application of 'sizeof' to an incomplete type 'void'}} struct r4 {}; }; @@ -41,7 +42,7 @@ template concept C2 = requires (T a) { requires sizeof(a) == 4; // OK - requires a == 0; // expected-note{{because 'a == 0' would be invalid: constraint variable 'a' cannot be used in an evaluated context}} + requires a == 0; // expected-note{{because substituted constraint expression is ill-formed: constraint variable 'a' cannot be used in an evaluated context}} }; static_assert(C2); // expected-note{{because 'int' does not satisfy 'C2'}} expected-error{{static assertion failed}} } @@ -51,3 +52,90 @@ X.next(); }; +namespace SubstitutionFailureNestedRequires { +template concept True = true; + +struct S { double value; }; + +template +concept Pipes = requires (T x) { + requires True || True; + requires True || True; +}; + +template +concept Amps1 = requires (T x) { + requires True && True; + // expected-note@-1{{because substituted constraint expression is ill-formed: member reference base type 'int' is not a structure or union}} +}; +template +concept Amps2 = requires (T x) { + requires True && True; +}; + +static_assert(Pipes); +static_assert(Pipes); + +static_assert(Amps1); +static_assert(!Amps1); + +static_assert(Amps2); +static_assert(!Amps2); + +template +void foo1() requires requires (T x) { // expected-note {{candidate template ignored: constraints not satisfied [with T = int]}} + requires + True // expected-note {{because substituted constraint expression is ill-formed: member reference base type 'int' is not a structure or union}} + && True; +} {} +template void fooPipes() requires Pipes {} +template void fooAmps1() requires Amps1 {} +// expected-note@-1 {{candidate template ignored: constraints not satisfied [with T = int]}} \ +// expected-note@-1 {{because 'int' does not satisfy 'Amps1'}} + +void foo() { + foo1(); + foo1(); // expected-error {{no matching function for call to 'foo1'}} + fooPipes(); + fooPipes(); + fooAmps1(); + fooAmps1(); // expected-error {{no matching function for call to 'fooAmps1'}} +} + +template +concept HasNoValue = requires (T x) { + requires !True && True; +}; +// FIXME: 'int' does not satisfy 'HasNoValue' currently since `!True` is an invalid expression. +// But, in principle, it should be constant-evaluated to true. +static_assert(!HasNoValue); +static_assert(!HasNoValue); + +template constexpr bool NotAConceptTrue = true; +template +concept SFinNestedRequires = requires (T x) { + // SF in a non-concept specialisation should also be evaluated to false. + requires NotAConceptTrue || NotAConceptTrue; +}; +static_assert(SFinNestedRequires); +static_assert(SFinNestedRequires); +template +void foo() requires SFinNestedRequires {} +void bar() { + foo(); + foo(); +} +namespace SFINAEWithoutConcept { + //Add more TODO(usx). +template struct X { static constexpr bool value = T::value; }; +struct True { static constexpr bool value = true; }; +template concept C = true; + +template requires requires(T) { requires C || X::value; } void foo(); + +void func() { + foo(); + foo(); +} +} +} \ No newline at end of file diff --git a/clang/test/SemaTemplate/instantiate-requires-expr.cpp b/clang/test/SemaTemplate/instantiate-requires-expr.cpp --- a/clang/test/SemaTemplate/instantiate-requires-expr.cpp +++ b/clang/test/SemaTemplate/instantiate-requires-expr.cpp @@ -24,8 +24,8 @@ template requires false_v; }> -// expected-note@-1 {{because 'false_v; }>' evaluated to false}} -// expected-note@-2 {{because 'false_v; }>' evaluated to false}} +// expected-note@-1 {{because 'false_v; }>' evaluated to false}} +// expected-note@-2 {{because 'false_v; }>' evaluated to false}} struct r1 {}; using r1i1 = r1; // expected-error {{constraints not satisfied for class template 'r1' [with T = int]}} @@ -35,8 +35,8 @@ template requires false_v -// expected-note@-1 {{because 'false_v'}} -// expected-note@-2 {{because 'false_v' evaluated to false}} +// expected-note@-1 {{because 'false_v; }>' evaluated to false}} +// expected-note@-2 {{because 'false_v; }>' evaluated to false}} struct r2 {}; using r2i1 = r2; // expected-error {{constraints not satisfied for class template 'r2' [with Ts = ]}} @@ -44,8 +44,8 @@ template requires false_v<(requires (Ts ts) {requires sizeof(ts) != 0;} && ...)> -// expected-note@-1 {{because 'false_v' evaluated to false}} -// expected-note@-2 {{because 'false_v' evaluated to false}} +// expected-note@-1 {{because 'false_v; } && requires (unsigned short ts) { requires ; }>' evaluated to false}} +// expected-note@-2 {{because 'false_v; }>' evaluated to false}} struct r3 {}; using r3i1 = r3; // expected-error {{constraints not satisfied for class template 'r3' [with Ts = ]}} @@ -181,7 +181,7 @@ namespace nested_requirement { // check that constraint expression is instantiated correctly - template requires false_v // expected-note{{because 'false_v' evaluated to false}} + template requires false_v // expected-note{{because 'false_v; }>' evaluated to false}} struct r1 {}; using r1i = r1; // expected-error{{constraints not satisfied for class template 'r1' [with T = int]}} @@ -190,7 +190,7 @@ template struct a { template requires - (requires { requires sizeof(T::a) == 0; }, false) // expected-note{{because 'requires { requires <>; } , false' evaluated to false}} + (requires { requires sizeof(T::a) == 0; }, false) // expected-note{{because 'requires { requires ; } , false' evaluated to false}} struct r {}; }; @@ -199,7 +199,7 @@ // Parameter pack inside expr template requires false_v<(requires { requires sizeof(Ts) == 0; } && ...)> - // expected-note@-1 {{because 'false_v' evaluated to false}} + // expected-note@-1 {{because 'false_v; } && requires { requires ; }>' evaluated to false}} struct r2 {}; using r2i = r2; // expected-error{{constraints not satisfied for class template 'r2' [with Ts = ]}} @@ -208,14 +208,14 @@ // Parameter pack inside multiple requirements template requires false_v<(requires { requires sizeof(Ts) == 0; sizeof(Ts); } && ...)> -// expected-note@-1 {{because 'false_v' evaluated to false}} +// expected-note@-1 {{because 'false_v; sizeof(Ts); } && requires { requires ; sizeof(Ts); }>' evaluated to false}} struct r4 {}; using r4i = r4; // expected-error{{constraints not satisfied for class template 'r4' [with Ts = ]}} template requires false_v<(requires(Ts t) { requires sizeof(t) == 0; t++; } && ...)> -// expected-note@-1 {{because 'false_v' evaluated to false}} +// expected-note@-1 {{because 'false_v; t++; } && requires (short t) { requires ; t++; }>' evaluated to false}} struct r5 {}; using r5i = r5; // expected-error{{constraints not satisfied for class template 'r5' [with Ts = ]}}