Skip to content

Commit ac62dce

Browse files
authored
Rollup merge of #71438 - estebank:resolve-sugg-tiny, r=petrochenkov
Tweak some suggestions in `rustc_resolve`
2 parents 94ac0ac + 6e3ba6f commit ac62dce

File tree

7 files changed

+56
-25
lines changed

7 files changed

+56
-25
lines changed

src/librustc_resolve/late/diagnostics.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
383383
has_self_arg
384384
}
385385

386-
fn followed_by_brace(&self, span: Span) -> (bool, Option<(Span, String)>) {
386+
fn followed_by_brace(&self, span: Span) -> (bool, Option<Span>) {
387387
// HACK(estebank): find a better way to figure out that this was a
388388
// parser issue where a struct literal is being used on an expression
389389
// where a brace being opened means a block is being started. Look
@@ -406,18 +406,15 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
406406
_ => false,
407407
};
408408
// In case this could be a struct literal that needs to be surrounded
409-
// by parenthesis, find the appropriate span.
409+
// by parentheses, find the appropriate span.
410410
let mut i = 0;
411411
let mut closing_brace = None;
412412
loop {
413413
sp = sm.next_point(sp);
414414
match sm.span_to_snippet(sp) {
415415
Ok(ref snippet) => {
416416
if snippet == "}" {
417-
let sp = span.to(sp);
418-
if let Ok(snippet) = sm.span_to_snippet(sp) {
419-
closing_brace = Some((sp, snippet));
420-
}
417+
closing_brace = Some(span.to(sp));
421418
break;
422419
}
423420
}
@@ -479,17 +476,23 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
479476
suggested = path_sep(err, &parent);
480477
}
481478
PathSource::Expr(None) if followed_by_brace => {
482-
if let Some((sp, snippet)) = closing_brace {
483-
err.span_suggestion(
484-
sp,
485-
"surround the struct literal with parenthesis",
486-
format!("({})", snippet),
479+
if let Some(sp) = closing_brace {
480+
err.multipart_suggestion(
481+
"surround the struct literal with parentheses",
482+
vec![
483+
(sp.shrink_to_lo(), "(".to_string()),
484+
(sp.shrink_to_hi(), ")".to_string()),
485+
],
487486
Applicability::MaybeIncorrect,
488487
);
489488
} else {
490489
err.span_label(
491-
span, // Note the parenthesis surrounding the suggestion below
492-
format!("did you mean `({} {{ /* fields */ }})`?", path_str),
490+
span, // Note the parentheses surrounding the suggestion below
491+
format!(
492+
"you might want to surround a struct literal with parentheses: \
493+
`({} {{ /* fields */ }})`?",
494+
path_str
495+
),
493496
);
494497
}
495498
suggested = true;
@@ -516,10 +519,16 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
516519
err.note("if you want the `try` keyword, you need to be in the 2018 edition");
517520
}
518521
}
519-
(Res::Def(DefKind::TyAlias, _), PathSource::Trait(_)) => {
522+
(Res::Def(DefKind::TyAlias, def_id), PathSource::Trait(_)) => {
520523
err.span_label(span, "type aliases cannot be used as traits");
521524
if nightly_options::is_nightly_build() {
522-
err.note("did you mean to use a trait alias?");
525+
let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \
526+
`type` alias";
527+
if let Some(span) = self.r.definitions.opt_span(def_id) {
528+
err.span_help(span, msg);
529+
} else {
530+
err.help(msg);
531+
}
523532
}
524533
}
525534
(Res::Def(DefKind::Mod, _), PathSource::Expr(Some(parent))) => {

src/test/ui/codemap_tests/two_files.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ error[E0404]: expected trait, found type alias `Bar`
44
LL | impl Bar for Baz { }
55
| ^^^ type aliases cannot be used as traits
66
|
7-
= note: did you mean to use a trait alias?
7+
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
8+
--> $DIR/two_files_data.rs:5:1
9+
|
10+
LL | type Bar = dyn Foo;
11+
| ^^^^^^^^^^^^^^^^^^^
812

913
error: aborting due to previous error
1014

src/test/ui/error-codes/E0423.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ error[E0423]: expected value, found struct `T`
4545
--> $DIR/E0423.rs:14:8
4646
|
4747
LL | if T {} == T {} { println!("Ok"); }
48-
| ^---
49-
| |
50-
| help: surround the struct literal with parenthesis: `(T {})`
48+
| ^
49+
|
50+
help: surround the struct literal with parentheses
51+
|
52+
LL | if (T {}) == T {} { println!("Ok"); }
53+
| ^ ^
5154

5255
error: aborting due to 5 previous errors
5356

src/test/ui/resolve/issue-3907.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ error[E0404]: expected trait, found type alias `Foo`
44
LL | impl Foo for S {
55
| ^^^ type aliases cannot be used as traits
66
|
7-
= note: did you mean to use a trait alias?
7+
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
8+
--> $DIR/issue-3907.rs:5:1
9+
|
10+
LL | type Foo = dyn issue_3907::Foo;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
812
help: possible better candidate is found in another module, you can import it into scope
913
|
1014
LL | use issue_3907::Foo;

src/test/ui/resolve/issue-5035.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ LL | impl K for isize {}
1616
| type aliases cannot be used as traits
1717
| help: a trait with a similar name exists: `I`
1818
|
19-
= note: did you mean to use a trait alias?
19+
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
20+
--> $DIR/issue-5035.rs:2:1
21+
|
22+
LL | type K = dyn I;
23+
| ^^^^^^^^^^^^^^^
2024

2125
error: aborting due to 2 previous errors
2226

src/test/ui/resolve/unboxed-closure-sugar-nonexistent-trait.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ error[E0404]: expected trait, found type alias `Typedef`
1010
LL | fn g<F:Typedef(isize) -> isize>(x: F) {}
1111
| ^^^^^^^^^^^^^^^^^^^^^^^ type aliases cannot be used as traits
1212
|
13-
= note: did you mean to use a trait alias?
13+
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
14+
--> $DIR/unboxed-closure-sugar-nonexistent-trait.rs:4:1
15+
|
16+
LL | type Typedef = isize;
17+
| ^^^^^^^^^^^^^^^^^^^^^
1418

1519
error: aborting due to 2 previous errors
1620

src/test/ui/struct-literal-variant-in-if.stderr

+6-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ error[E0423]: expected value, found struct variant `E::V`
4646
--> $DIR/struct-literal-variant-in-if.rs:10:13
4747
|
4848
LL | if x == E::V { field } {}
49-
| ^^^^----------
50-
| |
51-
| help: surround the struct literal with parenthesis: `(E::V { field })`
49+
| ^^^^
50+
|
51+
help: surround the struct literal with parentheses
52+
|
53+
LL | if x == (E::V { field }) {}
54+
| ^ ^
5255

5356
error[E0308]: mismatched types
5457
--> $DIR/struct-literal-variant-in-if.rs:10:20

0 commit comments

Comments
 (0)