Shadowing type names in Rust

I recently came across the following code. It does compile in rust 1.11 (if you omit the function body), but I think it's really strange. I don't know if something like this should be allowed.

trait MyTrait<T> {
  fn func(p: &T);
}

struct MyStruct { }

impl<bool> MyTrait<bool> for MyStruct {
  fn func(p: &bool) {
    let mut v: Vec<bool> = Vec::new();
    v.push(true); // error
  }
}

This leads to confusing compile errors such as:

expected type bool
found type bool

I am aware that this can only happen to a newbie.

I disagree, this can happen to the best of us. clippy (nightly only for now) has a lint to at least improve the error message.

We may want to get that message into rustc.

1 Like

Yes, a different message would help to locate the error more easily.

1 Like