Skip to content

Commit 5e140c8

Browse files
authored
[Clang] [NFC] Clarify assume diagnostic (#93077)
Currently, if the argument to `__builtin_assume` and friends contains side-effects, we issue the following diagnostic: ``` <source>:1:34: warning: the argument to '__builtin_assume' has side effects that will be discarded [-Wassume] 1 | void f(int x) { __builtin_assume(x++); } | ``` The issue here is that this diagnostic misrepresents what is actually happening: not only do we discard the side-effects of the expression, but we also don’t even emit any assumption information at all because the backend is not equipped to deal with eliminating side-effects in cases such as this. This has caused some confusion (see #91612) beacuse the current wording of the warning suggests that, sensibly, only the side-effects of the expression, and not the assumption itself, will be discarded. This pr updates the diagnostic to state what is actually happening: that the assumption has no effect at all because its argument contains side-effects: ``` <source>:1:34: warning: assumption is ignored because it contains (potential) side-effects [-Wassume] 1 | void f(int x) { __builtin_assume(x++); } | ``` I’ve deliberately included ‘(potential)’ here because even expressions that only contain potential side-effects (e.g. `true ? x : x++` or a call to a function that is pure, but we don’t know that it is) cause the assumption to be discarded. This, too, has caused some confusion because it was erroneously assumed that Clang would e.g. infer that a function call is pure and not discard the assumption as a result when that isn’t the case. This is intended to be temporary; we should revert back to the original diagnostic once we have proper support for assumptions with side-effects in the backend (in which case the side-effects will still be discarded, but the assumption won’t) This fixes #91612.
1 parent 6702594 commit 5e140c8

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ def note_strncat_wrong_size : Note<
855855
"the terminating null byte">;
856856

857857
def warn_assume_side_effects : Warning<
858-
"the argument to %0 has side effects that will be discarded">,
858+
"assumption is ignored because it contains (potential) side-effects">,
859859
InGroup<DiagGroup<"assume">>;
860860
def warn_omp_assume_attribute_string_unknown : Warning<
861861
"unknown assumption string '%0'; attribute is potentially ignored">,

clang/test/Parser/MicrosoftExtensions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ bool f(int);
426426
template <typename T>
427427
struct A {
428428
constexpr A(T t) {
429-
__assume(f(t)); // expected-warning{{the argument to '__assume' has side effects that will be discarded}}
429+
__assume(f(t)); // expected-warning{{assumption is ignored because it contains (potential) side-effects}}
430430
}
431431
constexpr bool g() { return false; }
432432
};

clang/test/Sema/builtin-assume.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ int ispure(int) __attribute__((pure));
88
int foo(int *a, int i) {
99
#ifdef _MSC_VER
1010
__assume(i != 4);
11-
__assume(++i > 2); //expected-warning {{the argument to '__assume' has side effects that will be discarded}}
12-
__assume(nonconst() > 2); //expected-warning {{the argument to '__assume' has side effects that will be discarded}}
11+
__assume(++i > 2); //expected-warning {{assumption is ignored because it contains (potential) side-effects}}
12+
__assume(nonconst() > 2); //expected-warning {{assumption is ignored because it contains (potential) side-effects}}
1313
__assume(isconst() > 2);
1414
__assume(ispure(i) > 2);
15-
__assume(ispure(++i) > 2); //expected-warning {{the argument to '__assume' has side effects that will be discarded}}
15+
__assume(ispure(++i) > 2); //expected-warning {{assumption is ignored because it contains (potential) side-effects}}
1616

1717
int test = sizeof(struct{char qq[(__assume(i != 5), 7)];});
1818
#else
1919
__builtin_assume(i != 4);
20-
__builtin_assume(++i > 2); //expected-warning {{the argument to '__builtin_assume' has side effects that will be discarded}}
21-
__builtin_assume(nonconst() > 2); //expected-warning {{the argument to '__builtin_assume' has side effects that will be discarded}}
20+
__builtin_assume(++i > 2); //expected-warning {{assumption is ignored because it contains (potential) side-effects}}
21+
__builtin_assume(nonconst() > 2); //expected-warning {{assumption is ignored because it contains (potential) side-effects}}
2222
__builtin_assume(isconst() > 2);
2323
__builtin_assume(ispure(i) > 2);
24-
__builtin_assume(ispure(++i) > 2); //expected-warning {{the argument to '__builtin_assume' has side effects that will be discarded}}
24+
__builtin_assume(ispure(++i) > 2); //expected-warning {{assumption is ignored because it contains (potential) side-effects}}
2525

2626
int test = sizeof(struct{char qq[(__builtin_assume(i != 5), 7)];}); // expected-warning {{variable length array}}
2727
#endif

clang/test/Sema/stmtexprs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ int stmtexpr_fn(void);
44
void stmtexprs(int i) {
55
__builtin_assume( ({ 1; }) ); // no warning about "side effects"
66
__builtin_assume( ({ if (i) { (void)0; }; 42; }) ); // no warning about "side effects"
7-
// expected-warning@+1 {{the argument to '__builtin_assume' has side effects that will be discarded}}
7+
// expected-warning@+1 {{assumption is ignored because it contains (potential) side-effects}}
88
__builtin_assume( ({ if (i) ({ stmtexpr_fn(); }); 1; }) );
99
}

clang/test/SemaCXX/cxx23-assume.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ bool f2();
2828

2929
template <typename T>
3030
constexpr void f3() {
31-
[[assume(T{})]]; // expected-error {{not contextually convertible to 'bool'}} expected-warning {{has side effects that will be discarded}} ext-warning {{C++23 extension}}
31+
[[assume(T{})]]; // expected-error {{not contextually convertible to 'bool'}} expected-warning {{assumption is ignored because it contains (potential) side-effects}} ext-warning {{C++23 extension}}
3232
}
3333

3434
void g(int x) {
@@ -38,13 +38,13 @@ void g(int x) {
3838
S<false>{}.f();
3939
S<true>{}.g<char>();
4040
S<true>{}.g<int>();
41-
[[assume(f2())]]; // expected-warning {{side effects that will be discarded}} ext-warning {{C++23 extension}}
41+
[[assume(f2())]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} ext-warning {{C++23 extension}}
4242

43-
[[assume((x = 3))]]; // expected-warning {{has side effects that will be discarded}} // ext-warning {{C++23 extension}}
44-
[[assume(x++)]]; // expected-warning {{has side effects that will be discarded}} // ext-warning {{C++23 extension}}
45-
[[assume(++x)]]; // expected-warning {{has side effects that will be discarded}} // ext-warning {{C++23 extension}}
46-
[[assume([]{ return true; }())]]; // expected-warning {{has side effects that will be discarded}} // ext-warning {{C++23 extension}}
47-
[[assume(B{})]]; // expected-warning {{has side effects that will be discarded}} // ext-warning {{C++23 extension}}
43+
[[assume((x = 3))]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
44+
[[assume(x++)]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
45+
[[assume(++x)]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
46+
[[assume([]{ return true; }())]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
47+
[[assume(B{})]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} // ext-warning {{C++23 extension}}
4848
[[assume((1, 2))]]; // expected-warning {{has no effect}} // ext-warning {{C++23 extension}}
4949

5050
f3<A>(); // expected-note {{in instantiation of}}
@@ -91,7 +91,7 @@ static_assert(S<false>{}.g<A>()); // expected-error {{not an integral constant e
9191

9292
template <typename T>
9393
constexpr bool f4() {
94-
[[assume(!T{})]]; // expected-error {{invalid argument type 'D'}} // expected-warning 2 {{side effects}} ext-warning {{C++23 extension}}
94+
[[assume(!T{})]]; // expected-error {{invalid argument type 'D'}} // expected-warning 2 {{assumption is ignored because it contains (potential) side-effects}} ext-warning {{C++23 extension}}
9595
return sizeof(T) == sizeof(int);
9696
}
9797

@@ -137,8 +137,8 @@ static_assert(f5<F>() == 2); // expected-note {{while checking constraint satisf
137137
// Do not validate assumptions whose evaluation would have side-effects.
138138
constexpr int foo() {
139139
int a = 0;
140-
[[assume(a++)]] [[assume(++a)]]; // expected-warning 2 {{has side effects that will be discarded}} ext-warning 2 {{C++23 extension}}
141-
[[assume((a+=1))]]; // expected-warning {{has side effects that will be discarded}} ext-warning {{C++23 extension}}
140+
[[assume(a++)]] [[assume(++a)]]; // expected-warning 2 {{assumption is ignored because it contains (potential) side-effects}} ext-warning 2 {{C++23 extension}}
141+
[[assume((a+=1))]]; // expected-warning {{assumption is ignored because it contains (potential) side-effects}} ext-warning {{C++23 extension}}
142142
return a;
143143
}
144144

@@ -154,7 +154,7 @@ int
154154
foo (int x, int y)
155155
{
156156
__attribute__((assume(x == 42)));
157-
__attribute__((assume(++y == 43))); // expected-warning {{has side effects that will be discarded}}
157+
__attribute__((assume(++y == 43))); // expected-warning {{assumption is ignored because it contains (potential) side-effects}}
158158
return x + y;
159159
}
160160
}

0 commit comments

Comments
 (0)