forked from wangliu-iscas/gcc-patch
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
c++: improve verify_constant diagnostic [PR91483]
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- When verify_constant complains, it's pretty terse. Consider void test () { constexpr int i = 42; constexpr const int *p = &i; } where it says "'& i' is not a constant expression". OK, but why? With this patch, we say: b.C:5:28: error: '& i' is not a constant expression 5 | constexpr const int *p = &i; | ^~ b.C:5:28: note: pointer to 'i' is not a constant expression b.C:4:17: note: address of non-static constexpr variable 'i' may differ on each invocation of the enclosing function; add 'static' to give it a constant address 4 | constexpr int i = 42; | ^ | static which brings g++ on par with clang++. gcc/cp/ChangeLog: * constexpr.cc (verify_constant_explain_r): New. (verify_constant): Call it. gcc/testsuite/ChangeLog: * g++.dg/diagnostic/constexpr3.C: New test.
- Loading branch information
1 parent
c2d62cd
commit 39102ef
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// { dg-do compile { target c++14 } } | ||
|
||
struct X { | ||
int const& var; | ||
}; | ||
|
||
struct A { | ||
A *ap = this; | ||
}; | ||
|
||
constexpr A | ||
foo () | ||
{ | ||
return {}; | ||
} | ||
|
||
void | ||
test () | ||
{ | ||
constexpr int i = 42; // { dg-message "may differ on each invocation" } | ||
|
||
constexpr X x{i}; // { dg-error "not a constant expression" } | ||
// { dg-message "reference to .i. is not a constant expression" "" { target *-*-* } .-1 } | ||
constexpr const int *p = &i; // { dg-error "not a constant expression" } | ||
// { dg-message "pointer to .i. is not a constant expression" "" { target *-*-* } .-1 } | ||
|
||
constexpr A a = foo (); // { dg-error "not a constant expression" } | ||
// { dg-message "pointer to .a. is not a constant expression|may differ" "" { target *-*-* } .-1 } | ||
|
||
constexpr const int *q = __builtin_launder (&i); // { dg-error "not a constant expression" } | ||
// { dg-message "pointer to .i. is not a constant expression" "" { target *-*-* } .-1 } | ||
} |