llvm-project
ef44d259 - [clang][ExprConst] Diagnose out-of-lifetime access consistently (#175562)

Commit
90 days ago
[clang][ExprConst] Diagnose out-of-lifetime access consistently (#175562) Previously, we had two very similar diagnostics, "read of object outside its lifetime" and "read of variable whose lifetime has ended". The difference, as far as I can tell, is that the latter was used when the variable was created in a function frame that has since vanished, i.e. in this case: ```c++ constexpr const int& return_local() { return 5; } static_assert(return_local() == 5); ``` so the output used to be: ```console array.cpp:602:15: error: static assertion expression is not an integral constant expression 602 | static_assert(return_local() == 5); | ^~~~~~~~~~~~~~~~~~~ array.cpp:602:15: note: read of temporary whose lifetime has ended array.cpp:601:46: note: temporary created here 601 | constexpr const int& return_local() { return 5; } | ^ ``` But then this scenario gets the other diagnostic: ```c++ constexpr int b = b; ``` ```console array.cpp:603:15: error: constexpr variable 'b' must be initialized by a constant expression 603 | constexpr int b = b; | ^ ~ array.cpp:603:19: note: read of object outside its lifetime is not allowed in a constant expression 603 | constexpr int b = b; | ^ ``` With this patch, we diagnose both cases similarly: ```c++ constexpr const int& return_local() { return 5; } static_assert(return_local() == 5); constexpr int b = b; ``` ```console array.cpp:602:15: error: static assertion expression is not an integral constant expression 602 | static_assert(return_local() == 5); | ^~~~~~~~~~~~~~~~~~~ array.cpp:602:15: note: read of object outside its lifetime is not allowed in a constant expression 602 | static_assert(return_local() == 5); | ^~~~~~~~~~~~~~ array.cpp:601:46: note: temporary created here 601 | constexpr const int& return_local() { return 5; } | ^ array.cpp:603:15: error: constexpr variable 'b' must be initialized by a constant expression 603 | constexpr int b = b; | ^ ~ array.cpp:603:19: note: read of object outside its lifetime is not allowed in a constant expression 603 | constexpr int b = b; | ^ ``` We do lose the "object" vs. "temporary" distinction in the note and only mention it in the "created here" note. That can be added back if it's important enough. I wasn't sure.
Author
Parents
Loading