Skip to content

Commit 4cd3edd

Browse files
committed
resolve: Avoid marking extern crate items as used in certain cases
1 parent dd31c0c commit 4cd3edd

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/librustc_resolve/lib.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1911,14 +1911,26 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
19111911
self.arenas.alloc_module(module)
19121912
}
19131913

1914-
fn record_use(&mut self, ident: Ident, ns: Namespace, binding: &'a NameBinding<'a>) {
1915-
match binding.kind {
1914+
fn record_use(&mut self, ident: Ident, ns: Namespace,
1915+
used_binding: &'a NameBinding<'a>, is_lexical_scope: bool) {
1916+
match used_binding.kind {
19161917
NameBindingKind::Import { directive, binding, ref used } if !used.get() => {
1918+
// Avoid marking `extern crate` items that refer to a name from extern prelude,
1919+
// but not introduce it, as used if they are accessed from lexical scope.
1920+
if is_lexical_scope {
1921+
if let Some(entry) = self.extern_prelude.get(&ident.modern()) {
1922+
if let Some(crate_item) = entry.extern_crate_item {
1923+
if ptr::eq(used_binding, crate_item) && !entry.introduced_by_item {
1924+
return;
1925+
}
1926+
}
1927+
}
1928+
}
19171929
used.set(true);
19181930
directive.used.set(true);
19191931
self.used_imports.insert((directive.id, ns));
19201932
self.add_to_glob_map(directive.id, ident);
1921-
self.record_use(ident, ns, binding);
1933+
self.record_use(ident, ns, binding, false);
19221934
}
19231935
NameBindingKind::Ambiguity { kind, b1, b2 } => {
19241936
self.ambiguity_errors.push(AmbiguityError {
@@ -2908,7 +2920,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
29082920
Def::Const(..) if is_syntactic_ambiguity => {
29092921
// Disambiguate in favor of a unit struct/variant
29102922
// or constant pattern.
2911-
self.record_use(ident, ValueNS, binding.unwrap());
2923+
self.record_use(ident, ValueNS, binding.unwrap(), false);
29122924
Some(PathResolution::new(def))
29132925
}
29142926
Def::StructCtor(..) | Def::VariantCtor(..) |

src/librustc_resolve/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
987987
&parent_scope, true, true, ident.span) {
988988
Ok(binding) => {
989989
let initial_def = initial_binding.map(|initial_binding| {
990-
self.record_use(ident, MacroNS, initial_binding);
990+
self.record_use(ident, MacroNS, initial_binding, false);
991991
initial_binding.def_ignoring_ambiguity()
992992
});
993993
let def = binding.def_ignoring_ambiguity();

src/librustc_resolve/resolve_imports.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
234234
if self.last_import_segment && check_usable(self, binding).is_err() {
235235
Err(DeterminacyExt::Determined)
236236
} else {
237-
self.record_use(ident, ns, binding);
237+
self.record_use(ident, ns, binding, restricted_shadowing);
238238

239239
if let Some(shadowed_glob) = resolution.shadowed_glob {
240240
// Forbid expanded shadowing to avoid time travel.
@@ -922,7 +922,8 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
922922
// Consistency checks, analogous to `finalize_current_module_macro_resolutions`.
923923
let initial_def = result[ns].get().map(|initial_binding| {
924924
all_ns_err = false;
925-
this.record_use(ident, MacroNS, initial_binding);
925+
this.record_use(ident, ns, initial_binding,
926+
directive.module_path.is_empty());
926927
initial_binding.def_ignoring_ambiguity()
927928
});
928929
let def = binding.def_ignoring_ambiguity();

0 commit comments

Comments
 (0)