Skip to content

Unsized types #13375

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/librustc/front/config.rs
Original file line number Diff line number Diff line change
@@ -103,12 +103,12 @@ fn fold_item_underscore(cx: &mut Context, item: &ast::Item_) -> ast::Item_ {
.map(|x| *x).collect();
ast::ItemImpl((*a).clone(), (*b).clone(), c, methods)
}
ast::ItemTrait(ref a, ref b, ref methods) => {
ast::ItemTrait(ref a, b, ref c, ref methods) => {
let methods = methods.iter()
.filter(|m| trait_method_in_cfg(cx, *m) )
.map(|x| (*x).clone())
.collect();
ast::ItemTrait((*a).clone(), (*b).clone(), methods)
ast::ItemTrait((*a).clone(), b, (*c).clone(), methods)
}
ast::ItemStruct(def, ref generics) => {
ast::ItemStruct(fold_struct(cx, def), generics.clone())
1 change: 1 addition & 0 deletions src/librustc/metadata/common.rs
Original file line number Diff line number Diff line change
@@ -169,6 +169,7 @@ pub static tag_lang_items_item_node_id: uint = 0x4b;

pub static tag_item_unnamed_field: uint = 0x4c;
pub static tag_items_data_item_visibility: uint = 0x4e;
pub static tag_items_data_item_sized: uint = 0x4f;

pub static tag_item_method_tps: uint = 0x51;
pub static tag_item_method_fty: uint = 0x52;
21 changes: 21 additions & 0 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
@@ -169,6 +169,19 @@ fn item_visibility(item: ebml::Doc) -> ast::Visibility {
}
}

fn item_sized(item: ebml::Doc) -> ast::Sized {
match reader::maybe_get_doc(item, tag_items_data_item_sized) {
None => ast::StaticSize,
Some(sized_doc) => {
match reader::doc_as_u8(sized_doc) as char {
'd' => ast::DynSize,
's' => ast::StaticSize,
_ => fail!("unknown sized-ness character")
}
}
}
}

fn item_method_sort(item: ebml::Doc) -> char {
let mut ret = 'r';
reader::tagged_docs(item, tag_item_trait_method_sort, |doc| {
@@ -376,6 +389,7 @@ pub fn get_trait_def(cdata: Cmd,
let tp_defs = item_ty_param_defs(item_doc, tcx, cdata,
tag_items_data_item_ty_param_bounds);
let rp_defs = item_region_param_defs(item_doc, cdata);
let sized = item_sized(item_doc);
let mut bounds = ty::EmptyBuiltinBounds();
// Collect the builtin bounds from the encoded supertraits.
// FIXME(#8559): They should be encoded directly.
@@ -387,6 +401,13 @@ pub fn get_trait_def(cdata: Cmd,
});
true
});
// Turn sized into a bound, FIXME(#8559).
if sized == ast::StaticSize {
tcx.lang_items.to_builtin_kind(tcx.lang_items.sized_trait().unwrap()).map(|bound| {
bounds.add(bound);
});
}

ty::TraitDef {
generics: ty::Generics {type_param_defs: tp_defs,
region_param_defs: rp_defs},
15 changes: 14 additions & 1 deletion src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
@@ -868,6 +868,16 @@ fn encode_extension_implementations(ecx: &EncodeContext,
}
}

fn encode_sized(ebml_w: &mut Encoder, sized: Sized) {
ebml_w.start_tag(tag_items_data_item_sized);
let ch = match sized {
DynSize => 'd',
StaticSize => 's',
};
ebml_w.wr_str(str::from_char(ch));
ebml_w.end_tag();
}

fn encode_info_for_item(ecx: &EncodeContext,
ebml_w: &mut Encoder,
item: &Item,
@@ -1103,7 +1113,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
ast_method)
}
}
ItemTrait(_, ref super_traits, ref ms) => {
ItemTrait(_, sized, ref super_traits, ref ms) => {
add_to_index(item, ebml_w, index);
ebml_w.start_tag(tag_items_data_item);
encode_def_id(ebml_w, def_id);
@@ -1117,6 +1127,9 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_trait_ref(ebml_w, ecx, trait_def.trait_ref, tag_item_trait_ref);
encode_name(ebml_w, item.ident.name);
encode_attributes(ebml_w, item.attrs.as_slice());
// When we fix the rest of the supertrait nastiness (FIXME(#8559)), we
// should no longer need this ugly little hack either.
encode_sized(ebml_w, sized);
encode_visibility(ebml_w, vis);
for &method_def_id in ty::trait_method_def_ids(tcx, def_id).iter() {
ebml_w.start_tag(tag_item_trait_method);
8 changes: 4 additions & 4 deletions src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ impl Visitor<()> for ParentVisitor {
// method to the root. In this case, if the trait is private, then
// parent all the methods to the trait to indicate that they're
// private.
ast::ItemTrait(_, _, ref methods) if item.vis != ast::Public => {
ast::ItemTrait(_, _, _, ref methods) if item.vis != ast::Public => {
for m in methods.iter() {
match *m {
ast::Provided(ref m) => self.parents.insert(m.id, item.id),
@@ -284,7 +284,7 @@ impl<'a> Visitor<()> for EmbargoVisitor<'a> {

// Default methods on traits are all public so long as the trait
// is public
ast::ItemTrait(_, _, ref methods) if public_first => {
ast::ItemTrait(_, _, _, ref methods) if public_first => {
for method in methods.iter() {
match *method {
ast::Provided(ref m) => {
@@ -1112,7 +1112,7 @@ impl<'a> SanePrivacyVisitor<'a> {

ast::ItemStruct(ref def, _) => check_struct(def),

ast::ItemTrait(_, _, ref methods) => {
ast::ItemTrait(_, _, _, ref methods) => {
for m in methods.iter() {
match *m {
ast::Provided(ref m) => {
@@ -1175,7 +1175,7 @@ impl<'a> SanePrivacyVisitor<'a> {

ast::ItemStruct(ref def, _) => check_struct(def),

ast::ItemTrait(_, _, ref methods) => {
ast::ItemTrait(_, _, _, ref methods) => {
for m in methods.iter() {
match *m {
ast::Required(..) => {}
7 changes: 3 additions & 4 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
@@ -1336,7 +1336,7 @@ impl<'a> Resolver<'a> {

ItemImpl(_, Some(_), _, _) => parent,

ItemTrait(_, _, ref methods) => {
ItemTrait(_, _, _, ref methods) => {
let (name_bindings, new_parent) =
self.add_child(ident, parent, ForbidDuplicateTypes, sp);

@@ -3627,7 +3627,7 @@ impl<'a> Resolver<'a> {
methods.as_slice());
}

ItemTrait(ref generics, ref traits, ref methods) => {
ItemTrait(ref generics, _, ref traits, ref methods) => {
// Create a new rib for the self type.
let self_type_rib = @Rib::new(NormalRibKind);
self.type_ribs.borrow_mut().push(self_type_rib);
@@ -3834,9 +3834,8 @@ impl<'a> Resolver<'a> {
}
Some(declaration) => {
for argument in declaration.inputs.iter() {
let binding_mode = ArgumentIrrefutableMode;
this.resolve_pattern(argument.pat,
binding_mode,
ArgumentIrrefutableMode,
None);

this.resolve_type(argument.ty);
2 changes: 1 addition & 1 deletion src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
@@ -84,7 +84,7 @@ impl<'a, 'b> Visitor<Scope<'a>> for LifetimeContext<'b> {
ast::ItemEnum(_, ref generics) |
ast::ItemStruct(_, ref generics) |
ast::ItemImpl(ref generics, _, _, _) |
ast::ItemTrait(ref generics, _, _) => {
ast::ItemTrait(ref generics, _, _, _) => {
self.check_lifetime_names(&generics.lifetimes);
EarlyScope(0, &generics.lifetimes, &root)
}
54 changes: 19 additions & 35 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
@@ -2574,16 +2574,24 @@ pub fn type_is_machine(ty: t) -> bool {
#[allow(dead_code)] // leaving in for DST
pub fn type_is_sized(cx: &ctxt, ty: ty::t) -> bool {
match get(ty).sty {
// FIXME(#6308) add trait, vec, str, etc here.
ty_param(p) => {
ty_param(tp) => {
assert_eq!(tp.def_id.krate, ast::LOCAL_CRATE);

let ty_param_defs = cx.ty_param_defs.borrow();
let param_def = ty_param_defs.get(&p.def_id.node);
if param_def.bounds.builtin_bounds.contains_elem(BoundSized) {
return true;
}
return false;
let param_def = ty_param_defs.get(&tp.def_id.node);
param_def.bounds.builtin_bounds.contains_elem(BoundSized)
},
_ => return true,
ty_self(def_id) => {
let trait_def = lookup_trait_def(cx, def_id);
trait_def.bounds.contains_elem(BoundSized)
},
ty_struct(def_id, ref substs) => {
let flds = lookup_struct_fields(cx, def_id);
let mut tps = flds.iter().map(|f| lookup_field_type(cx, def_id, f.id, substs));
!tps.any(|ty| !type_is_sized(cx, ty))
}
ty_tup(ref ts) => !ts.iter().any(|t| !type_is_sized(cx, *t)),
_ => true
}
}

@@ -3506,7 +3514,7 @@ pub fn provided_trait_methods(cx: &ctxt, id: ast::DefId) -> Vec<@Method> {
match cx.map.find(id.node) {
Some(ast_map::NodeItem(item)) => {
match item.node {
ItemTrait(_, _, ref ms) => {
ItemTrait(_, _, _, ref ms) => {
let (_, p) =
ast_util::split_trait_methods(ms.as_slice());
p.iter()
@@ -4040,32 +4048,8 @@ pub fn lookup_field_type(tcx: &ctxt,
// Fails if the id is not bound to a struct.
pub fn lookup_struct_fields(cx: &ctxt, did: ast::DefId) -> Vec<field_ty> {
if did.krate == ast::LOCAL_CRATE {
match cx.map.find(did.node) {
Some(ast_map::NodeItem(i)) => {
match i.node {
ast::ItemStruct(struct_def, _) => {
struct_field_tys(struct_def.fields.as_slice())
}
_ => cx.sess.bug("struct ID bound to non-struct")
}
}
Some(ast_map::NodeVariant(ref variant)) => {
match (*variant).node.kind {
ast::StructVariantKind(struct_def) => {
struct_field_tys(struct_def.fields.as_slice())
}
_ => {
cx.sess.bug("struct ID bound to enum variant that \
isn't struct-like")
}
}
}
_ => {
cx.sess.bug(
format!("struct ID not bound to an item: {}",
cx.map.node_to_str(did.node)));
}
}
let struct_def = cx.map.expect_struct(did.node);
struct_field_tys(struct_def.fields.as_slice())
} else {
csearch::get_struct_fields(&cx.sess.cstore, did)
}
57 changes: 41 additions & 16 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
@@ -369,21 +369,21 @@ impl<'a> GatherLocalsVisitor<'a> {
}

impl<'a> Visitor<()> for GatherLocalsVisitor<'a> {
// Add explicitly-declared locals.
// Add explicitly-declared locals.
fn visit_local(&mut self, local: &ast::Local, _: ()) {
let o_ty = match local.ty.node {
ast::TyInfer => None,
_ => Some(self.fcx.to_ty(local.ty))
};
self.assign(local.id, o_ty);
debug!("Local variable {} is assigned type {}",
self.fcx.pat_to_str(local.pat),
self.fcx.infcx().ty_to_str(
self.fcx.inh.locals.borrow().get_copy(&local.id)));
visit::walk_local(self, local, ());

let o_ty = match local.ty.node {
ast::TyInfer => None,
_ => Some(self.fcx.to_ty(local.ty))
};
self.assign(local.id, o_ty);
debug!("Local variable {} is assigned type {}",
self.fcx.pat_to_str(local.pat),
self.fcx.infcx().ty_to_str(
self.fcx.inh.locals.borrow().get_copy(&local.id)));
visit::walk_local(self, local, ());
}
// Add pattern bindings.

// Add pattern bindings.
fn visit_pat(&mut self, p: &ast::Pat, _: ()) {
match p.node {
ast::PatIdent(_, ref path, _)
@@ -510,14 +510,37 @@ fn check_fn<'a>(ccx: &'a CrateCtxt<'a>,
fcx
}

fn check_fields_sized(tcx: &ty::ctxt,
id: ast::NodeId) {
let struct_def = tcx.map.expect_struct(id);
// FIXME(#13121) allow the last field to be DST
for f in struct_def.fields.iter() {
let t = ty::node_id_to_type(tcx, f.node.id);
if !ty::type_is_sized(tcx, t) {
match f.node.kind {
ast::NamedField(ident, _) => {
tcx.sess.span_err(f.span, format!("Dynamically sized type in field {}",
token::get_ident(ident)));
}
ast::UnnamedField(_) => {
tcx.sess.span_err(f.span, "Dynamically sized type in field");
}
}
}
}
}

pub fn check_struct(ccx: &CrateCtxt, id: ast::NodeId, span: Span) {
let tcx = ccx.tcx;

// Check that the struct is representable
check_representable(tcx, span, id, "struct");

// Check that the struct is instantiable
check_instantiable(tcx, span, id);
if check_instantiable(tcx, span, id) {
// This might cause stack overflow if id is not instantiable.
check_fields_sized(tcx, id);
}

if ty::lookup_simd(tcx, local_def(id)) {
check_simd(tcx, span, id);
@@ -576,7 +599,7 @@ pub fn check_item(ccx: &CrateCtxt, it: &ast::Item) {
}

}
ast::ItemTrait(_, _, ref trait_methods) => {
ast::ItemTrait(_, _, _, ref trait_methods) => {
let trait_def = ty::lookup_trait_def(ccx.tcx, local_def(it.id));
for trait_method in (*trait_methods).iter() {
match *trait_method {
@@ -3398,14 +3421,16 @@ pub fn check_representable(tcx: &ty::ctxt,
/// is representable, but not instantiable.
pub fn check_instantiable(tcx: &ty::ctxt,
sp: Span,
item_id: ast::NodeId) {
item_id: ast::NodeId) -> bool {
let item_ty = ty::node_id_to_type(tcx, item_id);
if !ty::is_instantiable(tcx, item_ty) {
tcx.sess.span_err(sp, format!("this type cannot be instantiated \
without an instance of itself; \
consider using `Option<{}>`",
ppaux::ty_to_str(tcx, item_ty)));
return false
}
true
}

pub fn check_simd(tcx: &ty::ctxt, sp: Span, id: ast::NodeId) {
Loading