Skip to content

"~const` is not allowed here" is a sub-optimal error message in one case #90052

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

Closed
leonardo-m opened this issue Oct 19, 2021 · 4 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. F-const_trait_impl `#![feature(const_trait_impl)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

I think this is a small diagnostic problem. This code compiles (with two warnings):

#![feature(const_fn_trait_bound)]
#![feature(const_trait_impl)]

trait HasBar {
    fn bar(&self);
}
impl const HasBar for usize {
    fn bar(&self) {}
}

struct Foo<T>(T)
where T: HasBar;

impl<T> Foo<T> where T: HasBar {
    const fn foo(&self)
    where T: ~const HasBar {
        self.0.bar();
    }
}

fn main() {}

(There in the impl foo I've used both HasBar and ~const HasBar, I'm not sure if this is necessary).

If you miss the 'const' from the impl foo:

#![feature(const_fn_trait_bound)]
#![feature(const_trait_impl)]

trait HasBar {
    fn bar(&self);
}
impl const HasBar for usize {
    fn bar(&self) {}
}

struct Foo<T>(T)
where T: HasBar;

impl<T> Foo<T> where T: HasBar {
    fn foo(&self)
    where T: ~const HasBar {
        self.0.bar();
    }
}

fn main() {}

It gives an error that doesn't suggest that a 'const' is missing in foo:

error: `~const` is not allowed here
  --> ...\test.rs:16:14
   |
16 |     where T: ~const HasBar {
   |              ^^^^^^^^^^^^^
   |
   = note: only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions
@leonardo-m leonardo-m added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 19, 2021
@JohnTitor JohnTitor added C-enhancement Category: An issue proposing an enhancement or a PR with one. F-const_trait_impl `#![feature(const_trait_impl)]` labels Oct 20, 2021
@oli-obk
Copy link
Contributor

oli-obk commented Oct 20, 2021

hmm... so you're saying we should suggest to make the current function const?

@leonardo-m
Copy link
Author

This is a minor diagnostic enhancement request. If you think this isn't worth doing, then we can close this down.

Currently the diagnostic says:
"only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions"

I'd like a focused diagnostic that says something like:
"~const HasBar" isn't allowed as a bound for a non-const function."

I think less noisy error messages are better.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 20, 2021

Oh, good point. Yea, that seems straight forward enough and def an improvement

@fee1-dead
Copy link
Member

fixed in #103319.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. F-const_trait_impl `#![feature(const_trait_impl)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants