Skip to content

Commit 777cb9f

Browse files
committed
Skip suggestion with equal spans attribute affected in suggest_trait_and_bounds
Signed-off-by: xizheyin <[email protected]>
1 parent 9bad8ac commit 777cb9f

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -885,10 +885,10 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
885885
// `end_span` is the end of the poly trait ref (Foo + 'baz + Bar><)
886886
let end_span = last_bound.span();
887887
// `last_bound_span` is the last bound of the poly trait ref (Foo + >'baz< + Bar)
888-
let last_bound_span = spans.last().cloned().unwrap();
888+
let last_bound_span = spans.last().cloned();
889889
let mut multi_span: MultiSpan = spans.clone().into();
890890
for sp in spans {
891-
let msg = if sp == last_bound_span {
891+
let msg = if Some(sp) == last_bound_span {
892892
format!(
893893
"...because of {these} bound{s}",
894894
these = pluralize!("this", bounds.len() - 1),
@@ -917,11 +917,13 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
917917
sugg.push((base_error.span.shrink_to_hi().to(end_span), String::new()));
918918
}
919919

920-
err.multipart_suggestion(
921-
"if you meant to use a type and not a trait here, remove the bounds",
922-
sugg,
923-
Applicability::MaybeIncorrect,
924-
);
920+
if !sugg.is_empty() {
921+
err.multipart_suggestion(
922+
"if you meant to use a type and not a trait here, remove the bounds",
923+
sugg,
924+
Applicability::MaybeIncorrect,
925+
);
926+
}
925927
}
926928
}
927929
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![core::contracts::ensures]
2+
//~^ ERROR use of unstable library feature `contracts` [E0658]
3+
//~| ERROR inner macro attributes are unstable [E0658]
4+
//~| ERROR expected trait, found struct `A` [E0404]
5+
//~| ERROR `#[prelude_import]` is for use by rustc only [E0658]
6+
struct A {
7+
b: dyn A + 'static,
8+
}
9+
fn c() {}
10+
11+
fn main(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
error[E0658]: use of unstable library feature `contracts`
2+
--> $DIR/attribute-affected-trait-bound-issue-137129.rs:1:4
3+
|
4+
LL | #![core::contracts::ensures]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
8+
= help: add `#![feature(contracts)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: inner macro attributes are unstable
12+
--> $DIR/attribute-affected-trait-bound-issue-137129.rs:1:4
13+
|
14+
LL | #![core::contracts::ensures]
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #54726 <https://github.com/rust-lang/rust/issues/54726> for more information
18+
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0404]: expected trait, found struct `A`
22+
--> $DIR/attribute-affected-trait-bound-issue-137129.rs:1:1
23+
|
24+
LL | / #![core::contracts::ensures]
25+
... |
26+
LL | | fn main(){}
27+
| |___________^ not a trait
28+
|
29+
help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way
30+
31+
error[E0658]: `#[prelude_import]` is for use by rustc only
32+
--> $DIR/attribute-affected-trait-bound-issue-137129.rs:1:1
33+
|
34+
LL | / #![core::contracts::ensures]
35+
... |
36+
LL | | fn main(){}
37+
| |___________^
38+
|
39+
= help: add `#![feature(prelude_import)]` to the crate attributes to enable
40+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
41+
42+
error: aborting due to 4 previous errors
43+
44+
Some errors have detailed explanations: E0404, E0658.
45+
For more information about an error, try `rustc --explain E0404`.

0 commit comments

Comments
 (0)