From 319f0debd4c4bbd5f0dbd13ba87362c6cccd5e66 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 10 Aug 2019 01:40:05 +0300 Subject: [PATCH] resolve: Address FIXME from the previous commit Make the `is_import` flag in `ScopeSet` independent from namespace Fix rebase --- src/librustc_resolve/lib.rs | 17 ++++++++--------- src/librustc_resolve/macros.rs | 6 ++---- src/librustc_resolve/resolve_imports.rs | 5 +++-- .../ui/resolve/resolve-bad-visibility.stderr | 8 ++++---- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index c5aabb5e068..e786e102002 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -17,7 +17,7 @@ use rustc::hir::{self, PrimTy, Bool, Char, Float, Int, Uint, Str}; use rustc::middle::cstore::CrateStore; use rustc::session::Session; use rustc::lint; -use rustc::hir::def::{self, DefKind, PartialRes, CtorOf, NonMacroAttrKind, ExportMap}; +use rustc::hir::def::{self, DefKind, PartialRes, CtorKind, CtorOf, NonMacroAttrKind, ExportMap}; use rustc::hir::def::Namespace::*; use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId}; use rustc::hir::{TraitMap, GlobMap}; @@ -37,7 +37,7 @@ use syntax::visit::{self, Visitor}; use syntax::attr; use syntax::ast::{CRATE_NODE_ID, Crate}; use syntax::ast::{ItemKind, Path}; -use syntax::{span_err, struct_span_err, unwrap_or}; +use syntax::{struct_span_err, unwrap_or}; use syntax_pos::{Span, DUMMY_SP}; use errors::{Applicability, DiagnosticBuilder}; @@ -110,10 +110,12 @@ enum Scope<'a> { /// This enum is currently used only for early resolution (imports and macros), /// but not for late resolution yet. enum ScopeSet { - Import(Namespace), + /// All scopes with the given namespace. + All(Namespace, /*is_import*/ bool), + /// Crate root, then extern prelude (used for mixed 2015-2018 mode in macros). AbsolutePath(Namespace), + /// All scopes with macro namespace and the given macro kind restriction. Macro(MacroKind), - Module, } /// Everything you need to know about a name's location to resolve it. @@ -1330,10 +1332,9 @@ impl<'a> Resolver<'a> { let rust_2015 = ident.span.rust_2015(); let (ns, is_absolute_path) = match scope_set { - ScopeSet::Import(ns) => (ns, false), + ScopeSet::All(ns, _) => (ns, false), ScopeSet::AbsolutePath(ns) => (ns, true), ScopeSet::Macro(_) => (MacroNS, false), - ScopeSet::Module => (TypeNS, false), }; let mut scope = match ns { _ if is_absolute_path => Scope::CrateRoot, @@ -1858,9 +1859,7 @@ impl<'a> Resolver<'a> { module, ident, ns, parent_scope, record_used, path_span ) } else if ribs.is_none() || opt_ns.is_none() || opt_ns == Some(MacroNS) { - // FIXME: Decouple the import property from `ScopeSet`. - let is_import = opt_ns.is_none() || ns != TypeNS; - let scopes = if is_import { ScopeSet::Import(ns) } else { ScopeSet::Module }; + let scopes = ScopeSet::All(ns, opt_ns.is_none()); self.early_resolve_ident_in_lexical_scope(ident, scopes, parent_scope, record_used, record_used, path_span) } else { diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 1b6599e20e9..5af71a0170a 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -374,8 +374,7 @@ impl<'a> Resolver<'a> { self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span); res } else { - // Macro without a specific kind restriction is equvalent to a macro import. - let scope_set = kind.map_or(ScopeSet::Import(MacroNS), ScopeSet::Macro); + let scope_set = kind.map_or(ScopeSet::All(MacroNS, false), ScopeSet::Macro); let binding = self.early_resolve_ident_in_lexical_scope( path[0].ident, scope_set, parent_scope, false, force, path_span ); @@ -430,10 +429,9 @@ impl<'a> Resolver<'a> { } let (ns, macro_kind, is_import) = match scope_set { - ScopeSet::Import(ns) => (ns, None, true), + ScopeSet::All(ns, is_import) => (ns, None, is_import), ScopeSet::AbsolutePath(ns) => (ns, None, false), ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false), - ScopeSet::Module => (TypeNS, None, false), }; // This is *the* result, resolution from the scope closest to the resolved identifier. diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 5caaebf0e96..00e89f0fdae 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -232,8 +232,9 @@ impl<'a> Resolver<'a> { } } + let scopes = ScopeSet::All(ns, true); let binding = self.early_resolve_ident_in_lexical_scope( - ident, ScopeSet::Import(ns), parent_scope, record_used, record_used, path_span + ident, scopes, parent_scope, record_used, record_used, path_span ); return binding.map_err(|determinacy| (determinacy, Weak::No)); } @@ -1217,7 +1218,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> { match this.early_resolve_ident_in_lexical_scope( target, - ScopeSet::Import(ns), + ScopeSet::All(ns, false), &directive.parent_scope, false, false, diff --git a/src/test/ui/resolve/resolve-bad-visibility.stderr b/src/test/ui/resolve/resolve-bad-visibility.stderr index a133b02335c..d2fb7c7a9e6 100644 --- a/src/test/ui/resolve/resolve-bad-visibility.stderr +++ b/src/test/ui/resolve/resolve-bad-visibility.stderr @@ -16,17 +16,17 @@ error: visibilities can only be restricted to ancestor modules LL | pub(in std::vec) struct F; | ^^^^^^^^ -error[E0433]: failed to resolve: maybe a missing `extern crate nonexistent;`? +error[E0433]: failed to resolve: maybe a missing crate `nonexistent`? --> $DIR/resolve-bad-visibility.rs:7:8 | LL | pub(in nonexistent) struct G; - | ^^^^^^^^^^^ maybe a missing `extern crate nonexistent;`? + | ^^^^^^^^^^^ maybe a missing crate `nonexistent`? -error[E0433]: failed to resolve: maybe a missing `extern crate too_soon;`? +error[E0433]: failed to resolve: maybe a missing crate `too_soon`? --> $DIR/resolve-bad-visibility.rs:8:8 | LL | pub(in too_soon) struct H; - | ^^^^^^^^ maybe a missing `extern crate too_soon;`? + | ^^^^^^^^ maybe a missing crate `too_soon`? error: aborting due to 5 previous errors