Skip to content

Commit c7f5c9f

Browse files
committed
Make variance wfcheck only use the HIR in the error path
1 parent 3396d1d commit c7f5c9f

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -292,26 +292,24 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
292292
}
293293
hir::ItemKind::Fn { ident, sig, .. } => check_item_fn(tcx, def_id, ident, sig.decl),
294294
hir::ItemKind::Const(_, _, ty, _) => check_const_item(tcx, def_id, ty.span, item.span),
295-
hir::ItemKind::Struct(_, generics, _) => {
295+
hir::ItemKind::Struct(..) => {
296296
let res = check_type_defn(tcx, item, false);
297-
check_variances_for_type_defn(tcx, item, generics);
297+
check_variances_for_type_defn(tcx, def_id);
298298
res
299299
}
300-
hir::ItemKind::Union(_, generics, _) => {
300+
hir::ItemKind::Union(..) => {
301301
let res = check_type_defn(tcx, item, true);
302-
check_variances_for_type_defn(tcx, item, generics);
302+
check_variances_for_type_defn(tcx, def_id);
303303
res
304304
}
305-
hir::ItemKind::Enum(_, generics, _) => {
305+
hir::ItemKind::Enum(..) => {
306306
let res = check_type_defn(tcx, item, true);
307-
check_variances_for_type_defn(tcx, item, generics);
307+
check_variances_for_type_defn(tcx, def_id);
308308
res
309309
}
310310
hir::ItemKind::Trait(..) => check_trait(tcx, item),
311311
hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
312-
// `ForeignItem`s are handled separately.
313-
hir::ItemKind::ForeignMod { .. } => Ok(()),
314-
hir::ItemKind::TyAlias(_, generics, hir_ty) if tcx.type_alias_is_lazy(item.owner_id) => {
312+
hir::ItemKind::TyAlias(.., hir_ty) if tcx.type_alias_is_lazy(item.owner_id) => {
315313
let res = enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
316314
let ty = tcx.type_of(def_id).instantiate_identity();
317315
let item_ty =
@@ -324,7 +322,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
324322
check_where_clauses(wfcx, item.span, def_id);
325323
Ok(())
326324
});
327-
check_variances_for_type_defn(tcx, item, generics);
325+
check_variances_for_type_defn(tcx, def_id);
328326
res
329327
}
330328
_ => Ok(()),
@@ -1971,27 +1969,23 @@ fn legacy_receiver_is_implemented<'tcx>(
19711969
}
19721970
}
19731971

1974-
fn check_variances_for_type_defn<'tcx>(
1975-
tcx: TyCtxt<'tcx>,
1976-
item: &'tcx hir::Item<'tcx>,
1977-
hir_generics: &hir::Generics<'tcx>,
1978-
) {
1979-
match item.kind {
1980-
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
1972+
fn check_variances_for_type_defn<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
1973+
match tcx.def_kind(def_id) {
1974+
DefKind::Enum | DefKind::Struct | DefKind::Union => {
19811975
// Ok
19821976
}
1983-
ItemKind::TyAlias(..) => {
1977+
DefKind::TyAlias => {
19841978
assert!(
1985-
tcx.type_alias_is_lazy(item.owner_id),
1979+
tcx.type_alias_is_lazy(def_id),
19861980
"should not be computing variance of non-free type alias"
19871981
);
19881982
}
1989-
kind => span_bug!(item.span, "cannot compute the variances of {kind:?}"),
1983+
kind => span_bug!(tcx.def_span(def_id), "cannot compute the variances of {kind:?}"),
19901984
}
19911985

1992-
let ty_predicates = tcx.predicates_of(item.owner_id);
1986+
let ty_predicates = tcx.predicates_of(def_id);
19931987
assert_eq!(ty_predicates.parent, None);
1994-
let variances = tcx.variances_of(item.owner_id);
1988+
let variances = tcx.variances_of(def_id);
19951989

19961990
let mut constrained_parameters: FxHashSet<_> = variances
19971991
.iter()
@@ -2004,8 +1998,10 @@ fn check_variances_for_type_defn<'tcx>(
20041998

20051999
// Lazily calculated because it is only needed in case of an error.
20062000
let explicitly_bounded_params = LazyCell::new(|| {
2007-
let icx = crate::collect::ItemCtxt::new(tcx, item.owner_id.def_id);
2008-
hir_generics
2001+
let icx = crate::collect::ItemCtxt::new(tcx, def_id);
2002+
tcx.hir_node_by_def_id(def_id)
2003+
.generics()
2004+
.unwrap()
20092005
.predicates
20102006
.iter()
20112007
.filter_map(|predicate| match predicate.kind {
@@ -2020,18 +2016,20 @@ fn check_variances_for_type_defn<'tcx>(
20202016
.collect::<FxHashSet<_>>()
20212017
});
20222018

2023-
let ty_generics = tcx.generics_of(item.owner_id);
2024-
20252019
for (index, _) in variances.iter().enumerate() {
20262020
let parameter = Parameter(index as u32);
20272021

20282022
if constrained_parameters.contains(&parameter) {
20292023
continue;
20302024
}
20312025

2032-
let ty_param = &ty_generics.own_params[index];
2026+
let node = tcx.hir_node_by_def_id(def_id);
2027+
let item = node.expect_item();
2028+
let hir_generics = node.generics().unwrap();
20332029
let hir_param = &hir_generics.params[index];
20342030

2031+
let ty_param = &tcx.generics_of(item.owner_id).own_params[index];
2032+
20352033
if ty_param.def_id != hir_param.def_id.into() {
20362034
// Valid programs always have lifetimes before types in the generic parameter list.
20372035
// ty_generics are normalized to be in this required order, and variances are built
@@ -2049,7 +2047,7 @@ fn check_variances_for_type_defn<'tcx>(
20492047

20502048
// Look for `ErrorGuaranteed` deeply within this type.
20512049
if let ControlFlow::Break(ErrorGuaranteed { .. }) = tcx
2052-
.type_of(item.owner_id)
2050+
.type_of(def_id)
20532051
.instantiate_identity()
20542052
.visit_with(&mut HasErrorDeep { tcx, seen: Default::default() })
20552053
{

0 commit comments

Comments
 (0)