Skip to content

Commit 6a87a7f

Browse files
committed
---
yaml --- r: 39599 b: refs/heads/incoming c: 56227c4 h: refs/heads/master i: 39597: 594486d 39595: cfe1154 39591: fe5facd 39583: 71216d2 v: v3
1 parent bfffe82 commit 6a87a7f

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: a810c03263670238bccd64cabb12a23a46e3a278
9-
refs/heads/incoming: 2cf2d6aa923a3d05f5bc9caae4cac757e1e27b57
9+
refs/heads/incoming: 56227c42b387d0463f8c72f1a37d5a3b61197912
1010
refs/heads/dist-snap: 22efa39382d41b084fde1719df7ae8ce5697d8c9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/doc/rust.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,19 @@ let mut a: Animal = Dog;
11071107
a = Cat;
11081108
~~~~
11091109

1110+
Enumeration constructors can have either named or unnamed fields:
1111+
~~~~
1112+
enum Animal {
1113+
Dog (~str, float),
1114+
Cat { name: ~str, weight: float }
1115+
}
1116+
1117+
let mut a: Animal = Dog(~"Cocoa", 37.2);
1118+
a = Cat{ name: ~"Spotty", weight: 2.7 };
1119+
~~~~
1120+
1121+
In this example, `Cat` is a _struct-like enum variant_,
1122+
whereas `Dog` is simply called an enum variant.
11101123
### Constants
11111124

11121125
~~~~~~~~ {.ebnf .gram}

branches/incoming/doc/tutorial.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,24 @@ fn point_from_direction(dir: Direction) -> Point {
733733
}
734734
~~~~
735735

736+
A special kind of enum variant, called _struct-like enums_,
737+
can have its fields extracted with dot notation and not just destructuring.
738+
For example:
739+
740+
~~~~
741+
# struct Point {x: float, y: float}
742+
# fn square(x: float) -> float { x * x }
743+
enum Shape {
744+
Circle { center: Point, radius: float },
745+
Rectangle { left: Point, right: Point }
746+
}
747+
fn area(sh: Shape) -> float {
748+
match sh {
749+
Circle(c) => float::consts::pi * square(c.radius),
750+
Rectangle(r) => r.right.x - r.left.x * r.right.y - r.right.y
751+
}
752+
}
753+
~~~~
736754
## Tuples
737755

738756
Tuples in Rust behave exactly like structs, except that their fields

branches/incoming/src/librustc/middle/trans/base.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,10 @@ fn iter_structural_ty(cx: block, av: ValueRef, t: ty::t,
545545
j += 1u;
546546
}
547547
}
548-
_ => cx.tcx().sess.bug(~"iter_variant: not a function type")
548+
_ => cx.tcx().sess.bug(fmt!("iter_variant: not a function type: \
549+
%s (variant name = %s)",
550+
cx.ty_to_str(fn_ty),
551+
cx.sess().str_of(variant.name)))
549552
}
550553
return cx;
551554
}

branches/incoming/src/librustc/middle/typeck/collect.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,26 @@ fn get_enum_variant_types(ccx: @crate_ctxt,
158158
result_ty = Some(enum_ty);
159159
}
160160
ast::struct_variant_kind(struct_def) => {
161-
result_ty = Some(enum_ty);
162161
// XXX: Merge with computation of the the same value below?
163162
let tpt = {bounds: ty_param_bounds(ccx, ty_params),
164163
region_param: rp,
165164
ty: enum_ty};
166165
convert_struct(
167166
ccx, rp, struct_def, ty_params, tpt, variant.node.id);
167+
// Compute the ctor arg types from the struct fields
168+
let struct_fields = do struct_def.fields.map |struct_field| {
169+
{mode: ast::expl(ast::by_val),
170+
ty: ty::node_id_to_type(ccx.tcx, (*struct_field).node.id)
171+
}
172+
};
173+
result_ty = Some(ty::mk_fn(tcx, FnTyBase {
174+
meta: FnMeta {purity: ast::pure_fn,
175+
proto: ast::ProtoBare,
176+
onceness: ast::Many,
177+
bounds: @~[],
178+
region: ty::re_static,
179+
ret_style: ast::return_val},
180+
sig: FnSig {inputs: struct_fields, output: enum_ty }}));
168181
}
169182
ast::enum_variant_kind(ref enum_definition) => {
170183
get_enum_variant_types(ccx,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
enum Animal {
2+
Dog (~str, float),
3+
Cat { name: ~str, weight: float }
4+
}
5+
6+
fn main() {
7+
let mut a: Animal = Dog(~"Cocoa", 37.2);
8+
a = Cat{ name: ~"Spotty", weight: 2.7 };
9+
// permuting the fields should work too
10+
let c = Cat { weight: 3.1, name: ~"Spreckles" };
11+
}

0 commit comments

Comments
 (0)