Comments (6)
Theoretically a const if
could be specified to do something stronger than making the untaken branch "dead code" and thus get behavior more like if the branch is outlined in a separate function and thus (potentially) never monomorphized. The language that C++'s constexpr if uses is roughly that the statement is discarded without ever being instantiated. The difference between "if
expression where the condition is evaluated at const
time" and "conditional compilation selection of expression blocks controlled by const
time evaluation" is why I typically discuss the relevant feature as "static if
" rather than as "const if
".
However, it is still the case that "syntactically mentioned" is determined before any const
evaluation is done by any reasonable interpretation. #[cfg]
and macro expansion get intertwined with syntax, but const
is definitively after. At least while type associated syntax macros continue to not exist.
I'd personally be quite happy to see a written guarantee that if a const
expression is "evaluated" at runtime then that requires that the backing const
item must have been evaluated at compile time regardless of what is (or isn't) done with the result of that evaluation at runtime. It's both an obvious rule and entirely independent of any details of the compilation model.
What I'd prefer not to see (yet) is any (new) guarantees of when const
items are guaranteed to not be evaluated. While I concede to reality, I still strongly prefer const
s being more eagerly required to be well-formed.
from rust.
I'd personally be quite happy to see a written guarantee that if a const expression is "evaluated" at runtime then that requires that the backing const item must have been evaluated at compile time regardless of what is (or isn't) done with the result of that evaluation at runtime. It's both an obvious rule and entirely independent of any details of the compilation model.
Yeah that was my thinking as well, which is why proposed exactly that.
What I'd prefer not to see (yet) is any (new) guarantees of when const items are guaranteed to not be evaluated. While I concede to reality, I still strongly prefer consts being more eagerly required to be well-formed.
Agreed as well, which is why my proposal does not make any such guarantees.
from rust.
This should be enough for our needs. Thanks for your work on this!
cc @jswrenn
from rust.
I don't understand: isn't the entire purpose of const if
that even though constants in dead code are evaluated, this branch won't be evaluated if not taken?
from rust.
Those two statements are in contradiction, aren't they?
const if false {
const { panic!(); } // this is in dead code
}
So we can't just say "const in dead code are evaluated". We have to say "const in dead code are evaluated, except ...". It's not okay to make a blanket statement and then restrict it elsewhere -- that makes the blanket statement just false.
from rust.
@rustbot labels -I-lang-nominated
We discussed this today in lang triage. People agreed that this is a reasonable and minimal first step, and rust-lang/reference#1497 is now in FCP.
from rust.
Related Issues (20)
- regression: cycle detected when computing whether impls specialize one another HOT 2
- regression: can't call method `abs` on ambiguous numeric type `{float}` HOT 5
- regression: ambiguous outer attributes HOT 2
- regression: reached recursion limit HOT 8
- regression: cannot find macro in scope HOT 3
- high memory requirement during monomorphization HOT 1
- fixed-by-polonius loops/functions have very bad diagnostics HOT 1
- `rustc unexpectedly overflowed its stack! this is a bug` HOT 7
- `f16`/`f128` fallback code is not getting inlined HOT 4
- #[diagnostic::on_unimplemented] hint not shown for associated type mismatch HOT 4
- Unexpected trait bound not satisfied related to `std::io::Write` HOT 1
- Different behavior for macro by example and procedural macro HOT 1
- Unexpected comment position after macro expansion HOT 1
- Empty where clauses don't have an unused warning HOT 5
- Vec<T> implements PartialEq<[T; N]> but the other way round doesn't work HOT 1
- rustc can't find the linker when `--sysroot` is set to a local build of the sysroot HOT 14
- `async closure does not implement `FnMut` because it captures state from its environment` for async closures, while closure returning async block works fine HOT 1
- ICE: `assertion failed: unique` HOT 1
- Suboptimal interaction between lifetime oriented and value oriented type system in the presence of closures. HOT 1
- rustc should magically detect repeating refactoring errors and compress them
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust.