Skip to content

Commit 4a0c2d0

Browse files
committed
Do not access HIR to check impl wf.
1 parent 10f4ce3 commit 4a0c2d0

File tree

2 files changed

+33
-55
lines changed

2 files changed

+33
-55
lines changed

compiler/rustc_typeck/src/impl_wf_check.rs

+19-28
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use min_specialization::check_min_specialization;
1313

1414
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1515
use rustc_errors::struct_span_err;
16-
use rustc_hir as hir;
1716
use rustc_hir::def::DefKind;
1817
use rustc_hir::def_id::LocalDefId;
1918
use rustc_middle::ty::query::Providers;
@@ -66,13 +65,10 @@ fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
6665
let module = tcx.hir_module_items(module_def_id);
6766
for id in module.items() {
6867
if matches!(tcx.def_kind(id.def_id), DefKind::Impl) {
69-
let item = tcx.hir().item(id);
70-
if let hir::ItemKind::Impl(ref impl_) = item.kind {
71-
enforce_impl_params_are_constrained(tcx, item.def_id, impl_.items);
72-
enforce_impl_items_are_distinct(tcx, impl_.items);
73-
if min_specialization {
74-
check_min_specialization(tcx, item.def_id.to_def_id(), item.span);
75-
}
68+
enforce_impl_params_are_constrained(tcx, id.def_id);
69+
enforce_impl_items_are_distinct(tcx, id.def_id);
70+
if min_specialization {
71+
check_min_specialization(tcx, id.def_id);
7672
}
7773
}
7874
}
@@ -82,11 +78,7 @@ pub fn provide(providers: &mut Providers) {
8278
*providers = Providers { check_mod_impl_wf, ..*providers };
8379
}
8480

85-
fn enforce_impl_params_are_constrained(
86-
tcx: TyCtxt<'_>,
87-
impl_def_id: LocalDefId,
88-
impl_item_refs: &[hir::ImplItemRef],
89-
) {
81+
fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId) {
9082
// Every lifetime used in an associated type must be constrained.
9183
let impl_self_ty = tcx.type_of(impl_def_id);
9284
if impl_self_ty.references_error() {
@@ -114,9 +106,9 @@ fn enforce_impl_params_are_constrained(
114106
);
115107

116108
// Disallow unconstrained lifetimes, but only if they appear in assoc types.
117-
let lifetimes_in_associated_types: FxHashSet<_> = impl_item_refs
109+
let lifetimes_in_associated_types: FxHashSet<_> = tcx
110+
.associated_item_def_ids(impl_def_id)
118111
.iter()
119-
.map(|item_ref| item_ref.id.def_id)
120112
.flat_map(|def_id| {
121113
let item = tcx.associated_item(def_id);
122114
match item.kind {
@@ -216,33 +208,32 @@ fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: &str)
216208
}
217209

218210
/// Enforce that we do not have two items in an impl with the same name.
219-
fn enforce_impl_items_are_distinct(tcx: TyCtxt<'_>, impl_item_refs: &[hir::ImplItemRef]) {
211+
fn enforce_impl_items_are_distinct(tcx: TyCtxt<'_>, impl_def_id: LocalDefId) {
220212
let mut seen_type_items = FxHashMap::default();
221213
let mut seen_value_items = FxHashMap::default();
222-
for impl_item_ref in impl_item_refs {
223-
let impl_item = tcx.hir().impl_item(impl_item_ref.id);
214+
for &impl_item_ref in tcx.associated_item_def_ids(impl_def_id) {
215+
let impl_item = tcx.associated_item(impl_item_ref);
224216
let seen_items = match impl_item.kind {
225-
hir::ImplItemKind::TyAlias(_) => &mut seen_type_items,
217+
ty::AssocKind::Type => &mut seen_type_items,
226218
_ => &mut seen_value_items,
227219
};
228-
match seen_items.entry(impl_item.ident.normalize_to_macros_2_0()) {
220+
let span = tcx.def_span(impl_item_ref);
221+
let ident = impl_item.ident(tcx);
222+
match seen_items.entry(ident.normalize_to_macros_2_0()) {
229223
Occupied(entry) => {
230224
let mut err = struct_span_err!(
231225
tcx.sess,
232-
impl_item.span,
226+
span,
233227
E0201,
234228
"duplicate definitions with name `{}`:",
235-
impl_item.ident
236-
);
237-
err.span_label(
238-
*entry.get(),
239-
format!("previous definition of `{}` here", impl_item.ident),
229+
ident
240230
);
241-
err.span_label(impl_item.span, "duplicate definition");
231+
err.span_label(*entry.get(), format!("previous definition of `{}` here", ident));
232+
err.span_label(span, "duplicate definition");
242233
err.emit();
243234
}
244235
Vacant(entry) => {
245-
entry.insert(impl_item.span);
236+
entry.insert(span);
246237
}
247238
}
248239
}

compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs

+14-27
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ use rustc_middle::ty::{self, TyCtxt, TypeFoldable};
7979
use rustc_span::Span;
8080
use rustc_trait_selection::traits::{self, translate_substs, wf};
8181

82-
pub(super) fn check_min_specialization(tcx: TyCtxt<'_>, impl_def_id: DefId, span: Span) {
82+
pub(super) fn check_min_specialization(tcx: TyCtxt<'_>, impl_def_id: LocalDefId) {
8383
if let Some(node) = parent_specialization_node(tcx, impl_def_id) {
8484
tcx.infer_ctxt().enter(|infcx| {
85-
check_always_applicable(&infcx, impl_def_id, node, span);
85+
check_always_applicable(&infcx, impl_def_id, node);
8686
});
8787
}
8888
}
8989

90-
fn parent_specialization_node(tcx: TyCtxt<'_>, impl1_def_id: DefId) -> Option<Node> {
90+
fn parent_specialization_node(tcx: TyCtxt<'_>, impl1_def_id: LocalDefId) -> Option<Node> {
9191
let trait_ref = tcx.impl_trait_ref(impl1_def_id)?;
9292
let trait_def = tcx.trait_def(trait_ref.def_id);
9393

94-
let impl2_node = trait_def.ancestors(tcx, impl1_def_id).ok()?.nth(1)?;
94+
let impl2_node = trait_def.ancestors(tcx, impl1_def_id.to_def_id()).ok()?.nth(1)?;
9595

9696
let always_applicable_trait =
9797
matches!(trait_def.specialization_kind, TraitSpecializationKind::AlwaysApplicable);
@@ -103,15 +103,8 @@ fn parent_specialization_node(tcx: TyCtxt<'_>, impl1_def_id: DefId) -> Option<No
103103
}
104104

105105
/// Check that `impl1` is a sound specialization
106-
fn check_always_applicable(
107-
infcx: &InferCtxt<'_, '_>,
108-
impl1_def_id: DefId,
109-
impl2_node: Node,
110-
span: Span,
111-
) {
112-
if let Some((impl1_substs, impl2_substs)) =
113-
get_impl_substs(infcx, impl1_def_id, impl2_node, span)
114-
{
106+
fn check_always_applicable(infcx: &InferCtxt<'_, '_>, impl1_def_id: LocalDefId, impl2_node: Node) {
107+
if let Some((impl1_substs, impl2_substs)) = get_impl_substs(infcx, impl1_def_id, impl2_node) {
115108
let impl2_def_id = impl2_node.def_id();
116109
debug!(
117110
"check_always_applicable(\nimpl1_def_id={:?},\nimpl2_def_id={:?},\nimpl2_substs={:?}\n)",
@@ -126,17 +119,10 @@ fn check_always_applicable(
126119
unconstrained_parent_impl_substs(tcx, impl2_def_id, impl2_substs)
127120
};
128121

122+
let span = tcx.def_span(impl1_def_id);
129123
check_static_lifetimes(tcx, &parent_substs, span);
130124
check_duplicate_params(tcx, impl1_substs, &parent_substs, span);
131-
132-
check_predicates(
133-
infcx,
134-
impl1_def_id.expect_local(),
135-
impl1_substs,
136-
impl2_node,
137-
impl2_substs,
138-
span,
139-
);
125+
check_predicates(infcx, impl1_def_id, impl1_substs, impl2_node, impl2_substs, span);
140126
}
141127
}
142128

@@ -152,20 +138,21 @@ fn check_always_applicable(
152138
/// Would return `S1 = [C]` and `S2 = [Vec<C>, C]`.
153139
fn get_impl_substs<'tcx>(
154140
infcx: &InferCtxt<'_, 'tcx>,
155-
impl1_def_id: DefId,
141+
impl1_def_id: LocalDefId,
156142
impl2_node: Node,
157-
span: Span,
158143
) -> Option<(SubstsRef<'tcx>, SubstsRef<'tcx>)> {
159144
let tcx = infcx.tcx;
160145
let param_env = tcx.param_env(impl1_def_id);
161146

162-
let impl1_substs = InternalSubsts::identity_for_item(tcx, impl1_def_id);
163-
let impl2_substs = translate_substs(infcx, param_env, impl1_def_id, impl1_substs, impl2_node);
147+
let impl1_substs = InternalSubsts::identity_for_item(tcx, impl1_def_id.to_def_id());
148+
let impl2_substs =
149+
translate_substs(infcx, param_env, impl1_def_id.to_def_id(), impl1_substs, impl2_node);
164150

165151
// Conservatively use an empty `ParamEnv`.
166152
let outlives_env = OutlivesEnvironment::new(ty::ParamEnv::empty());
167-
infcx.resolve_regions_and_report_errors(impl1_def_id, &outlives_env);
153+
infcx.resolve_regions_and_report_errors(impl1_def_id.to_def_id(), &outlives_env);
168154
let Ok(impl2_substs) = infcx.fully_resolve(impl2_substs) else {
155+
let span = tcx.def_span(impl1_def_id);
169156
tcx.sess.emit_err(SubstsOnOverriddenImpl { span });
170157
return None;
171158
};

0 commit comments

Comments
 (0)