Skip to content

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

Closed
@leonardo-m

Description

@leonardo-m

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.F-const_trait_impl`#![feature(const_trait_impl)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions