Skip to content

Commit c885005

Browse files
authored
[Modules] Don't const eval VarDecls with dependent type (#147378)
EvaluateAsInitializer does not support evaluating values with dependent types. This was previously guarded with a check for the initializer expression, but it is possible for the VarDecl to have a dependent type without the initializer having a dependent type, when the initializer is a specialized template type and the VarDecl has the unspecialized type. This adds a guard checking for dependence in the VarDecl type as well. This fixes the issue raised by Google in #145447
1 parent 468275d commit c885005

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

clang/lib/AST/Decl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2450,7 +2450,8 @@ bool VarDecl::hasInitWithSideEffects() const {
24502450
ES->HasSideEffects =
24512451
E->HasSideEffects(getASTContext()) &&
24522452
// We can get a value-dependent initializer during error recovery.
2453-
(E->isValueDependent() || !evaluateValue());
2453+
(E->isValueDependent() || getType()->isDependentType() ||
2454+
!evaluateValue());
24542455
ES->CheckedForSideEffects = true;
24552456
}
24562457
return ES->HasSideEffects;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Tests referencing variable with initializer containing side effect across module boundary
2+
3+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %s -o %t
4+
5+
export module Foo;
6+
7+
export template <class Float>
8+
struct Wrapper {
9+
double value;
10+
};
11+
12+
export constexpr Wrapper<double> Compute() {
13+
return Wrapper<double>{1.0};
14+
}
15+
16+
export template <typename Float>
17+
Wrapper<Float> ComputeInFloat() {
18+
const Wrapper<Float> a = Compute();
19+
return a;
20+
}

0 commit comments

Comments
 (0)