Skip to content

Commit 7aec84d

Browse files
committed
resolve: Merge ExternPreludeEntry::introduced_by_item into item_binding
1 parent 41f2b6b commit 7aec84d

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,16 +1008,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10081008
let msg = format!("extern crate `{ident}` already in extern prelude");
10091009
self.r.tcx.dcx().span_delayed_bug(item.span, msg);
10101010
} else {
1011-
entry.item_binding = Some(imported_binding);
1012-
entry.introduced_by_item = orig_name.is_some();
1011+
entry.item_binding = Some((imported_binding, orig_name.is_some()));
10131012
}
10141013
entry
10151014
}
10161015
Entry::Vacant(vacant) => vacant.insert(ExternPreludeEntry {
1017-
item_binding: Some(imported_binding),
1016+
item_binding: Some((imported_binding, true)),
10181017
flag_binding: Cell::new(None),
10191018
only_item: true,
1020-
introduced_by_item: true,
10211019
}),
10221020
};
10231021
}

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> {
204204
.r
205205
.extern_prelude
206206
.get(&Macros20NormalizedIdent::new(extern_crate.ident))
207-
.is_none_or(|entry| entry.introduced_by_item)
207+
.is_none_or(|entry| entry.introduced_by_item())
208208
{
209209
continue;
210210
}

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
322322
let from_item = self
323323
.extern_prelude
324324
.get(&Macros20NormalizedIdent::new(ident))
325-
.is_none_or(|entry| entry.introduced_by_item);
325+
.is_none_or(|entry| entry.introduced_by_item());
326326
// Only suggest removing an import if both bindings are to the same def, if both spans
327327
// aren't dummy spans. Further, if both bindings are imports, then the ident must have
328328
// been introduced by an item.
@@ -1845,7 +1845,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18451845
let AmbiguityError { kind, ident, b1, b2, misc1, misc2, .. } = *ambiguity_error;
18461846
let extern_prelude_ambiguity = || {
18471847
self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)).is_some_and(|entry| {
1848-
entry.item_binding == Some(b1) && entry.flag_binding.get() == Some(b2)
1848+
entry.item_binding.map(|(b, _)| b) == Some(b1)
1849+
&& entry.flag_binding.get() == Some(b2)
18491850
})
18501851
};
18511852
let (b1, b2, misc1, misc2, swapped) = if b2.span.is_dummy() && !b1.span.is_dummy() {

compiler/rustc_resolve/src/lib.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,15 +1028,20 @@ impl<'ra> NameBindingData<'ra> {
10281028
#[derive(Default, Clone)]
10291029
struct ExternPreludeEntry<'ra> {
10301030
/// Binding from an `extern crate` item.
1031-
item_binding: Option<NameBinding<'ra>>,
1031+
/// The boolean flag is true is `item_binding` is non-redundant, happens either when
1032+
/// `only_item` is true, or when `extern crate` introducing `item_binding` used renaming.
1033+
item_binding: Option<(NameBinding<'ra>, /* introduced by item */ bool)>,
10321034
/// Binding from an `--extern` flag, lazily populated on first use.
10331035
flag_binding: Cell<Option<NameBinding<'ra>>>,
10341036
/// There was no `--extern` flag introducing this name,
10351037
/// `flag_binding` doesn't need to be populated.
10361038
only_item: bool,
1037-
/// `item_binding` is non-redundant, happens either when `only_item` is true,
1038-
/// or when `extern crate` introducing `item_binding` used renaming.
1039-
introduced_by_item: bool,
1039+
}
1040+
1041+
impl ExternPreludeEntry<'_> {
1042+
fn introduced_by_item(&self) -> bool {
1043+
matches!(self.item_binding, Some((_, true)))
1044+
}
10401045
}
10411046

10421047
struct DeriveData {
@@ -2062,12 +2067,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20622067
}
20632068
// Avoid marking `extern crate` items that refer to a name from extern prelude,
20642069
// but not introduce it, as used if they are accessed from lexical scope.
2065-
if used == Used::Scope {
2066-
if let Some(entry) = self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)) {
2067-
if !entry.introduced_by_item && entry.item_binding == Some(used_binding) {
2068-
return;
2069-
}
2070-
}
2070+
if used == Used::Scope
2071+
&& let Some(entry) = self.extern_prelude.get(&Macros20NormalizedIdent::new(ident))
2072+
&& entry.item_binding == Some((used_binding, false))
2073+
{
2074+
return;
20712075
}
20722076
let old_used = self.import_use_map.entry(import).or_insert(used);
20732077
if *old_used < used {
@@ -2226,7 +2230,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22262230
finalize: bool,
22272231
) -> Option<NameBinding<'ra>> {
22282232
let entry = self.extern_prelude.get(&Macros20NormalizedIdent::new(ident));
2229-
entry.and_then(|entry| entry.item_binding).map(|binding| {
2233+
entry.and_then(|entry| entry.item_binding).map(|(binding, _)| {
22302234
if finalize {
22312235
self.get_mut().record_use(ident, binding, Used::Scope);
22322236
}

0 commit comments

Comments
 (0)