Stabilize the supertrait_item_shadowing feature - #148605
Conversation
|
The Miri subtree was changed cc @rust-lang/miri
cc @tgross35 Some changes occurred to the CTFE / Miri interpreter |
|
r? @fee1-dead rustbot has assigned @fee1-dead. Use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Thanks @Amanieu for the stabilization PR. As you fill out the stabilization report for us, have a look at our stabilization report template: https://rustc-dev-guide.rust-lang.org/stabilization_report_template.html#stabilization-report |
277f962 to
a370e80
Compare
|
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
This comment has been minimized.
This comment has been minimized.
a370e80 to
bf256d4
Compare
|
☔ The latest upstream changes (presumably #139558) made this pull request unmergeable. Please resolve the merge conflicts. |
|
Stabilization looks fine. r=me after FCP. |
|
I've updated the summary to match the stabilization template. |
|
These don't lint. Should they? #![feature(supertrait_item_shadowing)]
#![allow(unused)]
#![deny(supertrait_item_shadowing_usage)]
trait Super {
fn f(&self) {}
}
trait Sub: Super {
fn f(&self) {}
}
fn f<T: Sub>(x: T) {
let _x = T::f; // Should lint?
let _ = T::f(&x); // Should lint?
} |
|
Regarding the lints:
Some suggestions on these names: Def-site lint
The name for this seems to call for using a gerund. It came up in a recent meeting whether we already use gerunds for lint names. We do, though it is the exception: If we're willing to lean into using gerunds when convenient, I'd suggest to name this Use-site lintLet's assume we expand (or might expand) the lint to cover all three cases here: trait Super {
fn f(&self) {}
}
trait Sub: Super {
fn f(&self) {}
}
fn f<T: Sub>(x: T) {
let _x = T::f; //~ Lints.
let _ = T::f(&x); //~ Lints.
let _ = x.f(); //~ Lints.
// But not:
let _x = <T as Sub>::f; //~ Does not lint.
let _ = <T as Sub>::f(&x); //~ Does not lint.
}This one is a bit tougher. We're linting on when we need to infer a trait to resolve an item and the resolved item is from a subtrait that shadows an item in one of its (transitive) supertraits. Again, if we're willing to lean into a gerund, it makes this a bit easier, and I'd suggest (This doesn't exactly capture the notion that we don't lint on the fully-qualified form that is, certainly, still a name resolution. Maybe that's OK.) The gerund questionFor my part, I think I'm OK with leaning into gerunds where convenient. |
|
#152225 fixes the inconsistency with type-level name resolution. |
|
Yeah that would resolve both of my concerns 👍 Thanks jack and amanieu :) |
… r=jackh726 Add supertrait item shadowing for type-level path resolution This makes type-level name resolution (used primarily for associated types) also prefer subtrait items to supertrait items in case of ambiguity. This addresses the concern from rust-lang#148605 about the inconsistency with name resolution in expressions and method calls.
… r=jackh726 Add supertrait item shadowing for type-level path resolution This makes type-level name resolution (used primarily for associated types) also prefer subtrait items to supertrait items in case of ambiguity. This addresses the concern from rust-lang#148605 about the inconsistency with name resolution in expressions and method calls.
… r=jackh726 Add supertrait item shadowing for type-level path resolution This makes type-level name resolution (used primarily for associated types) also prefer subtrait items to supertrait items in case of ambiguity. This addresses the concern from rust-lang#148605 about the inconsistency with name resolution in expressions and method calls.
… r=jackh726 Add supertrait item shadowing for type-level path resolution This makes type-level name resolution (used primarily for associated types) also prefer subtrait items to supertrait items in case of ambiguity. This addresses the concern from rust-lang#148605 about the inconsistency with name resolution in expressions and method calls.
… r=jackh726 Add supertrait item shadowing for type-level path resolution This makes type-level name resolution (used primarily for associated types) also prefer subtrait items to supertrait items in case of ambiguity. This addresses the concern from rust-lang#148605 about the inconsistency with name resolution in expressions and method calls.
Rollup merge of #152225 - Amanieu:type-supertrait-shadowing, r=jackh726 Add supertrait item shadowing for type-level path resolution This makes type-level name resolution (used primarily for associated types) also prefer subtrait items to supertrait items in case of ambiguity. This addresses the concern from #148605 about the inconsistency with name resolution in expressions and method calls.
Add supertrait item shadowing for type-level path resolution This makes type-level name resolution (used primarily for associated types) also prefer subtrait items to supertrait items in case of ambiguity. This addresses the concern from rust-lang/rust#148605 about the inconsistency with name resolution in expressions and method calls.
Add supertrait item shadowing for type-level path resolution This makes type-level name resolution (used primarily for associated types) also prefer subtrait items to supertrait items in case of ambiguity. This addresses the concern from rust-lang/rust#148605 about the inconsistency with name resolution in expressions and method calls.
…=wafflelapkin
Revert "implement and test `Iterator::{exactly_one, collect_array}`"
This reverts rust-lang/rust#149270
I was quite excited it merged, and immediately realized with ``@WaffleLapkin`` that this is a breaking change on nightly! Despite still being marked as unstable, the name conflicts with the name on itertools as was discussed on the PR itself: rust-lang/rust#149270 (comment).
I'll reopen the PR though, and mark it as blocked on rust-lang/rust#148605
|
@rfcbot resolve implementation-confidence I'd missed jacks comment here before |
|
@rfcbot resolve inconsistent-name-res-semantics |
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
|
I would kind of like us to restart this FCP given that the design here has changed and this feature now applies to more things 🤔 |
View all comments
Stabilization report
Summary
When name resolution encounters an ambiguity between two trait item when both traits are in scope, if one trait is a subtrait of the other then select the item from the subtrait instead of reporting an ambiguity error.
The motivation for this comes from several attempts to add methods to the
Iteratortrait (#79524, #145733, #141994) in the standard library which already exists in theitertoolscrate. Stabilizing theseIteratormethods leads to the following code failing to compile with an ambiguity error:Tracking: #89151
Reference PRs:
cc @rust-lang/lang @rust-lang/lang-advisors
What is stabilized
What isn't stabilized
N/A
Design
Reference
There isn't currently a reference section on name resolution, but there is work towards adding such a section in rust-lang/reference#2055.
RFC history
Answers to unresolved questions
As per the lang team decision, this also sets the default lint levels as follows:
supertrait_item_shadowing_definitionwarns by default.supertrait_item_shadowing_usageis allowed by default.Post-RFC changes
The lint levels were decided after the RFC was accepted.
Key points
N/A (already covered above)
Nightly extensions
N/A
Doors closed
This feature could limit the ways in which we resolve ambiguity errors for associated items in the future. This was extensively discussed in the RFC thread and the conclusion was that the proposed behavior is the only sensible one.
Feedback
Call for testing
No call for testing has been done.
Nightly use
There are no current nightly users of this feature.
Implementation
Major parts
This was implemented in
Coverage
There are UI tests for both the shadowing functionality and the lints.
Outstanding bugs
None
Outstanding FIXMEs
None
Tool changes
None
Breaking changes
This is not a breaking change because it only allows code to compile in situations where an ambiguity error would have previously been raised.
Type system, opsem
Compile-time checks
N/A
Type system rules
This feature only provides a way to resolve ambiguities in name resolution and doesn't interact with the type system otherwise.
Sound by default?
Yes
Breaks the AM?
No
Common interactions
Temporaries
N/A
Drop order
N/A
Pre-expansion / post-expansion
N/A
Edition hygiene
N/A
SemVer implications
This doesn't directly introduce a new SemVer hazard. It has always been possible for a sub-trait to accidentally use the same method name as a supertrait. However with this feature this will result in silently calling the subtrait method instead of causing an ambiguity error. The warn-by-default lint at the sub-trait definition site will help crate authors avoid such a situation unless it is intentional.
Exposing other features
N/A
History
supertrait_item_shadowing(v2) #125782Acknowledgments
Thanks to @compiler-errors for implementing this and @lcdr for the original RFC.
Open items