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

Warn on shadowing generics #11658

Closed
carllerche opened this issue Jan 19, 2014 · 4 comments
Closed

Warn on shadowing generics #11658

carllerche opened this issue Jan 19, 2014 · 4 comments

Comments

@carllerche
Copy link
Member

Given the following:

pub struct WrittenOutputStream<'a, 'b, W> {
  priv writer: &'a W,
  priv sizes: &'b SerializedSizeInfo
}

impl<'a, 'b, W: Writer> WrittenOutputStream<'a, 'b, W> {
  pub fn new<'a, 'b, W>(writer: &'a W, sizes: &'b SerializedSizeInfo) -> WrittenOutputStream<'a, 'b, W> {
    WrittenOutputStream { writer: writer, sizes: sizes }
  }
}

I would suggest that shadowing the generics in the new function should cause a warning. I'm still getting my mind wrapped around rust and the error message that I got in the previous case was pretty cryptic. Once I saw what was happening, it was obvious. I think a warning would have helped a lot.

@huonw
Copy link
Member

huonw commented Jan 19, 2014

Example of the errors:

struct Foo<T>(T);

impl<T> Foo<T> {
     fn new<T>(x: T) -> Foo<T> { Foo(x) }
}

fn main() {
     Foo::new(1i);
}
11658.rs:8:6: 8:14 error: cannot determine a type for this expression: unconstrained type
11658.rs:8      Foo::new(1i);
                ^~~~~~~~

It's the <T> from the impl that's unconstrained.

(This cannot be just a lint, since typechecking failures kill compilation well before lints run.)

@ben0x539
Copy link
Contributor

A solution for this would probably also fix #9914.

@FreeFull
Copy link
Contributor

DST seems to have made the error messages for this a lot less clearer than they have already been.

@steveklabnik
Copy link
Member

We do in fact warn for this now. From @huonw 's example:

hello.rs:4:10: 4:46 error: type parameter `T` shadows another type parameter of the same name
hello.rs:4          fn new<T>(x: T) -> Foo<T> { Foo(x) }
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants