Skip to content

Commit 7013437

Browse files
committed
Make variance wfcheck only use the HIR in the error path
1 parent 5329bee commit 7013437

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(()),
@@ -1977,27 +1975,23 @@ fn legacy_receiver_is_implemented<'tcx>(
19771975
}
19781976
}
19791977

1980-
fn check_variances_for_type_defn<'tcx>(
1981-
tcx: TyCtxt<'tcx>,
1982-
item: &'tcx hir::Item<'tcx>,
1983-
hir_generics: &hir::Generics<'tcx>,
1984-
) {
1985-
match item.kind {
1986-
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
1978+
fn check_variances_for_type_defn<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
1979+
match tcx.def_kind(def_id) {
1980+
DefKind::Enum | DefKind::Struct | DefKind::Union => {
19871981
// Ok
19881982
}
1989-
ItemKind::TyAlias(..) => {
1983+
DefKind::TyAlias => {
19901984
assert!(
1991-
tcx.type_alias_is_lazy(item.owner_id),
1985+
tcx.type_alias_is_lazy(def_id),
19921986
"should not be computing variance of non-free type alias"
19931987
);
19941988
}
1995-
kind => span_bug!(item.span, "cannot compute the variances of {kind:?}"),
1989+
kind => span_bug!(tcx.def_span(def_id), "cannot compute the variances of {kind:?}"),
19961990
}
19971991

1998-
let ty_predicates = tcx.predicates_of(item.owner_id);
1992+
let ty_predicates = tcx.predicates_of(def_id);
19991993
assert_eq!(ty_predicates.parent, None);
2000-
let variances = tcx.variances_of(item.owner_id);
1994+
let variances = tcx.variances_of(def_id);
20011995

20021996
let mut constrained_parameters: FxHashSet<_> = variances
20031997
.iter()
@@ -2010,8 +2004,10 @@ fn check_variances_for_type_defn<'tcx>(
20102004

20112005
// Lazily calculated because it is only needed in case of an error.
20122006
let explicitly_bounded_params = LazyCell::new(|| {
2013-
let icx = crate::collect::ItemCtxt::new(tcx, item.owner_id.def_id);
2014-
hir_generics
2007+
let icx = crate::collect::ItemCtxt::new(tcx, def_id);
2008+
tcx.hir_node_by_def_id(def_id)
2009+
.generics()
2010+
.unwrap()
20152011
.predicates
20162012
.iter()
20172013
.filter_map(|predicate| match predicate.kind {
@@ -2026,18 +2022,20 @@ fn check_variances_for_type_defn<'tcx>(
20262022
.collect::<FxHashSet<_>>()
20272023
});
20282024

2029-
let ty_generics = tcx.generics_of(item.owner_id);
2030-
20312025
for (index, _) in variances.iter().enumerate() {
20322026
let parameter = Parameter(index as u32);
20332027

20342028
if constrained_parameters.contains(&parameter) {
20352029
continue;
20362030
}
20372031

2038-
let ty_param = &ty_generics.own_params[index];
2032+
let node = tcx.hir_node_by_def_id(def_id);
2033+
let item = node.expect_item();
2034+
let hir_generics = node.generics().unwrap();
20392035
let hir_param = &hir_generics.params[index];
20402036

2037+
let ty_param = &tcx.generics_of(item.owner_id).own_params[index];
2038+
20412039
if ty_param.def_id != hir_param.def_id.into() {
20422040
// Valid programs always have lifetimes before types in the generic parameter list.
20432041
// ty_generics are normalized to be in this required order, and variances are built
@@ -2055,7 +2053,7 @@ fn check_variances_for_type_defn<'tcx>(
20552053

20562054
// Look for `ErrorGuaranteed` deeply within this type.
20572055
if let ControlFlow::Break(ErrorGuaranteed { .. }) = tcx
2058-
.type_of(item.owner_id)
2056+
.type_of(def_id)
20592057
.instantiate_identity()
20602058
.visit_with(&mut HasErrorDeep { tcx, seen: Default::default() })
20612059
{

0 commit comments

Comments
 (0)