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_evaluatable_unchecked doesn't correctly deal with non default substs #94293

Closed
lcnr opened this issue Feb 23, 2022 · 3 comments · Fixed by #106037
Closed

const_evaluatable_unchecked doesn't correctly deal with non default substs #94293

lcnr opened this issue Feb 23, 2022 · 3 comments · Fixed by #106037
Labels
A-const-generics Area: const generics (parameters and arguments) E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@lcnr
Copy link
Contributor

lcnr commented Feb 23, 2022

While checking whether the fixed crate can be ported from typenum to nightly with generic_const_exprs, I hit this warning. I am not sure whether it's a false positive or whether the behavior I want is meant to be disallowed, but I think it should be allowed when enabling generic_const_exprs.

I've reduced the code though I left some stuff to show why it would be useful.

#![feature(generic_const_exprs)]

pub struct If<const CONDITION: bool>;
pub trait True {}
impl True for If<true> {}

pub struct FixedI8<const FRAC: u32> {
    pub bits: i8,
}

impl<const FRAC_LHS: u32, const FRAC_RHS: u32> PartialEq<FixedI8<FRAC_RHS>> for FixedI8<FRAC_LHS>
where
    If<{ FRAC_RHS <= 8 }>: True,
{
    fn eq(&self, _rhs: &FixedI8<FRAC_RHS>) -> bool {
        unimplemented!()
    }
}

impl<const FRAC: u32> PartialEq<i8> for FixedI8<FRAC> {
    fn eq(&self, rhs: &i8) -> bool {
        let rhs_as_fixed = FixedI8::<0> { bits: *rhs };
        PartialEq::eq(self, &rhs_as_fixed)
    }
}

Originally posted by @tspiteri in #76200 (comment)

@lcnr
Copy link
Contributor Author

lcnr commented Feb 23, 2022

const_evaluatable_unchecked is emitted if we successfully evaluate an anonymous constant with generic const arguments.

Anonymous constants currently inherit all of their parents generics, so for If<{ FRAC_RHS <= 8 }>: True, the constant { FRAC_RHS <= 8 } has both FRAC_RHS and FRAC_LHS in scope, even though it only uses FRAC_RHS.

This would mean that pretty much all anonymous constants would cause that lint, so we add a flag to their MIR to detect whether they actually use any generic parameter: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Body.html#structfield.is_polymorphic.

{ FRAC_RHS <= 8 } uses FRAC_RHS so that flag is true. The problem is now that FRAC_RHS is concrete, so evaluating the constant is successful, but the constant refers to FRAC_LHS which is still generic.

We should just disable the const_evaluatable_unchecked lint if feature(generic_const_exprs) is enabled 🤷

cc @BoxyUwU

@peter-lyons-kehl
Copy link
Contributor

peter-lyons-kehl commented May 16, 2022

Another similar. Short example failing on Nightly (thanks to ilyvion#9946 for narrowing it down): https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c133c09b2fa155bb3f4b9ee45e6c011e:

#![feature(generic_const_exprs)]

trait Foo<const N: usize>
where
    [(); check_empty_array_size(N)]: Sized,
{
    type Narr: Foo<0>;
}

#[allow(unused_variables)]
pub const fn check_empty_array_size(array_size: usize) -> usize {
    0
}

My use case: https://github.com/ranging-rs/slicing-rs/blob/main/src/slices/mod.rs#L48=. I know it's a semantic/voluntary constraint only, and it's quite recursive, but I believe it deserves its place. (Alternatives I tried are at ranging-rs/slicing-rs#1.)

@lcnr lcnr added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. A-const-generics Area: const generics (parameters and arguments) labels Sep 21, 2022
@lcnr
Copy link
Contributor Author

lcnr commented Sep 21, 2022

this has been fixed, needs test to close

JohnTitor added a commit to JohnTitor/rust that referenced this issue Dec 22, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 22, 2022
Add regression test for rust-lang#94293

Closes rust-lang#94293
r? `@lcnr`

Signed-off-by: Yuki Okushi <[email protected]>
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 22, 2022
Add regression test for rust-lang#94293

Closes rust-lang#94293
r? ``@lcnr``

Signed-off-by: Yuki Okushi <[email protected]>
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 22, 2022
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#105567 (KCFI test: Also support LLVM 16 output)
 - rust-lang#105847 (Ensure param-env is const before calling `eval_to_valtree`)
 - rust-lang#105983 (Add a missing early return in drop tracking `handle_uninhabited_return`)
 - rust-lang#106027 (rustdoc: simplify CSS and DOM for more-scraped-examples)
 - rust-lang#106035 (Migrate search tab title color to CSS variable)
 - rust-lang#106037 (Add regression test for rust-lang#94293)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in ee43f34 Dec 23, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#105567 (KCFI test: Also support LLVM 16 output)
 - rust-lang#105847 (Ensure param-env is const before calling `eval_to_valtree`)
 - rust-lang#105983 (Add a missing early return in drop tracking `handle_uninhabited_return`)
 - rust-lang#106027 (rustdoc: simplify CSS and DOM for more-scraped-examples)
 - rust-lang#106035 (Migrate search tab title color to CSS variable)
 - rust-lang#106037 (Add regression test for rust-lang#94293)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) 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

Successfully merging a pull request may close this issue.

2 participants