1
Fork 0

Rollup merge of #134265 - compiler-errors:ty_def_id, r=oli-obk

Rename `ty_def_id` so people will stop using it by accident

This function is just for cycle detection, but people keep using it because they think it's the right way of getting the def id from a `Ty` (and I can't blame them necessarily).
This commit is contained in:
Matthias Krüger 2024-12-14 04:09:36 +01:00 committed by GitHub
commit c55a97ed84
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 17 additions and 19 deletions

View file

@ -9,7 +9,6 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::query::Key;
use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _}; use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
use rustc_middle::ty::{ use rustc_middle::ty::{
self, AdtDef, Binder, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeVisitableExt, self, AdtDef, Binder, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeVisitableExt,
@ -1007,8 +1006,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
)), )),
.. ..
}) = node }) = node
&& let Some(ty_def_id) = qself_ty.ty_def_id() && let Some(adt_def) = qself_ty.ty_adt_def()
&& let [inherent_impl] = tcx.inherent_impls(ty_def_id) && let [inherent_impl] = tcx.inherent_impls(adt_def.did())
&& let name = format!("{ident2}_{ident3}") && let name = format!("{ident2}_{ident3}")
&& let Some(ty::AssocItem { kind: ty::AssocKind::Fn, .. }) = tcx && let Some(ty::AssocItem { kind: ty::AssocKind::Fn, .. }) = tcx
.associated_items(inherent_impl) .associated_items(inherent_impl)

View file

@ -41,7 +41,8 @@ pub trait Key: Sized {
None None
} }
fn ty_def_id(&self) -> Option<DefId> { /// Used to detect when ADT def ids are used as keys in a cycle for better error reporting.
fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
None None
} }
} }
@ -423,7 +424,7 @@ impl<'tcx> Key for Ty<'tcx> {
DUMMY_SP DUMMY_SP
} }
fn ty_def_id(&self) -> Option<DefId> { fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
match *self.kind() { match *self.kind() {
ty::Adt(adt, _) => Some(adt.did()), ty::Adt(adt, _) => Some(adt.did()),
ty::Coroutine(def_id, ..) => Some(def_id), ty::Coroutine(def_id, ..) => Some(def_id),
@ -471,8 +472,8 @@ impl<'tcx, T: Key> Key for ty::PseudoCanonicalInput<'tcx, T> {
self.value.default_span(tcx) self.value.default_span(tcx)
} }
fn ty_def_id(&self) -> Option<DefId> { fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
self.value.ty_def_id() self.value.def_id_for_ty_in_cycle()
} }
} }
@ -593,7 +594,7 @@ impl<'tcx> Key for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx>
DUMMY_SP DUMMY_SP
} }
fn ty_def_id(&self) -> Option<DefId> { fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
match self.1.value.kind() { match self.1.value.kind() {
ty::Adt(adt, _) => Some(adt.did()), ty::Adt(adt, _) => Some(adt.did()),
_ => None, _ => None,

View file

@ -100,7 +100,7 @@ impl<'tcx> Value<TyCtxt<'tcx>> for Representability {
} }
for info in &cycle_error.cycle { for info in &cycle_error.cycle {
if info.query.dep_kind == dep_kinds::representability_adt_ty if info.query.dep_kind == dep_kinds::representability_adt_ty
&& let Some(def_id) = info.query.ty_def_id && let Some(def_id) = info.query.def_id_for_ty_in_cycle
&& let Some(def_id) = def_id.as_local() && let Some(def_id) = def_id.as_local()
&& !item_and_field_ids.iter().any(|&(id, _)| id == def_id) && !item_and_field_ids.iter().any(|&(id, _)| id == def_id)
{ {
@ -182,7 +182,7 @@ impl<'tcx, T> Value<TyCtxt<'tcx>> for Result<T, &'_ ty::layout::LayoutError<'_>>
&cycle_error.cycle, &cycle_error.cycle,
|cycle| { |cycle| {
if cycle[0].query.dep_kind == dep_kinds::layout_of if cycle[0].query.dep_kind == dep_kinds::layout_of
&& let Some(def_id) = cycle[0].query.ty_def_id && let Some(def_id) = cycle[0].query.def_id_for_ty_in_cycle
&& let Some(def_id) = def_id.as_local() && let Some(def_id) = def_id.as_local()
&& let def_kind = tcx.def_kind(def_id) && let def_kind = tcx.def_kind(def_id)
&& matches!(def_kind, DefKind::Closure) && matches!(def_kind, DefKind::Closure)
@ -209,7 +209,7 @@ impl<'tcx, T> Value<TyCtxt<'tcx>> for Result<T, &'_ ty::layout::LayoutError<'_>>
if frame.query.dep_kind != dep_kinds::layout_of { if frame.query.dep_kind != dep_kinds::layout_of {
continue; continue;
} }
let Some(frame_def_id) = frame.query.ty_def_id else { let Some(frame_def_id) = frame.query.def_id_for_ty_in_cycle else {
continue; continue;
}; };
let Some(frame_coroutine_kind) = tcx.coroutine_kind(frame_def_id) else { let Some(frame_coroutine_kind) = tcx.coroutine_kind(frame_def_id) else {

View file

@ -349,9 +349,9 @@ pub(crate) fn create_query_frame<
hasher.finish::<Hash64>() hasher.finish::<Hash64>()
}) })
}; };
let ty_def_id = key.ty_def_id(); let def_id_for_ty_in_cycle = key.def_id_for_ty_in_cycle();
QueryStackFrame::new(description, span, def_id, def_kind, kind, ty_def_id, hash) QueryStackFrame::new(description, span, def_id, def_kind, kind, def_id_for_ty_in_cycle, hash)
} }
pub(crate) fn encode_query_results<'a, 'tcx, Q>( pub(crate) fn encode_query_results<'a, 'tcx, Q>(

View file

@ -33,7 +33,7 @@ pub struct QueryStackFrame {
pub def_id: Option<DefId>, pub def_id: Option<DefId>,
pub def_kind: Option<DefKind>, pub def_kind: Option<DefKind>,
/// A def-id that is extracted from a `Ty` in a query key /// A def-id that is extracted from a `Ty` in a query key
pub ty_def_id: Option<DefId>, pub def_id_for_ty_in_cycle: Option<DefId>,
pub dep_kind: DepKind, pub dep_kind: DepKind,
/// This hash is used to deterministically pick /// This hash is used to deterministically pick
/// a query to remove cycles in the parallel compiler. /// a query to remove cycles in the parallel compiler.
@ -48,10 +48,10 @@ impl QueryStackFrame {
def_id: Option<DefId>, def_id: Option<DefId>,
def_kind: Option<DefKind>, def_kind: Option<DefKind>,
dep_kind: DepKind, dep_kind: DepKind,
ty_def_id: Option<DefId>, def_id_for_ty_in_cycle: Option<DefId>,
hash: impl FnOnce() -> Hash64, hash: impl FnOnce() -> Hash64,
) -> Self { ) -> Self {
Self { description, span, def_id, def_kind, ty_def_id, dep_kind, hash: hash() } Self { description, span, def_id, def_kind, def_id_for_ty_in_cycle, dep_kind, hash: hash() }
} }
// FIXME(eddyb) Get more valid `Span`s on queries. // FIXME(eddyb) Get more valid `Span`s on queries.

View file

@ -3,13 +3,12 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::ty::is_copy; use clippy_utils::ty::is_copy;
use clippy_utils::usage::mutated_variables; use clippy_utils::usage::mutated_variables;
use clippy_utils::visitors::{Descend, for_each_expr_without_closures}; use clippy_utils::visitors::{Descend, for_each_expr_without_closures};
use clippy_utils::{MaybePath, is_res_lang_ctor, is_trait_method, path_res, path_to_local_id}; use clippy_utils::{is_res_lang_ctor, is_trait_method, path_res, path_to_local_id};
use core::ops::ControlFlow; use core::ops::ControlFlow;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::LangItem::{OptionNone, OptionSome}; use rustc_hir::LangItem::{OptionNone, OptionSome};
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_middle::query::Key;
use rustc_middle::ty; use rustc_middle::ty;
use rustc_span::sym; use rustc_span::sym;
@ -44,7 +43,6 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>, a
if name == "filter_map" if name == "filter_map"
&& let hir::ExprKind::Call(expr, args) = body.value.kind && let hir::ExprKind::Call(expr, args) = body.value.kind
&& is_res_lang_ctor(cx, path_res(cx, expr), OptionSome) && is_res_lang_ctor(cx, path_res(cx, expr), OptionSome)
&& arg_id.ty_def_id() == args[0].hir_id().ty_def_id()
&& let hir::ExprKind::Path(_) = args[0].kind && let hir::ExprKind::Path(_) = args[0].kind
{ {
span_lint_and_sugg( span_lint_and_sugg(