Skip to content
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

enum variant values must be cast to their own type #13902

Closed
metajack opened this issue May 2, 2014 · 6 comments
Closed

enum variant values must be cast to their own type #13902

metajack opened this issue May 2, 2014 · 6 comments
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@metajack
Copy link
Contributor

metajack commented May 2, 2014

The following program does not compile:

#![allow(non_camel_case_types)]

static JSVAL_TAG_CLEAR: u32 = 0xFFFFFF80;
static JSVAL_TYPE_INT32: u8 = 0x01;
static JSVAL_TYPE_UNDEFINED: u8 = 0x02;
#[repr(u32)]
enum ValueTag {
    JSVAL_TAG_INT32 = JSVAL_TAG_CLEAR | (JSVAL_TYPE_INT32 as u32),
    JSVAL_TAG_UNDEFINED = JSVAL_TAG_CLEAR | (JSVAL_TYPE_UNDEFINED as u32),
}

fn main() {
    let _ = JSVAL_TAG_INT32;
}

If you add as u32 after JSVAL_TAG_CLEAR then it compiles fine. I assume this is interacting with repr such that it doesn't know the real type.

@luqmana luqmana added the I-wrong label May 3, 2014
@frewsxcv
Copy link
Member

frewsxcv commented Feb 3, 2015

visiting for triage

I updated your code so that it compiles with the latest Rust (enum namespacing):

static JSVAL_TAG_CLEAR: u32 = 0xFFFFFF80;
static JSVAL_TYPE_INT32: u8 = 0x01;
static JSVAL_TYPE_UNDEFINED: u8 = 0x02;
#[repr(u32)]
enum ValueTag {
    JSVAL_TAG_INT32 = JSVAL_TAG_CLEAR | (JSVAL_TYPE_INT32 as u32),
    JSVAL_TAG_UNDEFINED = JSVAL_TAG_CLEAR | (JSVAL_TYPE_UNDEFINED as u32),
}

fn main() {
    let _ = ValueTag::JSVAL_TAG_INT32;
}

Even with as u32 after JSVAL_TAG_CLEAR, it is not insistent upon using constants for enum values:

/t/triage $ rustc foo.rs
foo.rs:6:23: 6:74 error: expected constant: bad operands for binary [E0080]
foo.rs:6     JSVAL_TAG_INT32 = JSVAL_TAG_CLEAR as u32 | (JSVAL_TYPE_INT32 as u32),
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
foo.rs:7:27: 7:82 error: expected constant: bad operands for binary [E0080]
foo.rs:7     JSVAL_TAG_UNDEFINED = JSVAL_TAG_CLEAR as u32 | (JSVAL_TYPE_UNDEFINED as u32),
                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 2 previous errors

@arielb1
Copy link
Contributor

arielb1 commented Jun 17, 2015

This does work in 1.0

@arielb1 arielb1 added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. and removed I-wrong labels Jun 17, 2015
@frewsxcv
Copy link
Member

@arielb1 This doesn't work for me in 1.0

http://is.gd/CFYTay

@frewsxcv
Copy link
Member

@arielb1 What codeblock did you test this with?

@arielb1
Copy link
Contributor

arielb1 commented Jun 29, 2015

You need to use const instead of static of course.

frewsxcv added a commit to frewsxcv/rust that referenced this issue Jun 30, 2015
@frewsxcv
Copy link
Member

Regression test added in #26670

flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 9, 2025
…ust-lang#13906)

Proper parentheses need to be added to some expressions in receiver
position.

Fix rust-lang#13902

changelog: [`redundant_pattern_matching`]: use proper parentheses when
suggesting replacing `matches!(…, None)` by `.is_none()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

4 participants