Generalize TyCtxt::item_name.

This commit is contained in:
Camille GILLOT 2025-07-03 09:09:26 +00:00
parent 277b0ecf34
commit 36bc0948e0
5 changed files with 14 additions and 10 deletions

View file

@ -59,7 +59,7 @@ fn equate_intrinsic_type<'tcx>(
/// Returns the unsafety of the given intrinsic.
fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hir::Safety {
let is_in_list = match tcx.item_name(intrinsic_id.into()) {
let is_in_list = match tcx.item_name(intrinsic_id) {
// When adding a new intrinsic to this list,
// it's usually worth updating that intrinsic's documentation
// to note that it's safe to call, since
@ -144,7 +144,7 @@ fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hi
tcx.def_span(intrinsic_id),
DiagMessage::from(format!(
"intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `{}`",
tcx.item_name(intrinsic_id.into())
tcx.item_name(intrinsic_id)
)
)).emit();
}

View file

@ -1590,7 +1590,8 @@ impl<'tcx> TyCtxt<'tcx> {
}
/// Look up the name of a definition across crates. This does not look at HIR.
pub fn opt_item_name(self, def_id: DefId) -> Option<Symbol> {
pub fn opt_item_name(self, def_id: impl IntoQueryParam<DefId>) -> Option<Symbol> {
let def_id = def_id.into_query_param();
if let Some(cnum) = def_id.as_crate_root() {
Some(self.crate_name(cnum))
} else {
@ -1610,7 +1611,8 @@ impl<'tcx> TyCtxt<'tcx> {
/// [`opt_item_name`] instead.
///
/// [`opt_item_name`]: Self::opt_item_name
pub fn item_name(self, id: DefId) -> Symbol {
pub fn item_name(self, id: impl IntoQueryParam<DefId>) -> Symbol {
let id = id.into_query_param();
self.opt_item_name(id).unwrap_or_else(|| {
bug!("item_name: no name for {:?}", self.def_path(id));
})
@ -1619,7 +1621,8 @@ 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<Ident> {
pub fn opt_item_ident(self, def_id: impl IntoQueryParam<DefId>) -> Option<Ident> {
let def_id = def_id.into_query_param();
let def = self.opt_item_name(def_id)?;
let span = self
.def_ident_span(def_id)
@ -1630,7 +1633,8 @@ impl<'tcx> TyCtxt<'tcx> {
/// Look up the name and span of a definition.
///
/// See [`item_name`][Self::item_name] for more information.
pub fn item_ident(self, def_id: DefId) -> Ident {
pub fn item_ident(self, def_id: impl IntoQueryParam<DefId>) -> Ident {
let def_id = def_id.into_query_param();
self.opt_item_ident(def_id).unwrap_or_else(|| {
bug!("item_ident: no name for {:?}", self.def_path(def_id));
})

View file

@ -1683,7 +1683,7 @@ pub fn intrinsic_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::Intrinsi
_ => true,
};
Some(ty::IntrinsicDef {
name: tcx.item_name(def_id.into()),
name: tcx.item_name(def_id),
must_be_overridden,
const_stable: tcx.has_attr(def_id, sym::rustc_intrinsic_const_stable_indirect),
})

View file

@ -1084,7 +1084,7 @@ fn find_fallback_pattern_typo<'tcx>(
&& infcx.can_eq(param_env, ty, cx.tcx.type_of(item.owner_id).instantiate_identity())
{
// Look for local consts.
let item_name = cx.tcx.item_name(item.owner_id.into());
let item_name = cx.tcx.item_name(item.owner_id);
let vis = cx.tcx.visibility(item.owner_id);
if vis.is_accessible_from(parent, cx.tcx) {
accessible.push(item_name);

View file

@ -79,7 +79,7 @@ fn dump_abi_of_fn_item(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
for meta_item in meta_items {
match meta_item.name() {
Some(sym::debug) => {
let fn_name = tcx.item_name(item_def_id.into());
let fn_name = tcx.item_name(item_def_id);
tcx.dcx().emit_err(AbiOf {
span: tcx.def_span(item_def_id),
fn_name,
@ -135,7 +135,7 @@ fn dump_abi_of_fn_type(tcx: TyCtxt<'_>, item_def_id: LocalDefId, attr: &Attribut
item_def_id,
);
let fn_name = tcx.item_name(item_def_id.into());
let fn_name = tcx.item_name(item_def_id);
tcx.dcx().emit_err(AbiOf { span, fn_name, fn_abi: format!("{:#?}", abi) });
}
Some(sym::assert_eq) => {