Skip to content

Commit c8aacf2

Browse files
committed
resolve: Stop generating uniform path canaries
1 parent 3724f16 commit c8aacf2

File tree

2 files changed

+13
-266
lines changed

2 files changed

+13
-266
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 5 additions & 104 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: &[Ident],
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,98 +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<Ident>| {
200-
let subclass = SingleImport {
201-
target: Ident {
202-
name: keywords::Underscore.name().gensymed(),
203-
span: source.span,
204-
},
205-
source,
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.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(Ident {
228-
name: keywords::SelfValue.name(),
229-
span: source.span,
230-
}));
231-
232-
// One special unprefixed canary per block scope around
233-
// the import, to detect items unreachable by `self::x`.
234-
let orig_current_module = self.current_module;
235-
let mut span = source.span.modern();
236-
loop {
237-
match self.current_module.kind {
238-
ModuleKind::Block(..) => emit(self, None),
239-
ModuleKind::Def(..) => break,
240-
}
241-
match self.hygienic_lexical_parent(self.current_module, &mut span) {
242-
Some(module) => {
243-
self.current_module = module;
244-
}
245-
None => break,
246-
}
247-
}
248-
self.current_module = orig_current_module;
249-
250-
uniform_paths_canary_emitted = true;
251-
}
252-
253158
let empty_for_self = |prefix: &[Ident]| {
254159
prefix.is_empty() ||
255160
prefix.len() == 1 && prefix[0].name == keywords::CrateRoot.name()
@@ -344,7 +249,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
344249
item.id,
345250
vis,
346251
parent_scope,
347-
false, // is_uniform_paths_canary
348252
);
349253
}
350254
ast::UseTreeKind::Glob => {
@@ -361,7 +265,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
361265
item.id,
362266
vis,
363267
parent_scope,
364-
false, // is_uniform_paths_canary
365268
);
366269
}
367270
ast::UseTreeKind::Nested(ref items) => {
@@ -390,7 +293,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
390293
for &(ref tree, id) in items {
391294
self.build_reduced_graph_for_use_tree(
392295
// This particular use tree
393-
tree, id, &prefix, true, uniform_paths_canary_emitted,
296+
tree, id, &prefix, true,
394297
// The whole `use` item
395298
parent_scope.clone(), item, vis, root_span,
396299
);
@@ -414,7 +317,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
414317
};
415318
self.build_reduced_graph_for_use_tree(
416319
// This particular use tree
417-
&tree, id, &prefix, true, uniform_paths_canary_emitted,
320+
&tree, id, &prefix, true,
418321
// The whole `use` item
419322
parent_scope.clone(), item, ty::Visibility::Invisible, root_span,
420323
);
@@ -435,7 +338,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
435338
ItemKind::Use(ref use_tree) => {
436339
self.build_reduced_graph_for_use_tree(
437340
// This particular use tree
438-
use_tree, item.id, &[], false, false,
341+
use_tree, item.id, &[], false,
439342
// The whole `use` item
440343
parent_scope, item, vis, use_tree.span,
441344
);
@@ -486,7 +389,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
486389
module_path: Vec::new(),
487390
vis: Cell::new(vis),
488391
used: Cell::new(used),
489-
is_uniform_paths_canary: false,
490392
});
491393
self.potentially_unused_imports.push(directive);
492394
let imported_binding = self.import(binding, directive);
@@ -899,7 +801,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
899801
module_path: Vec::new(),
900802
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
901803
used: Cell::new(false),
902-
is_uniform_paths_canary: false,
903804
});
904805

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

0 commit comments

Comments
 (0)