Skip to content

Commit c55a97e

Browse files
authored
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).
2 parents 2fc9ce7 + efb66e7 commit c55a97e

File tree

6 files changed

+17
-19
lines changed

6 files changed

+17
-19
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_hir as hir;
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::def_id::DefId;
1111
use rustc_middle::bug;
12-
use rustc_middle::query::Key;
1312
use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
1413
use rustc_middle::ty::{
1514
self, AdtDef, Binder, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeVisitableExt,
@@ -1007,8 +1006,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
10071006
)),
10081007
..
10091008
}) = node
1010-
&& let Some(ty_def_id) = qself_ty.ty_def_id()
1011-
&& let [inherent_impl] = tcx.inherent_impls(ty_def_id)
1009+
&& let Some(adt_def) = qself_ty.ty_adt_def()
1010+
&& let [inherent_impl] = tcx.inherent_impls(adt_def.did())
10121011
&& let name = format!("{ident2}_{ident3}")
10131012
&& let Some(ty::AssocItem { kind: ty::AssocKind::Fn, .. }) = tcx
10141013
.associated_items(inherent_impl)

compiler/rustc_middle/src/query/keys.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ pub trait Key: Sized {
4141
None
4242
}
4343

44-
fn ty_def_id(&self) -> Option<DefId> {
44+
/// Used to detect when ADT def ids are used as keys in a cycle for better error reporting.
45+
fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
4546
None
4647
}
4748
}
@@ -423,7 +424,7 @@ impl<'tcx> Key for Ty<'tcx> {
423424
DUMMY_SP
424425
}
425426

426-
fn ty_def_id(&self) -> Option<DefId> {
427+
fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
427428
match *self.kind() {
428429
ty::Adt(adt, _) => Some(adt.did()),
429430
ty::Coroutine(def_id, ..) => Some(def_id),
@@ -471,8 +472,8 @@ impl<'tcx, T: Key> Key for ty::PseudoCanonicalInput<'tcx, T> {
471472
self.value.default_span(tcx)
472473
}
473474

474-
fn ty_def_id(&self) -> Option<DefId> {
475-
self.value.ty_def_id()
475+
fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
476+
self.value.def_id_for_ty_in_cycle()
476477
}
477478
}
478479

@@ -593,7 +594,7 @@ impl<'tcx> Key for (ValidityRequirement, ty::PseudoCanonicalInput<'tcx, Ty<'tcx>
593594
DUMMY_SP
594595
}
595596

596-
fn ty_def_id(&self) -> Option<DefId> {
597+
fn def_id_for_ty_in_cycle(&self) -> Option<DefId> {
597598
match self.1.value.kind() {
598599
ty::Adt(adt, _) => Some(adt.did()),
599600
_ => None,

compiler/rustc_middle/src/values.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<'tcx> Value<TyCtxt<'tcx>> for Representability {
100100
}
101101
for info in &cycle_error.cycle {
102102
if info.query.dep_kind == dep_kinds::representability_adt_ty
103-
&& let Some(def_id) = info.query.ty_def_id
103+
&& let Some(def_id) = info.query.def_id_for_ty_in_cycle
104104
&& let Some(def_id) = def_id.as_local()
105105
&& !item_and_field_ids.iter().any(|&(id, _)| id == def_id)
106106
{
@@ -182,7 +182,7 @@ impl<'tcx, T> Value<TyCtxt<'tcx>> for Result<T, &'_ ty::layout::LayoutError<'_>>
182182
&cycle_error.cycle,
183183
|cycle| {
184184
if cycle[0].query.dep_kind == dep_kinds::layout_of
185-
&& let Some(def_id) = cycle[0].query.ty_def_id
185+
&& let Some(def_id) = cycle[0].query.def_id_for_ty_in_cycle
186186
&& let Some(def_id) = def_id.as_local()
187187
&& let def_kind = tcx.def_kind(def_id)
188188
&& matches!(def_kind, DefKind::Closure)
@@ -209,7 +209,7 @@ impl<'tcx, T> Value<TyCtxt<'tcx>> for Result<T, &'_ ty::layout::LayoutError<'_>>
209209
if frame.query.dep_kind != dep_kinds::layout_of {
210210
continue;
211211
}
212-
let Some(frame_def_id) = frame.query.ty_def_id else {
212+
let Some(frame_def_id) = frame.query.def_id_for_ty_in_cycle else {
213213
continue;
214214
};
215215
let Some(frame_coroutine_kind) = tcx.coroutine_kind(frame_def_id) else {

compiler/rustc_query_impl/src/plumbing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ pub(crate) fn create_query_frame<
349349
hasher.finish::<Hash64>()
350350
})
351351
};
352-
let ty_def_id = key.ty_def_id();
352+
let def_id_for_ty_in_cycle = key.def_id_for_ty_in_cycle();
353353

354-
QueryStackFrame::new(description, span, def_id, def_kind, kind, ty_def_id, hash)
354+
QueryStackFrame::new(description, span, def_id, def_kind, kind, def_id_for_ty_in_cycle, hash)
355355
}
356356

357357
pub(crate) fn encode_query_results<'a, 'tcx, Q>(

compiler/rustc_query_system/src/query/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub struct QueryStackFrame {
3333
pub def_id: Option<DefId>,
3434
pub def_kind: Option<DefKind>,
3535
/// A def-id that is extracted from a `Ty` in a query key
36-
pub ty_def_id: Option<DefId>,
36+
pub def_id_for_ty_in_cycle: Option<DefId>,
3737
pub dep_kind: DepKind,
3838
/// This hash is used to deterministically pick
3939
/// a query to remove cycles in the parallel compiler.
@@ -48,10 +48,10 @@ impl QueryStackFrame {
4848
def_id: Option<DefId>,
4949
def_kind: Option<DefKind>,
5050
dep_kind: DepKind,
51-
ty_def_id: Option<DefId>,
51+
def_id_for_ty_in_cycle: Option<DefId>,
5252
hash: impl FnOnce() -> Hash64,
5353
) -> Self {
54-
Self { description, span, def_id, def_kind, ty_def_id, dep_kind, hash: hash() }
54+
Self { description, span, def_id, def_kind, def_id_for_ty_in_cycle, dep_kind, hash: hash() }
5555
}
5656

5757
// FIXME(eddyb) Get more valid `Span`s on queries.

src/tools/clippy/clippy_lints/src/methods/unnecessary_filter_map.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::ty::is_copy;
44
use clippy_utils::usage::mutated_variables;
55
use clippy_utils::visitors::{Descend, for_each_expr_without_closures};
6-
use clippy_utils::{MaybePath, is_res_lang_ctor, is_trait_method, path_res, path_to_local_id};
6+
use clippy_utils::{is_res_lang_ctor, is_trait_method, path_res, path_to_local_id};
77
use core::ops::ControlFlow;
88
use rustc_errors::Applicability;
99
use rustc_hir as hir;
1010
use rustc_hir::LangItem::{OptionNone, OptionSome};
1111
use rustc_lint::LateContext;
12-
use rustc_middle::query::Key;
1312
use rustc_middle::ty;
1413
use rustc_span::sym;
1514

@@ -44,7 +43,6 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>, a
4443
if name == "filter_map"
4544
&& let hir::ExprKind::Call(expr, args) = body.value.kind
4645
&& is_res_lang_ctor(cx, path_res(cx, expr), OptionSome)
47-
&& arg_id.ty_def_id() == args[0].hir_id().ty_def_id()
4846
&& let hir::ExprKind::Path(_) = args[0].kind
4947
{
5048
span_lint_and_sugg(

0 commit comments

Comments
 (0)