Skip to content

Commit c8a8e7d

Browse files
authored
Rollup merge of #102890 - camsteffen:adt-sized-representability, r=cjgillot
Check representability in adt_sized_constraint Now that representability is a query, we can use it to preemptively avoid a cycle in `adt_sized_constraint`. I moved the representability check into `check_mod_type_wf` to avoid a scenario where rustc quits before checking all the types for representability. This also removes the check from rustdoc, which is alright AFAIK. r? ``@cjgillot``
2 parents 658169b + d933092 commit c8a8e7d

15 files changed

+50
-91
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ fn check_struct(tcx: TyCtxt<'_>, def_id: LocalDefId) {
380380
let def = tcx.adt_def(def_id);
381381
let span = tcx.def_span(def_id);
382382
def.destructor(tcx); // force the destructor to be evaluated
383-
let _ = tcx.representability(def_id);
384383

385384
if def.repr().simd() {
386385
check_simd(tcx, span, def_id);
@@ -394,7 +393,6 @@ fn check_union(tcx: TyCtxt<'_>, def_id: LocalDefId) {
394393
let def = tcx.adt_def(def_id);
395394
let span = tcx.def_span(def_id);
396395
def.destructor(tcx); // force the destructor to be evaluated
397-
let _ = tcx.representability(def_id);
398396
check_transparent(tcx, span, def);
399397
check_union_fields(tcx, span, def_id);
400398
check_packed(tcx, span, def);
@@ -1489,7 +1487,6 @@ fn check_enum<'tcx>(tcx: TyCtxt<'tcx>, vs: &'tcx [hir::Variant<'tcx>], def_id: L
14891487

14901488
detect_discriminant_duplicate(tcx, def.discriminants(tcx).collect(), vs, sp);
14911489

1492-
let _ = tcx.representability(def_id);
14931490
check_transparent(tcx, sp, def);
14941491
}
14951492

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,8 @@ fn check_type_defn<'tcx, F>(
10411041
) where
10421042
F: FnMut(&WfCheckingCtxt<'_, 'tcx>) -> Vec<AdtVariant<'tcx>>,
10431043
{
1044+
let _ = tcx.representability(item.def_id.def_id);
1045+
10441046
enter_wf_checking_ctxt(tcx, item.span, item.def_id.def_id, |wfcx| {
10451047
let variants = lookup_fields(wfcx);
10461048
let packed = tcx.adt_def(item.def_id).repr().packed();

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -613,16 +613,8 @@ rustc_queries! {
613613
separate_provide_extern
614614
}
615615

616-
// The cycle error here should be reported as an error by `check_representable`.
617-
// We consider the type as Sized in the meanwhile to avoid
618-
// further errors (done in impl Value for AdtSizedConstraint).
619-
// Use `cycle_delay_bug` to delay the cycle error here to be emitted later
620-
// in case we accidentally otherwise don't emit an error.
621-
query adt_sized_constraint(
622-
key: DefId
623-
) -> AdtSizedConstraint<'tcx> {
616+
query adt_sized_constraint(key: DefId) -> &'tcx [Ty<'tcx>] {
624617
desc { |tcx| "computing `Sized` constraints for `{}`", tcx.def_path_str(key) }
625-
cycle_delay_bug
626618
}
627619

628620
query adt_dtorck_constraint(

compiler/rustc_middle/src/ty/adt.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ use super::{
2626
Destructor, FieldDef, GenericPredicates, ReprOptions, Ty, TyCtxt, VariantDef, VariantDiscr,
2727
};
2828

29-
#[derive(Copy, Clone, HashStable, Debug)]
30-
pub struct AdtSizedConstraint<'tcx>(pub &'tcx [Ty<'tcx>]);
31-
3229
bitflags! {
3330
#[derive(HashStable, TyEncodable, TyDecodable)]
3431
pub struct AdtFlags: u32 {
@@ -563,7 +560,7 @@ impl<'tcx> AdtDef<'tcx> {
563560
/// Due to normalization being eager, this applies even if
564561
/// the associated type is behind a pointer (e.g., issue #31299).
565562
pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> ty::EarlyBinder<&'tcx [Ty<'tcx>]> {
566-
ty::EarlyBinder(tcx.adt_sized_constraint(self.did()).0)
563+
ty::EarlyBinder(tcx.adt_sized_constraint(self.did()))
567564
}
568565
}
569566

compiler/rustc_middle/src/ty/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::ty::layout::TyAndLayout;
3232
use crate::ty::subst::{GenericArg, SubstsRef};
3333
use crate::ty::util::AlwaysRequiresDrop;
3434
use crate::ty::GeneratorDiagnosticData;
35-
use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
35+
use crate::ty::{self, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
3636
use rustc_ast as ast;
3737
use rustc_ast::expand::allocator::AllocatorKind;
3838
use rustc_attr as attr;

compiler/rustc_middle/src/values.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan};
33
use rustc_hir as hir;
44
use rustc_hir::def::DefKind;
55
use rustc_middle::ty::Representability;
6-
use rustc_middle::ty::{self, AdtSizedConstraint, DefIdTree, Ty, TyCtxt};
6+
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt};
77
use rustc_query_system::query::QueryInfo;
88
use rustc_query_system::Value;
99
use rustc_span::def_id::LocalDefId;
@@ -31,18 +31,6 @@ impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> {
3131
}
3232
}
3333

34-
impl<'tcx> Value<TyCtxt<'tcx>> for AdtSizedConstraint<'_> {
35-
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self {
36-
// SAFETY: This is never called when `Self` is not `AdtSizedConstraint<'tcx>`.
37-
// FIXME: Represent the above fact in the trait system somehow.
38-
unsafe {
39-
std::mem::transmute::<AdtSizedConstraint<'tcx>, AdtSizedConstraint<'_>>(
40-
AdtSizedConstraint(tcx.intern_type_list(&[tcx.ty_error()])),
41-
)
42-
}
43-
}
44-
}
45-
4634
impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
4735
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self {
4836
let err = tcx.ty_error();

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,13 @@ fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
8585
/// - a type parameter or projection whose Sizedness can't be known
8686
/// - a tuple of type parameters or projections, if there are multiple
8787
/// such.
88-
/// - an Error, if a type contained itself. The representability
89-
/// check should catch this case.
90-
fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstraint<'_> {
88+
/// - an Error, if a type is infinitely sized
89+
fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
90+
if let Some(def_id) = def_id.as_local() {
91+
if matches!(tcx.representability(def_id), ty::Representability::Infinite) {
92+
return tcx.intern_type_list(&[tcx.ty_error()]);
93+
}
94+
}
9195
let def = tcx.adt_def(def_id);
9296

9397
let result = tcx.mk_type_list(
@@ -99,7 +103,7 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstrain
99103

100104
debug!("adt_sized_constraint: {:?} => {:?}", def, result);
101105

102-
ty::AdtSizedConstraint(result)
106+
result
103107
}
104108

105109
/// See `ParamEnv` struct definition for details.

src/test/rustdoc-ui/infinite-recursive-type-impl-trait-return.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
// check-pass
12
// normalize-stderr-test: "`.*`" -> "`DEF_ID`"
23
// normalize-stdout-test: "`.*`" -> "`DEF_ID`"
34
// edition:2018
45

56
pub async fn f() -> impl std::fmt::Debug {
7+
// rustdoc doesn't care that this is infinitely sized
68
#[derive(Debug)]
79
enum E {
8-
//~^ ERROR recursive type `f::{closure#0}::E` has infinite size
910
This(E),
1011
Unit,
1112
}

src/test/rustdoc-ui/infinite-recursive-type-impl-trait-return.stderr

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/test/rustdoc-ui/infinite-recursive-type-impl-trait.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
// check-pass
2+
13
fn f() -> impl Sized {
4+
// rustdoc doesn't care that this is infinitely sized
25
enum E {
3-
//~^ ERROR recursive type `f::E` has infinite size
46
V(E),
57
}
68
unimplemented!()

0 commit comments

Comments
 (0)