diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -16817,7 +16817,10 @@ ConstantExpr::getStorageKind(Decl->getReturnType().getTypePtr(), getASTContext()), /*IsImmediateInvocation*/ true); - ExprEvalContexts.back().ImmediateInvocationCandidates.emplace_back(Res, 0); + /// Value-dependent constant expressions should not be immediately + /// evaluated until they are instantiated. + if (!Res->isValueDependent()) + ExprEvalContexts.back().ImmediateInvocationCandidates.emplace_back(Res, 0); return Res; } diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp --- a/clang/test/SemaCXX/cxx2a-consteval.cpp +++ b/clang/test/SemaCXX/cxx2a-consteval.cpp @@ -613,6 +613,32 @@ } // namespace unevaluated +namespace value_dependent { + +consteval int foo(int x) { + return x; +} + +template constexpr int bar() { + // Previously this call was rejected as value-dependent constant expressions + // can't be immediately evaluated. Now we show that we don't immediately + // evaluate them until they are instantiated. + return foo(X); +} + +template constexpr int baz() { + constexpr int t = sizeof(T); + // Previously this call was rejected as `t` is value-dependent and its value + // is unknown until the function is instantiated. Now we show that we don't + // reject such calls. + return foo(t); +} + +static_assert(bar<15>() == 15); +static_assert(baz() == sizeof(int)); + +} // namespace value_dependent + namespace PR50779 { struct derp { int b = 0;