Skip to content

improve associated-type suggestions from bounds#152471

Open
JohnTitor wants to merge 2 commits intorust-lang:mainfrom
JohnTitor:sugg-assoc-items-from-bounds
Open

improve associated-type suggestions from bounds#152471
JohnTitor wants to merge 2 commits intorust-lang:mainfrom
JohnTitor:sugg-assoc-items-from-bounds

Conversation

@JohnTitor
Copy link
Member

Should address invalid suggestions I could come up with, but the suggestion is too hand-crafted and invalid patterns may exist.
r? @estebank
Fix #73321

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 11, 2026
Comment on lines 374 to 391
match suggestions.as_slice() {
[one] => {
err.span_suggestion_verbose(
ident_span,
"use the associated type",
one,
Applicability::MaybeIncorrect,
);
}
_ => {
err.span_suggestions(
ident_span,
"use an associated type",
suggestions,
Applicability::MaybeIncorrect,
);
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have span_suggestions_verbose already? That would make the match unnecessary. If not, then we should just publish a PR making span_suggestions be always verbose and be done with it (inline suggestions are rarely the best option and we should inverse the default to verbose and opt-into inline suggestions on a per case basis).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK no, but I've found span_suggestions_with_style and it could be used here, I guess: 391a395 (this PR)

How about this?

[one] => {
err.span_suggestion_verbose(
ident_span,
"use the associated type",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about "you might have meant to use an associated type of the same name"?

Copy link
Member Author

@JohnTitor JohnTitor Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it should be! Since we emit the suggestion as MaybeIncorrect, your wording should be better. Addressed in 391a395 (this PR)

Comment on lines +82 to +97
fn path_to_string_without_assoc_item_bindings(path: &Path) -> String {
let mut path = path.clone();
for segment in &mut path.segments {
let mut remove_args = false;
if let Some(args) = segment.args.as_deref_mut()
&& let ast::GenericArgs::AngleBracketed(angle_bracketed) = args
{
angle_bracketed.args.retain(|arg| matches!(arg, ast::AngleBracketedArg::Arg(_)));
remove_args = angle_bracketed.args.is_empty();
}
if remove_args {
segment.args = None;
}
}
path_to_string(&path)
}
Copy link
Contributor

@estebank estebank Feb 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we don't already have something like this in pprint? I know there are a bunch of modifier macros that you can surround a format!("{ty}") with that will produce different output depending on the modifier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked existing support a bit but couldn't find anything for what we do (in particular, ast::Path) here.

Comment on lines 208 to 210
fn assoc_type_required_generic_args_suggestion(&self, assoc_type_def_id: DefId) -> String {
self.r.item_required_generic_args_suggestion(assoc_type_def_id)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this? It's only used twice :)

Copy link
Member Author

@JohnTitor JohnTitor Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, addressed in 391a395 (this PR)

Comment on lines 320 to 326
if let Some(item) = self.diag_metadata.current_item
&& let Some(generics) = item.kind.generics()
{
record_from_generics(self, generics);
}

if let Some(assoc) = self.diag_metadata.current_impl_item {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a test case for an impl declared within an fn, and vice-versa? I'm intrigued if this would cause record_from_generics to then provide suggestions that would incorrectly mention outer type params. This would only happen if diag_metadata isn't being properly updated elsewhere, so this logic might still be right.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch!
I've tweaked if branch and added tests you asked: 391a395 (this PR)

@JohnTitor JohnTitor force-pushed the sugg-assoc-items-from-bounds branch from 9645914 to 485f95f Compare February 13, 2026 05:34
@JohnTitor JohnTitor force-pushed the sugg-assoc-items-from-bounds branch from 6f5575f to 391a395 Compare February 13, 2026 07:41
@rustbot
Copy link
Collaborator

rustbot commented Feb 13, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@estebank
Copy link
Contributor

@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 13, 2026

📌 Commit 391a395 has been approved by estebank

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 13, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 14, 2026
…unds, r=estebank

improve associated-type suggestions from bounds

Should address invalid suggestions I could come up with, but the suggestion is too hand-crafted and invalid patterns may exist.
r? @estebank
Fix rust-lang#73321
rust-bors bot pushed a commit that referenced this pull request Feb 14, 2026
Rollup of 11 pull requests

Successful merges:

 - #151998 (Set hidden visibility on naked functions in compiler-builtins)
 - #149460 (rustdoc: sort stable items first)
 - #152076 (Feed `ErrorGuaranteed` from late lifetime resolution errors through to bound variable resolution)
 - #152471 (improve associated-type suggestions from bounds)
 - #152573 (move `escape_symbol_name` to `cg_ssa`)
 - #152594 (c-variadic: implement `va_arg` for `wasm64`)
 - #151386 (rustdoc: more js cleanup)
 - #152567 (nix-dev-shell: fix a typo)
 - #152568 (Port `#[lang]` and `#[panic_handler]` to the new attribute parsers)
 - #152575 (layout_of unexpected rigid alias delayed bug)
 - #152587 (A couple of tiny polonius things)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Search in associated items when type cannot be found

3 participants