@@ -81,7 +81,7 @@ use std::mem::replace;
81
81
use rustc_data_structures:: sync:: Lrc ;
82
82
83
83
use resolve_imports:: { ImportDirective , ImportDirectiveSubclass , NameResolution , ImportResolver } ;
84
- use macros:: { InvocationData , LegacyBinding } ;
84
+ use macros:: { InvocationData , LegacyBinding , LegacyScope } ;
85
85
86
86
// NB: This module needs to be declared first so diagnostics are
87
87
// registered before they are used.
@@ -1010,8 +1010,9 @@ pub struct ModuleData<'a> {
1010
1010
normal_ancestor_id : DefId ,
1011
1011
1012
1012
resolutions : RefCell < FxHashMap < ( Ident , Namespace ) , & ' a RefCell < NameResolution < ' a > > > > ,
1013
- legacy_macro_resolutions : RefCell < Vec < ( Mark , Ident , MacroKind , Option < Def > ) > > ,
1013
+ legacy_macro_resolutions : RefCell < Vec < ( Ident , MacroKind , Mark , LegacyScope < ' a > , Option < Def > ) > > ,
1014
1014
macro_resolutions : RefCell < Vec < ( Box < [ Ident ] > , Span ) > > ,
1015
+ builtin_attrs : RefCell < Vec < ( Ident , Mark , LegacyScope < ' a > ) > > ,
1015
1016
1016
1017
// Macro invocations that can expand into items in this module.
1017
1018
unresolved_invocations : RefCell < FxHashSet < Mark > > ,
@@ -1050,6 +1051,7 @@ impl<'a> ModuleData<'a> {
1050
1051
resolutions : RefCell :: new ( FxHashMap ( ) ) ,
1051
1052
legacy_macro_resolutions : RefCell :: new ( Vec :: new ( ) ) ,
1052
1053
macro_resolutions : RefCell :: new ( Vec :: new ( ) ) ,
1054
+ builtin_attrs : RefCell :: new ( Vec :: new ( ) ) ,
1053
1055
unresolved_invocations : RefCell :: new ( FxHashSet ( ) ) ,
1054
1056
no_implicit_prelude : false ,
1055
1057
glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
@@ -1166,7 +1168,6 @@ struct UseError<'a> {
1166
1168
struct AmbiguityError < ' a > {
1167
1169
span : Span ,
1168
1170
name : Name ,
1169
- lexical : bool ,
1170
1171
b1 : & ' a NameBinding < ' a > ,
1171
1172
b2 : & ' a NameBinding < ' a > ,
1172
1173
}
@@ -1270,6 +1271,23 @@ impl<'a> NameBinding<'a> {
1270
1271
fn descr ( & self ) -> & ' static str {
1271
1272
if self . is_extern_crate ( ) { "extern crate" } else { self . def ( ) . kind_name ( ) }
1272
1273
}
1274
+
1275
+ // Suppose that we resolved macro invocation with `invoc_parent_expansion` to binding `binding`
1276
+ // at some expansion round `max(invoc, binding)` when they both emerged from macros.
1277
+ // Then this function returns `true` if `self` may emerge from a macro *after* that
1278
+ // in some later round and screw up our previously found resolution.
1279
+ fn may_appear_after ( & self , invoc_parent_expansion : Mark , binding : & NameBinding ) -> bool {
1280
+ // self > max(invoc, binding) => !(self <= invoc || self <= binding)
1281
+ // Expansions are partially ordered, so "may appear after" is an inversion of
1282
+ // "certainly appears before or simultaneously" and includes unordered cases.
1283
+ let self_parent_expansion = self . expansion ;
1284
+ let other_parent_expansion = binding. expansion ;
1285
+ let certainly_before_other_or_simultaneously =
1286
+ other_parent_expansion. is_descendant_of ( self_parent_expansion) ;
1287
+ let certainly_before_invoc_or_simultaneously =
1288
+ invoc_parent_expansion. is_descendant_of ( self_parent_expansion) ;
1289
+ !( certainly_before_other_or_simultaneously || certainly_before_invoc_or_simultaneously)
1290
+ }
1273
1291
}
1274
1292
1275
1293
/// Interns the names of the primitive types.
@@ -1403,8 +1421,6 @@ pub struct Resolver<'a, 'b: 'a> {
1403
1421
proc_mac_errors : Vec < macros:: ProcMacError > ,
1404
1422
/// crate-local macro expanded `macro_export` referred to by a module-relative path
1405
1423
macro_expanded_macro_export_errors : BTreeSet < ( Span , Span ) > ,
1406
- /// macro-expanded `macro_rules` shadowing existing macros
1407
- disallowed_shadowing : Vec < & ' a LegacyBinding < ' a > > ,
1408
1424
1409
1425
arenas : & ' a ResolverArenas < ' a > ,
1410
1426
dummy_binding : & ' a NameBinding < ' a > ,
@@ -1715,7 +1731,6 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
1715
1731
ambiguity_errors : Vec :: new ( ) ,
1716
1732
use_injections : Vec :: new ( ) ,
1717
1733
proc_mac_errors : Vec :: new ( ) ,
1718
- disallowed_shadowing : Vec :: new ( ) ,
1719
1734
macro_expanded_macro_export_errors : BTreeSet :: new ( ) ,
1720
1735
1721
1736
arenas,
@@ -1814,7 +1829,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
1814
1829
NameBindingKind :: Import { .. } => false ,
1815
1830
NameBindingKind :: Ambiguity { b1, b2 } => {
1816
1831
self . ambiguity_errors . push ( AmbiguityError {
1817
- span, name : ident. name , lexical : false , b1, b2,
1832
+ span, name : ident. name , b1, b2,
1818
1833
} ) ;
1819
1834
true
1820
1835
}
@@ -3468,6 +3483,20 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
3468
3483
record_used : bool ,
3469
3484
path_span : Span ,
3470
3485
crate_lint : CrateLint ,
3486
+ ) -> PathResult < ' a > {
3487
+ self . resolve_path_with_parent_expansion ( base_module, path, opt_ns, Mark :: root ( ) ,
3488
+ record_used, path_span, crate_lint)
3489
+ }
3490
+
3491
+ fn resolve_path_with_parent_expansion (
3492
+ & mut self ,
3493
+ base_module : Option < ModuleOrUniformRoot < ' a > > ,
3494
+ path : & [ Ident ] ,
3495
+ opt_ns : Option < Namespace > , // `None` indicates a module path
3496
+ parent_expansion : Mark ,
3497
+ record_used : bool ,
3498
+ path_span : Span ,
3499
+ crate_lint : CrateLint ,
3471
3500
) -> PathResult < ' a > {
3472
3501
let mut module = base_module;
3473
3502
let mut allow_super = true ;
@@ -3557,8 +3586,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
3557
3586
self . resolve_ident_in_module ( module, ident, ns, record_used, path_span)
3558
3587
} else if opt_ns == Some ( MacroNS ) {
3559
3588
assert ! ( ns == TypeNS ) ;
3560
- self . resolve_lexical_macro_path_segment ( ident, ns, record_used, record_used,
3561
- false , path_span) . map ( |( b, _) | b)
3589
+ self . resolve_lexical_macro_path_segment ( ident, ns, parent_expansion, record_used,
3590
+ record_used, false , path_span)
3591
+ . map ( |( binding, _) | binding)
3562
3592
} else {
3563
3593
let record_used_id =
3564
3594
if record_used { crate_lint. node_id ( ) . or ( Some ( CRATE_NODE_ID ) ) } else { None } ;
@@ -4499,35 +4529,32 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
4499
4529
vis. is_accessible_from ( module. normal_ancestor_id , self )
4500
4530
}
4501
4531
4502
- fn report_ambiguity_error (
4503
- & self , name : Name , span : Span , _lexical : bool ,
4504
- def1 : Def , is_import1 : bool , is_glob1 : bool , from_expansion1 : bool , span1 : Span ,
4505
- def2 : Def , is_import2 : bool , _is_glob2 : bool , _from_expansion2 : bool , span2 : Span ,
4506
- ) {
4532
+ fn report_ambiguity_error ( & self , name : Name , span : Span , b1 : & NameBinding , b2 : & NameBinding ) {
4507
4533
let participle = |is_import : bool | if is_import { "imported" } else { "defined" } ;
4508
- let msg1 = format ! ( "`{}` could refer to the name {} here" , name, participle( is_import1) ) ;
4534
+ let msg1 =
4535
+ format ! ( "`{}` could refer to the name {} here" , name, participle( b1. is_import( ) ) ) ;
4509
4536
let msg2 =
4510
- format ! ( "`{}` could also refer to the name {} here" , name, participle( is_import2 ) ) ;
4511
- let note = if from_expansion1 {
4512
- Some ( if let Def :: Macro ( ..) = def1 {
4537
+ format ! ( "`{}` could also refer to the name {} here" , name, participle( b2 . is_import ( ) ) ) ;
4538
+ let note = if b1 . expansion != Mark :: root ( ) {
4539
+ Some ( if let Def :: Macro ( ..) = b1 . def ( ) {
4513
4540
format ! ( "macro-expanded {} do not shadow" ,
4514
- if is_import1 { "macro imports" } else { "macros" } )
4541
+ if b1 . is_import ( ) { "macro imports" } else { "macros" } )
4515
4542
} else {
4516
4543
format ! ( "macro-expanded {} do not shadow when used in a macro invocation path" ,
4517
- if is_import1 { "imports" } else { "items" } )
4544
+ if b1 . is_import ( ) { "imports" } else { "items" } )
4518
4545
} )
4519
- } else if is_glob1 {
4546
+ } else if b1 . is_glob_import ( ) {
4520
4547
Some ( format ! ( "consider adding an explicit import of `{}` to disambiguate" , name) )
4521
4548
} else {
4522
4549
None
4523
4550
} ;
4524
4551
4525
4552
let mut err = struct_span_err ! ( self . session, span, E0659 , "`{}` is ambiguous" , name) ;
4526
- err. span_note ( span1 , & msg1) ;
4527
- match def2 {
4528
- Def :: Macro ( ..) if span2 . is_dummy ( ) =>
4553
+ err. span_note ( b1 . span , & msg1) ;
4554
+ match b2 . def ( ) {
4555
+ Def :: Macro ( ..) if b2 . span . is_dummy ( ) =>
4529
4556
err. note ( & format ! ( "`{}` is also a builtin macro" , name) ) ,
4530
- _ => err. span_note ( span2 , & msg2) ,
4557
+ _ => err. span_note ( b2 . span , & msg2) ,
4531
4558
} ;
4532
4559
if let Some ( note) = note {
4533
4560
err. note ( & note) ;
@@ -4536,7 +4563,6 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
4536
4563
}
4537
4564
4538
4565
fn report_errors ( & mut self , krate : & Crate ) {
4539
- self . report_shadowing_errors ( ) ;
4540
4566
self . report_with_use_injections ( krate) ;
4541
4567
self . report_proc_macro_import ( krate) ;
4542
4568
let mut reported_spans = FxHashSet ( ) ;
@@ -4552,15 +4578,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
4552
4578
) ;
4553
4579
}
4554
4580
4555
- for & AmbiguityError { span, name, b1, b2, lexical } in & self . ambiguity_errors {
4581
+ for & AmbiguityError { span, name, b1, b2 } in & self . ambiguity_errors {
4556
4582
if reported_spans. insert ( span) {
4557
- self . report_ambiguity_error (
4558
- name, span, lexical,
4559
- b1. def ( ) , b1. is_import ( ) , b1. is_glob_import ( ) ,
4560
- b1. expansion != Mark :: root ( ) , b1. span ,
4561
- b2. def ( ) , b2. is_import ( ) , b2. is_glob_import ( ) ,
4562
- b2. expansion != Mark :: root ( ) , b2. span ,
4563
- ) ;
4583
+ self . report_ambiguity_error ( name, span, b1, b2) ;
4564
4584
}
4565
4585
}
4566
4586
@@ -4580,20 +4600,6 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
4580
4600
}
4581
4601
}
4582
4602
4583
- fn report_shadowing_errors ( & mut self ) {
4584
- let mut reported_errors = FxHashSet ( ) ;
4585
- for binding in replace ( & mut self . disallowed_shadowing , Vec :: new ( ) ) {
4586
- if self . resolve_legacy_scope ( & binding. parent , binding. ident , false ) . is_some ( ) &&
4587
- reported_errors. insert ( ( binding. ident , binding. span ) ) {
4588
- let msg = format ! ( "`{}` is already in scope" , binding. ident) ;
4589
- self . session . struct_span_err ( binding. span , & msg)
4590
- . note ( "macro-expanded `macro_rules!`s may not shadow \
4591
- existing macros (see RFC 1560)")
4592
- . emit ( ) ;
4593
- }
4594
- }
4595
- }
4596
-
4597
4603
fn report_conflict < ' b > ( & mut self ,
4598
4604
parent : Module ,
4599
4605
ident : Ident ,
0 commit comments