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_err does not trigger on associated constants #71282

Open
Nemo157 opened this issue Apr 18, 2020 · 2 comments
Open

const_err does not trigger on associated constants #71282

Nemo157 opened this issue Apr 18, 2020 · 2 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Nemo157
Copy link
Member

Nemo157 commented Apr 18, 2020

I tried this code:

pub struct Foo;

pub const FOO: Foo = Foo::new(1);

impl Foo {
    pub const FOO: Self = Self::new(1);

    pub const fn new(i: usize) -> Foo {
        [][i] = 5;
        Foo
    }
}

I expected to see this happen: Both FOO and Foo::FOO should trigger const_err errors.

Instead, this happened: Only FOO was marked with const_err

Meta

Checked on current stable (1.42) and nightly (2020-04-17) on the playground

@Nemo157 Nemo157 added the C-bug Category: This is a bug. label Apr 18, 2020
@Nemo157
Copy link
Member Author

Nemo157 commented Apr 18, 2020

This was discussed at #67191 (comment) and below, so might be covered by #70820 but I didn't really look into the details.

@RalfJung
Copy link
Member

RalfJung commented Apr 19, 2020

Cc @rust-lang/wg-const-eval

I would say this is expected. The error comes from actually computing the const (not some kind of static analysis). Basically rustc iterates over every const in the crate and computes it, to make sure the errors are triggered even if the const is not used. Associated consts cannot in general be evaluated eagerly as they could depend on generic parameters, unlike global consts.

You will still get the error if you actually use Foo::FOO.

I suppose we could try to still eagerly evaluate monomorphic associated consts like this one, but that scheme will also quickly reach its limit, and it feels rather ad-hoc. Currently, from what I recall, const-prop and the unused-const-err lint carefully balance who is running on what to avoid duplicate warnings; any such change would need careful testing to make sure we do not regress that.

@jonas-schievink jonas-schievink added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants