@@ -290,8 +290,20 @@ pub enum AllowCapturingSelfFlag {
290
290
291
291
#[ deriving( Eq ) ]
292
292
enum NameSearchType {
293
- SearchItemsAndPublicImports , //< Search items and public imports.
294
- SearchItemsAndAllImports , //< Search items and all imports.
293
+ /// We're doing a name search in order to resolve a `use` directive.
294
+ ImportSearch ,
295
+
296
+ /// We're doing a name search in order to resolve a path type, a path
297
+ /// expression, or a path pattern. We can select public or private
298
+ /// names.
299
+ ///
300
+ /// XXX: This should be ripped out of resolve and handled later, in
301
+ /// the privacy checking phase.
302
+ PathPublicOrPrivateSearch ,
303
+
304
+ /// We're doing a name search in order to resolve a path type, a path
305
+ /// expression, or a path pattern. Allow only public names to be selected.
306
+ PathPublicOnlySearch ,
295
307
}
296
308
297
309
pub enum BareIdentifierPatternResolution {
@@ -425,7 +437,10 @@ pub struct ImportState {
425
437
}
426
438
427
439
pub fn ImportState ( ) -> ImportState {
428
- ImportState { used : false , warned : false }
440
+ ImportState {
441
+ used : false ,
442
+ warned : false ,
443
+ }
429
444
}
430
445
431
446
/// The link from a module up to its nearest parent node.
@@ -440,6 +455,7 @@ pub enum ModuleKind {
440
455
NormalModuleKind ,
441
456
ExternModuleKind ,
442
457
TraitModuleKind ,
458
+ ImplModuleKind ,
443
459
AnonymousModuleKind ,
444
460
}
445
461
@@ -470,7 +486,6 @@ pub struct Module {
470
486
//
471
487
// There will be an anonymous module created around `g` with the ID of the
472
488
// entry block for `f`.
473
-
474
489
anonymous_children : @mut HashMap < node_id , @mut Module > ,
475
490
476
491
// The status of resolving each import in this module.
@@ -560,6 +575,38 @@ pub impl NameBindings {
560
575
}
561
576
}
562
577
578
+ /// Sets the kind of the module, creating a new one if necessary.
579
+ fn set_module_kind ( @mut self ,
580
+ privacy : Privacy ,
581
+ parent_link : ParentLink ,
582
+ def_id : Option < def_id > ,
583
+ kind : ModuleKind ,
584
+ sp : span ) {
585
+ match self . type_def {
586
+ None => {
587
+ let module = @mut Module ( parent_link, def_id, kind) ;
588
+ self . type_def = Some ( TypeNsDef {
589
+ privacy : privacy,
590
+ module_def : Some ( module) ,
591
+ type_def : None
592
+ } )
593
+ }
594
+ Some ( type_def) => {
595
+ match type_def. module_def {
596
+ None => {
597
+ let module = @mut Module ( parent_link, def_id, kind) ;
598
+ self . type_def = Some ( TypeNsDef {
599
+ privacy : privacy,
600
+ module_def : Some ( module) ,
601
+ type_def : type_def. type_def
602
+ } )
603
+ }
604
+ Some ( module_def) => module_def. kind = kind,
605
+ }
606
+ }
607
+ }
608
+ }
609
+
563
610
/// Records a type definition.
564
611
fn define_type ( @mut self , privacy : Privacy , def : def , sp : span ) {
565
612
// Merges the type with the existing type def or creates a new one.
@@ -1234,7 +1281,7 @@ pub impl Resolver {
1234
1281
name_bindings. define_module ( Public ,
1235
1282
parent_link,
1236
1283
Some ( def_id) ,
1237
- TraitModuleKind ,
1284
+ ImplModuleKind ,
1238
1285
sp) ;
1239
1286
1240
1287
let new_parent = ModuleReducedGraphParent (
@@ -1614,8 +1661,8 @@ pub impl Resolver {
1614
1661
// If this is a trait, add all the method names
1615
1662
// to the trait info.
1616
1663
1617
- let method_def_ids = get_trait_method_def_ids ( self . session . cstore ,
1618
- def_id) ;
1664
+ let method_def_ids =
1665
+ get_trait_method_def_ids ( self . session . cstore , def_id) ;
1619
1666
let mut interned_method_names = HashSet :: new ( ) ;
1620
1667
for method_def_ids. each |& method_def_id| {
1621
1668
let ( method_name, explicit_self) =
@@ -1635,6 +1682,14 @@ pub impl Resolver {
1635
1682
self . trait_info . insert ( def_id, interned_method_names) ;
1636
1683
1637
1684
child_name_bindings. define_type ( Public , def, dummy_sp ( ) ) ;
1685
+
1686
+ // Define a module if necessary.
1687
+ let parent_link = self . get_parent_link ( new_parent, ident) ;
1688
+ child_name_bindings. set_module_kind ( Public ,
1689
+ parent_link,
1690
+ Some ( def_id) ,
1691
+ TraitModuleKind ,
1692
+ dummy_sp ( ) )
1638
1693
}
1639
1694
def_ty( _) => {
1640
1695
debug ! ( "(building reduced graph for external \
@@ -1777,6 +1832,10 @@ pub impl Resolver {
1777
1832
// We already have a module. This
1778
1833
// is OK.
1779
1834
type_module = module_def;
1835
+
1836
+ // Mark it as an impl module if
1837
+ // necessary.
1838
+ type_module. kind = ImplModuleKind ;
1780
1839
}
1781
1840
Some ( _) | None => {
1782
1841
let parent_link =
@@ -1786,7 +1845,7 @@ pub impl Resolver {
1786
1845
Public ,
1787
1846
parent_link,
1788
1847
Some ( def) ,
1789
- NormalModuleKind ,
1848
+ ImplModuleKind ,
1790
1849
dummy_sp( ) ) ;
1791
1850
type_module =
1792
1851
child_name_bindings.
@@ -1897,10 +1956,8 @@ pub impl Resolver {
1897
1956
// remain or unsuccessfully when no forward progress in resolving imports
1898
1957
// is made.
1899
1958
1900
- /**
1901
- * Resolves all imports for the crate. This method performs the fixed-
1902
- * point iteration.
1903
- */
1959
+ /// Resolves all imports for the crate. This method performs the fixed-
1960
+ /// point iteration.
1904
1961
fn resolve_imports( @mut self ) {
1905
1962
let mut i = 0 ;
1906
1963
let mut prev_unresolved_imports = 0 ;
@@ -2022,9 +2079,10 @@ pub impl Resolver {
2022
2079
/// don't know whether the name exists at the moment due to other
2023
2080
/// currently-unresolved imports, or success if we know the name exists.
2024
2081
/// If successful, the resolved bindings are written into the module.
2025
- fn resolve_import_for_module( @mut self , module_: @mut Module ,
2082
+ fn resolve_import_for_module( @mut self ,
2083
+ module_: @mut Module ,
2026
2084
import_directive: @ImportDirective )
2027
- -> ResolveResult < ( ) > {
2085
+ -> ResolveResult < ( ) > {
2028
2086
let mut resolution_result = Failed ;
2029
2087
let module_path = & import_directive. module_path ;
2030
2088
@@ -2038,10 +2096,11 @@ pub impl Resolver {
2038
2096
// Use the crate root.
2039
2097
Some ( self . graph_root . get_module ( ) )
2040
2098
} else {
2041
- match self . resolve_module_path_for_import ( module_,
2042
- * module_path,
2043
- DontUseLexicalScope ,
2044
- import_directive. span ) {
2099
+ match self . resolve_module_path ( module_,
2100
+ * module_path,
2101
+ DontUseLexicalScope ,
2102
+ import_directive. span ,
2103
+ ImportSearch ) {
2045
2104
2046
2105
Failed => None ,
2047
2106
Indeterminate => {
@@ -2129,7 +2188,7 @@ pub impl Resolver {
2129
2188
target : ident ,
2130
2189
source : ident ,
2131
2190
span : span )
2132
- -> ResolveResult < ( ) > {
2191
+ -> ResolveResult < ( ) > {
2133
2192
debug ! ( "(resolving single import) resolving `%s` = `%s::%s` from \
2134
2193
`%s`",
2135
2194
* self . session. str_of( target) ,
@@ -2166,9 +2225,7 @@ pub impl Resolver {
2166
2225
// Unless we managed to find a result in both namespaces (unlikely),
2167
2226
// search imports as well.
2168
2227
match ( value_result, type_result) {
2169
- ( BoundResult ( * ) , BoundResult ( * ) ) => {
2170
- // Continue.
2171
- }
2228
+ ( BoundResult ( * ) , BoundResult ( * ) ) => { } // Continue.
2172
2229
_ => {
2173
2230
// If there is an unresolved glob at this point in the
2174
2231
// containing module, bail out. We don't know enough to be
@@ -2496,7 +2553,6 @@ pub impl Resolver {
2496
2553
// Resolve the module part of the path. This does not involve looking
2497
2554
// upward though scope chains; we simply resolve names directly in
2498
2555
// modules as we go.
2499
-
2500
2556
while index < module_path_len {
2501
2557
let name = module_path[ index] ;
2502
2558
match self . resolve_name_in_module ( search_module,
@@ -2506,12 +2562,17 @@ pub impl Resolver {
2506
2562
Failed => {
2507
2563
let segment_name = self . session . str_of ( name) ;
2508
2564
let module_name = self . module_to_str ( search_module) ;
2509
- if module_name == ~"???" {
2510
- self . session . span_err ( span { lo : span. lo , hi : span. lo +
2511
- BytePos ( str:: len ( * segment_name) ) , expn_info :
2512
- span. expn_info } , fmt ! ( "unresolved import. maybe \
2513
- a missing `extern mod %s`?",
2514
- * segment_name) ) ;
2565
+ if "???" == module_name {
2566
+ let span = span {
2567
+ lo : span. lo ,
2568
+ hi : span. lo + BytePos ( str:: len ( * segment_name) ) ,
2569
+ expn_info : span. expn_info ,
2570
+ } ;
2571
+ self . session . span_err ( span,
2572
+ fmt ! ( "unresolved import. maybe \
2573
+ a missing `extern mod \
2574
+ %s`?",
2575
+ * segment_name) ) ;
2515
2576
return Failed ;
2516
2577
}
2517
2578
self . session . span_err ( span, fmt ! ( "unresolved import: could not find `%s` in \
@@ -2540,8 +2601,22 @@ pub impl Resolver {
2540
2601
name) ) ) ;
2541
2602
return Failed ;
2542
2603
}
2543
- Some ( copy module_def) => {
2544
- search_module = module_def;
2604
+ Some ( module_def) => {
2605
+ // If we're doing the search for an
2606
+ // import, do not allow traits and impls
2607
+ // to be selected.
2608
+ match ( name_search_type,
2609
+ module_def. kind ) {
2610
+ ( ImportSearch , TraitModuleKind ) |
2611
+ ( ImportSearch , ImplModuleKind ) => {
2612
+ self . session . span_err (
2613
+ span,
2614
+ ~"cannot import from a trait \
2615
+ or type implementation") ;
2616
+ return Failed ;
2617
+ }
2618
+ ( _, _) => search_module = module_def,
2619
+ }
2545
2620
}
2546
2621
}
2547
2622
}
@@ -2559,31 +2634,27 @@ pub impl Resolver {
2559
2634
2560
2635
index += 1 ;
2561
2636
2562
- // After the first element of the path, allow searching through
2563
- // items and imports unconditionally. This allows things like:
2564
- //
2565
- // pub mod core {
2566
- // pub use vec;
2567
- // }
2637
+ // After the first element of the path, allow searching only
2638
+ // through public identifiers.
2568
2639
//
2569
- // pub mod something_else {
2570
- // use core::vec;
2571
- // }
2572
-
2573
- name_search_type = SearchItemsAndPublicImports ;
2640
+ // XXX: Rip this out and move it to the privacy checker.
2641
+ if name_search_type == PathPublicOrPrivateSearch {
2642
+ name_search_type = PathPublicOnlySearch
2643
+ }
2574
2644
}
2575
2645
2576
2646
return Success ( search_module) ;
2577
2647
}
2578
2648
2579
2649
/// Attempts to resolve the module part of an import directive or path
2580
2650
/// rooted at the given module.
2581
- fn resolve_module_path_for_import ( @mut self ,
2582
- module_ : @mut Module ,
2583
- module_path : & [ ident ] ,
2584
- use_lexical_scope : UseLexicalScopeFlag ,
2585
- span : span )
2586
- -> ResolveResult < @mut Module > {
2651
+ fn resolve_module_path ( @mut self ,
2652
+ module_ : @mut Module ,
2653
+ module_path : & [ ident ] ,
2654
+ use_lexical_scope : UseLexicalScopeFlag ,
2655
+ span : span ,
2656
+ name_search_type : NameSearchType )
2657
+ -> ResolveResult < @mut Module > {
2587
2658
let module_path_len = module_path. len ( ) ;
2588
2659
assert ! ( module_path_len > 0 ) ;
2589
2660
@@ -2666,7 +2737,7 @@ pub impl Resolver {
2666
2737
module_path,
2667
2738
start_index,
2668
2739
span,
2669
- SearchItemsAndPublicImports )
2740
+ name_search_type )
2670
2741
}
2671
2742
2672
2743
/// Invariant: This must only be called during main resolution, not during
@@ -2758,6 +2829,7 @@ pub impl Resolver {
2758
2829
}
2759
2830
ExternModuleKind |
2760
2831
TraitModuleKind |
2832
+ ImplModuleKind |
2761
2833
AnonymousModuleKind => {
2762
2834
search_module = parent_module_node;
2763
2835
}
@@ -2777,7 +2849,7 @@ pub impl Resolver {
2777
2849
match self . resolve_name_in_module ( search_module,
2778
2850
name,
2779
2851
namespace,
2780
- SearchItemsAndAllImports ) {
2852
+ PathPublicOrPrivateSearch ) {
2781
2853
Failed => {
2782
2854
// Continue up the search chain.
2783
2855
}
@@ -2858,6 +2930,7 @@ pub impl Resolver {
2858
2930
NormalModuleKind => return Some ( new_module) ,
2859
2931
ExternModuleKind |
2860
2932
TraitModuleKind |
2933
+ ImplModuleKind |
2861
2934
AnonymousModuleKind => module_ = new_module,
2862
2935
}
2863
2936
}
@@ -2874,7 +2947,10 @@ pub impl Resolver {
2874
2947
-> @mut Module {
2875
2948
match module_. kind {
2876
2949
NormalModuleKind => return module_,
2877
- ExternModuleKind | TraitModuleKind | AnonymousModuleKind => {
2950
+ ExternModuleKind |
2951
+ TraitModuleKind |
2952
+ ImplModuleKind |
2953
+ AnonymousModuleKind => {
2878
2954
match self . get_nearest_normal_module_parent ( module_) {
2879
2955
None => module_,
2880
2956
Some ( new_module) => new_module
@@ -2958,7 +3034,8 @@ pub impl Resolver {
2958
3034
2959
3035
// If this is a search of all imports, we should be done with glob
2960
3036
// resolution at this point.
2961
- if name_search_type == SearchItemsAndAllImports {
3037
+ if name_search_type == PathPublicOrPrivateSearch ||
3038
+ name_search_type == PathPublicOnlySearch {
2962
3039
assert ! ( module_. glob_count == 0 ) ;
2963
3040
}
2964
3041
@@ -2980,7 +3057,7 @@ pub impl Resolver {
2980
3057
}
2981
3058
Some ( target)
2982
3059
if name_search_type ==
2983
- SearchItemsAndAllImports ||
3060
+ PathPublicOrPrivateSearch ||
2984
3061
import_resolution. privacy == Public => {
2985
3062
debug ! ( "(resolving name in module) resolved to \
2986
3063
import") ;
@@ -4506,10 +4583,11 @@ pub impl Resolver {
4506
4583
let module_path_idents = self . intern_module_part_of_path( path) ;
4507
4584
4508
4585
let containing_module;
4509
- match self . resolve_module_path_for_import( self . current_module,
4510
- module_path_idents,
4511
- UseLexicalScope ,
4512
- path. span) {
4586
+ match self . resolve_module_path( self . current_module,
4587
+ module_path_idents,
4588
+ UseLexicalScope ,
4589
+ path. span,
4590
+ PathPublicOnlySearch ) {
4513
4591
Failed => {
4514
4592
self . session. span_err( path. span,
4515
4593
fmt ! ( "use of undeclared module `%s`" ,
@@ -4558,7 +4636,7 @@ pub impl Resolver {
4558
4636
module_path_idents,
4559
4637
0 ,
4560
4638
path. span,
4561
- SearchItemsAndAllImports ) {
4639
+ PathPublicOrPrivateSearch ) {
4562
4640
Failed => {
4563
4641
self . session. span_err( path. span,
4564
4642
fmt ! ( "use of undeclared module `::%s`" ,
0 commit comments