@@ -3401,6 +3401,29 @@ impl<'a> Resolver<'a> {
3401
3401
Ok ( ref snippet) if snippet == "{" => true ,
3402
3402
_ => false ,
3403
3403
} ;
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
+ }
3404
3427
match source {
3405
3428
PathSource :: Expr ( Some ( parent) ) => {
3406
3429
match parent. node {
@@ -3427,11 +3450,20 @@ impl<'a> Resolver<'a> {
3427
3450
}
3428
3451
} ,
3429
3452
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
+ }
3435
3467
return ( err, candidates) ;
3436
3468
} ,
3437
3469
_ => {
0 commit comments