Skip to content

Commit 0e8a97f

Browse files
committed
resolve: Avoid marking extern crate items as used in certain cases
1 parent 8e88c34 commit 0e8a97f

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/librustc_resolve/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,14 +1970,26 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
19701970
self.arenas.alloc_module(module)
19711971
}
19721972

1973-
fn record_use(&mut self, ident: Ident, ns: Namespace, binding: &'a NameBinding<'a>) {
1974-
match binding.kind {
1973+
fn record_use(&mut self, ident: Ident, ns: Namespace,
1974+
used_binding: &'a NameBinding<'a>, is_lexical_scope: bool) {
1975+
match used_binding.kind {
19751976
NameBindingKind::Import { directive, binding, ref used } if !used.get() => {
1977+
// Avoid marking `extern crate` items that refer to a name from extern prelude,
1978+
// but not introduce it, as used if they are accessed from lexical scope.
1979+
if is_lexical_scope {
1980+
if let Some(entry) = self.extern_prelude.get(&ident.modern()) {
1981+
if let Some(crate_item) = entry.extern_crate_item {
1982+
if ptr::eq(used_binding, crate_item) && !entry.introduced_by_item {
1983+
return;
1984+
}
1985+
}
1986+
}
1987+
}
19761988
used.set(true);
19771989
directive.used.set(true);
19781990
self.used_imports.insert((directive.id, ns));
19791991
self.add_to_glob_map(directive.id, ident);
1980-
self.record_use(ident, ns, binding);
1992+
self.record_use(ident, ns, binding, false);
19811993
}
19821994
NameBindingKind::Ambiguity { kind, b1, b2 } => {
19831995
self.ambiguity_errors.push(AmbiguityError {
@@ -2965,7 +2977,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
29652977
Def::Const(..) if is_syntactic_ambiguity => {
29662978
// Disambiguate in favor of a unit struct/variant
29672979
// or constant pattern.
2968-
self.record_use(ident, ValueNS, binding.unwrap());
2980+
self.record_use(ident, ValueNS, binding.unwrap(), false);
29692981
Some(PathResolution::new(def))
29702982
}
29712983
Def::StructCtor(..) | Def::VariantCtor(..) |

src/librustc_resolve/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
996996
&parent_scope, true, true, ident.span) {
997997
Ok(binding) => {
998998
let initial_def = initial_binding.map(|initial_binding| {
999-
self.record_use(ident, MacroNS, initial_binding);
999+
self.record_use(ident, MacroNS, initial_binding, false);
10001000
initial_binding.def_ignoring_ambiguity()
10011001
});
10021002
let def = binding.def_ignoring_ambiguity();

src/librustc_resolve/resolve_imports.rs

Lines changed: 3 additions & 2 deletions
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.
@@ -924,7 +924,8 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
924924
// Consistency checks, analogous to `finalize_current_module_macro_resolutions`.
925925
let initial_def = result[ns].get().map(|initial_binding| {
926926
all_ns_err = false;
927-
this.record_use(ident, MacroNS, initial_binding);
927+
this.record_use(ident, ns, initial_binding,
928+
directive.module_path.is_empty());
928929
initial_binding.def_ignoring_ambiguity()
929930
});
930931
let def = binding.def_ignoring_ambiguity();

0 commit comments

Comments
 (0)