@@ -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.
@@ -562,6 +577,38 @@ pub impl NameBindings {
562
577
self.type_span = Some(sp);
563
578
}
564
579
580
+ /// Sets the kind of the module, creating a new one if necessary.
581
+ fn set_module_kind(@mut self,
582
+ privacy: Privacy,
583
+ parent_link: ParentLink,
584
+ def_id: Option<def_id>,
585
+ kind: ModuleKind,
586
+ sp: span) {
587
+ match self.type_def {
588
+ None => {
589
+ let module = @mut Module(parent_link, def_id, kind);
590
+ self.type_def = Some(TypeNsDef {
591
+ privacy: privacy,
592
+ module_def: Some(module),
593
+ type_def: None
594
+ })
595
+ }
596
+ Some(type_def) => {
597
+ match type_def.module_def {
598
+ None => {
599
+ let module = @mut Module(parent_link, def_id, kind);
600
+ self.type_def = Some(TypeNsDef {
601
+ privacy: privacy,
602
+ module_def: Some(module),
603
+ type_def: type_def.type_def
604
+ })
605
+ }
606
+ Some(module_def) => module_def.kind = kind,
607
+ }
608
+ }
609
+ }
610
+ }
611
+
565
612
/// Records a type definition.
566
613
fn define_type(@mut self, privacy: Privacy, def: def, sp: span) {
567
614
// Merges the type with the existing type def or creates a new one.
@@ -1228,7 +1275,7 @@ pub impl Resolver {
1228
1275
name_bindings. define_module ( Public ,
1229
1276
parent_link,
1230
1277
Some ( def_id) ,
1231
- TraitModuleKind ,
1278
+ ImplModuleKind ,
1232
1279
sp) ;
1233
1280
1234
1281
let new_parent = ModuleReducedGraphParent (
@@ -1608,8 +1655,8 @@ pub impl Resolver {
1608
1655
// If this is a trait, add all the method names
1609
1656
// to the trait info.
1610
1657
1611
- let method_def_ids = get_trait_method_def_ids(self.session.cstore,
1612
- def_id);
1658
+ let method_def_ids =
1659
+ get_trait_method_def_ids(self.session.cstore, def_id);
1613
1660
let mut interned_method_names = HashSet::new();
1614
1661
for method_def_ids.each |&method_def_id| {
1615
1662
let (method_name, self_ty) =
@@ -1629,6 +1676,14 @@ pub impl Resolver {
1629
1676
self.trait_info.insert(def_id, interned_method_names);
1630
1677
1631
1678
child_name_bindings.define_type(Public, def, dummy_sp());
1679
+
1680
+ // Define a module if necessary.
1681
+ let parent_link = self.get_parent_link(new_parent, ident);
1682
+ child_name_bindings.set_module_kind(Public,
1683
+ parent_link,
1684
+ Some(def_id),
1685
+ TraitModuleKind,
1686
+ dummy_sp())
1632
1687
}
1633
1688
def_ty(_) => {
1634
1689
debug!(" ( building reduced graph for external \
@@ -1771,6 +1826,10 @@ pub impl Resolver {
1771
1826
// We already have a module. This
1772
1827
// is OK.
1773
1828
type_module = module_def;
1829
+
1830
+ // Mark it as an impl module if
1831
+ // necessary.
1832
+ type_module. kind = ImplModuleKind ;
1774
1833
}
1775
1834
Some ( _) | None => {
1776
1835
let parent_link =
@@ -1780,7 +1839,7 @@ pub impl Resolver {
1780
1839
Public ,
1781
1840
parent_link,
1782
1841
Some ( def) ,
1783
- NormalModuleKind ,
1842
+ ImplModuleKind ,
1784
1843
dummy_sp( ) ) ;
1785
1844
type_module =
1786
1845
child_name_bindings.
@@ -1891,10 +1950,8 @@ pub impl Resolver {
1891
1950
// remain or unsuccessfully when no forward progress in resolving imports
1892
1951
// is made.
1893
1952
1894
- /**
1895
- * Resolves all imports for the crate. This method performs the fixed-
1896
- * point iteration.
1897
- */
1953
+ /// Resolves all imports for the crate. This method performs the fixed-
1954
+ /// point iteration.
1898
1955
fn resolve_imports( @mut self ) {
1899
1956
let mut i = 0 ;
1900
1957
let mut prev_unresolved_imports = 0 ;
@@ -2016,9 +2073,10 @@ pub impl Resolver {
2016
2073
/// don't know whether the name exists at the moment due to other
2017
2074
/// currently-unresolved imports, or success if we know the name exists.
2018
2075
/// If successful, the resolved bindings are written into the module.
2019
- fn resolve_import_for_module( @mut self , module_: @mut Module ,
2076
+ fn resolve_import_for_module( @mut self ,
2077
+ module_: @mut Module ,
2020
2078
import_directive: @ImportDirective )
2021
- -> ResolveResult < ( ) > {
2079
+ -> ResolveResult < ( ) > {
2022
2080
let mut resolution_result = Failed ;
2023
2081
let module_path = & import_directive. module_path ;
2024
2082
@@ -2032,10 +2090,11 @@ pub impl Resolver {
2032
2090
// Use the crate root.
2033
2091
Some ( self . graph_root . get_module ( ) )
2034
2092
} else {
2035
- match self . resolve_module_path_for_import ( module_,
2036
- * module_path,
2037
- DontUseLexicalScope ,
2038
- import_directive. span ) {
2093
+ match self . resolve_module_path ( module_,
2094
+ * module_path,
2095
+ DontUseLexicalScope ,
2096
+ import_directive. span ,
2097
+ ImportSearch ) {
2039
2098
2040
2099
Failed => None ,
2041
2100
Indeterminate => {
@@ -2122,7 +2181,7 @@ pub impl Resolver {
2122
2181
containing_module : @mut Module ,
2123
2182
target : ident ,
2124
2183
source : ident )
2125
- -> ResolveResult < ( ) > {
2184
+ -> ResolveResult < ( ) > {
2126
2185
debug ! ( "(resolving single import) resolving `%s` = `%s::%s` from \
2127
2186
`%s`",
2128
2187
* self . session. str_of( target) ,
@@ -2159,9 +2218,7 @@ pub impl Resolver {
2159
2218
// Unless we managed to find a result in both namespaces (unlikely),
2160
2219
// search imports as well.
2161
2220
match ( value_result, type_result) {
2162
- ( BoundResult ( * ) , BoundResult ( * ) ) => {
2163
- // Continue.
2164
- }
2221
+ ( BoundResult ( * ) , BoundResult ( * ) ) => { } // Continue.
2165
2222
_ => {
2166
2223
// If there is an unresolved glob at this point in the
2167
2224
// containing module, bail out. We don't know enough to be
@@ -2489,7 +2546,6 @@ pub impl Resolver {
2489
2546
// Resolve the module part of the path. This does not involve looking
2490
2547
// upward though scope chains; we simply resolve names directly in
2491
2548
// modules as we go.
2492
-
2493
2549
while index < module_path_len {
2494
2550
let name = module_path[ index] ;
2495
2551
match self . resolve_name_in_module( search_module,
@@ -2499,12 +2555,17 @@ pub impl Resolver {
2499
2555
Failed => {
2500
2556
let segment_name = self . session. str_of( name) ;
2501
2557
let module_name = self . module_to_str( search_module) ;
2502
- if module_name == ~"???" {
2503
- self.session.span_err(span {lo: span.lo, hi: span.lo +
2504
- BytePos(str::len(*segment_name)), expn_info:
2505
- span.expn_info}, fmt!(" unresolved import. maybe \
2506
- a missing `extern mod %s`?",
2507
- *segment_name));
2558
+ if "???" == module_name {
2559
+ let span = span {
2560
+ lo: span. lo,
2561
+ hi: span. lo + BytePos ( str :: len( * segment_name) ) ,
2562
+ expn_info: span. expn_info,
2563
+ } ;
2564
+ self . session. span_err( span,
2565
+ fmt!( "unresolved import. maybe \
2566
+ a missing `extern mod \
2567
+ %s`?",
2568
+ * segment_name) ) ;
2508
2569
return Failed ;
2509
2570
}
2510
2571
self . session. span_err( span, fmt!( "unresolved import: could not find `%s` in \
@@ -2533,8 +2594,22 @@ pub impl Resolver {
2533
2594
name) ) ) ;
2534
2595
return Failed ;
2535
2596
}
2536
- Some ( copy module_def) => {
2537
- search_module = module_def;
2597
+ Some ( module_def) => {
2598
+ // If we're doing the search for an
2599
+ // import, do not allow traits and impls
2600
+ // to be selected.
2601
+ match ( name_search_type,
2602
+ module_def. kind) {
2603
+ ( ImportSearch , TraitModuleKind ) |
2604
+ ( ImportSearch , ImplModuleKind ) => {
2605
+ self . session. span_err(
2606
+ span,
2607
+ ~"cannot import from a trait \
2608
+ or type implementation");
2609
+ return Failed;
2610
+ }
2611
+ (_, _) => search_module = module_def,
2612
+ }
2538
2613
}
2539
2614
}
2540
2615
}
@@ -2552,31 +2627,27 @@ pub impl Resolver {
2552
2627
2553
2628
index += 1 ;
2554
2629
2555
- // After the first element of the path, allow searching through
2556
- // items and imports unconditionally. This allows things like:
2557
- //
2558
- // pub mod core {
2559
- // pub use vec;
2560
- // }
2630
+ // After the first element of the path, allow searching only
2631
+ // through public identifiers.
2561
2632
//
2562
- // pub mod something_else {
2563
- // use core::vec;
2564
- // }
2565
-
2566
- name_search_type = SearchItemsAndPublicImports ;
2633
+ // XXX: Rip this out and move it to the privacy checker.
2634
+ if name_search_type == PathPublicOrPrivateSearch {
2635
+ name_search_type = PathPublicOnlySearch
2636
+ }
2567
2637
}
2568
2638
2569
2639
return Success ( search_module) ;
2570
2640
}
2571
2641
2572
2642
/// Attempts to resolve the module part of an import directive or path
2573
2643
/// rooted at the given module.
2574
- fn resolve_module_path_for_import( @mut self ,
2575
- module_: @mut Module ,
2576
- module_path: & [ ident] ,
2577
- use_lexical_scope: UseLexicalScopeFlag ,
2578
- span: span)
2579
- -> ResolveResult <@mut Module > {
2644
+ fn resolve_module_path( @mut self ,
2645
+ module_: @mut Module ,
2646
+ module_path: & [ ident] ,
2647
+ use_lexical_scope: UseLexicalScopeFlag ,
2648
+ span: span,
2649
+ name_search_type: NameSearchType )
2650
+ -> ResolveResult <@mut Module > {
2580
2651
let module_path_len = module_path. len( ) ;
2581
2652
assert!( module_path_len > 0 ) ;
2582
2653
@@ -2648,7 +2719,7 @@ pub impl Resolver {
2648
2719
module_path,
2649
2720
start_index,
2650
2721
span,
2651
- SearchItemsAndPublicImports )
2722
+ name_search_type )
2652
2723
}
2653
2724
2654
2725
/// Invariant: This must only be called during main resolution, not during
@@ -2740,6 +2811,7 @@ pub impl Resolver {
2740
2811
}
2741
2812
ExternModuleKind |
2742
2813
TraitModuleKind |
2814
+ ImplModuleKind |
2743
2815
AnonymousModuleKind => {
2744
2816
search_module = parent_module_node;
2745
2817
}
@@ -2759,7 +2831,7 @@ pub impl Resolver {
2759
2831
match self . resolve_name_in_module( search_module,
2760
2832
name,
2761
2833
namespace,
2762
- SearchItemsAndAllImports ) {
2834
+ PathPublicOrPrivateSearch ) {
2763
2835
Failed => {
2764
2836
// Continue up the search chain.
2765
2837
}
@@ -2840,6 +2912,7 @@ pub impl Resolver {
2840
2912
NormalModuleKind => return Some ( new_module) ,
2841
2913
ExternModuleKind |
2842
2914
TraitModuleKind |
2915
+ ImplModuleKind |
2843
2916
AnonymousModuleKind => module_ = new_module,
2844
2917
}
2845
2918
}
@@ -2856,7 +2929,10 @@ pub impl Resolver {
2856
2929
-> @mut Module {
2857
2930
match module_. kind {
2858
2931
NormalModuleKind => return module_,
2859
- ExternModuleKind | TraitModuleKind | AnonymousModuleKind => {
2932
+ ExternModuleKind |
2933
+ TraitModuleKind |
2934
+ ImplModuleKind |
2935
+ AnonymousModuleKind => {
2860
2936
match self . get_nearest_normal_module_parent( module_) {
2861
2937
None => module_,
2862
2938
Some ( new_module) => new_module
@@ -2940,7 +3016,8 @@ pub impl Resolver {
2940
3016
2941
3017
// If this is a search of all imports, we should be done with glob
2942
3018
// resolution at this point.
2943
- if name_search_type == SearchItemsAndAllImports {
3019
+ if name_search_type == PathPublicOrPrivateSearch ||
3020
+ name_search_type == PathPublicOnlySearch {
2944
3021
assert!( module_. glob_count == 0 ) ;
2945
3022
}
2946
3023
@@ -2962,7 +3039,7 @@ pub impl Resolver {
2962
3039
}
2963
3040
Some ( target)
2964
3041
if name_search_type ==
2965
- SearchItemsAndAllImports ||
3042
+ PathPublicOrPrivateSearch ||
2966
3043
import_resolution. privacy == Public => {
2967
3044
debug!( "(resolving name in module) resolved to \
2968
3045
import") ;
@@ -4488,10 +4565,11 @@ pub impl Resolver {
4488
4565
let module_path_idents = self.intern_module_part_of_path(path);
4489
4566
4490
4567
let containing_module;
4491
- match self.resolve_module_path_for_import(self.current_module,
4492
- module_path_idents,
4493
- UseLexicalScope,
4494
- path.span) {
4568
+ match self.resolve_module_path(self.current_module,
4569
+ module_path_idents,
4570
+ UseLexicalScope,
4571
+ path.span,
4572
+ PathPublicOnlySearch) {
4495
4573
Failed => {
4496
4574
self.session.span_err(path.span,
4497
4575
fmt!(" use of undeclared module `%s`",
@@ -4540,7 +4618,7 @@ pub impl Resolver {
4540
4618
module_path_idents,
4541
4619
0,
4542
4620
path.span,
4543
- SearchItemsAndAllImports ) {
4621
+ PathPublicOrPrivateSearch ) {
4544
4622
Failed => {
4545
4623
self.session.span_err(path.span,
4546
4624
fmt!(" use of undeclared module `:: %s`",
0 commit comments