Skip to content

Commit 7780408

Browse files
committed
Auto merge of #28132 - arielb1:uninstantiable, r=nikomatsakis
It is *very* easy to bypass, and is a relic of a bygone age where the type-checker was *much* less robust. Fixes #27497 r? @nikomatsakis
2 parents 40c00e9 + 321f8d8 commit 7780408

File tree

3 files changed

+1
-115
lines changed

3 files changed

+1
-115
lines changed

src/librustc/middle/ty.rs

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -4821,100 +4821,6 @@ impl<'tcx> TyS<'tcx> {
48214821

48224822
result
48234823
}
4824-
4825-
// True if instantiating an instance of `r_ty` requires an instance of `r_ty`.
4826-
pub fn is_instantiable(&'tcx self, cx: &ctxt<'tcx>) -> bool {
4827-
fn type_requires<'tcx>(cx: &ctxt<'tcx>, seen: &mut Vec<AdtDef<'tcx>>,
4828-
r_ty: Ty<'tcx>, ty: Ty<'tcx>) -> bool {
4829-
debug!("type_requires({:?}, {:?})?",
4830-
r_ty, ty);
4831-
4832-
let r = r_ty == ty || subtypes_require(cx, seen, r_ty, ty);
4833-
4834-
debug!("type_requires({:?}, {:?})? {:?}",
4835-
r_ty, ty, r);
4836-
return r;
4837-
}
4838-
4839-
fn subtypes_require<'tcx>(cx: &ctxt<'tcx>, seen: &mut Vec<AdtDef<'tcx>>,
4840-
r_ty: Ty<'tcx>, ty: Ty<'tcx>) -> bool {
4841-
debug!("subtypes_require({:?}, {:?})?",
4842-
r_ty, ty);
4843-
4844-
let r = match ty.sty {
4845-
// fixed length vectors need special treatment compared to
4846-
// normal vectors, since they don't necessarily have the
4847-
// possibility to have length zero.
4848-
TyArray(_, 0) => false, // don't need no contents
4849-
TyArray(ty, _) => type_requires(cx, seen, r_ty, ty),
4850-
4851-
TyBool |
4852-
TyChar |
4853-
TyInt(_) |
4854-
TyUint(_) |
4855-
TyFloat(_) |
4856-
TyStr |
4857-
TyBareFn(..) |
4858-
TyParam(_) |
4859-
TyProjection(_) |
4860-
TySlice(_) => {
4861-
false
4862-
}
4863-
TyBox(typ) => {
4864-
type_requires(cx, seen, r_ty, typ)
4865-
}
4866-
TyRef(_, ref mt) => {
4867-
type_requires(cx, seen, r_ty, mt.ty)
4868-
}
4869-
4870-
TyRawPtr(..) => {
4871-
false // unsafe ptrs can always be NULL
4872-
}
4873-
4874-
TyTrait(..) => {
4875-
false
4876-
}
4877-
4878-
TyStruct(def, substs) | TyEnum(def, substs) => {
4879-
if seen.contains(&def) {
4880-
// FIXME(#27497) ???
4881-
false
4882-
} else if def.is_empty() {
4883-
// HACK: required for empty types to work. This
4884-
// check is basically a lint anyway.
4885-
false
4886-
} else {
4887-
seen.push(def);
4888-
let r = def.variants.iter().all(|v| v.fields.iter().any(|f| {
4889-
type_requires(cx, seen, r_ty, f.ty(cx, substs))
4890-
}));
4891-
seen.pop().unwrap();
4892-
r
4893-
}
4894-
}
4895-
4896-
TyError |
4897-
TyInfer(_) |
4898-
TyClosure(..) => {
4899-
// this check is run on type definitions, so we don't expect to see
4900-
// inference by-products or closure types
4901-
cx.sess.bug(&format!("requires check invoked on inapplicable type: {:?}", ty))
4902-
}
4903-
4904-
TyTuple(ref ts) => {
4905-
ts.iter().any(|ty| type_requires(cx, seen, r_ty, *ty))
4906-
}
4907-
};
4908-
4909-
debug!("subtypes_require({:?}, {:?})? {:?}",
4910-
r_ty, ty, r);
4911-
4912-
return r;
4913-
}
4914-
4915-
let mut seen = Vec::new();
4916-
!subtypes_require(cx, &mut seen, self, self)
4917-
}
49184824
}
49194825

49204826
/// Describes whether a type is representable. For types that are not

src/librustc_typeck/check/mod.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ use syntax::attr::AttrMetaMethods;
117117
use syntax::ast::{self, Visibility};
118118
use syntax::ast_util;
119119
use syntax::codemap::{self, Span};
120-
use syntax::feature_gate::emit_feature_err;
121120
use syntax::owned_slice::OwnedSlice;
122121
use syntax::parse::token::{self, InternedString};
123122
use syntax::print::pprust;
@@ -669,7 +668,6 @@ pub fn check_struct(ccx: &CrateCtxt, id: ast::NodeId, span: Span) {
669668
let tcx = ccx.tcx;
670669

671670
check_representable(tcx, span, id, "struct");
672-
check_instantiable(tcx, span, id);
673671

674672
if tcx.lookup_simd(DefId::local(id)) {
675673
check_simd(tcx, span, id);
@@ -4202,22 +4200,6 @@ pub fn check_representable(tcx: &ty::ctxt,
42024200
return true
42034201
}
42044202

4205-
/// Checks whether a type can be constructed at runtime without
4206-
/// an existing instance of that type.
4207-
pub fn check_instantiable(tcx: &ty::ctxt,
4208-
sp: Span,
4209-
item_id: ast::NodeId) {
4210-
let item_ty = tcx.node_id_to_type(item_id);
4211-
if !item_ty.is_instantiable(tcx) &&
4212-
!tcx.sess.features.borrow().static_recursion {
4213-
emit_feature_err(&tcx.sess.parse_sess.span_diagnostic,
4214-
"static_recursion",
4215-
sp,
4216-
"this type cannot be instantiated at runtime \
4217-
without an instance of itself");
4218-
}
4219-
}
4220-
42214203
pub fn check_simd(tcx: &ty::ctxt, sp: Span, id: ast::NodeId) {
42224204
let t = tcx.node_id_to_type(id);
42234205
match t.sty {
@@ -4352,7 +4334,6 @@ pub fn check_enum_variants<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
43524334
do_check(ccx, vs, id, hint);
43534335

43544336
check_representable(ccx.tcx, sp, id, "enum");
4355-
check_instantiable(ccx.tcx, sp, id);
43564337
}
43574338

43584339
// Returns the type parameter count and the type for the given definition.

src/test/compile-fail/static-recursion-gate-2.rs renamed to src/test/run-pass/uninstantiable-struct.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
struct Z(&'static Z);
12-
//~^ ERROR this type cannot be instantiated
11+
pub struct Z(&'static Z);
1312

1413
pub fn main() {}

0 commit comments

Comments
 (0)