Skip to content

Commit 874ca08

Browse files
authored
[Clang][ExprConstant] fix constant expression did not evaluate to integer (#97146)
fixes #96670 The cause is that we might return a lvalue here at https://github.com/llvm/llvm-project/blob/3e53c97d33210db68188e731e93ee48dbaeeae32/clang/lib/AST/ExprConstant.cpp#L15861-L15865 This PR will make sure we return a rvalue in `FastEvaluateAsRValue`.
1 parent acd7a68 commit 874ca08

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,8 @@ Bug Fixes to C++ Support
967967
- Fixed an assertion failure about invalid conversion when calling lambda. (#GH96205).
968968
- Fixed a bug where the first operand of binary ``operator&`` would be transformed as if it was the operand
969969
of the address of operator. (#GH97483).
970+
- Fixed an assertion failure about a constant expression which is a known integer but is not
971+
evaluated to an integer. (#GH96670).
970972

971973
Bug Fixes to AST Handling
972974
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/AST/ExprConstant.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15859,9 +15859,12 @@ static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result,
1585915859

1586015860
if (const auto *CE = dyn_cast<ConstantExpr>(Exp)) {
1586115861
if (CE->hasAPValueResult()) {
15862-
Result.Val = CE->getAPValueResult();
15863-
IsConst = true;
15864-
return true;
15862+
APValue APV = CE->getAPValueResult();
15863+
if (!APV.isLValue()) {
15864+
Result.Val = std::move(APV);
15865+
IsConst = true;
15866+
return true;
15867+
}
1586515868
}
1586615869

1586715870
// The SubExpr is usually just an IntegerLiteral.

clang/test/SemaCXX/eval-crashes.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,13 @@ struct array {
6161
array() : data(*new int[1][2]) {}
6262
};
6363
}
64+
65+
namespace GH96670 {
66+
inline constexpr long ullNil = -1;
67+
68+
template<typename T = long, const T &Nil = ullNil>
69+
struct Test {};
70+
71+
inline constexpr long lNil = -1;
72+
Test<long, lNil> c;
73+
}

0 commit comments

Comments
 (0)