Skip to content

Commit 82ee839

Browse files
committed
resolve: Resolve single-segment imports using in-scope resolution on 2018 edition
1 parent 35b778f commit 82ee839

File tree

7 files changed

+148
-124
lines changed

7 files changed

+148
-124
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
806806
ModuleOrUniformRoot::Module(module),
807807
ident,
808808
MacroNS,
809+
None,
809810
false,
810811
span,
811812
);

src/librustc_resolve/lib.rs

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ enum DeterminacyExt {
103103
WeakUndetermined,
104104
}
105105

106+
impl DeterminacyExt {
107+
fn to_determinacy(self) -> Determinacy {
108+
match self {
109+
DeterminacyExt::Determined => Determined,
110+
DeterminacyExt::Undetermined | DeterminacyExt::WeakUndetermined => Undetermined,
111+
}
112+
}
113+
}
114+
106115
/// A free importable items suggested in case of resolution failure.
107116
struct ImportSuggestion {
108117
path: Path,
@@ -961,15 +970,22 @@ impl<'a> LexicalScopeBinding<'a> {
961970
}
962971
}
963972

973+
974+
#[derive(Clone, Copy, PartialEq, Debug)]
975+
enum UniformRootKind {
976+
CurrentScope,
977+
ExternPrelude,
978+
}
979+
964980
#[derive(Copy, Clone, Debug)]
965-
pub enum ModuleOrUniformRoot<'a> {
981+
enum ModuleOrUniformRoot<'a> {
966982
/// Regular module.
967983
Module(Module<'a>),
968984

969-
/// The `{{root}}` (`CrateRoot` aka "global") / `extern` initial segment
970-
/// in which external crates resolve, and also `crate` (only in `{{root}}`,
971-
/// but *not* `extern`), in the Rust 2018 edition.
972-
UniformRoot(Name),
985+
/// This "virtual module" denotes either resolution in extern prelude
986+
/// for paths starting with `::` on 2018 edition or `extern::`,
987+
/// or resolution in current scope for single-segment imports.
988+
UniformRoot(UniformRootKind),
973989
}
974990

975991
#[derive(Clone, Debug)]
@@ -2099,23 +2115,31 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
20992115
None
21002116
}
21012117

2102-
fn resolve_ident_in_module(&mut self,
2103-
module: ModuleOrUniformRoot<'a>,
2104-
mut ident: Ident,
2105-
ns: Namespace,
2106-
record_used: bool,
2107-
span: Span)
2108-
-> Result<&'a NameBinding<'a>, Determinacy> {
2118+
fn resolve_ident_in_module(
2119+
&mut self,
2120+
module: ModuleOrUniformRoot<'a>,
2121+
mut ident: Ident,
2122+
ns: Namespace,
2123+
parent_scope: Option<&ParentScope<'a>>,
2124+
record_used: bool,
2125+
path_span: Span
2126+
) -> Result<&'a NameBinding<'a>, Determinacy> {
21092127
ident.span = ident.span.modern();
21102128
let orig_current_module = self.current_module;
2111-
if let ModuleOrUniformRoot::Module(module) = module {
2112-
if let Some(def) = ident.span.adjust(module.expansion) {
2113-
self.current_module = self.macro_def_scope(def);
2129+
match module {
2130+
ModuleOrUniformRoot::Module(module) => {
2131+
if let Some(def) = ident.span.adjust(module.expansion) {
2132+
self.current_module = self.macro_def_scope(def);
2133+
}
2134+
}
2135+
ModuleOrUniformRoot::UniformRoot(UniformRootKind::ExternPrelude) => {
2136+
ident.span.adjust(Mark::root());
21142137
}
2138+
_ => {}
21152139
}
2116-
let result = self.resolve_ident_in_module_unadjusted(
2117-
module, ident, ns, record_used, span,
2118-
);
2140+
let result = self.resolve_ident_in_module_unadjusted_ext(
2141+
module, ident, ns, parent_scope, false, record_used, path_span,
2142+
).map_err(DeterminacyExt::to_determinacy);
21192143
self.current_module = orig_current_module;
21202144
result
21212145
}
@@ -2614,6 +2638,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
26142638
ModuleOrUniformRoot::Module(module),
26152639
ident,
26162640
ns,
2641+
None,
26172642
false,
26182643
span,
26192644
).is_err() {
@@ -3624,9 +3649,9 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
36243649
continue;
36253650
}
36263651
if name == keywords::Extern.name() ||
3627-
name == keywords::CrateRoot.name() &&
3628-
self.session.rust_2018() {
3629-
module = Some(ModuleOrUniformRoot::UniformRoot(name));
3652+
name == keywords::CrateRoot.name() && self.session.rust_2018() {
3653+
module =
3654+
Some(ModuleOrUniformRoot::UniformRoot(UniformRootKind::ExternPrelude));
36303655
continue;
36313656
}
36323657
if name == keywords::CrateRoot.name() ||
@@ -3656,7 +3681,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
36563681
}
36573682

36583683
let binding = if let Some(module) = module {
3659-
self.resolve_ident_in_module(module, ident, ns, record_used, path_span)
3684+
self.resolve_ident_in_module(module, ident, ns, None, record_used, path_span)
36603685
} else if opt_ns.is_none() || opt_ns == Some(MacroNS) {
36613686
assert!(ns == TypeNS);
36623687
self.early_resolve_ident_in_lexical_scope(ident, ns, None, opt_ns.is_none(),
@@ -3675,7 +3700,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
36753700
def, path.len() - 1
36763701
));
36773702
}
3678-
_ => Err(if record_used { Determined } else { Undetermined }),
3703+
_ => Err(Determinacy::determined(record_used)),
36793704
}
36803705
};
36813706

@@ -3748,7 +3773,8 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
37483773

37493774
PathResult::Module(match module {
37503775
Some(module) => module,
3751-
None if path.is_empty() => ModuleOrUniformRoot::UniformRoot(keywords::Invalid.name()),
3776+
None if path.is_empty() =>
3777+
ModuleOrUniformRoot::UniformRoot(UniformRootKind::CurrentScope),
37523778
_ => span_bug!(path_span, "resolve_path: non-empty path `{:?}` has no module", path),
37533779
})
37543780
}
@@ -3960,6 +3986,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
39603986
ModuleOrUniformRoot::Module(module),
39613987
ident,
39623988
ns,
3989+
None,
39633990
false,
39643991
module.span,
39653992
) {
@@ -4282,6 +4309,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
42824309
ModuleOrUniformRoot::Module(module),
42834310
ident,
42844311
ns,
4312+
None,
42854313
false,
42864314
module.span,
42874315
).is_ok() {
@@ -4879,6 +4907,10 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
48794907

48804908
fn extern_prelude_get(&mut self, ident: Ident, speculative: bool, skip_feature_gate: bool)
48814909
-> Option<&'a NameBinding<'a>> {
4910+
if ident.is_path_segment_keyword() {
4911+
// Make sure `self`, `super` etc produce an error when passed to here.
4912+
return None;
4913+
}
48824914
self.extern_prelude.get(&ident.modern()).cloned().and_then(|entry| {
48834915
if let Some(binding) = entry.extern_crate_item {
48844916
if !speculative && !skip_feature_gate && entry.introduced_by_item &&

src/librustc_resolve/macros.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,11 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
371371
}
372372

373373
impl<'a, 'cl> Resolver<'a, 'cl> {
374-
pub fn dummy_parent_scope(&mut self) -> ParentScope<'a> {
374+
pub fn dummy_parent_scope(&self) -> ParentScope<'a> {
375375
self.invoc_parent_scope(Mark::root(), Vec::new())
376376
}
377377

378-
fn invoc_parent_scope(&mut self, invoc_id: Mark, derives: Vec<ast::Path>) -> ParentScope<'a> {
378+
fn invoc_parent_scope(&self, invoc_id: Mark, derives: Vec<ast::Path>) -> ParentScope<'a> {
379379
let invoc = self.invocations[&invoc_id];
380380
ParentScope {
381381
module: invoc.module.get().nearest_item_scope(),
@@ -606,6 +606,11 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
606606
assert!(macro_kind.is_none() || !is_import); // `is_import` implies no macro kind
607607
ident = ident.modern();
608608

609+
// Make sure `self`, `super` etc produce an error when passed to here.
610+
if ident.is_path_segment_keyword() {
611+
return Err(Determinacy::Determined);
612+
}
613+
609614
// This is *the* result, resolution from the scope closest to the resolved identifier.
610615
// However, sometimes this result is "weak" because it comes from a glob import or
611616
// a macro expansion, and in this case it cannot shadow names from outer scopes, e.g.
@@ -667,6 +672,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
667672
ModuleOrUniformRoot::Module(module),
668673
ident,
669674
ns,
675+
None,
670676
true,
671677
record_used,
672678
path_span,

0 commit comments

Comments
 (0)