-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Suggest associated type when the specified one cannot be found #67406
Conversation
e9d418f
to
ce57747
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks good and love the new output.
Left some nitpicks inline.
src/librustc_typeck/astconv.rs
Outdated
.filter_map(|(p, _)| p.to_opt_poly_trait_ref()); | ||
|
||
traits::transitive_bounds(tcx, bounds) | ||
}, | ||
¶m_name.as_str(), | ||
assoc_name, | ||
span) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you clean this up to be a bit terser?
self.one_bound_for_assoc_type(
|| traits::transitive_bounds(tcx, predicates.iter()
.filter_map(|(p, _)| p.to_opt_poly_trait_ref())),
¶m_name.as_str(),
assoc_name,
span,
)
src/librustc_typeck/astconv.rs
Outdated
let mut err = struct_span_err!(self.tcx().sess, span, E0220, | ||
"associated type `{}` not found for `{}`", | ||
assoc_name, | ||
ty_param_name) | ||
.span_label(span, format!("associated type `{}` not found", assoc_name)) | ||
.emit(); | ||
ty_param_name); | ||
|
||
let all_candidate_names: Vec<_> = all_candidates() | ||
.map(|r| self.tcx().associated_items(r.def_id())) | ||
.flatten() | ||
.filter_map(|item| | ||
if item.kind == ty::AssocKind::Type { | ||
Some(item.ident.name) | ||
} else { | ||
None | ||
} | ||
) | ||
.collect(); | ||
|
||
if let Some(suggested_name) = find_best_match_for_name( | ||
all_candidate_names.iter(), | ||
&assoc_name.as_str(), | ||
None, | ||
) { | ||
err.span_suggestion( | ||
span, | ||
"there is an associated type with a similar name", | ||
suggested_name.to_string(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} else { | ||
err.span_label( | ||
span, | ||
format!("associated type `{}` not found", assoc_name) | ||
); | ||
} | ||
|
||
err.emit(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we move all this to a separate method called complain_about_assoc_type_not_found
?
ce57747
to
a4a2fc0
Compare
@bors r+ |
📌 Commit a4a2fc0 has been approved by |
🌲 The tree is currently closed for pull requests below priority 100, this pull request will be tested once the tree is reopened |
…ebank Suggest associated type when the specified one cannot be found Fixes rust-lang#67386, so code like this: ``` use std::ops::Deref; fn homura<T: Deref<Trget = i32>>(_: T) {} fn main() {} ``` results in: ``` error[E0220]: associated type `Trget` not found for `std::ops::Deref` --> type-binding.rs:6:20 | 6 | fn homura<T: Deref<Trget = i32>>(_: T) {} | ^^^^^^^^^^^ help: there is an associated type with a similar name: `Target` error: aborting due to previous error ``` (The `help` is new) I used an `all_candidates: impl Fn() -> Iterator<...>` instead of `collect`ing to avoid the cost of allocating the Vec when no errors are found, at the expense of a little added complexity. r? @estebank
Rollup of 8 pull requests Successful merges: - #67189 (Unify binop wording) - #67270 (std: Implement `LineWriter::write_vectored`) - #67286 (Fix the configure.py TOML field for a couple LLVM options) - #67321 (make htons const fn) - #67382 (Remove some unnecessary `ATTR_*` constants.) - #67389 (Remove `SO_NOSIGPIPE` dummy variable on platforms that don't use it.) - #67394 (Remove outdated references to @t from comments) - #67406 (Suggest associated type when the specified one cannot be found) Failed merges: r? @ghost
Fixes #67386, so code like this:
results in:
(The
help
is new)I used an
all_candidates: impl Fn() -> Iterator<...>
instead ofcollect
ing to avoid the cost of allocating the Vec when no errors are found, at the expense of a little added complexity.r? @estebank