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

Inconsistency in whether methods of shadowed traits are usable #31379

Closed
jseyfried opened this Issue Feb 3, 2016 · 11 comments

Comments

Projects
None yet
5 participants
@jseyfried
Copy link
Contributor

jseyfried commented Feb 3, 2016

This compiles:

mod foo {
    trait IntoIterator {}    
    fn f() { Some(0).into_iter(); }
}

but this doesn't:

trait T {}
mod bar {
    use T as IntoIterator;
    fn f() { Some(0).into_iter(); }
}

More generally, a shadowed trait's methods are usable if it is shadowed by an item, but not if it is shadowed by an import.

Should methods from shadowed traits be usable?

@jseyfried

This comment has been minimized.

Copy link
Contributor Author

jseyfried commented Feb 3, 2016

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Feb 3, 2016

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Feb 3, 2016

Nominating for discussion at meeting. Clearly we should be consistent. I'm inclined to think that they should not be available, ever.

@dirk

This comment has been minimized.

Copy link
Contributor

dirk commented Feb 3, 2016

I'm inclined to think that they should not be available, ever.

I'm of the same thoughts as @nikomatsakis. A new "thing" with the same name as another outer "thing" should not inherit/infer/have/etc. any part of that outer thing. This is the case with variable bindings, but—according to the presented opinion—it seems like it needs to be made consistent for trait bindings, right?

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Feb 4, 2016

Is there a reason to shadow traits by name? What happens here is that it must find a method named .into_iter() among the imported traits, and that name has not been shadowed in any way, so it should be callable.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Feb 4, 2016

@bluss

Is there a reason to shadow traits by name? What happens here is that it must find a method named .into_iter() among the imported traits, and that name has not been shadowed in any way, so it should be callable.

Well, that's the question, isn't it. Imagine then that trait T did have an into_iter method -- would you prefer an ambiguity error here, or success?

@bluss

This comment has been minimized.

Copy link
Contributor

bluss commented Feb 4, 2016

Ambiguity error if T was implemented for Option<i32> in the example (and this error is already implemented). Otherwise it doesn't interfere in any way.

@nikomatsakis nikomatsakis added the T-lang label Feb 25, 2016

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Feb 25, 2016

Another more challenging example might be:

mod foo {
    use path1::Trait;
    fn bar() {
        use path2::Trait;
    }
}

should the methods from path1::Trait be available, given that it is shadowed by path2::Trait?

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Feb 25, 2016

My feeling is that the simplest rule is to say that methods come from "traits that are in scope", and shadowed traits are not in scope. This interpretation is shadowing of items from prelude and globs, which seem like they should clearly not count towards method resolution.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Feb 25, 2016

Discussed in @rust-lang/lang meeting and settled on "methods from shadowed traits should be unavailable".

triage: P-medium

@jseyfried

This comment has been minimized.

Copy link
Contributor Author

jseyfried commented Feb 25, 2016

This interpretation is shadowing of items from prelude and globs

We also want methods from path1::Trait to be unavailable (from your more challenging example), right?

bors added a commit that referenced this issue Mar 25, 2016

Auto merge of #31908 - jseyfried:disallow_shadowed_traits, r=nikomats…
…akis

Disallow methods from traits that are not in scope

This PR only allows a trait method to be used if the trait is in scope (fixes #31379).
This is a [breaking-change]. For example, the following would break:
```rust
mod foo {
    pub trait T { fn f(&self) {} }
    impl T for () {}
}

mod bar { pub use foo::T; }

fn main() {
    pub use bar::*;
    struct T; // This shadows the trait `T`,
    ().f() // making this an error.
}
```
r? @nikomatsakis

@bors bors closed this in #31908 Mar 25, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.