From e576d8850de702f6c712eaca77f758f72ef13419 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 25 Mar 2025 11:10:45 +1100 Subject: [PATCH] Use `Option` in `DuplicateLangItem`. For the the symbols that might not be present, instead of `kw::Empty`. --- compiler/rustc_passes/src/errors.rs | 20 +++++++++++++------- compiler/rustc_passes/src/lang_items.rs | 14 +++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index a72f40cd843a..0ee17430aab8 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -1348,12 +1348,12 @@ pub(crate) struct DuplicateLangItem { pub local_span: Option, pub lang_item_name: Symbol, pub crate_name: Symbol, - pub dependency_of: Symbol, + pub dependency_of: Option, pub is_local: bool, pub path: String, pub first_defined_span: Option, - pub orig_crate_name: Symbol, - pub orig_dependency_of: Symbol, + pub orig_crate_name: Option, + pub orig_dependency_of: Option, pub orig_is_local: bool, pub orig_path: String, pub(crate) duplicate: Duplicate, @@ -1374,10 +1374,16 @@ fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> { diag.code(E0152); diag.arg("lang_item_name", self.lang_item_name); diag.arg("crate_name", self.crate_name); - diag.arg("dependency_of", self.dependency_of); + if let Some(dependency_of) = self.dependency_of { + diag.arg("dependency_of", dependency_of); + } diag.arg("path", self.path); - diag.arg("orig_crate_name", self.orig_crate_name); - diag.arg("orig_dependency_of", self.orig_dependency_of); + if let Some(orig_crate_name) = self.orig_crate_name { + diag.arg("orig_crate_name", orig_crate_name); + } + if let Some(orig_dependency_of) = self.orig_dependency_of { + diag.arg("orig_dependency_of", orig_dependency_of); + } diag.arg("orig_path", self.orig_path); if let Some(span) = self.local_span { diag.span(span); @@ -1385,7 +1391,7 @@ fn into_diag(self, dcx: DiagCtxtHandle<'_>, level: Level) -> Diag<'_, G> { if let Some(span) = self.first_defined_span { diag.span_note(span, fluent::passes_first_defined_span); } else { - if self.orig_dependency_of.is_empty() { + if self.orig_dependency_of.is_none() { diag.note(fluent::passes_first_defined_crate); } else { diag.note(fluent::passes_first_defined_crate_depends); diff --git a/compiler/rustc_passes/src/lang_items.rs b/compiler/rustc_passes/src/lang_items.rs index fe1140d893e3..9d4b46cd3066 100644 --- a/compiler/rustc_passes/src/lang_items.rs +++ b/compiler/rustc_passes/src/lang_items.rs @@ -16,7 +16,7 @@ use rustc_middle::query::Providers; use rustc_middle::ty::{ResolverAstLowering, TyCtxt}; use rustc_session::cstore::ExternCrate; -use rustc_span::{Span, kw}; +use rustc_span::Span; use crate::errors::{ DuplicateLangItem, IncorrectTarget, LangItemOnIncorrectTarget, UnknownLangItem, @@ -98,7 +98,7 @@ fn collect_item(&mut self, lang_item: LangItem, item_def_id: DefId, item_span: O { let lang_item_name = lang_item.name(); let crate_name = self.tcx.crate_name(item_def_id.krate); - let mut dependency_of = kw::Empty; + let mut dependency_of = None; let is_local = item_def_id.is_local(); let path = if is_local { String::new() @@ -112,8 +112,8 @@ fn collect_item(&mut self, lang_item: LangItem, item_def_id: DefId, item_span: O }; let first_defined_span = self.item_spans.get(&original_def_id).copied(); - let mut orig_crate_name = kw::Empty; - let mut orig_dependency_of = kw::Empty; + let mut orig_crate_name = None; + let mut orig_dependency_of = None; let orig_is_local = original_def_id.is_local(); let orig_path = if orig_is_local { String::new() @@ -127,11 +127,11 @@ fn collect_item(&mut self, lang_item: LangItem, item_def_id: DefId, item_span: O }; if first_defined_span.is_none() { - orig_crate_name = self.tcx.crate_name(original_def_id.krate); + orig_crate_name = Some(self.tcx.crate_name(original_def_id.krate)); if let Some(ExternCrate { dependency_of: inner_dependency_of, .. }) = self.tcx.extern_crate(original_def_id.krate) { - orig_dependency_of = self.tcx.crate_name(*inner_dependency_of); + orig_dependency_of = Some(self.tcx.crate_name(*inner_dependency_of)); } } @@ -140,7 +140,7 @@ fn collect_item(&mut self, lang_item: LangItem, item_def_id: DefId, item_span: O } else { match self.tcx.extern_crate(item_def_id.krate) { Some(ExternCrate { dependency_of: inner_dependency_of, .. }) => { - dependency_of = self.tcx.crate_name(*inner_dependency_of); + dependency_of = Some(self.tcx.crate_name(*inner_dependency_of)); Duplicate::CrateDepends } _ => Duplicate::Crate,