Skip to content

Commit 237098c

Browse files
authored
Rollup merge of #144335 - fmease:no-angle-no-colon, r=SparrowLii
Don't suggest assoc ty bound on non-angle-bracketed problematic assoc ty binding Fixes #140543.
2 parents 456acaa + 9bb9c39 commit 237098c

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,17 +447,30 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
447447
fn maybe_suggest_assoc_ty_bound(&self, self_ty: &hir::Ty<'_>, diag: &mut Diag<'_>) {
448448
let mut parents = self.tcx().hir_parent_iter(self_ty.hir_id);
449449

450-
if let Some((_, hir::Node::AssocItemConstraint(constraint))) = parents.next()
450+
if let Some((c_hir_id, hir::Node::AssocItemConstraint(constraint))) = parents.next()
451451
&& let Some(obj_ty) = constraint.ty()
452+
&& let Some((_, hir::Node::TraitRef(trait_ref))) = parents.next()
452453
{
453-
if let Some((_, hir::Node::TraitRef(..))) = parents.next()
454-
&& let Some((_, hir::Node::Ty(ty))) = parents.next()
454+
if let Some((_, hir::Node::Ty(ty))) = parents.next()
455455
&& let hir::TyKind::TraitObject(..) = ty.kind
456456
{
457457
// Assoc ty bounds aren't permitted inside trait object types.
458458
return;
459459
}
460460

461+
if trait_ref
462+
.path
463+
.segments
464+
.iter()
465+
.find_map(|seg| {
466+
seg.args.filter(|args| args.constraints.iter().any(|c| c.hir_id == c_hir_id))
467+
})
468+
.is_none_or(|args| args.parenthesized != hir::GenericArgsParentheses::No)
469+
{
470+
// Only consider angle-bracketed args (where we have a `=` to replace with `:`).
471+
return;
472+
}
473+
461474
let lo = if constraint.gen_args.span_ext.is_dummy() {
462475
constraint.ident.span
463476
} else {

tests/ui/associated-type-bounds/suggest-assoc-ty-bound-on-eq-bound.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Regression test for issue #105056.
1+
// issue: <https://github.com/rust-lang/rust/issues/105056>
22
//@ edition: 2021
33

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

2424
trait Trait { type T; }
2525

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

tests/ui/associated-type-bounds/suggest-assoc-ty-bound-on-eq-bound.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ help: you can add the `dyn` keyword if you want a trait object
5757
LL | type Obj = dyn Trait<T = dyn Clone>;
5858
| +++
5959

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

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

0 commit comments

Comments
 (0)