Skip to content

constants are not constant due to ptr->int casts #18294

Closed
@thestinger

Description

@thestinger
Contributor
fn main() {
    const X: u32 = 1;
    const Y: uint = &X as *const u32 as uint;
    println!("{}", Y);
}

Fixing this requires backwards incompatible changes to constant expressions. At a minimum, casts from pointers to integers need to be forbidden. It's possible that there are other issues.

Activity

jdm

jdm commented on Oct 24, 2014

@jdm
Contributor

Can you elaborate on the problem here?

thestinger

thestinger commented on Oct 24, 2014

@thestinger
ContributorAuthor

@jdm: If it's not obvious from the code, compile and run it several times. The value of Y is determined at runtime so it's not a valid constant expression. The const type was supposed to be distinct from static by being a valid constant expression in every case but it is not.

arielb1

arielb1 commented on Oct 24, 2014

@arielb1
Contributor

We really shouldn't be allowing pointer -> uint conversions in constexprs.

changed the title [-]constants are not constant[/-] [+]constants are not constant due to ptr->int casts[/+] on Oct 24, 2014
added a commit that references this issue on Oct 25, 2014
81b7e62
added a commit that references this issue on Oct 22, 2024

Auto merge of rust-lang#18294 - Giga-Bowser:master, r=Veykril

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @jdm@thestinger@arielb1

      Issue actions

        constants are not constant due to ptr->int casts · Issue #18294 · rust-lang/rust