Skip to content

Commit a775836

Browse files
authored
Rollup merge of rust-lang#57725 - estebank:parens, r=michaelwoerister
Use structured suggestion to surround struct literal with parenthesis
2 parents bb28809 + ec3c5b0 commit a775836

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

src/librustc_resolve/lib.rs

+37-5
Original file line numberDiff line numberDiff line change
@@ -3401,6 +3401,29 @@ impl<'a> Resolver<'a> {
34013401
Ok(ref snippet) if snippet == "{" => true,
34023402
_ => false,
34033403
};
3404+
// In case this could be a struct literal that needs to be surrounded
3405+
// by parenthesis, find the appropriate span.
3406+
let mut i = 0;
3407+
let mut closing_brace = None;
3408+
loop {
3409+
sp = sm.next_point(sp);
3410+
match sm.span_to_snippet(sp) {
3411+
Ok(ref snippet) => {
3412+
if snippet == "}" {
3413+
let sp = span.to(sp);
3414+
if let Ok(snippet) = sm.span_to_snippet(sp) {
3415+
closing_brace = Some((sp, snippet));
3416+
}
3417+
break;
3418+
}
3419+
}
3420+
_ => break,
3421+
}
3422+
i += 1;
3423+
if i > 100 { // The bigger the span the more likely we're
3424+
break; // incorrect. Bound it to 100 chars long.
3425+
}
3426+
}
34043427
match source {
34053428
PathSource::Expr(Some(parent)) => {
34063429
match parent.node {
@@ -3427,11 +3450,20 @@ impl<'a> Resolver<'a> {
34273450
}
34283451
},
34293452
PathSource::Expr(None) if followed_by_brace == true => {
3430-
err.span_label(
3431-
span,
3432-
format!("did you mean `({} {{ /* fields */ }})`?",
3433-
path_str),
3434-
);
3453+
if let Some((sp, snippet)) = closing_brace {
3454+
err.span_suggestion_with_applicability(
3455+
sp,
3456+
"surround the struct literal with parenthesis",
3457+
format!("({})", snippet),
3458+
Applicability::MaybeIncorrect,
3459+
);
3460+
} else {
3461+
err.span_label(
3462+
span,
3463+
format!("did you mean `({} {{ /* fields */ }})`?",
3464+
path_str),
3465+
);
3466+
}
34353467
return (err, candidates);
34363468
},
34373469
_ => {

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,25 @@ error[E0423]: expected value, found struct `S`
2929
--> $DIR/E0423.rs:12:32
3030
|
3131
LL | if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
32-
| ^ did you mean `(S { /* fields */ })`?
32+
| ^---------------
33+
| |
34+
| help: surround the struct literal with parenthesis: `(S { x: 1, y: 2 })`
3335

3436
error[E0423]: expected value, found struct `T`
3537
--> $DIR/E0423.rs:15:8
3638
|
3739
LL | if T {} == T {} { println!("Ok"); }
38-
| ^ did you mean `(T { /* fields */ })`?
40+
| ^---
41+
| |
42+
| help: surround the struct literal with parenthesis: `(T {})`
3943

4044
error[E0423]: expected value, found struct `std::ops::Range`
4145
--> $DIR/E0423.rs:21:14
4246
|
4347
LL | for _ in std::ops::Range { start: 0, end: 10 } {}
44-
| ^^^^^^^^^^^^^^^ did you mean `(std::ops::Range { /* fields */ })`?
48+
| ^^^^^^^^^^^^^^^----------------------
49+
| |
50+
| help: surround the struct literal with parenthesis: `(std::ops::Range { start: 0, end: 10 })`
4551

4652
error: aborting due to 7 previous errors
4753

0 commit comments

Comments
 (0)