Skip to content

Commit e89ce46

Browse files
committed
Suggest removing bounds even when potential typo
1 parent 5217007 commit e89ce46

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,11 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
439439
}
440440
}
441441

442-
if !self.type_ascription_suggestion(&mut err, base_span)
443-
&& !self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span)
444-
{
445-
// Fallback label.
446-
err.span_label(base_span, fallback_label);
447-
442+
if !self.type_ascription_suggestion(&mut err, base_span) {
443+
let mut fallback = false;
448444
if let PathSource::Trait(AliasPossibility::Maybe) = source {
449445
if let Some(bounds @ [_, .., _]) = self.diagnostic_metadata.current_trait_object {
446+
fallback = true;
450447
let spans: Vec<Span> = bounds
451448
.iter()
452449
.map(|bound| bound.span())
@@ -500,16 +497,25 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
500497
}
501498
}
502499
}
503-
match self.diagnostic_metadata.current_let_binding {
504-
Some((pat_sp, Some(ty_sp), None)) if ty_sp.contains(base_span) && could_be_expr => {
505-
err.span_suggestion_short(
506-
pat_sp.between(ty_sp),
507-
"use `=` if you meant to assign",
508-
" = ".to_string(),
509-
Applicability::MaybeIncorrect,
510-
);
500+
if !self.r.add_typo_suggestion(&mut err, typo_sugg, ident_span) {
501+
fallback = true;
502+
match self.diagnostic_metadata.current_let_binding {
503+
Some((pat_sp, Some(ty_sp), None))
504+
if ty_sp.contains(base_span) && could_be_expr =>
505+
{
506+
err.span_suggestion_short(
507+
pat_sp.between(ty_sp),
508+
"use `=` if you meant to assign",
509+
" = ".to_string(),
510+
Applicability::MaybeIncorrect,
511+
);
512+
}
513+
_ => {}
511514
}
512-
_ => {}
515+
}
516+
if fallback {
517+
// Fallback label.
518+
err.span_label(base_span, fallback_label);
513519
}
514520
}
515521
(err, candidates)

src/test/ui/traits/trait-bounds-not-on-struct.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,23 @@ error[E0404]: expected trait, found struct `Traitor`
144144
LL | trait Trait {}
145145
| ----------- similarly named trait `Trait` defined here
146146
LL | fn g() -> Traitor + 'static {
147-
| ^^^^^^^ help: a trait with a similar name exists: `Trait`
147+
| ^^^^^^^ not a trait
148+
|
149+
help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way
150+
--> $DIR/trait-bounds-not-on-struct.rs:35:21
151+
|
152+
LL | fn g() -> Traitor + 'static {
153+
| ------- ^^^^^^^ ...because of this bound
154+
| |
155+
| expected this type to be a trait...
156+
help: if you meant to use a type and not a trait here, remove the bounds
157+
|
158+
LL | fn g() -> Traitor {
159+
| --
160+
help: a trait with a similar name exists
161+
|
162+
LL | fn g() -> Trait + 'static {
163+
| ^^^^^
148164

149165
error: aborting due to 11 previous errors
150166

0 commit comments

Comments
 (0)