Skip to content

Commit ee16ce1

Browse files
committed
temp
1 parent e54d7f8 commit ee16ce1

24 files changed

+413
-144
lines changed

src/librustc_resolve/build_reduced_graph.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
5252
vis: self.1,
5353
span: self.2,
5454
expansion: self.3,
55+
is_macro_export: false,
5556
})
5657
}
5758
}
@@ -63,6 +64,19 @@ impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
6364
vis: self.1,
6465
span: self.2,
6566
expansion: self.3,
67+
is_macro_export: false,
68+
})
69+
}
70+
}
71+
72+
impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark, bool) {
73+
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
74+
arenas.alloc_name_binding(NameBinding {
75+
kind: NameBindingKind::Def(self.0),
76+
vis: self.1,
77+
span: self.2,
78+
expansion: self.3,
79+
is_macro_export: self.4,
6680
})
6781
}
6882
}
@@ -674,8 +688,7 @@ impl<'a> Resolver<'a> {
674688
} else {
675689
for (name, span) in legacy_imports.imports {
676690
let ident = Ident::with_empty_ctxt(name);
677-
let result = self.resolve_ident_in_module(module, ident, MacroNS,
678-
false, false, span);
691+
let result = self.resolve_ident_in_module(module, ident, MacroNS, false, span);
679692
if let Ok(binding) = result {
680693
let directive = macro_use_directive(span);
681694
self.potentially_unused_imports.push(directive);
@@ -743,6 +756,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
743756
fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> {
744757
let mark = id.placeholder_to_mark();
745758
self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
759+
self.resolver.unresolved_invocations_macro_export.insert(mark);
746760
let invocation = self.resolver.invocations[&mark];
747761
invocation.module.set(self.resolver.current_module);
748762
invocation.legacy_scope.set(self.legacy_scope);

src/librustc_resolve/lib.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ impl<'a> fmt::Debug for ModuleData<'a> {
11001100
pub struct NameBinding<'a> {
11011101
kind: NameBindingKind<'a>,
11021102
expansion: Mark,
1103+
is_macro_export: bool,
11031104
span: Span,
11041105
vis: ty::Visibility,
11051106
}
@@ -1141,12 +1142,20 @@ struct UseError<'a> {
11411142
better: bool,
11421143
}
11431144

1145+
#[derive(Clone, Copy, Debug)]
1146+
enum AmbiguityErrorKind {
1147+
RecordUse,
1148+
ResolveLexical,
1149+
ResolveInModule,
1150+
}
1151+
11441152
struct AmbiguityError<'a> {
11451153
span: Span,
11461154
name: Name,
11471155
lexical: bool,
11481156
b1: &'a NameBinding<'a>,
11491157
b2: &'a NameBinding<'a>,
1158+
kind: AmbiguityErrorKind,
11501159
}
11511160

11521161
impl<'a> NameBinding<'a> {
@@ -1393,7 +1402,6 @@ pub struct Resolver<'a> {
13931402
macro_map: FxHashMap<DefId, Lrc<SyntaxExtension>>,
13941403
macro_defs: FxHashMap<Mark, DefId>,
13951404
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
1396-
macro_exports: Vec<Export>,
13971405
pub whitelisted_legacy_custom_derives: Vec<Name>,
13981406
pub found_unresolved_macro: bool,
13991407

@@ -1426,6 +1434,9 @@ pub struct Resolver<'a> {
14261434

14271435
/// Only supposed to be used by rustdoc, otherwise should be false.
14281436
pub ignore_extern_prelude_feature: bool,
1437+
1438+
/// Macro invocations in the whole crate that can expand into a `#[macro_export] macro_rules`.
1439+
unresolved_invocations_macro_export: FxHashSet<Mark>,
14291440
}
14301441

14311442
/// Nothing really interesting here, it just provides memory for the rest of the crate.
@@ -1703,6 +1714,7 @@ impl<'a> Resolver<'a> {
17031714
expansion: Mark::root(),
17041715
span: DUMMY_SP,
17051716
vis: ty::Visibility::Public,
1717+
is_macro_export: false,
17061718
}),
17071719

17081720
crate_loader,
@@ -1711,7 +1723,6 @@ impl<'a> Resolver<'a> {
17111723
all_macros: FxHashMap(),
17121724
lexical_macro_resolutions: Vec::new(),
17131725
macro_map: FxHashMap(),
1714-
macro_exports: Vec::new(),
17151726
invocations,
17161727
macro_defs,
17171728
local_macro_def_scopes: FxHashMap(),
@@ -1726,6 +1737,7 @@ impl<'a> Resolver<'a> {
17261737
current_type_ascription: Vec::new(),
17271738
injected_crate: None,
17281739
ignore_extern_prelude_feature: false,
1740+
unresolved_invocations_macro_export: FxHashSet(),
17291741
}
17301742
}
17311743

@@ -1797,6 +1809,7 @@ impl<'a> Resolver<'a> {
17971809
NameBindingKind::Ambiguity { b1, b2 } => {
17981810
self.ambiguity_errors.push(AmbiguityError {
17991811
span, name: ident.name, lexical: false, b1, b2,
1812+
kind: AmbiguityErrorKind::RecordUse
18001813
});
18011814
true
18021815
}
@@ -1957,7 +1970,6 @@ impl<'a> Resolver<'a> {
19571970
module: Module<'a>,
19581971
mut ident: Ident,
19591972
ns: Namespace,
1960-
ignore_unresolved_invocations: bool,
19611973
record_used: bool,
19621974
span: Span)
19631975
-> Result<&'a NameBinding<'a>, Determinacy> {
@@ -1967,7 +1979,7 @@ impl<'a> Resolver<'a> {
19671979
self.current_module = self.macro_def_scope(def);
19681980
}
19691981
let result = self.resolve_ident_in_module_unadjusted(
1970-
module, ident, ns, ignore_unresolved_invocations, record_used, span,
1982+
module, ident, ns, false, record_used, span,
19711983
);
19721984
self.current_module = orig_current_module;
19731985
result
@@ -2460,7 +2472,7 @@ impl<'a> Resolver<'a> {
24602472
// If there is a TraitRef in scope for an impl, then the method must be in the
24612473
// trait.
24622474
if let Some((module, _)) = self.current_trait_ref {
2463-
if self.resolve_ident_in_module(module, ident, ns, false, false, span).is_err() {
2475+
if self.resolve_ident_in_module(module, ident, ns, false, span).is_err() {
24642476
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
24652477
resolve_error(self, span, err(ident.name, &path_names_to_string(path)));
24662478
}
@@ -3410,7 +3422,7 @@ impl<'a> Resolver<'a> {
34103422
}
34113423

34123424
let binding = if let Some(module) = module {
3413-
self.resolve_ident_in_module(module, ident, ns, false, record_used, path_span)
3425+
self.resolve_ident_in_module(module, ident, ns, record_used, path_span)
34143426
} else if opt_ns == Some(MacroNS) {
34153427
self.resolve_lexical_macro_path_segment(ident, ns, record_used, path_span)
34163428
.map(MacroBinding::binding)
@@ -3704,7 +3716,7 @@ impl<'a> Resolver<'a> {
37043716
// Look for associated items in the current trait.
37053717
if let Some((module, _)) = self.current_trait_ref {
37063718
if let Ok(binding) =
3707-
self.resolve_ident_in_module(module, ident, ns, false, false, module.span) {
3719+
self.resolve_ident_in_module(module, ident, ns, false, module.span) {
37083720
let def = binding.def();
37093721
if filter_fn(def) {
37103722
return Some(if self.has_self.contains(&def.def_id()) {
@@ -4017,7 +4029,7 @@ impl<'a> Resolver<'a> {
40174029
let mut found_traits = Vec::new();
40184030
// Look for the current trait.
40194031
if let Some((module, _)) = self.current_trait_ref {
4020-
if self.resolve_ident_in_module(module, ident, ns, false, false, module.span).is_ok() {
4032+
if self.resolve_ident_in_module(module, ident, ns, false, module.span).is_ok() {
40214033
let def_id = module.def_id().unwrap();
40224034
found_traits.push(TraitCandidate { def_id: def_id, import_id: None });
40234035
}
@@ -4290,7 +4302,7 @@ impl<'a> Resolver<'a> {
42904302
self.report_proc_macro_import(krate);
42914303
let mut reported_spans = FxHashSet();
42924304

4293-
for &AmbiguityError { span, name, b1, b2, lexical } in &self.ambiguity_errors {
4305+
for &AmbiguityError { span, name, b1, b2, lexical, kind } in &self.ambiguity_errors {
42944306
if !reported_spans.insert(span) { continue }
42954307
let participle = |binding: &NameBinding| {
42964308
if binding.is_import() { "imported" } else { "defined" }
@@ -4307,7 +4319,8 @@ impl<'a> Resolver<'a> {
43074319
if b1.is_import() { "imports" } else { "items" })
43084320
};
43094321

4310-
let mut err = struct_span_err!(self.session, span, E0659, "`{}` is ambiguous", name);
4322+
let mut err = struct_span_err!(self.session, span, E0659,
4323+
"`{}` is ambiguous {:?}", name, kind);
43114324
err.span_note(b1.span, &msg1);
43124325
match b2.def() {
43134326
Def::Macro(..) if b2.span.is_dummy() =>

src/librustc_resolve/macros.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use {AmbiguityError, CrateLint, Resolver, ResolutionError, resolve_error};
11+
use {AmbiguityError, AmbiguityErrorKind, CrateLint, Resolver, ResolutionError, resolve_error};
1212
use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult};
1313
use Namespace::{self, MacroNS};
1414
use build_reduced_graph::BuildReducedGraphVisitor;
1515
use resolve_imports::ImportResolver;
1616
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex,
1717
DefIndexAddressSpace};
18-
use rustc::hir::def::{Def, Export};
18+
use rustc::hir::def::Def;
1919
use rustc::hir::map::{self, DefCollector};
2020
use rustc::{ty, lint};
2121
use syntax::ast::{self, Name, Ident};
@@ -193,7 +193,9 @@ impl<'a> base::Resolver for Resolver<'a> {
193193

194194
self.current_module = invocation.module.get();
195195
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
196+
self.unresolved_invocations_macro_export.remove(&mark);
196197
self.current_module.unresolved_invocations.borrow_mut().extend(derives);
198+
self.unresolved_invocations_macro_export.extend(derives);
197199
for &derive in derives {
198200
self.invocations.insert(derive, invocation);
199201
}
@@ -219,6 +221,7 @@ impl<'a> base::Resolver for Resolver<'a> {
219221
span: DUMMY_SP,
220222
vis: ty::Visibility::Invisible,
221223
expansion: Mark::root(),
224+
is_macro_export: false,
222225
});
223226
self.global_macros.insert(ident.name, binding);
224227
}
@@ -552,11 +555,12 @@ impl<'a> Resolver<'a> {
552555
b1: shadower,
553556
b2: binding,
554557
lexical: true,
558+
kind: AmbiguityErrorKind::ResolveLexical,
555559
});
556560
return potential_illegal_shadower;
557561
}
558562
}
559-
if binding.expansion != Mark::root() ||
563+
if binding.expansion != Mark::root() || binding.is_macro_export ||
560564
(binding.is_glob_import() && module.unwrap().def().is_some()) {
561565
potential_illegal_shadower = result;
562566
} else {
@@ -666,12 +670,16 @@ impl<'a> Resolver<'a> {
666670

667671
match (legacy_resolution, resolution) {
668672
(Some(MacroBinding::Legacy(legacy_binding)), Ok(MacroBinding::Modern(binding))) => {
669-
let msg1 = format!("`{}` could refer to the macro defined here", ident);
670-
let msg2 = format!("`{}` could also refer to the macro imported here", ident);
671-
self.session.struct_span_err(span, &format!("`{}` is ambiguous", ident))
672-
.span_note(legacy_binding.span, &msg1)
673-
.span_note(binding.span, &msg2)
674-
.emit();
673+
if legacy_binding.def_id != binding.def_ignoring_ambiguity().def_id() {
674+
let msg1 =
675+
format!("`{}` could refer to the macro defined here", ident);
676+
let msg2 =
677+
format!("`{}` could also refer to the macro imported here", ident);
678+
self.session.struct_span_err(span, &format!("`{}` is ambiguous", ident))
679+
.span_note(legacy_binding.span, &msg1)
680+
.span_note(binding.span, &msg2)
681+
.emit();
682+
}
675683
},
676684
(None, Err(_)) => {
677685
assert!(def.is_none());
@@ -802,12 +810,9 @@ impl<'a> Resolver<'a> {
802810
let def = Def::Macro(def_id, MacroKind::Bang);
803811
self.all_macros.insert(ident.name, def);
804812
if attr::contains_name(&item.attrs, "macro_export") {
805-
self.macro_exports.push(Export {
806-
ident: ident.modern(),
807-
def: def,
808-
vis: ty::Visibility::Public,
809-
span: item.span,
810-
});
813+
let module = self.graph_root;
814+
let vis = ty::Visibility::Public;
815+
self.define(module, ident, MacroNS, (def, vis, item.span, expansion, false));
811816
} else {
812817
self.unused_macros.insert(def_id);
813818
}

0 commit comments

Comments
 (0)