@@ -3369,6 +3369,29 @@ impl<'a> Resolver<'a> {
3369
3369
Ok ( ref snippet) if snippet == "{" => true ,
3370
3370
_ => false ,
3371
3371
} ;
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
+ }
3372
3395
match source {
3373
3396
PathSource :: Expr ( Some ( parent) ) => {
3374
3397
match parent. node {
@@ -3395,11 +3418,20 @@ impl<'a> Resolver<'a> {
3395
3418
}
3396
3419
} ,
3397
3420
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
+ }
3403
3435
return ( err, candidates) ;
3404
3436
} ,
3405
3437
_ => {
0 commit comments