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

Effects soundness hole: Non-const assoc fns in const fns accepted if the trait is not #[const_trait] #125831

Closed
clarfonthey opened this issue May 31, 2024 · 1 comment · Fixed by #132169
Labels
C-bug Category: This is a bug. F-effects `#![feature(effects)]` PG-const-traits Project group: Const traits T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@clarfonthey
Copy link
Contributor

I tried this code:

#![feature(effects)]
enum FlipFlop {
    Flip,
    Flop,
}
impl FlipFlop {
    const fn negating(self) -> FlipFlop {
        !self
    }
}
impl core::ops::Not for FlipFlop {
    type Output = FlipFlop;
    fn not(self) -> FlipFlop {
        match self {
            FlipFlop::Flip => FlipFlop::Flop,
            FlipFlop::Flop => FlipFlop::Flip,
        }
    }
}

I expected this code to fail, since impl Not for FlipFlop is not a constant implementation, but const fn negating requires a constant implementation.

Instead, this code succeeds with no error, failing if you actually use the method like so:

const FLOP: FlipFlop = !FlipFlop::Flip;

Giving the following result:

error[E0080]: evaluation of constant value failed
  --> src/lib.rs:21:24
   |
21 | const FLOP: FlipFlop = !FlipFlop::Flip;
   |                        ^^^^^^^^^^^^^^^ calling non-const function `<FlipFlop as Not>::not`

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error

I believe that this code should fail to compile regardless of whether it's used at constant time.

Meta

rustc --version --verbose:

1.80.0-nightly (2024-05-30 6f3df08aadf71e8d4bf7)
@clarfonthey clarfonthey added the C-bug Category: This is a bug. label May 31, 2024
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. F-effects `#![feature(effects)]` labels May 31, 2024
@fmease
Copy link
Member

fmease commented May 31, 2024

Oh wow! A soundness hole in the effect system with such a simple reproducer ^^'. Apparently,
we don't detect (non-const) impls of traits that aren't marked #[const_trait].

@fmease fmease added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. PG-const-traits Project group: Const traits and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 31, 2024
@fmease fmease changed the title feature(effects) lazily evaluates whether traits are const Effects soundness hole: Non-const assoc fns in const fns accepted if the trait is not #[const_trait] Oct 25, 2024
@fmease fmease linked a pull request Oct 26, 2024 that will close this issue
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. F-effects `#![feature(effects)]` PG-const-traits Project group: Const traits T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants