Skip to content

Inconsistency in whether methods of shadowed traits are usable #31379

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

Closed
jseyfried opened this issue Feb 3, 2016 · 11 comments · Fixed by #31908
Closed

Inconsistency in whether methods of shadowed traits are usable #31379

jseyfried opened this issue Feb 3, 2016 · 11 comments · Fixed by #31908
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) P-medium Medium priority T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@jseyfried
Copy link
Contributor

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
Copy link
Contributor Author

cc @nikomatsakis

@nikomatsakis
Copy link
Contributor

cc @rust-lang/lang

@nikomatsakis nikomatsakis added I-nominated I-needs-decision Issue: In need of a decision. labels Feb 3, 2016
@nikomatsakis
Copy link
Contributor

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

@dirk
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
Copy link
Member

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
Copy link
Contributor

@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
Copy link
Member

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 Relevant to the language team, which will review and decide on the PR/issue. label Feb 25, 2016
@nikomatsakis
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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

triage: P-medium

@rust-highfive rust-highfive added P-medium Medium priority and removed I-nominated I-needs-decision Issue: In need of a decision. labels Feb 25, 2016
@jseyfried
Copy link
Contributor Author

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
…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
@RalfJung RalfJung added the A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) label Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) P-medium Medium priority T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants