Skip to content

Commit f1b0a4f

Browse files
committed
An expression should only contain an unexpanded parameter pack if it
lexically contains a mention of the pack. Systematically distinguish between syntactic and semantic references to packs, especially when propagating dependence from a type into an expression. We should consult the type-as-written when computing syntactic dependence and should consult the semantic type when computing semantic dependence. Fixes #54402.
1 parent a7c0b75 commit f1b0a4f

File tree

8 files changed

+318
-70
lines changed

8 files changed

+318
-70
lines changed

clang/include/clang/AST/ComputeDependence.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class UnaryExprOrTypeTraitExpr;
3030
class ArraySubscriptExpr;
3131
class MatrixSubscriptExpr;
3232
class CompoundLiteralExpr;
33-
class CastExpr;
33+
class ImplicitCastExpr;
34+
class ExplicitCastExpr;
3435
class BinaryOperator;
3536
class ConditionalOperator;
3637
class BinaryConditionalOperator;
@@ -70,6 +71,7 @@ class CXXPseudoDestructorExpr;
7071
class OverloadExpr;
7172
class DependentScopeDeclRefExpr;
7273
class CXXConstructExpr;
74+
class CXXTemporaryObjectExpr;
7375
class CXXDefaultInitExpr;
7476
class CXXDefaultArgExpr;
7577
class LambdaExpr;
@@ -114,7 +116,8 @@ ExprDependence computeDependence(UnaryExprOrTypeTraitExpr *E);
114116
ExprDependence computeDependence(ArraySubscriptExpr *E);
115117
ExprDependence computeDependence(MatrixSubscriptExpr *E);
116118
ExprDependence computeDependence(CompoundLiteralExpr *E);
117-
ExprDependence computeDependence(CastExpr *E);
119+
ExprDependence computeDependence(ImplicitCastExpr *E);
120+
ExprDependence computeDependence(ExplicitCastExpr *E);
118121
ExprDependence computeDependence(BinaryOperator *E);
119122
ExprDependence computeDependence(ConditionalOperator *E);
120123
ExprDependence computeDependence(BinaryConditionalOperator *E);
@@ -156,6 +159,7 @@ ExprDependence computeDependence(OverloadExpr *E, bool KnownDependent,
156159
bool KnownContainsUnexpandedParameterPack);
157160
ExprDependence computeDependence(DependentScopeDeclRefExpr *E);
158161
ExprDependence computeDependence(CXXConstructExpr *E);
162+
ExprDependence computeDependence(CXXTemporaryObjectExpr *E);
159163
ExprDependence computeDependence(CXXDefaultInitExpr *E);
160164
ExprDependence computeDependence(CXXDefaultArgExpr *E);
161165
ExprDependence computeDependence(LambdaExpr *E,

clang/include/clang/AST/DependenceFlags.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ class Dependence {
130130

131131
// Dependence that is propagated syntactically, regardless of semantics.
132132
Syntactic = UnexpandedPack | Instantiation | Error,
133+
// Dependence that is propagated semantically, even in cases where the
134+
// type doesn't syntactically appear. This currently excludes only
135+
// UnexpandedPack. Even though Instantiation dependence is also notionally
136+
// syntactic, we also want to propagate it semantically because anything
137+
// that semantically depends on an instantiation-dependent entity should
138+
// always be instantiated when that instantiation-dependent entity is.
139+
Semantic =
140+
Instantiation | Type | Value | Dependent | Error | VariablyModified,
133141

134142
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/VariablyModified)
135143
};
@@ -175,6 +183,14 @@ class Dependence {
175183
return Result;
176184
}
177185

186+
/// Extract the semantic portions of this type's dependence that apply even
187+
/// to uses where the type does not appear syntactically.
188+
Dependence semantic() {
189+
Dependence Result = *this;
190+
Result.V &= Semantic;
191+
return Result;
192+
}
193+
178194
TypeDependence type() const {
179195
return translate(V, UnexpandedPack, TypeDependence::UnexpandedPack) |
180196
translate(V, Instantiation, TypeDependence::Instantiation) |
@@ -231,7 +247,10 @@ class Dependence {
231247
inline ExprDependence toExprDependence(TemplateArgumentDependence TA) {
232248
return Dependence(TA).expr();
233249
}
234-
inline ExprDependence toExprDependence(TypeDependence D) {
250+
inline ExprDependence toExprDependenceForImpliedType(TypeDependence D) {
251+
return Dependence(D).semantic().expr();
252+
}
253+
inline ExprDependence toExprDependenceAsWritten(TypeDependence D) {
235254
return Dependence(D).expr();
236255
}
237256
// Note: it's often necessary to strip `Dependent` from qualifiers.
@@ -269,6 +288,9 @@ inline TypeDependence toTypeDependence(TemplateArgumentDependence D) {
269288
inline TypeDependence toSyntacticDependence(TypeDependence D) {
270289
return Dependence(D).syntactic().type();
271290
}
291+
inline TypeDependence toSemanticDependence(TypeDependence D) {
292+
return Dependence(D).semantic().type();
293+
}
272294

273295
inline NestedNameSpecifierDependence
274296
toNestedNameSpecifierDependendence(TypeDependence D) {

clang/include/clang/AST/Expr.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3497,7 +3497,6 @@ class CastExpr : public Expr {
34973497
CastExprBits.BasePathSize = BasePathSize;
34983498
assert((CastExprBits.BasePathSize == BasePathSize) &&
34993499
"BasePathSize overflow!");
3500-
setDependence(computeDependence(this));
35013500
assert(CastConsistency());
35023501
CastExprBits.HasFPFeatures = HasFPFeatures;
35033502
}
@@ -3631,6 +3630,7 @@ class ImplicitCastExpr final
36313630
ExprValueKind VK)
36323631
: CastExpr(ImplicitCastExprClass, ty, VK, kind, op, BasePathLength,
36333632
FPO.requiresTrailingStorage()) {
3633+
setDependence(computeDependence(this));
36343634
if (hasStoredFPFeatures())
36353635
*getTrailingFPFeatures() = FPO;
36363636
}
@@ -3708,7 +3708,9 @@ class ExplicitCastExpr : public CastExpr {
37083708
CastKind kind, Expr *op, unsigned PathSize,
37093709
bool HasFPFeatures, TypeSourceInfo *writtenTy)
37103710
: CastExpr(SC, exprTy, VK, kind, op, PathSize, HasFPFeatures),
3711-
TInfo(writtenTy) {}
3711+
TInfo(writtenTy) {
3712+
setDependence(computeDependence(this));
3713+
}
37123714

37133715
/// Construct an empty explicit cast.
37143716
ExplicitCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize,

0 commit comments

Comments
 (0)