@@ -310,18 +310,23 @@ impl<'a> Resolver<'a> {
310
310
t
311
311
}
312
312
313
- // Define a " dummy" resolution containing a Res::Err as a placeholder for a
314
- // failed resolution
313
+ // Define a dummy resolution containing a ` Res::Err` as a placeholder for a failed resolution,
314
+ // also mark such failed imports as used to avoid duplicate diagnostics.
315
315
fn import_dummy_binding ( & mut self , import : & ' a Import < ' a > ) {
316
- if let ImportKind :: Single { target, .. } = import. kind {
316
+ if let ImportKind :: Single { target, ref target_bindings, .. } = import. kind {
317
+ if target_bindings. iter ( ) . any ( |binding| binding. get ( ) . is_some ( ) ) {
318
+ return ; // Has resolution, do not create the dummy binding
319
+ }
317
320
let dummy_binding = self . dummy_binding ;
318
321
let dummy_binding = self . import ( dummy_binding, import) ;
319
322
self . per_ns ( |this, ns| {
320
323
let key = this. new_key ( target, ns) ;
321
324
let _ = this. try_define ( import. parent_scope . module , key, dummy_binding) ;
322
325
} ) ;
323
- // Consider erroneous imports used to avoid duplicate diagnostics.
324
326
self . record_use ( target, dummy_binding, false ) ;
327
+ } else if import. imported_module . get ( ) . is_none ( ) {
328
+ import. used . set ( true ) ;
329
+ self . used_imports . insert ( import. id ) ;
325
330
}
326
331
}
327
332
}
@@ -386,7 +391,13 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
386
391
. map ( |i| ( false , i) )
387
392
. chain ( indeterminate_imports. into_iter ( ) . map ( |i| ( true , i) ) )
388
393
{
389
- if let Some ( err) = self . finalize_import ( import) {
394
+ let unresolved_import_error = self . finalize_import ( import) ;
395
+
396
+ // If this import is unresolved then create a dummy import
397
+ // resolution for it so that later resolve stages won't complain.
398
+ self . r . import_dummy_binding ( import) ;
399
+
400
+ if let Some ( err) = unresolved_import_error {
390
401
if let ImportKind :: Single { source, ref source_bindings, .. } = import. kind {
391
402
if source. name == kw:: SelfLower {
392
403
// Silence `unresolved import` error if E0429 is already emitted
@@ -396,9 +407,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
396
407
}
397
408
}
398
409
399
- // If the error is a single failed import then create a "fake" import
400
- // resolution for it so that later resolve stages won't complain.
401
- self . r . import_dummy_binding ( import) ;
402
410
if prev_root_id. as_u32 ( ) != 0
403
411
&& prev_root_id. as_u32 ( ) != import. root_id . as_u32 ( )
404
412
&& !errors. is_empty ( )
@@ -418,8 +426,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
418
426
prev_root_id = import. root_id ;
419
427
}
420
428
} else if is_indeterminate {
421
- // Consider erroneous imports used to avoid duplicate diagnostics.
422
- self . r . used_imports . insert ( import. id ) ;
423
429
let path = import_path_to_string (
424
430
& import. module_path . iter ( ) . map ( |seg| seg. ident ) . collect :: < Vec < _ > > ( ) ,
425
431
& import. kind ,
@@ -553,26 +559,23 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
553
559
Err ( Undetermined ) => indeterminate = true ,
554
560
// Don't update the resolution, because it was never added.
555
561
Err ( Determined ) if target. name == kw:: Underscore => { }
556
- Err ( Determined ) => {
562
+ Ok ( binding) if binding. is_importable ( ) => {
563
+ let imported_binding = this. import ( binding, import) ;
564
+ target_bindings[ ns] . set ( Some ( imported_binding) ) ;
565
+ this. define ( parent, target, ns, imported_binding) ;
566
+ }
567
+ source_binding @ ( Ok ( ..) | Err ( Determined ) ) => {
568
+ if source_binding. is_ok ( ) {
569
+ let msg = format ! ( "`{}` is not directly importable" , target) ;
570
+ struct_span_err ! ( this. session, import. span, E0253 , "{}" , & msg)
571
+ . span_label ( import. span , "cannot be imported directly" )
572
+ . emit ( ) ;
573
+ }
557
574
let key = this. new_key ( target, ns) ;
558
575
this. update_resolution ( parent, key, |_, resolution| {
559
576
resolution. single_imports . remove ( & Interned :: new_unchecked ( import) ) ;
560
577
} ) ;
561
578
}
562
- Ok ( binding) if !binding. is_importable ( ) => {
563
- let msg = format ! ( "`{}` is not directly importable" , target) ;
564
- struct_span_err ! ( this. session, import. span, E0253 , "{}" , & msg)
565
- . span_label ( import. span , "cannot be imported directly" )
566
- . emit ( ) ;
567
- // Do not import this illegal binding. Import a dummy binding and pretend
568
- // everything is fine
569
- this. import_dummy_binding ( import) ;
570
- }
571
- Ok ( binding) => {
572
- let imported_binding = this. import ( binding, import) ;
573
- target_bindings[ ns] . set ( Some ( imported_binding) ) ;
574
- this. define ( parent, target, ns, imported_binding) ;
575
- }
576
579
}
577
580
}
578
581
} ) ;
@@ -605,10 +608,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
605
608
) ;
606
609
let no_ambiguity = self . r . ambiguity_errors . len ( ) == prev_ambiguity_errors_len;
607
610
import. vis . set ( orig_vis) ;
608
- if let PathResult :: Failed { .. } | PathResult :: NonModule ( ..) = path_res {
609
- // Consider erroneous imports used to avoid duplicate diagnostics.
610
- self . r . used_imports . insert ( import. id ) ;
611
- }
612
611
let module = match path_res {
613
612
PathResult :: Module ( module) => {
614
613
// Consistency checks, analogous to `finalize_macro_resolutions`.
@@ -872,7 +871,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
872
871
} )
873
872
} else {
874
873
// `resolve_ident_in_module` reported a privacy error.
875
- self . r . import_dummy_binding ( import) ;
876
874
None
877
875
} ;
878
876
}
0 commit comments