@@ -5,7 +5,7 @@ use log::debug;
5
5
use rustc:: hir:: def:: { Def , CtorKind , Namespace :: * } ;
6
6
use rustc:: hir:: def_id:: { CRATE_DEF_INDEX , DefId } ;
7
7
use rustc:: session:: config:: nightly_options;
8
- use syntax:: ast:: { ExprKind } ;
8
+ use syntax:: ast:: { Expr , ExprKind } ;
9
9
use syntax:: symbol:: keywords;
10
10
use syntax_pos:: Span ;
11
11
@@ -250,6 +250,29 @@ impl<'a> Resolver<'a> {
250
250
let ns = source. namespace ( ) ;
251
251
let is_expected = & |def| source. is_expected ( def) ;
252
252
253
+ let path_sep = |err : & mut DiagnosticBuilder < ' _ > , expr : & Expr | match expr. node {
254
+ ExprKind :: Field ( _, ident) => {
255
+ err. span_suggestion (
256
+ expr. span ,
257
+ "use the path separator to refer to an item" ,
258
+ format ! ( "{}::{}" , path_str, ident) ,
259
+ Applicability :: MaybeIncorrect ,
260
+ ) ;
261
+ true
262
+ }
263
+ ExprKind :: MethodCall ( ref segment, ..) => {
264
+ let span = expr. span . with_hi ( segment. ident . span . hi ( ) ) ;
265
+ err. span_suggestion (
266
+ span,
267
+ "use the path separator to refer to an item" ,
268
+ format ! ( "{}::{}" , path_str, segment. ident) ,
269
+ Applicability :: MaybeIncorrect ,
270
+ ) ;
271
+ true
272
+ }
273
+ _ => false ,
274
+ } ;
275
+
253
276
match ( def, source) {
254
277
( Def :: Macro ( ..) , _) => {
255
278
err. span_suggestion (
@@ -259,8 +282,7 @@ impl<'a> Resolver<'a> {
259
282
Applicability :: MaybeIncorrect ,
260
283
) ;
261
284
if path_str == "try" && span. rust_2015 ( ) {
262
- err. note ( "if you want the `try` keyword, \
263
- you need to be in the 2018 edition") ;
285
+ err. note ( "if you want the `try` keyword, you need to be in the 2018 edition" ) ;
264
286
}
265
287
}
266
288
( Def :: TyAlias ( ..) , PathSource :: Trait ( _) ) => {
@@ -269,25 +291,8 @@ impl<'a> Resolver<'a> {
269
291
err. note ( "did you mean to use a trait alias?" ) ;
270
292
}
271
293
}
272
- ( Def :: Mod ( ..) , PathSource :: Expr ( Some ( parent) ) ) => match parent. node {
273
- ExprKind :: Field ( _, ident) => {
274
- err. span_suggestion (
275
- parent. span ,
276
- "use the path separator to refer to an item" ,
277
- format ! ( "{}::{}" , path_str, ident) ,
278
- Applicability :: MaybeIncorrect ,
279
- ) ;
280
- }
281
- ExprKind :: MethodCall ( ref segment, ..) => {
282
- let span = parent. span . with_hi ( segment. ident . span . hi ( ) ) ;
283
- err. span_suggestion (
284
- span,
285
- "use the path separator to refer to an item" ,
286
- format ! ( "{}::{}" , path_str, segment. ident) ,
287
- Applicability :: MaybeIncorrect ,
288
- ) ;
289
- }
290
- _ => return false ,
294
+ ( Def :: Mod ( ..) , PathSource :: Expr ( Some ( parent) ) ) => if !path_sep ( err, & parent) {
295
+ return false ;
291
296
} ,
292
297
( Def :: Enum ( ..) , PathSource :: TupleStruct )
293
298
| ( Def :: Enum ( ..) , PathSource :: Expr ( ..) ) => {
@@ -315,8 +320,10 @@ impl<'a> Resolver<'a> {
315
320
= self . struct_constructors . get ( & def_id) . cloned ( ) {
316
321
let accessible_ctor = self . is_accessible ( ctor_vis) ;
317
322
if is_expected ( ctor_def) && !accessible_ctor {
318
- err. span_label ( span, format ! ( "constructor is not visible \
319
- here due to private fields") ) ;
323
+ err. span_label (
324
+ span,
325
+ format ! ( "constructor is not visible here due to private fields" ) ,
326
+ ) ;
320
327
}
321
328
} else {
322
329
// HACK(estebank): find a better way to figure out that this was a
@@ -366,28 +373,12 @@ impl<'a> Resolver<'a> {
366
373
}
367
374
}
368
375
match source {
369
- PathSource :: Expr ( Some ( parent) ) => {
370
- match parent. node {
371
- ExprKind :: MethodCall ( ref path_assignment, _) => {
372
- err. span_suggestion (
373
- sm. start_point ( parent. span )
374
- . to ( path_assignment. ident . span ) ,
375
- "use `::` to access an associated function" ,
376
- format ! ( "{}::{}" ,
377
- path_str,
378
- path_assignment. ident) ,
379
- Applicability :: MaybeIncorrect
380
- ) ;
381
- } ,
382
- _ => {
383
- err. span_label (
384
- span,
385
- format ! ( "did you mean `{} {{ /* fields */ }}`?" ,
386
- path_str) ,
387
- ) ;
388
- } ,
389
- }
390
- } ,
376
+ PathSource :: Expr ( Some ( parent) ) => if !path_sep ( err, & parent) {
377
+ err. span_label (
378
+ span,
379
+ format ! ( "did you mean `{} {{ /* fields */ }}`?" , path_str) ,
380
+ ) ;
381
+ }
391
382
PathSource :: Expr ( None ) if followed_by_brace == true => {
392
383
if let Some ( ( sp, snippet) ) = closing_brace {
393
384
err. span_suggestion (
@@ -399,16 +390,14 @@ impl<'a> Resolver<'a> {
399
390
} else {
400
391
err. span_label (
401
392
span,
402
- format ! ( "did you mean `({} {{ /* fields */ }})`?" ,
403
- path_str) ,
393
+ format ! ( "did you mean `({} {{ /* fields */ }})`?" , path_str) ,
404
394
) ;
405
395
}
406
396
} ,
407
397
_ => {
408
398
err. span_label (
409
399
span,
410
- format ! ( "did you mean `{} {{ /* fields */ }}`?" ,
411
- path_str) ,
400
+ format ! ( "did you mean `{} {{ /* fields */ }}`?" , path_str) ,
412
401
) ;
413
402
} ,
414
403
}
@@ -417,13 +406,11 @@ impl<'a> Resolver<'a> {
417
406
( Def :: Union ( ..) , _) |
418
407
( Def :: Variant ( ..) , _) |
419
408
( Def :: Ctor ( _, _, CtorKind :: Fictive ) , _) if ns == ValueNS => {
420
- err. span_label ( span, format ! ( "did you mean `{} {{ /* fields */ }}`?" ,
421
- path_str) ) ;
409
+ err. span_label ( span, format ! ( "did you mean `{} {{ /* fields */ }}`?" , path_str) ) ;
422
410
}
423
411
( Def :: SelfTy ( ..) , _) if ns == ValueNS => {
424
412
err. span_label ( span, fallback_label) ;
425
- err. note ( "can't use `Self` as a constructor, you must use the \
426
- implemented struct") ;
413
+ err. note ( "can't use `Self` as a constructor, you must use the implemented struct" ) ;
427
414
}
428
415
( Def :: TyAlias ( _) , _) | ( Def :: AssociatedTy ( ..) , _) if ns == ValueNS => {
429
416
err. note ( "can't use a type alias as a constructor" ) ;
0 commit comments