Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jul 10, 2018
1 parent e54d7f8 commit ee16ce1
Show file tree
Hide file tree
Showing 24 changed files with 413 additions and 144 deletions.
18 changes: 16 additions & 2 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
vis: self.1,
span: self.2,
expansion: self.3,
is_macro_export: false,
})
}
}
Expand All @@ -63,6 +64,19 @@ impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
vis: self.1,
span: self.2,
expansion: self.3,
is_macro_export: false,
})
}
}

impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark, bool) {
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Def(self.0),
vis: self.1,
span: self.2,
expansion: self.3,
is_macro_export: self.4,
})
}
}
Expand Down Expand Up @@ -674,8 +688,7 @@ impl<'a> Resolver<'a> {
} else {
for (name, span) in legacy_imports.imports {
let ident = Ident::with_empty_ctxt(name);
let result = self.resolve_ident_in_module(module, ident, MacroNS,
false, false, span);
let result = self.resolve_ident_in_module(module, ident, MacroNS, false, span);
if let Ok(binding) = result {
let directive = macro_use_directive(span);
self.potentially_unused_imports.push(directive);
Expand Down Expand Up @@ -743,6 +756,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> {
let mark = id.placeholder_to_mark();
self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
self.resolver.unresolved_invocations_macro_export.insert(mark);
let invocation = self.resolver.invocations[&mark];
invocation.module.set(self.resolver.current_module);
invocation.legacy_scope.set(self.legacy_scope);
Expand Down
33 changes: 23 additions & 10 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ impl<'a> fmt::Debug for ModuleData<'a> {
pub struct NameBinding<'a> {
kind: NameBindingKind<'a>,
expansion: Mark,
is_macro_export: bool,
span: Span,
vis: ty::Visibility,
}
Expand Down Expand Up @@ -1141,12 +1142,20 @@ struct UseError<'a> {
better: bool,
}

#[derive(Clone, Copy, Debug)]
enum AmbiguityErrorKind {
RecordUse,
ResolveLexical,
ResolveInModule,
}

struct AmbiguityError<'a> {
span: Span,
name: Name,
lexical: bool,
b1: &'a NameBinding<'a>,
b2: &'a NameBinding<'a>,
kind: AmbiguityErrorKind,
}

impl<'a> NameBinding<'a> {
Expand Down Expand Up @@ -1393,7 +1402,6 @@ pub struct Resolver<'a> {
macro_map: FxHashMap<DefId, Lrc<SyntaxExtension>>,
macro_defs: FxHashMap<Mark, DefId>,
local_macro_def_scopes: FxHashMap<NodeId, Module<'a>>,
macro_exports: Vec<Export>,
pub whitelisted_legacy_custom_derives: Vec<Name>,
pub found_unresolved_macro: bool,

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

/// Only supposed to be used by rustdoc, otherwise should be false.
pub ignore_extern_prelude_feature: bool,

/// Macro invocations in the whole crate that can expand into a `#[macro_export] macro_rules`.
unresolved_invocations_macro_export: FxHashSet<Mark>,
}

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

crate_loader,
Expand All @@ -1711,7 +1723,6 @@ impl<'a> Resolver<'a> {
all_macros: FxHashMap(),
lexical_macro_resolutions: Vec::new(),
macro_map: FxHashMap(),
macro_exports: Vec::new(),
invocations,
macro_defs,
local_macro_def_scopes: FxHashMap(),
Expand All @@ -1726,6 +1737,7 @@ impl<'a> Resolver<'a> {
current_type_ascription: Vec::new(),
injected_crate: None,
ignore_extern_prelude_feature: false,
unresolved_invocations_macro_export: FxHashSet(),
}
}

Expand Down Expand Up @@ -1797,6 +1809,7 @@ impl<'a> Resolver<'a> {
NameBindingKind::Ambiguity { b1, b2 } => {
self.ambiguity_errors.push(AmbiguityError {
span, name: ident.name, lexical: false, b1, b2,
kind: AmbiguityErrorKind::RecordUse
});
true
}
Expand Down Expand Up @@ -1957,7 +1970,6 @@ impl<'a> Resolver<'a> {
module: Module<'a>,
mut ident: Ident,
ns: Namespace,
ignore_unresolved_invocations: bool,
record_used: bool,
span: Span)
-> Result<&'a NameBinding<'a>, Determinacy> {
Expand All @@ -1967,7 +1979,7 @@ impl<'a> Resolver<'a> {
self.current_module = self.macro_def_scope(def);
}
let result = self.resolve_ident_in_module_unadjusted(
module, ident, ns, ignore_unresolved_invocations, record_used, span,
module, ident, ns, false, record_used, span,
);
self.current_module = orig_current_module;
result
Expand Down Expand Up @@ -2460,7 +2472,7 @@ impl<'a> Resolver<'a> {
// If there is a TraitRef in scope for an impl, then the method must be in the
// trait.
if let Some((module, _)) = self.current_trait_ref {
if self.resolve_ident_in_module(module, ident, ns, false, false, span).is_err() {
if self.resolve_ident_in_module(module, ident, ns, false, span).is_err() {
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
resolve_error(self, span, err(ident.name, &path_names_to_string(path)));
}
Expand Down Expand Up @@ -3410,7 +3422,7 @@ impl<'a> Resolver<'a> {
}

let binding = if let Some(module) = module {
self.resolve_ident_in_module(module, ident, ns, false, record_used, path_span)
self.resolve_ident_in_module(module, ident, ns, record_used, path_span)
} else if opt_ns == Some(MacroNS) {
self.resolve_lexical_macro_path_segment(ident, ns, record_used, path_span)
.map(MacroBinding::binding)
Expand Down Expand Up @@ -3704,7 +3716,7 @@ impl<'a> Resolver<'a> {
// Look for associated items in the current trait.
if let Some((module, _)) = self.current_trait_ref {
if let Ok(binding) =
self.resolve_ident_in_module(module, ident, ns, false, false, module.span) {
self.resolve_ident_in_module(module, ident, ns, false, module.span) {
let def = binding.def();
if filter_fn(def) {
return Some(if self.has_self.contains(&def.def_id()) {
Expand Down Expand Up @@ -4017,7 +4029,7 @@ impl<'a> Resolver<'a> {
let mut found_traits = Vec::new();
// Look for the current trait.
if let Some((module, _)) = self.current_trait_ref {
if self.resolve_ident_in_module(module, ident, ns, false, false, module.span).is_ok() {
if self.resolve_ident_in_module(module, ident, ns, false, module.span).is_ok() {
let def_id = module.def_id().unwrap();
found_traits.push(TraitCandidate { def_id: def_id, import_id: None });
}
Expand Down Expand Up @@ -4290,7 +4302,7 @@ impl<'a> Resolver<'a> {
self.report_proc_macro_import(krate);
let mut reported_spans = FxHashSet();

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

let mut err = struct_span_err!(self.session, span, E0659, "`{}` is ambiguous", name);
let mut err = struct_span_err!(self.session, span, E0659,
"`{}` is ambiguous {:?}", name, kind);
err.span_note(b1.span, &msg1);
match b2.def() {
Def::Macro(..) if b2.span.is_dummy() =>
Expand Down
35 changes: 20 additions & 15 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use {AmbiguityError, CrateLint, Resolver, ResolutionError, resolve_error};
use {AmbiguityError, AmbiguityErrorKind, CrateLint, Resolver, ResolutionError, resolve_error};
use {Module, ModuleKind, NameBinding, NameBindingKind, PathResult};
use Namespace::{self, MacroNS};
use build_reduced_graph::BuildReducedGraphVisitor;
use resolve_imports::ImportResolver;
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex,
DefIndexAddressSpace};
use rustc::hir::def::{Def, Export};
use rustc::hir::def::Def;
use rustc::hir::map::{self, DefCollector};
use rustc::{ty, lint};
use syntax::ast::{self, Name, Ident};
Expand Down Expand Up @@ -193,7 +193,9 @@ impl<'a> base::Resolver for Resolver<'a> {

self.current_module = invocation.module.get();
self.current_module.unresolved_invocations.borrow_mut().remove(&mark);
self.unresolved_invocations_macro_export.remove(&mark);
self.current_module.unresolved_invocations.borrow_mut().extend(derives);
self.unresolved_invocations_macro_export.extend(derives);
for &derive in derives {
self.invocations.insert(derive, invocation);
}
Expand All @@ -219,6 +221,7 @@ impl<'a> base::Resolver for Resolver<'a> {
span: DUMMY_SP,
vis: ty::Visibility::Invisible,
expansion: Mark::root(),
is_macro_export: false,
});
self.global_macros.insert(ident.name, binding);
}
Expand Down Expand Up @@ -552,11 +555,12 @@ impl<'a> Resolver<'a> {
b1: shadower,
b2: binding,
lexical: true,
kind: AmbiguityErrorKind::ResolveLexical,
});
return potential_illegal_shadower;
}
}
if binding.expansion != Mark::root() ||
if binding.expansion != Mark::root() || binding.is_macro_export ||
(binding.is_glob_import() && module.unwrap().def().is_some()) {
potential_illegal_shadower = result;
} else {
Expand Down Expand Up @@ -666,12 +670,16 @@ impl<'a> Resolver<'a> {

match (legacy_resolution, resolution) {
(Some(MacroBinding::Legacy(legacy_binding)), Ok(MacroBinding::Modern(binding))) => {
let msg1 = format!("`{}` could refer to the macro defined here", ident);
let msg2 = format!("`{}` could also refer to the macro imported here", ident);
self.session.struct_span_err(span, &format!("`{}` is ambiguous", ident))
.span_note(legacy_binding.span, &msg1)
.span_note(binding.span, &msg2)
.emit();
if legacy_binding.def_id != binding.def_ignoring_ambiguity().def_id() {
let msg1 =
format!("`{}` could refer to the macro defined here", ident);
let msg2 =
format!("`{}` could also refer to the macro imported here", ident);
self.session.struct_span_err(span, &format!("`{}` is ambiguous", ident))
.span_note(legacy_binding.span, &msg1)
.span_note(binding.span, &msg2)
.emit();
}
},
(None, Err(_)) => {
assert!(def.is_none());
Expand Down Expand Up @@ -802,12 +810,9 @@ impl<'a> Resolver<'a> {
let def = Def::Macro(def_id, MacroKind::Bang);
self.all_macros.insert(ident.name, def);
if attr::contains_name(&item.attrs, "macro_export") {
self.macro_exports.push(Export {
ident: ident.modern(),
def: def,
vis: ty::Visibility::Public,
span: item.span,
});
let module = self.graph_root;
let vis = ty::Visibility::Public;
self.define(module, ident, MacroNS, (def, vis, item.span, expansion, false));
} else {
self.unused_macros.insert(def_id);
}
Expand Down
Loading

0 comments on commit ee16ce1

Please sign in to comment.