Skip to content

Check representability in adt_sized_constraint #102890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
@@ -380,7 +380,6 @@ fn check_struct(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let def = tcx.adt_def(def_id);
let span = tcx.def_span(def_id);
def.destructor(tcx); // force the destructor to be evaluated
let _ = tcx.representability(def_id);

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

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

let _ = tcx.representability(def_id);
check_transparent(tcx, sp, def);
}

2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
@@ -1041,6 +1041,8 @@ fn check_type_defn<'tcx, F>(
) where
F: FnMut(&WfCheckingCtxt<'_, 'tcx>) -> Vec<AdtVariant<'tcx>>,
{
let _ = tcx.representability(item.def_id.def_id);

enter_wf_checking_ctxt(tcx, item.span, item.def_id.def_id, |wfcx| {
let variants = lookup_fields(wfcx);
let packed = tcx.adt_def(item.def_id).repr().packed();
10 changes: 1 addition & 9 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
@@ -613,16 +613,8 @@ rustc_queries! {
separate_provide_extern
}

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

query adt_dtorck_constraint(
5 changes: 1 addition & 4 deletions compiler/rustc_middle/src/ty/adt.rs
Original file line number Diff line number Diff line change
@@ -26,9 +26,6 @@ use super::{
Destructor, FieldDef, GenericPredicates, ReprOptions, Ty, TyCtxt, VariantDef, VariantDiscr,
};

#[derive(Copy, Clone, HashStable, Debug)]
pub struct AdtSizedConstraint<'tcx>(pub &'tcx [Ty<'tcx>]);

bitflags! {
#[derive(HashStable, TyEncodable, TyDecodable)]
pub struct AdtFlags: u32 {
@@ -563,7 +560,7 @@ impl<'tcx> AdtDef<'tcx> {
/// Due to normalization being eager, this applies even if
/// the associated type is behind a pointer (e.g., issue #31299).
pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> ty::EarlyBinder<&'tcx [Ty<'tcx>]> {
ty::EarlyBinder(tcx.adt_sized_constraint(self.did()).0)
ty::EarlyBinder(tcx.adt_sized_constraint(self.did()))
}
}

2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/query.rs
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ use crate::ty::layout::TyAndLayout;
use crate::ty::subst::{GenericArg, SubstsRef};
use crate::ty::util::AlwaysRequiresDrop;
use crate::ty::GeneratorDiagnosticData;
use crate::ty::{self, AdtSizedConstraint, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
use crate::ty::{self, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt};
use rustc_ast as ast;
use rustc_ast::expand::allocator::AllocatorKind;
use rustc_attr as attr;
14 changes: 1 addition & 13 deletions compiler/rustc_middle/src/values.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_middle::ty::Representability;
use rustc_middle::ty::{self, AdtSizedConstraint, DefIdTree, Ty, TyCtxt};
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt};
use rustc_query_system::query::QueryInfo;
use rustc_query_system::Value;
use rustc_span::def_id::LocalDefId;
@@ -31,18 +31,6 @@ impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> {
}
}

impl<'tcx> Value<TyCtxt<'tcx>> for AdtSizedConstraint<'_> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self {
// SAFETY: This is never called when `Self` is not `AdtSizedConstraint<'tcx>`.
// FIXME: Represent the above fact in the trait system somehow.
unsafe {
std::mem::transmute::<AdtSizedConstraint<'tcx>, AdtSizedConstraint<'_>>(
AdtSizedConstraint(tcx.intern_type_list(&[tcx.ty_error()])),
)
}
}
}

impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self {
let err = tcx.ty_error();
12 changes: 8 additions & 4 deletions compiler/rustc_ty_utils/src/ty.rs
Original file line number Diff line number Diff line change
@@ -85,9 +85,13 @@ fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
/// - a type parameter or projection whose Sizedness can't be known
/// - a tuple of type parameters or projections, if there are multiple
/// such.
/// - an Error, if a type contained itself. The representability
/// check should catch this case.
fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstraint<'_> {
/// - an Error, if a type is infinitely sized
fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
if let Some(def_id) = def_id.as_local() {
if matches!(tcx.representability(def_id), ty::Representability::Infinite) {
return tcx.intern_type_list(&[tcx.ty_error()]);
}
}
let def = tcx.adt_def(def_id);

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

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

ty::AdtSizedConstraint(result)
result
}

/// See `ParamEnv` struct definition for details.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// check-pass
// normalize-stderr-test: "`.*`" -> "`DEF_ID`"
// normalize-stdout-test: "`.*`" -> "`DEF_ID`"
// edition:2018

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

This file was deleted.

4 changes: 3 additions & 1 deletion src/test/rustdoc-ui/infinite-recursive-type-impl-trait.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// check-pass

fn f() -> impl Sized {
// rustdoc doesn't care that this is infinitely sized
enum E {
//~^ ERROR recursive type `f::E` has infinite size
V(E),
}
unimplemented!()
17 changes: 0 additions & 17 deletions src/test/rustdoc-ui/infinite-recursive-type-impl-trait.stderr

This file was deleted.

1 change: 0 additions & 1 deletion src/test/ui/issues/issue-72554.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ use std::collections::BTreeSet;
#[derive(Hash)]
pub enum ElemDerived {
//~^ ERROR recursive type `ElemDerived` has infinite size
//~| ERROR cycle detected when computing drop-check constraints for `ElemDerived`
A(ElemDerived)
}

20 changes: 3 additions & 17 deletions src/test/ui/issues/issue-72554.stderr
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ error[E0072]: recursive type `ElemDerived` has infinite size
|
LL | pub enum ElemDerived {
| ^^^^^^^^^^^^^^^^^^^^
...
LL |
LL | A(ElemDerived)
| ----------- recursive without indirection
|
@@ -12,20 +12,6 @@ help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
LL | A(Box<ElemDerived>)
| ++++ +

error[E0391]: cycle detected when computing drop-check constraints for `ElemDerived`
--> $DIR/issue-72554.rs:4:1
|
LL | pub enum ElemDerived {
| ^^^^^^^^^^^^^^^^^^^^
|
= note: ...which immediately requires computing drop-check constraints for `ElemDerived` again
note: cycle used when computing drop-check constraints for `Elem`
--> $DIR/issue-72554.rs:11:1
|
LL | pub enum Elem {
| ^^^^^^^^^^^^^

error: aborting due to 2 previous errors
error: aborting due to previous error

Some errors have detailed explanations: E0072, E0391.
For more information about an error, try `rustc --explain E0072`.
For more information about this error, try `rustc --explain E0072`.
1 change: 1 addition & 0 deletions src/test/ui/variance/variance-regions-unused-indirect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Test that disallow lifetime parameters that are unused.

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

30 changes: 27 additions & 3 deletions src/test/ui/variance/variance-regions-unused-indirect.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
error[E0072]: recursive types `Foo` and `Bar` have infinite size
--> $DIR/variance-regions-unused-indirect.rs:3:1
|
LL | enum Foo<'a> {
| ^^^^^^^^^^^^
LL |
LL | Foo1(Bar<'a>)
| ------- recursive without indirection
...
LL | enum Bar<'a> {
| ^^^^^^^^^^^^
LL | Bar1(Foo<'a>)
| ------- recursive without indirection
|
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
|
LL ~ Foo1(Box<Bar<'a>>)
LL | }
LL |
LL | enum Bar<'a> {
LL ~ Bar1(Box<Foo<'a>>)
|

error[E0392]: parameter `'a` is never used
--> $DIR/variance-regions-unused-indirect.rs:3:10
|
@@ -7,13 +30,14 @@ LL | enum Foo<'a> {
= help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData`

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

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

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