Skip to content

Wrong error message for associated constants: error[E0158]: statics cannot be referenced in patterns #47570

Closed
@matklad

Description

@matklad
trait X {
  const X: usize;
}

struct A;
struct B;

impl X for A {
  const X: usize = 0;
}

impl X for B {
  const X: usize = 1;
}

fn do_match<T1: X, T2: X>(x: usize) {
  let r = match x {
     T1::X => "T1",
     T2::X => "T2",
     _ => "?",
  };
  println!("{}", r);
}

fn main() {
  do_match::<A, B>(1);
  do_match::<A, A>(1);
  do_match::<B, B>(1);
}
λ rustc main.rs                                                                                                                     ~/trash
error[E0158]: statics cannot be referenced in patterns
  --> main.rs:18:6
   |
18 |      T1::X => "T1",
   |      ^^^^^

error[E0158]: statics cannot be referenced in patterns
  --> main.rs:19:6
   |
19 |      T2::X => "T2",
   |      ^^^^^

error: aborting due to 2 previous errors

Looks like the error message is slightly off-base? Should it be something along the lines of "can't match on generic constants"?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions