@@ -459,6 +459,22 @@ impl ImportResolution {
459
459
460
460
target. unwrap ( ) . shadowable
461
461
}
462
+
463
+ fn set_target_and_id ( & mut self ,
464
+ namespace : Namespace ,
465
+ target : Option < Target > ,
466
+ id : NodeId ) {
467
+ match namespace {
468
+ TypeNS => {
469
+ self . type_target = target;
470
+ self . type_id = id;
471
+ }
472
+ ValueNS => {
473
+ self . value_target = target;
474
+ self . value_id = id;
475
+ }
476
+ }
477
+ }
462
478
}
463
479
464
480
/// The link from a module up to its nearest parent node.
@@ -2705,64 +2721,45 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2705
2721
// We've successfully resolved the import. Write the results in.
2706
2722
let mut import_resolutions = module_. import_resolutions . borrow_mut ( ) ;
2707
2723
let import_resolution = & mut ( * import_resolutions) [ target] ;
2724
+ {
2725
+ let check_and_write_import = |namespace, result : & _ , used_public : & mut bool | {
2726
+ let namespace_name = match namespace {
2727
+ TypeNS => "type" ,
2728
+ ValueNS => "value" ,
2729
+ } ;
2708
2730
2709
- match value_result {
2710
- BoundResult ( ref target_module, ref name_bindings) => {
2711
- debug ! ( "(resolving single import) found value target: {}" ,
2712
- { name_bindings. value_def. borrow( ) . clone( ) . unwrap( ) . def } ) ;
2713
- self . check_for_conflicting_import (
2714
- & import_resolution. value_target ,
2715
- directive. span ,
2716
- target,
2717
- ValueNS ) ;
2718
-
2719
- self . check_that_import_is_importable (
2720
- & * * name_bindings,
2721
- directive. span ,
2722
- target,
2723
- ValueNS ) ;
2724
-
2725
- import_resolution. value_target =
2726
- Some ( Target :: new ( target_module. clone ( ) ,
2727
- name_bindings. clone ( ) ,
2728
- directive. shadowable ) ) ;
2729
- import_resolution. value_id = directive. id ;
2730
- import_resolution. is_public = directive. is_public ;
2731
- value_used_public = name_bindings. defined_in_public_namespace ( ValueNS ) ;
2732
- }
2733
- UnboundResult => { /* Continue. */ }
2734
- UnknownResult => {
2735
- panic ! ( "value result should be known at this point" ) ;
2736
- }
2737
- }
2738
- match type_result {
2739
- BoundResult ( ref target_module, ref name_bindings) => {
2740
- debug ! ( "(resolving single import) found type target: {}" ,
2741
- { name_bindings. type_def. borrow( ) . clone( ) . unwrap( ) . type_def } ) ;
2742
- self . check_for_conflicting_import (
2743
- & import_resolution. type_target ,
2744
- directive. span ,
2745
- target,
2746
- TypeNS ) ;
2747
-
2748
- self . check_that_import_is_importable (
2749
- & * * name_bindings,
2750
- directive. span ,
2751
- target,
2752
- TypeNS ) ;
2753
-
2754
- import_resolution. type_target =
2755
- Some ( Target :: new ( target_module. clone ( ) ,
2756
- name_bindings. clone ( ) ,
2757
- directive. shadowable ) ) ;
2758
- import_resolution. type_id = directive. id ;
2759
- import_resolution. is_public = directive. is_public ;
2760
- type_used_public = name_bindings. defined_in_public_namespace ( TypeNS ) ;
2761
- }
2762
- UnboundResult => { /* Continue. */ }
2763
- UnknownResult => {
2764
- panic ! ( "type result should be known at this point" ) ;
2765
- }
2731
+ match * result {
2732
+ BoundResult ( ref target_module, ref name_bindings) => {
2733
+ debug ! ( "(resolving single import) found {} target: {}" ,
2734
+ namespace_name,
2735
+ name_bindings. def_for_namespace( namespace) ) ;
2736
+ self . check_for_conflicting_import (
2737
+ & import_resolution. target_for_namespace ( namespace) ,
2738
+ directive. span ,
2739
+ target,
2740
+ namespace) ;
2741
+
2742
+ self . check_that_import_is_importable (
2743
+ & * * name_bindings,
2744
+ directive. span ,
2745
+ target,
2746
+ namespace) ;
2747
+
2748
+ let target = Some ( Target :: new ( target_module. clone ( ) ,
2749
+ name_bindings. clone ( ) ,
2750
+ directive. shadowable ) ) ;
2751
+ import_resolution. set_target_and_id ( namespace, target, directive. id ) ;
2752
+ import_resolution. is_public = directive. is_public ;
2753
+ * used_public = name_bindings. defined_in_public_namespace ( namespace) ;
2754
+ }
2755
+ UnboundResult => { /* Continue. */ }
2756
+ UnknownResult => {
2757
+ panic ! ( "{} result should be known at this point" , namespace_name) ;
2758
+ }
2759
+ }
2760
+ } ;
2761
+ check_and_write_import ( ValueNS , & value_result, & mut value_used_public) ;
2762
+ check_and_write_import ( TypeNS , & type_result, & mut type_used_public) ;
2766
2763
}
2767
2764
2768
2765
self . check_for_conflicts_between_imports_and_items (
@@ -2818,7 +2815,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2818
2815
2819
2816
// Resolves a glob import. Note that this function cannot fail; it either
2820
2817
// succeeds or bails out (as importing * from an empty module or a module
2821
- // that exports nothing is valid).
2818
+ // that exports nothing is valid). containing_module is the module we are
2819
+ // actually importing, i.e., `foo` in `use foo::*`.
2822
2820
fn resolve_glob_import ( & mut self ,
2823
2821
module_ : & Module ,
2824
2822
containing_module : Rc < Module > ,
@@ -2844,12 +2842,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2844
2842
assert_eq ! ( containing_module. glob_count. get( ) , 0 ) ;
2845
2843
2846
2844
// Add all resolved imports from the containing module.
2847
- let import_resolutions = containing_module. import_resolutions
2848
- . borrow ( ) ;
2845
+ let import_resolutions = containing_module. import_resolutions . borrow ( ) ;
2849
2846
for ( ident, target_import_resolution) in import_resolutions. iter ( ) {
2850
2847
debug ! ( "(resolving glob import) writing module resolution \
2851
2848
{} into `{}`",
2852
- target_import_resolution . type_target . is_none ( ) ,
2849
+ token :: get_name ( * ident ) ,
2853
2850
self . module_to_string( module_) ) ;
2854
2851
2855
2852
if !target_import_resolution. is_public {
@@ -2869,17 +2866,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2869
2866
// Continue.
2870
2867
}
2871
2868
Some ( ref value_target) => {
2872
- dest_import_resolution. value_target =
2873
- Some ( value_target. clone ( ) ) ;
2869
+ dest_import_resolution. value_target = Some ( value_target. clone ( ) ) ;
2874
2870
}
2875
2871
}
2876
2872
match target_import_resolution. type_target {
2877
2873
None => {
2878
2874
// Continue.
2879
2875
}
2880
2876
Some ( ref type_target) => {
2881
- dest_import_resolution. type_target =
2882
- Some ( type_target. clone ( ) ) ;
2877
+ dest_import_resolution. type_target = Some ( type_target. clone ( ) ) ;
2883
2878
}
2884
2879
}
2885
2880
dest_import_resolution. is_public = is_public;
@@ -2901,8 +2896,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2901
2896
// Add all children from the containing module.
2902
2897
self . populate_module_if_necessary ( & containing_module) ;
2903
2898
2904
- for ( & name, name_bindings) in containing_module. children
2905
- . borrow ( ) . iter ( ) {
2899
+ for ( & name, name_bindings) in containing_module. children . borrow ( ) . iter ( ) {
2906
2900
self . merge_import_resolution ( module_,
2907
2901
containing_module. clone ( ) ,
2908
2902
import_directive,
@@ -2912,8 +2906,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2912
2906
}
2913
2907
2914
2908
// Add external module children from the containing module.
2915
- for ( & name, module) in containing_module. external_module_children
2916
- . borrow ( ) . iter ( ) {
2909
+ for ( & name, module) in containing_module. external_module_children . borrow ( ) . iter ( ) {
2917
2910
let name_bindings =
2918
2911
Rc :: new ( Resolver :: create_name_bindings_from_module ( module. clone ( ) ) ) ;
2919
2912
self . merge_import_resolution ( module_,
@@ -2958,41 +2951,39 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
2958
2951
2959
2952
debug ! ( "(resolving glob import) writing resolution `{}` in `{}` \
2960
2953
to `{}`",
2961
- token:: get_name( name) . get( ) . to_string ( ) ,
2954
+ token:: get_name( name) . get( ) ,
2962
2955
self . module_to_string( & * containing_module) ,
2963
2956
self . module_to_string( module_) ) ;
2964
2957
2965
2958
// Merge the child item into the import resolution.
2966
- if name_bindings. defined_in_namespace_with ( ValueNS , IMPORTABLE | PUBLIC ) {
2967
- debug ! ( "(resolving glob import) ... for value target" ) ;
2968
- if dest_import_resolution. shadowable ( ValueNS ) == Shadowable :: Never {
2969
- let msg = format ! ( "a value named `{}` has already been imported \
2970
- in this module",
2971
- token:: get_name( name) . get( ) ) ;
2972
- self . session . span_err ( import_directive. span , msg. as_slice ( ) ) ;
2973
- } else {
2974
- dest_import_resolution. value_target =
2975
- Some ( Target :: new ( containing_module. clone ( ) ,
2976
- name_bindings. clone ( ) ,
2977
- import_directive. shadowable ) ) ;
2978
- dest_import_resolution. value_id = id;
2979
- }
2980
- }
2981
- if name_bindings. defined_in_namespace_with ( TypeNS , IMPORTABLE | PUBLIC ) {
2982
- debug ! ( "(resolving glob import) ... for type target" ) ;
2983
- if dest_import_resolution. shadowable ( TypeNS ) == Shadowable :: Never {
2984
- let msg = format ! ( "a type named `{}` has already been imported \
2985
- in this module",
2986
- token:: get_name( name) . get( ) ) ;
2987
- self . session . span_err ( import_directive. span , msg. as_slice ( ) ) ;
2988
- } else {
2989
- dest_import_resolution. type_target =
2990
- Some ( Target :: new ( containing_module,
2991
- name_bindings. clone ( ) ,
2992
- import_directive. shadowable ) ) ;
2993
- dest_import_resolution. type_id = id;
2994
- }
2959
+ {
2960
+ let merge_child_item = |namespace| {
2961
+ if name_bindings. defined_in_namespace_with ( namespace, IMPORTABLE | PUBLIC ) {
2962
+ let namespace_name = match namespace {
2963
+ TypeNS => "type" ,
2964
+ ValueNS => "value" ,
2965
+ } ;
2966
+ debug ! ( "(resolving glob import) ... for {} target" , namespace_name) ;
2967
+ if dest_import_resolution. shadowable ( namespace) == Shadowable :: Never {
2968
+ let msg = format ! ( "a {} named `{}` has already been imported \
2969
+ in this module",
2970
+ namespace_name,
2971
+ token:: get_name( name) . get( ) ) ;
2972
+ self . session . span_err ( import_directive. span , msg. as_slice ( ) ) ;
2973
+ } else {
2974
+ let target = Target :: new ( containing_module. clone ( ) ,
2975
+ name_bindings. clone ( ) ,
2976
+ import_directive. shadowable ) ;
2977
+ dest_import_resolution. set_target_and_id ( namespace,
2978
+ Some ( target) ,
2979
+ id) ;
2980
+ }
2981
+ }
2982
+ } ;
2983
+ merge_child_item ( ValueNS ) ;
2984
+ merge_child_item ( TypeNS ) ;
2995
2985
}
2986
+
2996
2987
dest_import_resolution. is_public = is_public;
2997
2988
2998
2989
self . check_for_conflicts_between_imports_and_items (
@@ -3012,6 +3003,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
3012
3003
return
3013
3004
}
3014
3005
3006
+ debug ! ( "check_for_conflicting_import: {}; target exists: {}" ,
3007
+ token:: get_name( name) . get( ) ,
3008
+ target. is_some( ) ) ;
3009
+
3015
3010
match * target {
3016
3011
Some ( ref target) if target. shadowable != Shadowable :: Always => {
3017
3012
let msg = format ! ( "a {} named `{}` has already been imported \
0 commit comments