Coder Social home page Coder Social logo

Comments (6)

CAD97 avatar CAD97 commented on May 31, 2024 1

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 consts being more eagerly required to be well-formed.

from rust.

RalfJung avatar RalfJung commented on May 31, 2024 1

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.

joshlf avatar joshlf commented on May 31, 2024

This should be enough for our needs. Thanks for your work on this!

cc @jswrenn

from rust.

ChayimFriedman2 avatar ChayimFriedman2 commented on May 31, 2024

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.

RalfJung avatar RalfJung commented on May 31, 2024

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.

traviscross avatar traviscross commented on May 31, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.