Skip to content

Commit b39160d

Browse files
authored
[Clang][Sema] Expression in assumption attribute should be full expression (#150814)
Add missing `ActOnFinishFullExpr` to `BuildCXXAssumeExpr`. We did it during template instantiation but forgot non-template case.
1 parent 7c14c53 commit b39160d

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ Bug Fixes to C++ Support
149149
- Diagnose binding a reference to ``*nullptr`` during constant evaluation. (#GH48665)
150150
- Suppress ``-Wdeprecated-declarations`` in implicitly generated functions. (#GH147293)
151151
- Fix a crash when deleting a pointer to an incomplete array (#GH150359).
152+
- Fix an assertion failure when expression in assumption attribute
153+
(``[[assume(expr)]]``) creates temporary objects.
152154

153155
Bug Fixes to AST Handling
154156
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaStmtAttr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,10 @@ ExprResult Sema::BuildCXXAssumeExpr(Expr *Assumption,
795795
if (Res.isInvalid())
796796
return ExprError();
797797

798+
Res = ActOnFinishFullExpr(Res.get(), /*DiscardedValue=*/false);
799+
if (Res.isInvalid())
800+
return ExprError();
801+
798802
Assumption = Res.get();
799803
if (Assumption->HasSideEffects(Context))
800804
Diag(Assumption->getBeginLoc(), diag::warn_assume_side_effects)

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,11 +2270,6 @@ TemplateInstantiator::TransformCXXAssumeAttr(const CXXAssumeAttr *AA) {
22702270
if (!Res.isUsable())
22712271
return AA;
22722272

2273-
Res = getSema().ActOnFinishFullExpr(Res.get(),
2274-
/*DiscardedValue=*/false);
2275-
if (!Res.isUsable())
2276-
return AA;
2277-
22782273
if (!(Res.get()->getDependence() & ExprDependence::TypeValueInstantiation)) {
22792274
Res = getSema().BuildCXXAssumeExpr(Res.get(), AA->getAttrName(),
22802275
AA->getRange());

clang/test/Parser/cxx23-assume.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ void f(int x, int y) {
55
[[assume(1)]];
66
[[assume(1.0)]];
77
[[assume(1 + 2 == 3)]];
8-
[[assume(x ? 1 : 2)]];
8+
[[assume(x ? 1 : 2)]]; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
99
[[assume(x && y)]];
1010
[[assume(true)]] [[assume(true)]];
1111

clang/test/SemaCXX/cxx23-assume.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
struct A{};
99
struct B{ explicit operator bool() { return true; } };
1010

11+
// This should be the first test case of this file.
12+
void IsActOnFinishFullExprCalled() {
13+
// Do not add other test cases to this function.
14+
// Make sure `ActOnFinishFullExpr` is called and creates `ExprWithCleanups`
15+
// to avoid assertion failure.
16+
[[assume(B{})]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
17+
}
18+
1119
template <bool cond>
1220
void f() {
1321
[[assume(cond)]]; // ext-warning {{C++23 extension}}

0 commit comments

Comments
 (0)