-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Prelude collision lint: invalid suggestion for T::from_iter when T is generic. #86940
Comments
My guess is this should be a simple fix, just copying the code the lint already uses for adding unspecified generics ( |
@rustbot assign @jam1garner |
@rustbot release-assignment |
Mentoring notesThe code related to this lint lives in the prelude2021 module. The relevant suggestion is generated here: rust/compiler/rustc_typeck/src/check/method/prelude2021.rs Lines 264 to 269 in b919797
The "self type" string comes from here: rust/compiler/rustc_typeck/src/check/method/prelude2021.rs Lines 258 to 262 in b919797
The code for traits is here: rust/compiler/rustc_typeck/src/check/method/prelude2021.rs Lines 242 to 251 in b919797
This is actually just a touch tricky, because currently we are getting the "self type" just by copying the text the user wrote. Probably the most correct thing for us to do would be to examine the 'self expression' HIR and, if it is a plain path, then to append the Specifically, the other case that I am thinking about is code like this: #![warn(rust_2021_compatibility)]
struct Generic<T>(T);
trait Hey { fn from_iter(_: i32) -> Self; }
impl Hey for Generic<i32> { fn from_iter(x: i32) -> Self { Self(x) } }
impl std::iter::FromIterator<i32> for Generic<i32> { fn from_iter<T: IntoIterator<Item = i32>>(_: T) -> Self { todo!() } }
fn main() {
<Generic<_>>::from_iter(1);
} Currently this test works, meaning that it offers the suggestion "disambiguate the associated function: |
@rustbot claim |
@Pyther99 great! Let us know if you need anything or make progress. This issue is fairly time sensitive. |
@rustbot release-assignment |
@rustbot claim |
…-error, r=nikomatsakis Fix issue with autofix for ambiguous associated function from Rust 2021 prelude when struct is generic Fixes rust-lang#86940 The test cases and associated issue should make it clear what specifically this is meant to fix. The fix is slightly hacky in that we check against the literal source code of the call site for the presence of `<` in order to determine if the user has included the generics for the struct (meaning we don't need to include them for them). r? `@nikomatsakis`
For generic types, the prelude collision lint can produce suggestions that do not compile:
Code:
Suggestion from the prelude collision lint:
Error after applying the suggestion:
The text was updated successfully, but these errors were encountered: