Skip to content

Commit 65d24f0

Browse files
committed
[clang] remove workaround for SubstNonTypeTemplateParmExpr transform
This removes a workaround which gets in the way of #141776, including the test changes as expected under that implementation.
1 parent a5bff94 commit 65d24f0

File tree

3 files changed

+17
-41
lines changed

3 files changed

+17
-41
lines changed

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,45 +2441,17 @@ TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr(
24412441
ExprResult
24422442
TemplateInstantiator::TransformSubstNonTypeTemplateParmExpr(
24432443
SubstNonTypeTemplateParmExpr *E) {
2444-
ExprResult SubstReplacement = E->getReplacement();
2445-
if (!isa<ConstantExpr>(SubstReplacement.get()))
2446-
SubstReplacement = TransformExpr(E->getReplacement());
2444+
ExprResult SubstReplacement = TransformExpr(E->getReplacement());
24472445
if (SubstReplacement.isInvalid())
24482446
return true;
2449-
QualType SubstType = TransformType(E->getParameterType(getSema().Context));
2450-
if (SubstType.isNull())
2451-
return true;
2452-
// The type may have been previously dependent and not now, which means we
2453-
// might have to implicit cast the argument to the new type, for example:
2454-
// template<auto T, decltype(T) U>
2455-
// concept C = sizeof(U) == 4;
2456-
// void foo() requires C<2, 'a'> { }
2457-
// When normalizing foo(), we first form the normalized constraints of C:
2458-
// AtomicExpr(sizeof(U) == 4,
2459-
// U=SubstNonTypeTemplateParmExpr(Param=U,
2460-
// Expr=DeclRef(U),
2461-
// Type=decltype(T)))
2462-
// Then we substitute T = 2, U = 'a' into the parameter mapping, and need to
2463-
// produce:
2464-
// AtomicExpr(sizeof(U) == 4,
2465-
// U=SubstNonTypeTemplateParmExpr(Param=U,
2466-
// Expr=ImpCast(
2467-
// decltype(2),
2468-
// SubstNTTPE(Param=U, Expr='a',
2469-
// Type=char)),
2470-
// Type=decltype(2)))
2471-
// The call to CheckTemplateArgument here produces the ImpCast.
2472-
TemplateArgument SugaredConverted, CanonicalConverted;
2473-
if (SemaRef
2474-
.CheckTemplateArgument(E->getParameter(), SubstType,
2475-
SubstReplacement.get(), SugaredConverted,
2476-
CanonicalConverted,
2477-
/*StrictCheck=*/false, Sema::CTAK_Specified)
2478-
.isInvalid())
2447+
auto *Param = cast_or_null<NonTypeTemplateParmDecl>(
2448+
TransformDecl(E->getNameLoc(), E->getParameter()));
2449+
if (!Param)
24792450
return true;
24802451
return transformNonTypeTemplateParmRef(
2481-
E->getAssociatedDecl(), E->getParameter(), E->getExprLoc(),
2482-
SugaredConverted, E->getPackIndex(), E->getFinal());
2452+
E->getAssociatedDecl(), Param, E->getExprLoc(),
2453+
TemplateArgument(SubstReplacement.get(), /*IsCanonical=*/false),
2454+
E->getPackIndex(), E->getFinal());
24832455
}
24842456

24852457
ExprResult TemplateInstantiator::RebuildVarDeclRefExpr(ValueDecl *PD,

clang/test/SemaCXX/cxx20-ctad-type-alias.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ using Bar = Foo<X, sizeof(X)>; // expected-note {{candidate template ignored: co
113113
// expected-note {{implicit deduction guide declared as 'template <typename X> requires __is_deducible(test9::Bar, test9::Foo<X, sizeof(X)>) Bar(test9::Foo<X, sizeof(X)>) -> test9::Foo<X, sizeof(X)>'}} \
114114
// expected-note {{implicit deduction guide declared as 'template <typename X> requires __is_deducible(test9::Bar, test9::Foo<X, sizeof(X)>) Bar(const X (&)[sizeof(X)]) -> test9::Foo<X, sizeof(X)>'}} \
115115
// expected-note {{candidate template ignored: constraints not satisfied [with X = int]}} \
116-
// expected-note {{cannot deduce template arguments for 'test9::Bar' from 'test9::Foo<int, 4UL>'}}
116+
// expected-note {{cannot deduce template arguments for 'test9::Bar' from 'test9::Foo<int, sizeof(int)>'}}
117117

118118

119119
Bar s = {{1}}; // expected-error {{no viable constructor or deduction guide }}

clang/test/SemaTemplate/instantiate-template-argument.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@ concept C2 = C1<Y{}, V>;
99
// sizeof(U) >= 4 [U = V (decltype(Y{}))]
1010

1111
template<char W>
12-
constexpr int foo() requires C2<int, W> { return 1; }
12+
constexpr int foo() requires C2<int, W> { return 1; } // #cand1
1313
// sizeof(U) >= 4 [U = W (decltype(int{}))]
1414

1515
template<char X>
1616
// expected-note@+1{{candidate function}}
17-
constexpr int foo() requires C1<1, X> && true { return 2; }
17+
constexpr int foo() requires C1<1, X> && true { return 2; } // #cand2
1818
// sizeof(U) >= 4 [U = X (decltype(1))]
1919

2020
static_assert(foo<'a'>() == 2);
21+
// expected-error@-1 {{call to 'foo' is ambiguous}}
22+
// expected-note@#cand1 {{candidate function}}
23+
// expected-note@#cand2 {{candidate function}}
2124

2225
template<char Z>
23-
// expected-note@+1{{candidate function}}
24-
constexpr int foo() requires C2<long long, Z> && true { return 3; }
26+
constexpr int foo() requires C2<long long, Z> && true { return 3; } // #cand3
2527
// sizeof(U) >= 4 [U = Z (decltype(long long{}))]
2628

2729
static_assert(foo<'a'>() == 3);
28-
// expected-error@-1{{call to 'foo' is ambiguous}}
30+
// expected-error@-1{{call to 'foo' is ambiguous}}
31+
// expected-note@#cand1 {{candidate function}}
32+
// expected-note@#cand3 {{candidate function}}

0 commit comments

Comments
 (0)