usaxena95 updated this revision to Diff 478658. usaxena95 marked an inline comment as done. usaxena95 added a comment.
Added tests. Fixed regressions in diagnositcs. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D138914/new/ https://reviews.llvm.org/D138914 Files: clang/include/clang/AST/ExprConcepts.h clang/lib/Sema/SemaConcept.cpp clang/lib/Sema/SemaTemplateInstantiate.cpp clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp clang/test/SemaTemplate/instantiate-requires-expr.cpp
Index: clang/test/SemaTemplate/instantiate-requires-expr.cpp =================================================================== --- clang/test/SemaTemplate/instantiate-requires-expr.cpp +++ clang/test/SemaTemplate/instantiate-requires-expr.cpp @@ -190,7 +190,7 @@ template<typename T> struct a { template<typename U> requires - (requires { requires sizeof(T::a) == 0; }, false) // expected-note{{because 'requires { requires <<error-expression>>; } , false' evaluated to false}} + (requires { requires sizeof(T::a) == 0; }, false) // expected-note{{because 'requires { requires <null expr>; } , false' evaluated to false}} struct r {}; }; Index: clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp =================================================================== --- clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp +++ clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp @@ -19,7 +19,8 @@ template<typename T> struct X { - template<typename U> 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<typename U> 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<typename T> 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<int>); // expected-note{{because 'int' does not satisfy 'C2'}} expected-error{{static assertion failed}} } @@ -51,3 +52,110 @@ X.next(); }; +namespace SubstitutionFailureNestedRequires { +template<class T> concept True = true; +template<class T> concept False = false; + +struct S { double value; }; + +template <class T> +concept Pipes = requires (T x) { + requires True<decltype(x.value)> || True<T> || False<T>; + requires False<T> || True<T> || True<decltype(x.value)>; +}; + +template <class T> +concept Amps1 = requires (T x) { + requires True<decltype(x.value)> && True<T> && !False<T>; + // expected-note@-1{{because substituted constraint expression is ill-formed: member reference base type 'int' is not a structure or union}} +}; +template <class T> +concept Amps2 = requires (T x) { + requires True<T> && True<decltype(x.value)>; +}; + +static_assert(Pipes<S>); +static_assert(Pipes<double>); + +static_assert(Amps1<S>); +static_assert(!Amps1<double>); + +static_assert(Amps2<S>); +static_assert(!Amps2<double>); + +template<class T> +void foo1() requires requires (T x) { // expected-note {{candidate template ignored: constraints not satisfied [with T = int]}} + requires + True<decltype(x.value)> // expected-note {{because substituted constraint expression is ill-formed: member reference base type 'int' is not a structure or union}} + && True<T>; +} {} +template<class T> void fooPipes() requires Pipes<T> {} +template<class T> void fooAmps1() requires Amps1<T> {} +// expected-note@-1 {{candidate template ignored: constraints not satisfied [with T = int]}} \ +// expected-note@-1 {{because 'int' does not satisfy 'Amps1'}} + +void foo() { + foo1<S>(); + foo1<int>(); // expected-error {{no matching function for call to 'foo1'}} + fooPipes<S>(); + fooPipes<int>(); + fooAmps1<S>(); + fooAmps1<int>(); // expected-error {{no matching function for call to 'fooAmps1'}} +} + +template<class T> +concept HasNoValue = requires (T x) { + requires !True<decltype(x.value)> && True<T>; +}; +// FIXME: 'int' does not satisfy 'HasNoValue' currently since `!True<decltype(x.value)>` is an invalid expression. +// But, in principle, it should be constant-evaluated to true. +static_assert(!HasNoValue<int>); +static_assert(!HasNoValue<S>); + +template<class T> constexpr bool NotAConceptTrue = true; +template <class T> +concept SFinNestedRequires = requires (T x) { + // SF in a non-concept specialisation should also be evaluated to false. + requires NotAConceptTrue<decltype(x.value)> || NotAConceptTrue<T>; +}; +static_assert(SFinNestedRequires<int>); +static_assert(SFinNestedRequires<S>); +template <class T> +void foo() requires SFinNestedRequires<T> {} +void bar() { + foo<int>(); + foo<S>(); +} +namespace ErrorExpressions_NotSF { +template<typename T> struct X { static constexpr bool value = T::value; }; \ +// expected-error {{type 'int' cannot be used prior to '::' because it has no members}} +struct True { static constexpr bool value = true; }; +struct False { static constexpr bool value = false; }; +template<typename T> concept C = true; +template<typename T> concept F = false; + +template<typename T> requires requires(T) { requires C<T> || X<T>::value; } void foo(); \ + +template<typename T> requires requires(T) { requires C<T> && X<T>::value; } void bar(); \ +// expected-note {{while substituting template arguments into constraint expression here}} \ +// expected-note {{while checking the satisfaction of nested requirement requested here}} \ +// expected-note {{candidate template ignored: constraints not satisfied [with T = False]}} \ +// expected-note {{because 'X<False>::value' evaluated to false}} \ +// expected-note {{in instantiation of static data member}} \ +// expected-note {{in instantiation of requirement here}} \ +// expected-note {{while checking the satisfaction of nested requirement requested here}} \ +// expected-note {{while substituting template arguments into constraint expression here}} +template<typename T> requires requires(T) { requires F<T> || (X<T>::value && C<T>); } void baz(); + +void func() { + foo<True>(); + foo<False>(); + foo<int>(); + + bar<True>(); + bar<False>(); // expected-error {{no matching function for call to 'bar'}} + bar<int>(); // expected-note {{while checking constraint satisfaction for template 'bar<int>' required here}} \ + // expected-note {{in instantiation of function template specialization}} +} +} +} Index: clang/lib/Sema/SemaTemplateInstantiate.cpp =================================================================== --- clang/lib/Sema/SemaTemplateInstantiate.cpp +++ clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -19,6 +19,7 @@ #include "clang/AST/Expr.h" #include "clang/AST/ExprConcepts.h" #include "clang/AST/PrettyDeclStackTrace.h" +#include "clang/AST/Type.h" #include "clang/AST/TypeVisitor.h" #include "clang/Basic/LangOptions.h" #include "clang/Basic/Stack.h" @@ -2328,8 +2329,12 @@ Req->getConstraintExpr()->getSourceRange()); if (ConstrInst.isInvalid()) return nullptr; - TransConstraint = TransformExpr(Req->getConstraintExpr()); - if (!TransConstraint.isInvalid()) { + llvm::SmallVector<Expr *> 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; @@ -2337,17 +2342,6 @@ "CheckConstraintExpression failed, but " "did not produce a SFINAE error"); } - // Use version of CheckConstraintSatisfaction that does no substitutions. - if (!TransConstraint.isInvalid() && - !TransConstraint.get()->isInstantiationDependent() && - !Trap.hasErrorOccurred()) { - bool CheckFailed = SemaRef.CheckConstraintSatisfaction( - TransConstraint.get(), Satisfaction); - (void)CheckFailed; - assert((!CheckFailed || Trap.hasErrorOccurred()) && - "CheckConstraintSatisfaction failed, " - "but did not produce a SFINAE error"); - } if (TransConstraint.isInvalid() || Trap.hasErrorOccurred()) return RebuildNestedRequirement(createSubstDiag(SemaRef, Info, [&] (llvm::raw_ostream& OS) { @@ -2355,7 +2349,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( Index: clang/lib/Sema/SemaConcept.cpp =================================================================== --- clang/lib/Sema/SemaConcept.cpp +++ clang/lib/Sema/SemaConcept.cpp @@ -169,7 +169,8 @@ // is checked. If that is satisfied, the disjunction is satisfied. // Otherwise, the disjunction is satisfied if and only if the second // operand is satisfied. - return BO.recreateBinOp(S, LHSRes); + // LHS is instantiated while RHS is not. Skip creating invalid BinaryOp. + return LHSRes; if (BO.isAnd() && !IsLHSSatisfied) // [temp.constr.op] p2 @@ -178,7 +179,8 @@ // is checked. If that is not satisfied, the conjunction is not // satisfied. Otherwise, the conjunction is satisfied if and only if // the second operand is satisfied. - return BO.recreateBinOp(S, LHSRes); + // LHS is instantiated while RHS is not. Skip creating invalid BinaryOp. + return LHSRes; ExprResult RHSRes = calculateConstraintSatisfaction( S, BO.getRHS(), Satisfaction, std::forward<AtomicEvaluator>(Evaluator)); @@ -286,7 +288,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()) { Index: clang/include/clang/AST/ExprConcepts.h =================================================================== --- clang/include/clang/AST/ExprConcepts.h +++ 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<SubstitutionDiagnostic *>();
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits