Skip to content

Commit 67feeeb

Browse files
committed
resolve: Stop generating uniform path canaries
1 parent f37247f commit 67feeeb

File tree

2 files changed

+13
-269
lines changed

2 files changed

+13
-269
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 5 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,14 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
116116
id: NodeId,
117117
parent_prefix: &[Segment],
118118
nested: bool,
119-
mut uniform_paths_canary_emitted: bool,
120119
// The whole `use` item
121120
parent_scope: ParentScope<'a>,
122121
item: &Item,
123122
vis: ty::Visibility,
124123
root_span: Span,
125124
) {
126-
debug!("build_reduced_graph_for_use_tree(parent_prefix={:?}, \
127-
uniform_paths_canary_emitted={}, \
128-
use_tree={:?}, nested={})",
129-
parent_prefix, uniform_paths_canary_emitted, use_tree, nested);
125+
debug!("build_reduced_graph_for_use_tree(parent_prefix={:?}, use_tree={:?}, nested={})",
126+
parent_prefix, use_tree, nested);
130127

131128
let uniform_paths =
132129
self.session.rust_2018() &&
@@ -158,101 +155,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
158155
let prefix: Vec<_> = root.into_iter().chain(prefix_iter()).collect();
159156

160157
debug!("build_reduced_graph_for_use_tree: prefix={:?}", prefix);
161-
162-
// `#[feature(uniform_paths)]` allows an unqualified import path,
163-
// e.g. `use x::...;` to resolve not just globally (`use ::x::...;`)
164-
// but also relatively (`use self::x::...;`). To catch ambiguities
165-
// that might arise from both of these being available and resolution
166-
// silently picking one of them, an artificial `use self::x as _;`
167-
// import is injected as a "canary", and an error is emitted if it
168-
// successfully resolves while an `x` external crate exists.
169-
//
170-
// For each block scope around the `use` item, one special canary
171-
// import of the form `use x as _;` is also injected, having its
172-
// parent set to that scope; `resolve_imports` will only resolve
173-
// it within its appropriate scope; if any of them successfully
174-
// resolve, an ambiguity error is emitted, since the original
175-
// import can't see the item in the block scope (`self::x` only
176-
// looks in the enclosing module), but a non-`use` path could.
177-
//
178-
// Additionally, the canary might be able to catch limitations of the
179-
// current implementation, where `::x` may be chosen due to `self::x`
180-
// not existing, but `self::x` could appear later, from macro expansion.
181-
//
182-
// NB. The canary currently only errors if the `x::...` path *could*
183-
// resolve as a relative path through the extern crate, i.e. `x` is
184-
// in `extern_prelude`, *even though* `::x` might still forcefully
185-
// load a non-`extern_prelude` crate.
186-
// While always producing an ambiguity errors if `self::x` exists and
187-
// a crate *could* be loaded, would be more conservative, imports for
188-
// local modules named `test` (or less commonly, `syntax` or `log`),
189-
// would need to be qualified (e.g. `self::test`), which is considered
190-
// ergonomically unacceptable.
191-
let emit_uniform_paths_canary =
192-
!uniform_paths_canary_emitted &&
193-
self.session.rust_2018() &&
194-
starts_with_non_keyword;
195-
if emit_uniform_paths_canary {
196-
let source = prefix_start.unwrap();
197-
198-
// Helper closure to emit a canary with the given base path.
199-
let emit = |this: &mut Self, base: Option<Segment>| {
200-
let subclass = SingleImport {
201-
target: Ident {
202-
name: keywords::Underscore.name().gensymed(),
203-
span: source.ident.span,
204-
},
205-
source: source.ident,
206-
result: PerNS {
207-
type_ns: Cell::new(Err(Undetermined)),
208-
value_ns: Cell::new(Err(Undetermined)),
209-
macro_ns: Cell::new(Err(Undetermined)),
210-
},
211-
type_ns_only: false,
212-
};
213-
this.add_import_directive(
214-
base.into_iter().collect(),
215-
subclass,
216-
source.ident.span,
217-
id,
218-
root_span,
219-
item.id,
220-
ty::Visibility::Invisible,
221-
parent_scope.clone(),
222-
true, // is_uniform_paths_canary
223-
);
224-
};
225-
226-
// A single simple `self::x` canary.
227-
emit(self, Some(Segment {
228-
ident: Ident {
229-
name: keywords::SelfValue.name(),
230-
span: source.ident.span,
231-
},
232-
id: source.id
233-
}));
234-
235-
// One special unprefixed canary per block scope around
236-
// the import, to detect items unreachable by `self::x`.
237-
let orig_current_module = self.current_module;
238-
let mut span = source.ident.span.modern();
239-
loop {
240-
match self.current_module.kind {
241-
ModuleKind::Block(..) => emit(self, None),
242-
ModuleKind::Def(..) => break,
243-
}
244-
match self.hygienic_lexical_parent(self.current_module, &mut span) {
245-
Some(module) => {
246-
self.current_module = module;
247-
}
248-
None => break,
249-
}
250-
}
251-
self.current_module = orig_current_module;
252-
253-
uniform_paths_canary_emitted = true;
254-
}
255-
256158
let empty_for_self = |prefix: &[Segment]| {
257159
prefix.is_empty() ||
258160
prefix.len() == 1 && prefix[0].ident.name == keywords::CrateRoot.name()
@@ -350,7 +252,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
350252
item.id,
351253
vis,
352254
parent_scope,
353-
false, // is_uniform_paths_canary
354255
);
355256
}
356257
ast::UseTreeKind::Glob => {
@@ -367,7 +268,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
367268
item.id,
368269
vis,
369270
parent_scope,
370-
false, // is_uniform_paths_canary
371271
);
372272
}
373273
ast::UseTreeKind::Nested(ref items) => {
@@ -396,7 +296,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
396296
for &(ref tree, id) in items {
397297
self.build_reduced_graph_for_use_tree(
398298
// This particular use tree
399-
tree, id, &prefix, true, uniform_paths_canary_emitted,
299+
tree, id, &prefix, true,
400300
// The whole `use` item
401301
parent_scope.clone(), item, vis, root_span,
402302
);
@@ -420,7 +320,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
420320
};
421321
self.build_reduced_graph_for_use_tree(
422322
// This particular use tree
423-
&tree, id, &prefix, true, uniform_paths_canary_emitted,
323+
&tree, id, &prefix, true,
424324
// The whole `use` item
425325
parent_scope.clone(), item, ty::Visibility::Invisible, root_span,
426326
);
@@ -441,7 +341,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
441341
ItemKind::Use(ref use_tree) => {
442342
self.build_reduced_graph_for_use_tree(
443343
// This particular use tree
444-
use_tree, item.id, &[], false, false,
344+
use_tree, item.id, &[], false,
445345
// The whole `use` item
446346
parent_scope, item, vis, use_tree.span,
447347
);
@@ -492,7 +392,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
492392
module_path: Vec::new(),
493393
vis: Cell::new(vis),
494394
used: Cell::new(used),
495-
is_uniform_paths_canary: false,
496395
});
497396
self.potentially_unused_imports.push(directive);
498397
let imported_binding = self.import(binding, directive);
@@ -905,7 +804,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
905804
module_path: Vec::new(),
906805
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
907806
used: Cell::new(false),
908-
is_uniform_paths_canary: false,
909807
});
910808

911809
let allow_shadowing = parent_scope.expansion == Mark::root();

0 commit comments

Comments
 (0)