-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Migration lints for 2021 prelude #84594
Comments
Here are the instructions written in RFC 3114: Migration LintAs for all edition changes, we will implement a migration lint to detect cases where code would break in the new edition. It includes a MachineApplicable suggestion for an alternative that will work in both the current and next edition. The migration lint will be implemented as follows:
Currently, diagnostics for trait methods that are not in scope suggest importing the originating trait. For traits that have become part of the prelude in a newer edition, the diagnostics should be updated such that they suggest upgrading to the latest edition as an alternative to importing the relevant trait. |
Here are some mentoring instructions for how to do this. To start, I would go with hard-coding the names of the methods for now. It's easier. Those methods are the methods from The method call resolution is is n the rust/compiler/rustc_typeck/src/check/method/mod.rs Lines 170 to 192 in ef0ec30
The call to rust/compiler/rustc_typeck/src/check/method/mod.rs Lines 198 to 199 in ef0ec30
We want to check if the method name is in the desired set of methods. The method name can be found in rust/compiler/rustc_span/src/symbol.rs Lines 105 to 123 in ef0ec30
similar to how we already have You can then compare Assuming it does, we want to see if the method being invoked is from a trait in libstd/libcore. If it is, we don't have to give a warning, since we are already using the method we expect. Otherwise, we should. To determine what method is being invoked, we want to check the We then check the |
@rustbot release-assignment |
@rustbot claim |
Currently the big issue on the PR is that a majority of the breaking changes (by cases to handle, likely not by how common it is) look involve associated functions and thus something like this: trait TryFromU8 {
fn try_from(x: u8) -> Result<Self, !>;
}
impl TryFromU8 for u32 {
fn try_from(x: u8) -> Result<Self, !> {
x as u32
}
}
let x = u32::try_from(3); The part of typeck we were looking at only handles dot-call syntax and can only handle |
@jam1garner ah, good catch, I didn't consider this case. It should be relatively easy to manage this as well. |
…_lint, r=nikomatsakis Add `future_prelude_collision` lint Implements rust-lang#84594. (RFC rust-lang/rfcs#3114 ([rendered](https://github.com/rust-lang/rfcs/blob/master/text/3114-prelude-2021.md))) Not entirely complete but wanted to have my progress decently available while I finish off the last little bits. Things left to implement: * [x] UI tests for lints * [x] Only emit lint for 2015 and 2018 editions * [ ] Lint name/message bikeshedding * [x] Implement for `FromIterator` (from best I can tell, the current approach as mentioned from [this comment](rust-lang#84594 (comment)) won't work due to `FromIterator` instances not using dot-call syntax, but if I'm correct about this then that would also need to be fixed for `TryFrom`/`TryInto`)* * [x] Add to `rust-2021-migration` group? (See rust-lang#85512) (added to `rust-2021-compatibility` group) * [ ] Link to edition guide in lint docs *edit: looked into it, `lookup_method` will also not be hit for `TryFrom`/`TryInto` for non-dotcall syntax. If anyone who is more familiar with typecheck knows the equivalent for looking up associated functions, feel free to chime in.
…int, r=nikomatsakis Add `future_prelude_collision` lint Implements rust-lang#84594. (RFC rust-lang/rfcs#3114 ([rendered](https://github.com/rust-lang/rfcs/blob/master/text/3114-prelude-2021.md))) Not entirely complete but wanted to have my progress decently available while I finish off the last little bits. Things left to implement: * [x] UI tests for lints * [x] Only emit lint for 2015 and 2018 editions * [ ] Lint name/message bikeshedding * [x] Implement for `FromIterator` (from best I can tell, the current approach as mentioned from [this comment](rust-lang#84594 (comment)) won't work due to `FromIterator` instances not using dot-call syntax, but if I'm correct about this then that would also need to be fixed for `TryFrom`/`TryInto`)* * [x] Add to `rust-2021-migration` group? (See rust-lang#85512) (added to `rust-2021-compatibility` group) * [ ] Link to edition guide in lint docs *edit: looked into it, `lookup_method` will also not be hit for `TryFrom`/`TryInto` for non-dotcall syntax. If anyone who is more familiar with typecheck knows the equivalent for looking up associated functions, feel free to chime in.
Fixed by #85707 🎉 |
The 2021 Edition plans to have a new prelude, which will include a number of new traits. This means that new methods will be in scope which could introduce ambiguities into some code bases if they are using methods with the same name (but from different traits). We need a migration lint that detects such cases and suggests a rewrite into fully qualified form: e.g.,
x.foo()
would become something likeTrait::foo(&x)
.The text was updated successfully, but these errors were encountered: