Skip to content

Don't suggest assoc ty bound on non-angle-bracketed problematic assoc ty binding #144335

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

Merged
merged 1 commit into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,30 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
fn maybe_suggest_assoc_ty_bound(&self, self_ty: &hir::Ty<'_>, diag: &mut Diag<'_>) {
let mut parents = self.tcx().hir_parent_iter(self_ty.hir_id);

if let Some((_, hir::Node::AssocItemConstraint(constraint))) = parents.next()
if let Some((c_hir_id, hir::Node::AssocItemConstraint(constraint))) = parents.next()
&& let Some(obj_ty) = constraint.ty()
&& let Some((_, hir::Node::TraitRef(trait_ref))) = parents.next()
{
if let Some((_, hir::Node::TraitRef(..))) = parents.next()
&& let Some((_, hir::Node::Ty(ty))) = parents.next()
if let Some((_, hir::Node::Ty(ty))) = parents.next()
&& let hir::TyKind::TraitObject(..) = ty.kind
{
// Assoc ty bounds aren't permitted inside trait object types.
return;
}

if trait_ref
.path
.segments
.iter()
.find_map(|seg| {
seg.args.filter(|args| args.constraints.iter().any(|c| c.hir_id == c_hir_id))
})
.is_none_or(|args| args.parenthesized != hir::GenericArgsParentheses::No)
{
// Only consider angle-bracketed args (where we have a `=` to replace with `:`).
return;
}

let lo = if constraint.gen_args.span_ext.is_dummy() {
constraint.ident.span
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Regression test for issue #105056.
// issue: <https://github.com/rust-lang/rust/issues/105056>
//@ edition: 2021

fn f(_: impl Trait<T = Copy>) {}
Expand All @@ -23,4 +23,11 @@ type Obj = dyn Trait<T = Clone>;

trait Trait { type T; }

// Don't suggest assoc ty bounds when we have parenthesized args (the underlying assoc type
// binding `Output` isn't introduced by `=` but by `->`, suggesting `:` wouldn't be valid).
// issue: <https://github.com/rust-lang/rust/issues/140543>
fn i(_: impl Fn() -> std::fmt::Debug) {}
//~^ ERROR expected a type, found a trait
//~| HELP you can add the `dyn` keyword if you want a trait object

fn main() {}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ help: you can add the `dyn` keyword if you want a trait object
LL | type Obj = dyn Trait<T = dyn Clone>;
| +++

error: aborting due to 4 previous errors
error[E0782]: expected a type, found a trait
--> $DIR/suggest-assoc-ty-bound-on-eq-bound.rs:29:22
|
LL | fn i(_: impl Fn() -> std::fmt::Debug) {}
| ^^^^^^^^^^^^^^^
|
help: you can add the `dyn` keyword if you want a trait object
|
LL | fn i(_: impl Fn() -> dyn std::fmt::Debug) {}
| +++

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0782`.
Loading