-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Comments
cc @rust-lang/lang |
Nominating for discussion at meeting. Clearly we should be consistent. 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? |
Is there a reason to shadow traits by name? What happens here is that it must find a method named |
Well, that's the question, isn't it. Imagine then that |
Ambiguity error if |
Another more challenging example might be: mod foo {
use path1::Trait;
fn bar() {
use path2::Trait;
}
} should the methods from |
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. |
Discussed in @rust-lang/lang meeting and settled on "methods from shadowed traits should be unavailable". triage: P-medium |
We also want methods from |
…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
This compiles:
but this doesn't:
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?
The text was updated successfully, but these errors were encountered: