-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Why does dead code have to be valid? #117048
Comments
It's not possible for the compiler to prove that arbitrary code is dead. There are warnings for some cases of dead code that are easy to detect, but because the compiler cannot perfectly determine if code is dead, it requires all code to be valid. Additionally, dead code can have effects, such as code in an |
|
@Vzz1c When a program includes a call like In the specific case of using the type names, the compiler does not know that the type names of two types cannot resolve to the same string value when pretty-printed by Because of that, there is less than one might imagine stopping the compiler from compiling-in an "impossible" condition check, failing to remove it because the condition is not clearly impossible, and at runtime, evaluating to something that causes a jump to that "undefined" code segment. This does happen in C++ compilers, where legal optimizations remove some "dead code", resulting in falling-through to a random function. Because of this, it is much, much easier to simply reject all programs that try this than to accept any of them, because all of them are ill-formed. Rust already has too many problems with accidentally compiling correct programs in invalid ways for it to be likely anyone wants to try to allow this. This is working-as-intended. |
I'm fairly certain this is an XY problem, anyway. Best as I can tell, your fundamental desire is not to ensure dead code doesn't have to be valid. Your fundamental desire is to achieve type introspection in Rust. The question is, do you want to achieve that statically (ie at compile-time) or dynamically (ie at run-time)? If it's the latter, I guess you could use If it's the former… hoo boy, I don't envy you. With |
I started out using |
I expected to see this happen: It's intuitive. It should compile.
Instead, this happened: temp_vec.push($item.white().clear());
In this code, the compiler reports that there is no white() method.
But here, as long as the if condition is met, the call will succeed without error.
I think the cause of this is similar to the following code
I don't think anyone in practice would write such code, here if the condition is successful or not, can not call the foo() method, all here reported an error, I can understand. But in the example I defined through the macro, as long as the if condition is met, it will be called successfully. Why does it not compile here?
The text was updated successfully, but these errors were encountered: