Skip to content

Commit 9af95bb

Browse files
committed
Make check_variances_for_type_defn fallible
1 parent b2e129e commit 9af95bb

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+16-27
Original file line numberDiff line numberDiff line change
@@ -308,21 +308,12 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
308308
hir::ItemKind::Const(ty, ..) => {
309309
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
310310
}
311-
hir::ItemKind::Struct(_, hir_generics) => {
312-
let res = check_type_defn(tcx, item, false);
313-
check_variances_for_type_defn(tcx, item, hir_generics);
314-
res
315-
}
316-
hir::ItemKind::Union(_, hir_generics) => {
317-
let res = check_type_defn(tcx, item, true);
318-
check_variances_for_type_defn(tcx, item, hir_generics);
319-
res
320-
}
321-
hir::ItemKind::Enum(_, hir_generics) => {
322-
let res = check_type_defn(tcx, item, true);
323-
check_variances_for_type_defn(tcx, item, hir_generics);
324-
res
325-
}
311+
hir::ItemKind::Struct(_, hir_generics) => check_type_defn(tcx, item, false)
312+
.and(check_variances_for_type_defn(tcx, item, hir_generics)),
313+
hir::ItemKind::Union(_, hir_generics) => check_type_defn(tcx, item, true)
314+
.and(check_variances_for_type_defn(tcx, item, hir_generics)),
315+
hir::ItemKind::Enum(_, hir_generics) => check_type_defn(tcx, item, true)
316+
.and(check_variances_for_type_defn(tcx, item, hir_generics)),
326317
hir::ItemKind::Trait(..) => check_trait(tcx, item),
327318
hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
328319
// `ForeignItem`s are handled separately.
@@ -331,9 +322,8 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
331322
if tcx.type_alias_is_lazy(item.owner_id) {
332323
// Bounds of lazy type aliases and of eager ones that contain opaque types are respected.
333324
// E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`.
334-
let res = check_item_type(tcx, def_id, hir_ty.span, UnsizedHandling::Allow);
335-
check_variances_for_type_defn(tcx, item, hir_generics);
336-
res
325+
check_item_type(tcx, def_id, hir_ty.span, UnsizedHandling::Allow)
326+
.and(check_variances_for_type_defn(tcx, item, hir_generics))
337327
} else {
338328
Ok(())
339329
}
@@ -1813,25 +1803,21 @@ fn check_variances_for_type_defn<'tcx>(
18131803
tcx: TyCtxt<'tcx>,
18141804
item: &hir::Item<'tcx>,
18151805
hir_generics: &hir::Generics<'tcx>,
1816-
) {
1806+
) -> Result<(), ErrorGuaranteed> {
18171807
let identity_args = ty::GenericArgs::identity_for_item(tcx, item.owner_id);
18181808

18191809
match item.kind {
18201810
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
18211811
for field in tcx.adt_def(item.owner_id).all_fields() {
1822-
if field.ty(tcx, identity_args).references_error() {
1823-
return;
1824-
}
1812+
field.ty(tcx, identity_args).error_reported()?;
18251813
}
18261814
}
18271815
ItemKind::TyAlias(..) => {
18281816
assert!(
18291817
tcx.type_alias_is_lazy(item.owner_id),
18301818
"should not be computing variance of non-weak type alias"
18311819
);
1832-
if tcx.type_of(item.owner_id).skip_binder().references_error() {
1833-
return;
1834-
}
1820+
tcx.type_of(item.owner_id).skip_binder().error_reported()?;
18351821
}
18361822
kind => span_bug!(item.span, "cannot compute the variances of {kind:?}"),
18371823
}
@@ -1869,6 +1855,8 @@ fn check_variances_for_type_defn<'tcx>(
18691855

18701856
let ty_generics = tcx.generics_of(item.owner_id);
18711857

1858+
let mut res = Ok(());
1859+
18721860
for (index, _) in variances.iter().enumerate() {
18731861
let parameter = Parameter(index as u32);
18741862

@@ -1895,13 +1883,14 @@ fn check_variances_for_type_defn<'tcx>(
18951883
}
18961884

18971885
match hir_param.name {
1898-
hir::ParamName::Error(_guar) => {}
1886+
hir::ParamName::Error(guar) => res = Err(guar),
18991887
_ => {
19001888
let has_explicit_bounds = explicitly_bounded_params.contains(&parameter);
1901-
report_bivariance(tcx, hir_param, has_explicit_bounds, item.kind);
1889+
res = Err(report_bivariance(tcx, hir_param, has_explicit_bounds, item.kind));
19021890
}
19031891
}
19041892
}
1893+
res
19051894
}
19061895

19071896
fn report_bivariance(

0 commit comments

Comments
 (0)