Skip to content

Commit d933092

Browse files
committed
Check representability in adt_sized_constraint
1 parent 0265a3e commit d933092

15 files changed

+50
-91
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

-3
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);
@@ -1487,7 +1485,6 @@ fn check_enum<'tcx>(tcx: TyCtxt<'tcx>, vs: &'tcx [hir::Variant<'tcx>], def_id: L
14871485

14881486
detect_discriminant_duplicate(tcx, def.discriminants(tcx).collect(), vs, sp);
14891487

1490-
let _ = tcx.representability(def_id);
14911488
check_transparent(tcx, sp, def);
14921489
}
14931490

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+2
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

+1-9
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

+1-4
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

+1-1
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

+1-13
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

+8-4
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

+2-1
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

-17
This file was deleted.

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

+3-1
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!()

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

-17
This file was deleted.

src/test/ui/issues/issue-72554.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::collections::BTreeSet;
33
#[derive(Hash)]
44
pub enum ElemDerived {
55
//~^ ERROR recursive type `ElemDerived` has infinite size
6-
//~| ERROR cycle detected when computing drop-check constraints for `ElemDerived`
76
A(ElemDerived)
87
}
98

src/test/ui/issues/issue-72554.stderr

+3-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ error[E0072]: recursive type `ElemDerived` has infinite size
33
|
44
LL | pub enum ElemDerived {
55
| ^^^^^^^^^^^^^^^^^^^^
6-
...
6+
LL |
77
LL | A(ElemDerived)
88
| ----------- recursive without indirection
99
|
@@ -12,20 +12,6 @@ help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
1212
LL | A(Box<ElemDerived>)
1313
| ++++ +
1414

15-
error[E0391]: cycle detected when computing drop-check constraints for `ElemDerived`
16-
--> $DIR/issue-72554.rs:4:1
17-
|
18-
LL | pub enum ElemDerived {
19-
| ^^^^^^^^^^^^^^^^^^^^
20-
|
21-
= note: ...which immediately requires computing drop-check constraints for `ElemDerived` again
22-
note: cycle used when computing drop-check constraints for `Elem`
23-
--> $DIR/issue-72554.rs:11:1
24-
|
25-
LL | pub enum Elem {
26-
| ^^^^^^^^^^^^^
27-
28-
error: aborting due to 2 previous errors
15+
error: aborting due to previous error
2916

30-
Some errors have detailed explanations: E0072, E0391.
31-
For more information about an error, try `rustc --explain E0072`.
17+
For more information about this error, try `rustc --explain E0072`.

src/test/ui/variance/variance-regions-unused-indirect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Test that disallow lifetime parameters that are unused.
22

33
enum Foo<'a> { //~ ERROR parameter `'a` is never used
4+
//~^ ERROR recursive types `Foo` and `Bar` have infinite size
45
Foo1(Bar<'a>)
56
}
67

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
error[E0072]: recursive types `Foo` and `Bar` have infinite size
2+
--> $DIR/variance-regions-unused-indirect.rs:3:1
3+
|
4+
LL | enum Foo<'a> {
5+
| ^^^^^^^^^^^^
6+
LL |
7+
LL | Foo1(Bar<'a>)
8+
| ------- recursive without indirection
9+
...
10+
LL | enum Bar<'a> {
11+
| ^^^^^^^^^^^^
12+
LL | Bar1(Foo<'a>)
13+
| ------- recursive without indirection
14+
|
15+
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
16+
|
17+
LL ~ Foo1(Box<Bar<'a>>)
18+
LL | }
19+
LL |
20+
LL | enum Bar<'a> {
21+
LL ~ Bar1(Box<Foo<'a>>)
22+
|
23+
124
error[E0392]: parameter `'a` is never used
225
--> $DIR/variance-regions-unused-indirect.rs:3:10
326
|
@@ -7,13 +30,14 @@ LL | enum Foo<'a> {
730
= help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData`
831

932
error[E0392]: parameter `'a` is never used
10-
--> $DIR/variance-regions-unused-indirect.rs:7:10
33+
--> $DIR/variance-regions-unused-indirect.rs:8:10
1134
|
1235
LL | enum Bar<'a> {
1336
| ^^ unused parameter
1437
|
1538
= help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData`
1639

17-
error: aborting due to 2 previous errors
40+
error: aborting due to 3 previous errors
1841

19-
For more information about this error, try `rustc --explain E0392`.
42+
Some errors have detailed explanations: E0072, E0392.
43+
For more information about an error, try `rustc --explain E0072`.

0 commit comments

Comments
 (0)