diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2025-08-29 17:03:38 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2025-08-29 17:23:36 +0300 |
| commit | 7aec84d485ec1159c3ead3800ba601fd6d1d7869 (patch) | |
| tree | 50a1d0aff203a786c5c4627d8d62bf56de42bb46 /compiler/rustc_resolve | |
| parent | 41f2b6b39e7526a28d50ff6918dda6de48add5e4 (diff) | |
| download | rust-7aec84d485ec1159c3ead3800ba601fd6d1d7869.tar.gz rust-7aec84d485ec1159c3ead3800ba601fd6d1d7869.zip | |
resolve: Merge `ExternPreludeEntry::introduced_by_item` into `item_binding`
Diffstat (limited to 'compiler/rustc_resolve')
| -rw-r--r-- | compiler/rustc_resolve/src/build_reduced_graph.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/check_unused.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/diagnostics.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/lib.rs | 26 |
4 files changed, 21 insertions, 18 deletions
diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index f75a625a279..feaaaa6f539 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -1008,16 +1008,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> { let msg = format!("extern crate `{ident}` already in extern prelude"); self.r.tcx.dcx().span_delayed_bug(item.span, msg); } else { - entry.item_binding = Some(imported_binding); - entry.introduced_by_item = orig_name.is_some(); + entry.item_binding = Some((imported_binding, orig_name.is_some())); } entry } Entry::Vacant(vacant) => vacant.insert(ExternPreludeEntry { - item_binding: Some(imported_binding), + item_binding: Some((imported_binding, true)), flag_binding: Cell::new(None), only_item: true, - introduced_by_item: true, }), }; } diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index 11d93a58ae2..50a1ad23a54 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -204,7 +204,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> { .r .extern_prelude .get(&Macros20NormalizedIdent::new(extern_crate.ident)) - .is_none_or(|entry| entry.introduced_by_item) + .is_none_or(|entry| entry.introduced_by_item()) { continue; } diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 337e7d2dd86..1ec527e3b81 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -322,7 +322,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { let from_item = self .extern_prelude .get(&Macros20NormalizedIdent::new(ident)) - .is_none_or(|entry| entry.introduced_by_item); + .is_none_or(|entry| entry.introduced_by_item()); // Only suggest removing an import if both bindings are to the same def, if both spans // aren't dummy spans. Further, if both bindings are imports, then the ident must have // been introduced by an item. @@ -1845,7 +1845,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { let AmbiguityError { kind, ident, b1, b2, misc1, misc2, .. } = *ambiguity_error; let extern_prelude_ambiguity = || { self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)).is_some_and(|entry| { - entry.item_binding == Some(b1) && entry.flag_binding.get() == Some(b2) + entry.item_binding.map(|(b, _)| b) == Some(b1) + && entry.flag_binding.get() == Some(b2) }) }; let (b1, b2, misc1, misc2, swapped) = if b2.span.is_dummy() && !b1.span.is_dummy() { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 2afb52ef4d4..e3d896be0d2 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1028,15 +1028,20 @@ impl<'ra> NameBindingData<'ra> { #[derive(Default, Clone)] struct ExternPreludeEntry<'ra> { /// Binding from an `extern crate` item. - item_binding: Option<NameBinding<'ra>>, + /// The boolean flag is true is `item_binding` is non-redundant, happens either when + /// `only_item` is true, or when `extern crate` introducing `item_binding` used renaming. + item_binding: Option<(NameBinding<'ra>, /* introduced by item */ bool)>, /// Binding from an `--extern` flag, lazily populated on first use. flag_binding: Cell<Option<NameBinding<'ra>>>, /// There was no `--extern` flag introducing this name, /// `flag_binding` doesn't need to be populated. only_item: bool, - /// `item_binding` is non-redundant, happens either when `only_item` is true, - /// or when `extern crate` introducing `item_binding` used renaming. - introduced_by_item: bool, +} + +impl ExternPreludeEntry<'_> { + fn introduced_by_item(&self) -> bool { + matches!(self.item_binding, Some((_, true))) + } } struct DeriveData { @@ -2062,12 +2067,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } // Avoid marking `extern crate` items that refer to a name from extern prelude, // but not introduce it, as used if they are accessed from lexical scope. - if used == Used::Scope { - if let Some(entry) = self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)) { - if !entry.introduced_by_item && entry.item_binding == Some(used_binding) { - return; - } - } + if used == Used::Scope + && let Some(entry) = self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)) + && entry.item_binding == Some((used_binding, false)) + { + return; } let old_used = self.import_use_map.entry(import).or_insert(used); if *old_used < used { @@ -2226,7 +2230,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { finalize: bool, ) -> Option<NameBinding<'ra>> { let entry = self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)); - entry.and_then(|entry| entry.item_binding).map(|binding| { + entry.and_then(|entry| entry.item_binding).map(|(binding, _)| { if finalize { self.get_mut().record_use(ident, binding, Used::Scope); } |
