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

Const values in traits not treated the same as global const values for const_err #89063

Closed
bhgomes opened this issue Sep 18, 2021 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@bhgomes
Copy link
Contributor

bhgomes commented Sep 18, 2021

The following code only reports one error instead of two:

#[derive(Clone, Copy, Debug)]
pub struct GreaterThanTwo(u16);

impl GreaterThanTwo {
    /// Builds a [`GreaterThanTwo`] who's inner value is at least `2`.
    pub const fn new(value: u16) -> Self {
        let _  = value - 2;
        Self(value)
    }
}

pub trait Constants {
    const COUNT: GreaterThanTwo;
}

pub struct Config;

impl Constants for Config {
    const COUNT: GreaterThanTwo = GreaterThanTwo::new(0);
}

pub const OTHER_COUNT: GreaterThanTwo = GreaterThanTwo::new(0);

The COUNT variable inside of the Constants implementation does not trigger a const_err error where OTHER_COUNT does.

error: any use of this value will cause an error
  --> src/lib.rs:7:18
   |
7  |         let _  = value - 2;
   |                  ^^^^^^^^^
   |                  |
   |                  attempt to compute `0_u16 - 2_u16`, which would overflow
   |                  inside `GreaterThanTwo::new` at src/lib.rs:7:18
   |                  inside `OTHER_COUNT` at src/lib.rs:22:41
...
22 | pub const OTHER_COUNT: GreaterThanTwo = GreaterThanTwo::new(0);
   | ---------------------------------------------------------------
   |
   = note: `#[deny(const_err)]` on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>

error: could not compile `playground` due to previous error

Rust Playground Link

@bhgomes bhgomes added the C-bug Category: This is a bug. label Sep 18, 2021
@FabianWolff
Copy link
Contributor

FabianWolff commented Sep 20, 2021

@rustbot claim

Sorry for the noise. I had a patch that fixes your example, but then I read #71282 (comment), which essentially states that the behavior you described is the intended/expected one, so my patch doesn't solve this, and your issue is probably a duplicate of #71282 and #71403.

@FabianWolff FabianWolff removed their assignment Sep 20, 2021
@bhgomes bhgomes closed this as completed Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants