@@ -583,18 +583,23 @@ impl<'a> Resolver<'a> {
583
583
t
584
584
}
585
585
586
- // Define a " dummy" resolution containing a Res::Err as a placeholder for a
587
- // failed resolution
586
+ // Define a dummy resolution containing a ` Res::Err` as a placeholder for a failed resolution,
587
+ // also mark such failed imports as used to avoid duplicate diagnostics.
588
588
fn import_dummy_binding ( & mut self , import : & ' a Import < ' a > ) {
589
- if let ImportKind :: Single { target, .. } = import. kind {
589
+ if let ImportKind :: Single { target, ref target_bindings, .. } = import. kind {
590
+ if target_bindings. iter ( ) . any ( |binding| binding. get ( ) . is_some ( ) ) {
591
+ return ; // Has resolution, do not create the dummy binding
592
+ }
590
593
let dummy_binding = self . dummy_binding ;
591
594
let dummy_binding = self . import ( dummy_binding, import) ;
592
595
self . per_ns ( |this, ns| {
593
596
let key = this. new_key ( target, ns) ;
594
597
let _ = this. try_define ( import. parent_scope . module , key, dummy_binding) ;
595
598
} ) ;
596
- // Consider erroneous imports used to avoid duplicate diagnostics.
597
599
self . record_use ( target, dummy_binding, false ) ;
600
+ } else if import. imported_module . get ( ) . is_none ( ) {
601
+ import. used . set ( true ) ;
602
+ self . used_imports . insert ( import. id ) ;
598
603
}
599
604
}
600
605
}
@@ -659,7 +664,13 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
659
664
. map ( |i| ( false , i) )
660
665
. chain ( indeterminate_imports. into_iter ( ) . map ( |i| ( true , i) ) )
661
666
{
662
- if let Some ( err) = self . finalize_import ( import) {
667
+ let unresolved_import_error = self . finalize_import ( import) ;
668
+
669
+ // If this import is unresolved then create a dummy import
670
+ // resolution for it so that later resolve stages won't complain.
671
+ self . r . import_dummy_binding ( import) ;
672
+
673
+ if let Some ( err) = unresolved_import_error {
663
674
if let ImportKind :: Single { source, ref source_bindings, .. } = import. kind {
664
675
if source. name == kw:: SelfLower {
665
676
// Silence `unresolved import` error if E0429 is already emitted
@@ -669,9 +680,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
669
680
}
670
681
}
671
682
672
- // If the error is a single failed import then create a "fake" import
673
- // resolution for it so that later resolve stages won't complain.
674
- self . r . import_dummy_binding ( import) ;
675
683
if prev_root_id. as_u32 ( ) != 0
676
684
&& prev_root_id. as_u32 ( ) != import. root_id . as_u32 ( )
677
685
&& !errors. is_empty ( )
@@ -691,8 +699,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
691
699
prev_root_id = import. root_id ;
692
700
}
693
701
} else if is_indeterminate {
694
- // Consider erroneous imports used to avoid duplicate diagnostics.
695
- self . r . used_imports . insert ( import. id ) ;
696
702
let path = import_path_to_string (
697
703
& import. module_path . iter ( ) . map ( |seg| seg. ident ) . collect :: < Vec < _ > > ( ) ,
698
704
& import. kind ,
@@ -824,26 +830,23 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
824
830
Err ( Undetermined ) => indeterminate = true ,
825
831
// Don't update the resolution, because it was never added.
826
832
Err ( Determined ) if target. name == kw:: Underscore => { }
827
- Err ( Determined ) => {
833
+ Ok ( binding) if binding. is_importable ( ) => {
834
+ let imported_binding = this. import ( binding, import) ;
835
+ target_bindings[ ns] . set ( Some ( imported_binding) ) ;
836
+ this. define ( parent, target, ns, imported_binding) ;
837
+ }
838
+ source_binding @ ( Ok ( ..) | Err ( Determined ) ) => {
839
+ if source_binding. is_ok ( ) {
840
+ let msg = format ! ( "`{}` is not directly importable" , target) ;
841
+ struct_span_err ! ( this. session, import. span, E0253 , "{}" , & msg)
842
+ . span_label ( import. span , "cannot be imported directly" )
843
+ . emit ( ) ;
844
+ }
828
845
let key = this. new_key ( target, ns) ;
829
846
this. update_resolution ( parent, key, |_, resolution| {
830
847
resolution. single_imports . remove ( & Interned :: new_unchecked ( import) ) ;
831
848
} ) ;
832
849
}
833
- Ok ( binding) if !binding. is_importable ( ) => {
834
- let msg = format ! ( "`{}` is not directly importable" , target) ;
835
- struct_span_err ! ( this. session, import. span, E0253 , "{}" , & msg)
836
- . span_label ( import. span , "cannot be imported directly" )
837
- . emit ( ) ;
838
- // Do not import this illegal binding. Import a dummy binding and pretend
839
- // everything is fine
840
- this. import_dummy_binding ( import) ;
841
- }
842
- Ok ( binding) => {
843
- let imported_binding = this. import ( binding, import) ;
844
- target_bindings[ ns] . set ( Some ( imported_binding) ) ;
845
- this. define ( parent, target, ns, imported_binding) ;
846
- }
847
850
}
848
851
}
849
852
} ) ;
@@ -876,10 +879,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
876
879
self . r . unusable_binding = orig_unusable_binding;
877
880
}
878
881
import. vis . set ( orig_vis) ;
879
- if let PathResult :: Failed { .. } | PathResult :: NonModule ( ..) = path_res {
880
- // Consider erroneous imports used to avoid duplicate diagnostics.
881
- self . r . used_imports . insert ( import. id ) ;
882
- }
883
882
let module = match path_res {
884
883
PathResult :: Module ( module) => {
885
884
// Consistency checks, analogous to `finalize_macro_resolutions`.
@@ -1144,7 +1143,6 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
1144
1143
} )
1145
1144
} else {
1146
1145
// `resolve_ident_in_module` reported a privacy error.
1147
- self . r . import_dummy_binding ( import) ;
1148
1146
None
1149
1147
} ;
1150
1148
}
0 commit comments