@@ -255,8 +255,20 @@ pub enum AllowCapturingSelfFlag {
255
255
256
256
#[ deriving( Eq ) ]
257
257
enum NameSearchType {
258
- SearchItemsAndPublicImports , //< Search items and public imports.
259
- SearchItemsAndAllImports , //< Search items and all imports.
258
+ /// We're doing a name search in order to resolve a `use` directive.
259
+ ImportSearch ,
260
+
261
+ /// We're doing a name search in order to resolve a path type, a path
262
+ /// expression, or a path pattern. We can select public or private
263
+ /// names.
264
+ ///
265
+ /// XXX: This should be ripped out of resolve and handled later, in
266
+ /// the privacy checking phase.
267
+ PathPublicOrPrivateSearch ,
268
+
269
+ /// We're doing a name search in order to resolve a path type, a path
270
+ /// expression, or a path pattern. Allow only public names to be selected.
271
+ PathPublicOnlySearch ,
260
272
}
261
273
262
274
pub enum BareIdentifierPatternResolution {
@@ -394,6 +406,7 @@ pub enum ModuleKind {
394
406
NormalModuleKind ,
395
407
ExternModuleKind ,
396
408
TraitModuleKind ,
409
+ ImplModuleKind ,
397
410
AnonymousModuleKind ,
398
411
}
399
412
@@ -424,7 +437,6 @@ pub struct Module {
424
437
//
425
438
// There will be an anonymous module created around `g` with the ID of the
426
439
// entry block for `f`.
427
-
428
440
anonymous_children : @mut HashMap < node_id , @mut Module > ,
429
441
430
442
// The status of resolving each import in this module.
@@ -514,6 +526,38 @@ pub impl NameBindings {
514
526
}
515
527
}
516
528
529
+ /// Sets the kind of the module, creating a new one if necessary.
530
+ fn set_module_kind ( @mut self ,
531
+ privacy : Privacy ,
532
+ parent_link : ParentLink ,
533
+ def_id : Option < def_id > ,
534
+ kind : ModuleKind ,
535
+ sp : span ) {
536
+ match self . type_def {
537
+ None => {
538
+ let module = @mut Module ( parent_link, def_id, kind) ;
539
+ self . type_def = Some ( TypeNsDef {
540
+ privacy : privacy,
541
+ module_def : Some ( module) ,
542
+ type_def : None
543
+ } )
544
+ }
545
+ Some ( type_def) => {
546
+ match type_def. module_def {
547
+ None => {
548
+ let module = @mut Module ( parent_link, def_id, kind) ;
549
+ self . type_def = Some ( TypeNsDef {
550
+ privacy : privacy,
551
+ module_def : Some ( module) ,
552
+ type_def : type_def. type_def
553
+ } )
554
+ }
555
+ Some ( module_def) => module_def. kind = kind,
556
+ }
557
+ }
558
+ }
559
+ }
560
+
517
561
/// Records a type definition.
518
562
fn define_type ( @mut self , privacy : Privacy , def : def , sp : span ) {
519
563
// Merges the type with the existing type def or creates a new one.
@@ -1191,7 +1235,7 @@ pub impl Resolver {
1191
1235
name_bindings. define_module ( Public ,
1192
1236
parent_link,
1193
1237
Some ( def_id) ,
1194
- TraitModuleKind ,
1238
+ ImplModuleKind ,
1195
1239
sp) ;
1196
1240
1197
1241
let new_parent = ModuleReducedGraphParent (
@@ -1579,8 +1623,8 @@ pub impl Resolver {
1579
1623
// If this is a trait, add all the method names
1580
1624
// to the trait info.
1581
1625
1582
- let method_def_ids = get_trait_method_def_ids ( self . session . cstore ,
1583
- def_id) ;
1626
+ let method_def_ids =
1627
+ get_trait_method_def_ids ( self . session . cstore , def_id) ;
1584
1628
let mut interned_method_names = HashSet :: new ( ) ;
1585
1629
for method_def_ids. each |& method_def_id| {
1586
1630
let ( method_name, explicit_self) =
@@ -1608,6 +1652,14 @@ pub impl Resolver {
1608
1652
}
1609
1653
1610
1654
child_name_bindings. define_type ( Public , def, dummy_sp ( ) ) ;
1655
+
1656
+ // Define a module if necessary.
1657
+ let parent_link = self . get_parent_link ( new_parent, ident) ;
1658
+ child_name_bindings. set_module_kind ( Public ,
1659
+ parent_link,
1660
+ Some ( def_id) ,
1661
+ TraitModuleKind ,
1662
+ dummy_sp ( ) )
1611
1663
}
1612
1664
def_ty( _) => {
1613
1665
debug ! ( "(building reduced graph for external \
@@ -1750,6 +1802,10 @@ pub impl Resolver {
1750
1802
// We already have a module. This
1751
1803
// is OK.
1752
1804
type_module = module_def;
1805
+
1806
+ // Mark it as an impl module if
1807
+ // necessary.
1808
+ type_module. kind = ImplModuleKind ;
1753
1809
}
1754
1810
Some ( _) | None => {
1755
1811
let parent_link =
@@ -1759,7 +1815,7 @@ pub impl Resolver {
1759
1815
Public ,
1760
1816
parent_link,
1761
1817
Some ( def) ,
1762
- NormalModuleKind ,
1818
+ ImplModuleKind ,
1763
1819
dummy_sp( ) ) ;
1764
1820
type_module =
1765
1821
child_name_bindings.
@@ -1866,10 +1922,8 @@ pub impl Resolver {
1866
1922
// remain or unsuccessfully when no forward progress in resolving imports
1867
1923
// is made.
1868
1924
1869
- /**
1870
- * Resolves all imports for the crate. This method performs the fixed-
1871
- * point iteration.
1872
- */
1925
+ /// Resolves all imports for the crate. This method performs the fixed-
1926
+ /// point iteration.
1873
1927
fn resolve_imports( @mut self ) {
1874
1928
let mut i = 0 ;
1875
1929
let mut prev_unresolved_imports = 0 ;
@@ -1991,9 +2045,10 @@ pub impl Resolver {
1991
2045
/// don't know whether the name exists at the moment due to other
1992
2046
/// currently-unresolved imports, or success if we know the name exists.
1993
2047
/// If successful, the resolved bindings are written into the module.
1994
- fn resolve_import_for_module( @mut self , module_: @mut Module ,
2048
+ fn resolve_import_for_module( @mut self ,
2049
+ module_: @mut Module ,
1995
2050
import_directive: @ImportDirective )
1996
- -> ResolveResult < ( ) > {
2051
+ -> ResolveResult < ( ) > {
1997
2052
let mut resolution_result = Failed ;
1998
2053
let module_path = & import_directive. module_path ;
1999
2054
@@ -2007,10 +2062,11 @@ pub impl Resolver {
2007
2062
// Use the crate root.
2008
2063
Some ( self . graph_root . get_module ( ) )
2009
2064
} else {
2010
- match self . resolve_module_path_for_import ( module_,
2011
- * module_path,
2012
- DontUseLexicalScope ,
2013
- import_directive. span ) {
2065
+ match self . resolve_module_path ( module_,
2066
+ * module_path,
2067
+ DontUseLexicalScope ,
2068
+ import_directive. span ,
2069
+ ImportSearch ) {
2014
2070
2015
2071
Failed => None ,
2016
2072
Indeterminate => {
@@ -2097,7 +2153,7 @@ pub impl Resolver {
2097
2153
target : ident ,
2098
2154
source : ident ,
2099
2155
span : span )
2100
- -> ResolveResult < ( ) > {
2156
+ -> ResolveResult < ( ) > {
2101
2157
debug ! ( "(resolving single import) resolving `%s` = `%s::%s` from \
2102
2158
`%s`",
2103
2159
* self . session. str_of( target) ,
@@ -2134,9 +2190,7 @@ pub impl Resolver {
2134
2190
// Unless we managed to find a result in both namespaces (unlikely),
2135
2191
// search imports as well.
2136
2192
match ( value_result, type_result) {
2137
- ( BoundResult ( * ) , BoundResult ( * ) ) => {
2138
- // Continue.
2139
- }
2193
+ ( BoundResult ( * ) , BoundResult ( * ) ) => { } // Continue.
2140
2194
_ => {
2141
2195
// If there is an unresolved glob at this point in the
2142
2196
// containing module, bail out. We don't know enough to be
@@ -2460,7 +2514,6 @@ pub impl Resolver {
2460
2514
// Resolve the module part of the path. This does not involve looking
2461
2515
// upward though scope chains; we simply resolve names directly in
2462
2516
// modules as we go.
2463
-
2464
2517
while index < module_path_len {
2465
2518
let name = module_path[ index] ;
2466
2519
match self . resolve_name_in_module ( search_module,
@@ -2470,12 +2523,17 @@ pub impl Resolver {
2470
2523
Failed => {
2471
2524
let segment_name = self . session . str_of ( name) ;
2472
2525
let module_name = self . module_to_str ( search_module) ;
2473
- if module_name == ~"???" {
2474
- self . session . span_err ( span { lo : span. lo , hi : span. lo +
2475
- BytePos ( str:: len ( * segment_name) ) , expn_info :
2476
- span. expn_info } , fmt ! ( "unresolved import. maybe \
2477
- a missing `extern mod %s`?",
2478
- * segment_name) ) ;
2526
+ if "???" == module_name {
2527
+ let span = span {
2528
+ lo : span. lo ,
2529
+ hi : span. lo + BytePos ( str:: len ( * segment_name) ) ,
2530
+ expn_info : span. expn_info ,
2531
+ } ;
2532
+ self . session . span_err ( span,
2533
+ fmt ! ( "unresolved import. maybe \
2534
+ a missing `extern mod \
2535
+ %s`?",
2536
+ * segment_name) ) ;
2479
2537
return Failed ;
2480
2538
}
2481
2539
self . session . span_err ( span, fmt ! ( "unresolved import: could not find `%s` in \
@@ -2504,8 +2562,22 @@ pub impl Resolver {
2504
2562
name) ) ) ;
2505
2563
return Failed ;
2506
2564
}
2507
- Some ( copy module_def) => {
2508
- search_module = module_def;
2565
+ Some ( module_def) => {
2566
+ // If we're doing the search for an
2567
+ // import, do not allow traits and impls
2568
+ // to be selected.
2569
+ match ( name_search_type,
2570
+ module_def. kind ) {
2571
+ ( ImportSearch , TraitModuleKind ) |
2572
+ ( ImportSearch , ImplModuleKind ) => {
2573
+ self . session . span_err (
2574
+ span,
2575
+ ~"cannot import from a trait \
2576
+ or type implementation") ;
2577
+ return Failed ;
2578
+ }
2579
+ ( _, _) => search_module = module_def,
2580
+ }
2509
2581
}
2510
2582
}
2511
2583
}
@@ -2523,31 +2595,27 @@ pub impl Resolver {
2523
2595
2524
2596
index += 1 ;
2525
2597
2526
- // After the first element of the path, allow searching through
2527
- // items and imports unconditionally. This allows things like:
2528
- //
2529
- // pub mod core {
2530
- // pub use vec;
2531
- // }
2598
+ // After the first element of the path, allow searching only
2599
+ // through public identifiers.
2532
2600
//
2533
- // pub mod something_else {
2534
- // use core::vec;
2535
- // }
2536
-
2537
- name_search_type = SearchItemsAndPublicImports ;
2601
+ // XXX: Rip this out and move it to the privacy checker.
2602
+ if name_search_type == PathPublicOrPrivateSearch {
2603
+ name_search_type = PathPublicOnlySearch
2604
+ }
2538
2605
}
2539
2606
2540
2607
return Success ( search_module) ;
2541
2608
}
2542
2609
2543
2610
/// Attempts to resolve the module part of an import directive or path
2544
2611
/// rooted at the given module.
2545
- fn resolve_module_path_for_import ( @mut self ,
2546
- module_ : @mut Module ,
2547
- module_path : & [ ident ] ,
2548
- use_lexical_scope : UseLexicalScopeFlag ,
2549
- span : span )
2550
- -> ResolveResult < @mut Module > {
2612
+ fn resolve_module_path ( @mut self ,
2613
+ module_ : @mut Module ,
2614
+ module_path : & [ ident ] ,
2615
+ use_lexical_scope : UseLexicalScopeFlag ,
2616
+ span : span ,
2617
+ name_search_type : NameSearchType )
2618
+ -> ResolveResult < @mut Module > {
2551
2619
let module_path_len = module_path. len ( ) ;
2552
2620
assert ! ( module_path_len > 0 ) ;
2553
2621
@@ -2630,7 +2698,7 @@ pub impl Resolver {
2630
2698
module_path,
2631
2699
start_index,
2632
2700
span,
2633
- SearchItemsAndPublicImports )
2701
+ name_search_type )
2634
2702
}
2635
2703
2636
2704
/// Invariant: This must only be called during main resolution, not during
@@ -2722,6 +2790,7 @@ pub impl Resolver {
2722
2790
}
2723
2791
ExternModuleKind |
2724
2792
TraitModuleKind |
2793
+ ImplModuleKind |
2725
2794
AnonymousModuleKind => {
2726
2795
search_module = parent_module_node;
2727
2796
}
@@ -2741,7 +2810,7 @@ pub impl Resolver {
2741
2810
match self . resolve_name_in_module ( search_module,
2742
2811
name,
2743
2812
namespace,
2744
- SearchItemsAndAllImports ) {
2813
+ PathPublicOrPrivateSearch ) {
2745
2814
Failed => {
2746
2815
// Continue up the search chain.
2747
2816
}
@@ -2822,6 +2891,7 @@ pub impl Resolver {
2822
2891
NormalModuleKind => return Some ( new_module) ,
2823
2892
ExternModuleKind |
2824
2893
TraitModuleKind |
2894
+ ImplModuleKind |
2825
2895
AnonymousModuleKind => module_ = new_module,
2826
2896
}
2827
2897
}
@@ -2838,7 +2908,10 @@ pub impl Resolver {
2838
2908
-> @mut Module {
2839
2909
match module_. kind {
2840
2910
NormalModuleKind => return module_,
2841
- ExternModuleKind | TraitModuleKind | AnonymousModuleKind => {
2911
+ ExternModuleKind |
2912
+ TraitModuleKind |
2913
+ ImplModuleKind |
2914
+ AnonymousModuleKind => {
2842
2915
match self . get_nearest_normal_module_parent ( module_) {
2843
2916
None => module_,
2844
2917
Some ( new_module) => new_module
@@ -2922,8 +2995,14 @@ pub impl Resolver {
2922
2995
2923
2996
// If this is a search of all imports, we should be done with glob
2924
2997
// resolution at this point.
2998
+ <<<<<<< HEAD
2925
2999
if name_search_type == SearchItemsAndAllImports {
2926
3000
assert_eq ! ( module_. glob_count, 0 ) ;
3001
+ =======
3002
+ if name_search_type == PathPublicOrPrivateSearch ||
3003
+ name_search_type == PathPublicOnlySearch {
3004
+ assert ! ( module_. glob_count == 0 ) ;
3005
+ >>>>>>> librustc: Disallow `use ` from reaching into impls or traits.
2927
3006
}
2928
3007
2929
3008
// Check the list of resolved imports.
@@ -2944,7 +3023,7 @@ pub impl Resolver {
2944
3023
}
2945
3024
Some ( target)
2946
3025
if name_search_type ==
2947
- SearchItemsAndAllImports ||
3026
+ PathPublicOrPrivateSearch ||
2948
3027
import_resolution. privacy == Public => {
2949
3028
debug ! ( "(resolving name in module) resolved to \
2950
3029
import") ;
@@ -4483,10 +4562,11 @@ pub impl Resolver {
4483
4562
let module_path_idents = self . intern_module_part_of_path( path) ;
4484
4563
4485
4564
let containing_module;
4486
- match self . resolve_module_path_for_import( self . current_module,
4487
- module_path_idents,
4488
- UseLexicalScope ,
4489
- path. span) {
4565
+ match self . resolve_module_path( self . current_module,
4566
+ module_path_idents,
4567
+ UseLexicalScope ,
4568
+ path. span,
4569
+ PathPublicOnlySearch ) {
4490
4570
Failed => {
4491
4571
self . session. span_err( path. span,
4492
4572
fmt ! ( "use of undeclared module `%s`" ,
@@ -4535,7 +4615,7 @@ pub impl Resolver {
4535
4615
module_path_idents,
4536
4616
0 ,
4537
4617
path. span,
4538
- SearchItemsAndAllImports ) {
4618
+ PathPublicOrPrivateSearch ) {
4539
4619
Failed => {
4540
4620
self . session. span_err( path. span,
4541
4621
fmt ! ( "use of undeclared module `::%s`" ,
0 commit comments