Skip to content

Commit ec3c5b0

Browse files
committed
Use structured suggestion to surround struct literal with parenthesis
1 parent 9aee7ed commit ec3c5b0

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
@@ -3369,6 +3369,29 @@ impl<'a> Resolver<'a> {
33693369
Ok(ref snippet) if snippet == "{" => true,
33703370
_ => false,
33713371
};
3372+
// In case this could be a struct literal that needs to be surrounded
3373+
// by parenthesis, find the appropriate span.
3374+
let mut i = 0;
3375+
let mut closing_brace = None;
3376+
loop {
3377+
sp = sm.next_point(sp);
3378+
match sm.span_to_snippet(sp) {
3379+
Ok(ref snippet) => {
3380+
if snippet == "}" {
3381+
let sp = span.to(sp);
3382+
if let Ok(snippet) = sm.span_to_snippet(sp) {
3383+
closing_brace = Some((sp, snippet));
3384+
}
3385+
break;
3386+
}
3387+
}
3388+
_ => break,
3389+
}
3390+
i += 1;
3391+
if i > 100 { // The bigger the span the more likely we're
3392+
break; // incorrect. Bound it to 100 chars long.
3393+
}
3394+
}
33723395
match source {
33733396
PathSource::Expr(Some(parent)) => {
33743397
match parent.node {
@@ -3395,11 +3418,20 @@ impl<'a> Resolver<'a> {
33953418
}
33963419
},
33973420
PathSource::Expr(None) if followed_by_brace == true => {
3398-
err.span_label(
3399-
span,
3400-
format!("did you mean `({} {{ /* fields */ }})`?",
3401-
path_str),
3402-
);
3421+
if let Some((sp, snippet)) = closing_brace {
3422+
err.span_suggestion_with_applicability(
3423+
sp,
3424+
"surround the struct literal with parenthesis",
3425+
format!("({})", snippet),
3426+
Applicability::MaybeIncorrect,
3427+
);
3428+
} else {
3429+
err.span_label(
3430+
span,
3431+
format!("did you mean `({} {{ /* fields */ }})`?",
3432+
path_str),
3433+
);
3434+
}
34033435
return (err, candidates);
34043436
},
34053437
_ => {

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)