-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Malformed lambda call inside inspect
lead to compilation without error and silent default value
#433
Comments
The problem are those trailing else
return std::string{};
else
return std::string{}; I think they should be replaced with a contract check or something along those lines. |
See https://compiler-explorer.com/z/MKTTxoTn7.
|
Assert is good but I'm surprised that cpp1 |
It's basically |
Folded the is-convertible check into the requires expression: https://compiler-explorer.com/z/zvxn7Mea6. |
This is more complicated than that. These multiple constexpr ifs are there to eliminate cases when the evaluation of a branch is invalid for a specific inspected value/type. I need to check if asserts will work for complex cases. For sure static_asserts were not an option. |
This single branch should be enough for an alternative's statement: if constexpr(requires { { 𝘦𝘹𝘱𝘳𝘦𝘴𝘴𝘪𝘰𝘯 } -> std::convertible_to<T>; })
return 𝘦𝘹𝘱𝘳𝘦𝘴𝘴𝘪𝘰𝘯; Followed by some kind of assertion in case the branch was discarded for the chosen alternative. |
(Sorry I haven't tried any fixes) Another observation, currently it's impossible to capture in lambda inside inspect
https://cpp2.godbolt.org/z/MGbxfTd6z
|
Please, open a separate issue for that. |
Inside
inspect
, I have lambda where I forgot to pass argument. Like:(x) -> _ = {return "int"} ();
instead of:(x) -> _ = {return "int"} (x);
It was accepted by compilers and emitted code, which runs and silently returns ""
To Reproduce
https://cpp2.godbolt.org/z/aerezGKqq
Reproduction code
Command line
cppfront/cppfront $1.cpp2 -p
clang++-15 -Icppfront/include $1.cpp -std=c++20 -o $1
Expected result
Diagnostic like cpp1 for line 13:
Actual result/error
Code silently compiles at cpp2 and cpp1 and silently runs without calling lambda and returning default value for inspect case, in this case an empty string.
Additional context
I understand this may be alpha version limitation of
inspect
Emitted cpp1 code is roughly
if (cpp2::is(__expr)) { if constexpr( requires{V} ) if constexpr( std::is_convertible_v<V,T>) return V; else return T{}; else return T{}; }
Edit: after some investigation, it appears that cpp1
if constexpr( requires{[](auto const& x) -> auto{ ... }();} )
returns false.The text was updated successfully, but these errors were encountered: