From 42014b8abdbd245c25887f1357988e7e786006e1 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 25 Jan 2025 17:50:39 +0000 Subject: [PATCH] Remove opt_item_ident --- compiler/rustc_lint/src/lints.rs | 2 +- compiler/rustc_lint/src/ptr_nulls.rs | 4 ++-- compiler/rustc_metadata/src/rmeta/decoder.rs | 10 +++----- compiler/rustc_middle/src/ty/mod.rs | 11 --------- .../src/error_reporting/traits/suggestions.rs | 23 +++++++++---------- .../clippy_lints/src/error_impl_error.rs | 3 +-- 6 files changed, 18 insertions(+), 35 deletions(-) diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 84a6272198fa2..914b337353f62 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -600,7 +600,7 @@ pub(crate) enum PtrNullChecksDiag<'a> { label: Span, }, #[diag(lint_ptr_null_checks_fn_ret)] - FnRet { fn_name: Ident }, + FnRet { fn_name: Symbol }, } // for_loops_over_fallibles.rs diff --git a/compiler/rustc_lint/src/ptr_nulls.rs b/compiler/rustc_lint/src/ptr_nulls.rs index 1489f9de81991..08ec2ed4c3f27 100644 --- a/compiler/rustc_lint/src/ptr_nulls.rs +++ b/compiler/rustc_lint/src/ptr_nulls.rs @@ -48,14 +48,14 @@ fn incorrect_check<'a, 'tcx: 'a>( if let ExprKind::MethodCall(_, _expr, [], _) = e.kind && let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id) && cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr) - && let Some(fn_name) = cx.tcx.opt_item_ident(def_id) + && let Some(fn_name) = cx.tcx.opt_item_name(def_id) { return Some(PtrNullChecksDiag::FnRet { fn_name }); } else if let ExprKind::Call(path, _args) = e.kind && let ExprKind::Path(ref qpath) = path.kind && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id() && cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr) - && let Some(fn_name) = cx.tcx.opt_item_ident(def_id) + && let Some(fn_name) = cx.tcx.opt_item_name(def_id) { return Some(PtrNullChecksDiag::FnRet { fn_name }); } diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index e02c4871f35bd..a06ffc8855358 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -999,8 +999,8 @@ impl<'a> CrateMetadataRef<'a> { self.opt_item_name(item_index).expect("no encoded ident for item") } - fn opt_item_ident(self, item_index: DefIndex, sess: &Session) -> Option { - let name = self.opt_item_name(item_index)?; + fn item_ident(self, item_index: DefIndex, sess: &Session) -> Ident { + let name = self.opt_item_name(item_index).expect("no encoded ident for item"); let span = self .root .tables @@ -1008,11 +1008,7 @@ impl<'a> CrateMetadataRef<'a> { .get(self, item_index) .unwrap_or_else(|| self.missing("def_ident_span", item_index)) .decode((self, sess)); - Some(Ident::new(name, span)) - } - - fn item_ident(self, item_index: DefIndex, sess: &Session) -> Ident { - self.opt_item_ident(item_index, sess).expect("no encoded ident for item") + Ident::new(name, span) } #[inline] diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index ca70ae794c530..25fb4affc1353 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1585,17 +1585,6 @@ impl<'tcx> TyCtxt<'tcx> { }) } - /// Look up the name and span of a definition. - /// - /// See [`item_name`][Self::item_name] for more information. - pub fn opt_item_ident(self, def_id: DefId) -> Option { - let def = self.opt_item_name(def_id)?; - let span = self - .def_ident_span(def_id) - .unwrap_or_else(|| bug!("missing ident span for {def_id:?}")); - Some(Ident::new(def, span)) - } - pub fn opt_associated_item(self, def_id: DefId) -> Option { if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = self.def_kind(def_id) { Some(self.associated_item(def_id)) diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index 471105773e2b0..6ceeb4db80959 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -2795,15 +2795,15 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let short_item_name = with_forced_trimmed_paths!(tcx.def_path_str(item_def_id)); let mut multispan = MultiSpan::from(span); let sm = tcx.sess.source_map(); - if let Some(ident) = tcx.opt_item_ident(item_def_id) { + if let Some(ident_span) = tcx.def_ident_span(item_def_id) { let same_line = - match (sm.lookup_line(ident.span.hi()), sm.lookup_line(span.lo())) { + match (sm.lookup_line(ident_span.hi()), sm.lookup_line(span.lo())) { (Ok(l), Ok(r)) => l.line == r.line, _ => true, }; - if ident.span.is_visible(sm) && !ident.span.overlaps(span) && !same_line { + if ident_span.is_visible(sm) && !ident_span.overlaps(span) && !same_line { multispan.push_span_label( - ident.span, + ident_span, format!( "required by a bound in this {}", tcx.def_kind(item_def_id).descr(item_def_id) @@ -3253,9 +3253,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { let ty_str = tcx.short_ty_string(ty, long_ty_file); let msg = format!("required because it appears within the type `{ty_str}`"); match ty.kind() { - ty::Adt(def, _) => match tcx.opt_item_ident(def.did()) { - Some(ident) => { - err.span_note(ident.span, msg); + ty::Adt(def, _) => match tcx.def_ident_span(def.did()) { + Some(ident_span) => { + err.span_note(ident_span, msg); } None => { err.note(msg); @@ -3595,19 +3595,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { `{item_name}` but not on the corresponding trait's {kind}", ); let sp = tcx - .opt_item_ident(trait_item_def_id) - .map(|i| i.span) + .def_ident_span(trait_item_def_id) .unwrap_or_else(|| tcx.def_span(trait_item_def_id)); let mut assoc_span: MultiSpan = sp.into(); assoc_span.push_span_label( sp, format!("this trait's {kind} doesn't have the requirement `{predicate}`"), ); - if let Some(ident) = tcx + if let Some(ident_span) = tcx .opt_associated_item(trait_item_def_id) - .and_then(|i| tcx.opt_item_ident(i.container_id(tcx))) + .and_then(|i| tcx.def_ident_span(i.container_id(tcx))) { - assoc_span.push_span_label(ident.span, "in this trait"); + assoc_span.push_span_label(ident_span, "in this trait"); } err.span_note(assoc_span, msg); } diff --git a/src/tools/clippy/clippy_lints/src/error_impl_error.rs b/src/tools/clippy/clippy_lints/src/error_impl_error.rs index 1e6447dc25370..bf8d11a89de5d 100644 --- a/src/tools/clippy/clippy_lints/src/error_impl_error.rs +++ b/src/tools/clippy/clippy_lints/src/error_impl_error.rs @@ -56,8 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for ErrorImplError { && let Some(error_def_id) = cx.tcx.get_diagnostic_item(sym::Error) && error_def_id == trait_def_id && let Some(def_id) = path_res(cx, imp.self_ty).opt_def_id().and_then(DefId::as_local) - && let Some(ident) = cx.tcx.opt_item_ident(def_id.to_def_id()) - && ident.name == sym::Error + && let Some(sym::Error) = cx.tcx.opt_item_name(def_id.to_def_id()) && is_visible_outside_module(cx, def_id) => { span_lint_hir_and_then(