Skip to content

Commit 4da58a5

Browse files
committed
rustc: Implement typechecking for tuple structs. r=nmatsakis
1 parent 61bb357 commit 4da58a5

File tree

5 files changed

+87
-11
lines changed

5 files changed

+87
-11
lines changed

src/libsyntax/ast.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,31 @@ enum struct_field_kind {
14441444
unnamed_field // element of a tuple-like struct
14451445
}
14461446

1447+
impl struct_field_kind : cmp::Eq {
1448+
pure fn eq(other: &struct_field_kind) -> bool {
1449+
match self {
1450+
named_field(ident_a, class_mutability_a, visibility_a) => {
1451+
match *other {
1452+
named_field(ident_b, class_mutability_b, visibility_b)
1453+
=> {
1454+
ident_a == ident_b &&
1455+
class_mutability_a == class_mutability_b &&
1456+
visibility_a == visibility_b
1457+
}
1458+
unnamed_field => false
1459+
}
1460+
}
1461+
unnamed_field => {
1462+
match *other {
1463+
named_field(*) => false,
1464+
unnamed_field => true
1465+
}
1466+
}
1467+
}
1468+
}
1469+
pure fn ne(other: &struct_field_kind) -> bool { !self.eq(other) }
1470+
}
1471+
14471472
#[auto_serialize]
14481473
#[auto_deserialize]
14491474
type struct_def = {
@@ -1452,7 +1477,10 @@ type struct_def = {
14521477
methods: ~[@method], /* methods */
14531478
/* (not including ctor or dtor) */
14541479
/* dtor is optional */
1455-
dtor: Option<class_dtor>
1480+
dtor: Option<class_dtor>,
1481+
/* ID of the constructor. This is only used for tuple- or enum-like
1482+
* structs. */
1483+
ctor_id: node_id
14561484
};
14571485

14581486
/*

src/libsyntax/fold.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ fn fold_struct_def(struct_def: @ast::struct_def, fld: ast_fold)
280280
traits: vec::map(struct_def.traits, |p| fold_trait_ref(*p, fld)),
281281
fields: vec::map(struct_def.fields, |f| fold_struct_field(*f, fld)),
282282
methods: vec::map(struct_def.methods, |m| fld.fold_method(*m)),
283-
dtor: dtor
283+
dtor: dtor,
284+
ctor_id: fld.new_id(struct_def.ctor_id)
284285
};
285286
}
286287

@@ -563,7 +564,8 @@ fn noop_fold_variant(v: variant_, fld: ast_fold) -> variant_ {
563564
|f| fld.fold_struct_field(*f)),
564565
methods: vec::map(struct_def.methods,
565566
|m| fld.fold_method(*m)),
566-
dtor: dtor
567+
dtor: dtor,
568+
ctor_id: fld.new_id(struct_def.ctor_id)
567569
})
568570
}
569571

src/libsyntax/parse/parser.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -2722,12 +2722,14 @@ impl Parser {
27222722
self_id: self.get_id(),
27232723
body: d_body},
27242724
span: d_s}};
2725+
let _ = self.get_id(); // XXX: Workaround for crazy bug.
27252726
(class_name,
27262727
item_class(@{
27272728
traits: traits,
27282729
fields: move fields,
27292730
methods: move methods,
2730-
dtor: actual_dtor
2731+
dtor: actual_dtor,
2732+
ctor_id: self.get_id()
27312733
}, ty_params),
27322734
None)
27332735
}
@@ -3073,7 +3075,8 @@ impl Parser {
30733075
traits: ~[],
30743076
fields: move fields,
30753077
methods: move methods,
3076-
dtor: actual_dtor
3078+
dtor: actual_dtor,
3079+
ctor_id: self.get_id()
30773080
};
30783081
}
30793082

src/rustc/middle/resolve.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use middle::pat_util::{pat_bindings};
99
use syntax::ast::{_mod, add, arm};
1010
use syntax::ast::{bind_by_ref, bind_by_implicit_ref, bind_by_value};
1111
use syntax::ast::{bitand, bitor, bitxor};
12-
use syntax::ast::{binding_mode, blk,
13-
capture_clause, class_ctor, class_dtor};
12+
use syntax::ast::{binding_mode, blk, capture_clause, class_ctor, class_dtor};
1413
use syntax::ast::{crate, crate_num, decl_item};
1514
use syntax::ast::{def, def_arg, def_binding, def_class, def_const, def_fn};
1615
use syntax::ast::{def_foreign_mod, def_id, def_label, def_local, def_mod};
@@ -39,7 +38,7 @@ use syntax::ast::{trait_ref, tuple_variant_kind, Ty, ty_bool, ty_char};
3938
use syntax::ast::{ty_f, ty_f32, ty_f64, ty_float, ty_i, ty_i16, ty_i32};
4039
use syntax::ast::{ty_i64, ty_i8, ty_int, ty_param, ty_path, ty_str, ty_u};
4140
use syntax::ast::{ty_u16, ty_u32, ty_u64, ty_u8, ty_uint, type_value_ns};
42-
use syntax::ast::{ty_param_bound};
41+
use syntax::ast::{ty_param_bound, unnamed_field};
4342
use syntax::ast::{variant, view_item, view_item_export, view_item_import};
4443
use syntax::ast::{view_item_use, view_path_glob, view_path_list};
4544
use syntax::ast::{view_path_simple, visibility, anonymous, named};
@@ -1179,12 +1178,22 @@ impl Resolver {
11791178
}
11801179

11811180
// These items live in both the type and value namespaces.
1182-
item_class(*) => {
1181+
item_class(struct_def, _) => {
11831182
let (name_bindings, new_parent) =
11841183
self.add_child(ident, parent, ForbidDuplicateTypes, sp);
11851184

1186-
(*name_bindings).define_type
1187-
(privacy, def_ty(local_def(item.id)), sp);
1185+
name_bindings.define_type(
1186+
privacy, def_ty(local_def(item.id)), sp);
1187+
1188+
// If this struct is tuple-like or enum-like, define a name
1189+
// in the value namespace.
1190+
if struct_def.fields.len() == 0 ||
1191+
struct_def.fields[0].node.kind == unnamed_field {
1192+
name_bindings.define_value(
1193+
privacy,
1194+
def_class(local_def(struct_def.ctor_id)),
1195+
sp);
1196+
}
11881197

11891198
// Record the def ID of this struct.
11901199
self.structs.insert(local_def(item.id), ());

src/rustc/middle/typeck/collect.rs

+34
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ are represented as `ty_param()` instances.
2222

2323
use astconv::{ast_conv, ty_of_fn_decl, ty_of_arg, ast_ty_to_ty};
2424
use ast_util::trait_method_to_ty_method;
25+
use middle::ty::{FnMeta, FnSig, FnTyBase};
2526
use rscope::*;
2627
use ty::{FnTyBase, FnMeta, FnSig, InstantiatedTraitRef};
2728
use util::common::pluralize;
@@ -559,6 +560,39 @@ fn convert_struct(ccx: @crate_ctxt,
559560
write_ty_to_tcx(tcx, trait_ref.impl_id, tpt.ty);
560561
tcx.tcache.insert(local_def(trait_ref.impl_id), tpt);
561562
}
563+
564+
// If this struct is enum-like or tuple-like, create the type of its
565+
// constructor.
566+
if struct_def.fields.len() == 0 {
567+
// Enum-like.
568+
write_ty_to_tcx(tcx, struct_def.ctor_id, selfty);
569+
tcx.tcache.insert(local_def(struct_def.ctor_id), tpt);
570+
} else if struct_def.fields[0].node.kind == ast::unnamed_field {
571+
// Tuple-like.
572+
let ctor_fn_ty = ty::mk_fn(tcx, FnTyBase {
573+
meta: FnMeta {
574+
purity: ast::pure_fn,
575+
proto: ty::proto_bare,
576+
bounds: @~[],
577+
ret_style: ast::return_val,
578+
},
579+
sig: FnSig {
580+
inputs: do struct_def.fields.map |field| {
581+
{
582+
mode: ast::expl(ast::by_copy),
583+
ty: ccx.tcx.tcache.get(local_def(field.node.id)).ty
584+
}
585+
},
586+
output: selfty
587+
}
588+
});
589+
write_ty_to_tcx(tcx, struct_def.ctor_id, ctor_fn_ty);
590+
tcx.tcache.insert(local_def(struct_def.ctor_id), {
591+
bounds: tpt.bounds,
592+
region_param: tpt.region_param,
593+
ty: ctor_fn_ty
594+
});
595+
}
562596
}
563597

564598
fn convert_foreign(ccx: @crate_ctxt, i: @ast::foreign_item) {

0 commit comments

Comments
 (0)