Skip to content

Commit b8a53b5

Browse files
committed
Add 'Sized' builtin kind; doesn't do anything yet
1 parent 237dce1 commit b8a53b5

File tree

8 files changed

+154
-88
lines changed

8 files changed

+154
-88
lines changed

src/librustc/metadata/tydecode.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ fn parse_bounds(st: @mut PState, conv: conv_did) -> ty::ParamBounds {
569569
'O' => {
570570
param_bounds.builtin_bounds.add(ty::BoundStatic);
571571
}
572+
'Z' => {
573+
param_bounds.builtin_bounds.add(ty::BoundSized);
574+
}
572575
'I' => {
573576
param_bounds.trait_bounds.push(@parse_trait_ref(st, conv));
574577
}

src/librustc/metadata/tyencode.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ fn enc_bounds(w: @io::Writer, cx: @ctxt, bs: &ty::ParamBounds) {
403403
ty::BoundCopy => w.write_char('C'),
404404
ty::BoundConst => w.write_char('K'),
405405
ty::BoundStatic => w.write_char('O'),
406+
ty::BoundSized => w.write_char('Z'),
406407
}
407408
}
408409

src/librustc/middle/lang_items.rs

Lines changed: 82 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -36,55 +36,56 @@ pub enum LangItem {
3636
ConstTraitLangItem, // 0
3737
CopyTraitLangItem, // 1
3838
OwnedTraitLangItem, // 2
39+
SizedTraitLangItem, // 3
3940

40-
DropTraitLangItem, // 3
41+
DropTraitLangItem, // 4
4142

42-
AddTraitLangItem, // 4
43-
SubTraitLangItem, // 5
44-
MulTraitLangItem, // 6
45-
DivTraitLangItem, // 7
46-
RemTraitLangItem, // 8
47-
NegTraitLangItem, // 9
48-
NotTraitLangItem, // 10
43+
AddTraitLangItem, // 5
44+
SubTraitLangItem, // 6
45+
MulTraitLangItem, // 7
46+
DivTraitLangItem, // 8
47+
RemTraitLangItem, // 9
48+
NegTraitLangItem, // 10
49+
NotTraitLangItem, // 11
4950
BitXorTraitLangItem, // 11
50-
BitAndTraitLangItem, // 12
51-
BitOrTraitLangItem, // 13
52-
ShlTraitLangItem, // 14
53-
ShrTraitLangItem, // 15
54-
IndexTraitLangItem, // 16
55-
56-
EqTraitLangItem, // 17
57-
OrdTraitLangItem, // 18
58-
59-
StrEqFnLangItem, // 19
60-
UniqStrEqFnLangItem, // 20
61-
AnnihilateFnLangItem, // 21
62-
LogTypeFnLangItem, // 22
63-
FailFnLangItem, // 23
64-
FailBoundsCheckFnLangItem, // 24
65-
ExchangeMallocFnLangItem, // 25
66-
ExchangeFreeFnLangItem, // 26
67-
MallocFnLangItem, // 27
68-
FreeFnLangItem, // 28
69-
BorrowAsImmFnLangItem, // 29
70-
BorrowAsMutFnLangItem, // 30
71-
ReturnToMutFnLangItem, // 31
72-
CheckNotBorrowedFnLangItem, // 32
73-
StrDupUniqFnLangItem, // 33
74-
RecordBorrowFnLangItem, // 34
75-
UnrecordBorrowFnLangItem, // 35
76-
77-
StartFnLangItem, // 36
51+
BitAndTraitLangItem, // 13
52+
BitOrTraitLangItem, // 14
53+
ShlTraitLangItem, // 15
54+
ShrTraitLangItem, // 16
55+
IndexTraitLangItem, // 17
56+
57+
EqTraitLangItem, // 18
58+
OrdTraitLangItem, // 19
59+
60+
StrEqFnLangItem, // 20
61+
UniqStrEqFnLangItem, // 21
62+
AnnihilateFnLangItem, // 22
63+
LogTypeFnLangItem, // 23
64+
FailFnLangItem, // 24
65+
FailBoundsCheckFnLangItem, // 25
66+
ExchangeMallocFnLangItem, // 26
67+
ExchangeFreeFnLangItem, // 27
68+
MallocFnLangItem, // 28
69+
FreeFnLangItem, // 29
70+
BorrowAsImmFnLangItem, // 30
71+
BorrowAsMutFnLangItem, // 31
72+
ReturnToMutFnLangItem, // 32
73+
CheckNotBorrowedFnLangItem, // 33
74+
StrDupUniqFnLangItem, // 34
75+
RecordBorrowFnLangItem, // 35
76+
UnrecordBorrowFnLangItem, // 36
77+
78+
StartFnLangItem, // 37
7879
}
7980

8081
pub struct LanguageItems {
81-
items: [Option<def_id>, ..37]
82+
items: [Option<def_id>, ..38]
8283
}
8384

8485
pub impl LanguageItems {
8586
pub fn new() -> LanguageItems {
8687
LanguageItems {
87-
items: [ None, ..37 ]
88+
items: [ None, ..38 ]
8889
}
8990
}
9091

@@ -97,44 +98,45 @@ pub impl LanguageItems {
9798
0 => "const",
9899
1 => "copy",
99100
2 => "owned",
100-
101-
3 => "drop",
102-
103-
4 => "add",
104-
5 => "sub",
105-
6 => "mul",
106-
7 => "div",
107-
8 => "rem",
108-
9 => "neg",
109-
10 => "not",
110-
11 => "bitxor",
111-
12 => "bitand",
112-
13 => "bitor",
113-
14 => "shl",
114-
15 => "shr",
115-
16 => "index",
116-
17 => "eq",
117-
18 => "ord",
118-
119-
19 => "str_eq",
120-
20 => "uniq_str_eq",
121-
21 => "annihilate",
122-
22 => "log_type",
123-
23 => "fail_",
124-
24 => "fail_bounds_check",
125-
25 => "exchange_malloc",
126-
26 => "exchange_free",
127-
27 => "malloc",
128-
28 => "free",
129-
29 => "borrow_as_imm",
130-
30 => "borrow_as_mut",
131-
31 => "return_to_mut",
132-
32 => "check_not_borrowed",
133-
33 => "strdup_uniq",
134-
34 => "record_borrow",
135-
35 => "unrecord_borrow",
136-
137-
36 => "start",
101+
3 => "sized",
102+
103+
4 => "drop",
104+
105+
5 => "add",
106+
6 => "sub",
107+
7 => "mul",
108+
8 => "div",
109+
9 => "rem",
110+
10 => "neg",
111+
11 => "not",
112+
12 => "bitxor",
113+
13 => "bitand",
114+
14 => "bitor",
115+
15 => "shl",
116+
16 => "shr",
117+
17 => "index",
118+
18 => "eq",
119+
19 => "ord",
120+
121+
20 => "str_eq",
122+
21 => "uniq_str_eq",
123+
22 => "annihilate",
124+
23 => "log_type",
125+
24 => "fail_",
126+
25 => "fail_bounds_check",
127+
26 => "exchange_malloc",
128+
27 => "exchange_free",
129+
28 => "malloc",
130+
29 => "free",
131+
30 => "borrow_as_imm",
132+
31 => "borrow_as_mut",
133+
32 => "return_to_mut",
134+
33 => "check_not_borrowed",
135+
34 => "strdup_uniq",
136+
35 => "record_borrow",
137+
36 => "unrecord_borrow",
138+
139+
37 => "start",
138140

139141
_ => "???"
140142
}
@@ -151,6 +153,9 @@ pub impl LanguageItems {
151153
pub fn owned_trait(&const self) -> def_id {
152154
self.items[OwnedTraitLangItem as uint].get()
153155
}
156+
pub fn sized_trait(&const self) -> def_id {
157+
self.items[SizedTraitLangItem as uint].get()
158+
}
154159

155160
pub fn drop_trait(&const self) -> def_id {
156161
self.items[DropTraitLangItem as uint].get()
@@ -267,6 +272,7 @@ fn LanguageItemCollector(crate: @crate,
267272
item_refs.insert(@~"const", ConstTraitLangItem as uint);
268273
item_refs.insert(@~"copy", CopyTraitLangItem as uint);
269274
item_refs.insert(@~"owned", OwnedTraitLangItem as uint);
275+
item_refs.insert(@~"sized", SizedTraitLangItem as uint);
270276

271277
item_refs.insert(@~"drop", DropTraitLangItem as uint);
272278

src/librustc/middle/ty.rs

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ pub enum BuiltinBound {
683683
BoundStatic,
684684
BoundOwned,
685685
BoundConst,
686+
BoundSized,
686687
}
687688

688689
pub fn EmptyBuiltinBounds() -> BuiltinBounds {
@@ -695,6 +696,7 @@ pub fn AllBuiltinBounds() -> BuiltinBounds {
695696
set.add(BoundStatic);
696697
set.add(BoundOwned);
697698
set.add(BoundConst);
699+
set.add(BoundSized);
698700
set
699701
}
700702

@@ -1826,7 +1828,8 @@ pub impl TypeContents {
18261828
BoundCopy => self.is_copy(cx),
18271829
BoundStatic => self.is_static(cx),
18281830
BoundConst => self.is_const(cx),
1829-
BoundOwned => self.is_owned(cx)
1831+
BoundOwned => self.is_owned(cx),
1832+
BoundSized => self.is_sized(cx),
18301833
}
18311834
}
18321835

@@ -1871,6 +1874,14 @@ pub impl TypeContents {
18711874
TC_MUTABLE
18721875
}
18731876

1877+
fn is_sized(&self, cx: ctxt) -> bool {
1878+
!self.intersects(TypeContents::dynamically_sized(cx))
1879+
}
1880+
1881+
fn dynamically_sized(_cx: ctxt) -> TypeContents {
1882+
TC_DYNAMIC_SIZE
1883+
}
1884+
18741885
fn moves_by_default(&self, cx: ctxt) -> bool {
18751886
self.intersects(TypeContents::nonimplicitly_copyable(cx))
18761887
}
@@ -1944,8 +1955,11 @@ static TC_EMPTY_ENUM: TypeContents = TypeContents{bits: 0b0010_0000_0000};
19441955
/// Contains a type marked with `#[non_owned]`
19451956
static TC_NON_OWNED: TypeContents = TypeContents{bits: 0b0100_0000_0000};
19461957

1958+
/// Is a bare vector, str, function, trait, etc (only relevant at top level).
1959+
static TC_DYNAMIC_SIZE: TypeContents = TypeContents{bits: 0b1000_0000_0000};
1960+
19471961
/// All possible contents.
1948-
static TC_ALL: TypeContents = TypeContents{bits: 0b0111_1111_1111};
1962+
static TC_ALL: TypeContents = TypeContents{bits: 0b1111_1111_1111};
19491963

19501964
pub fn type_is_copyable(cx: ctxt, t: ty::t) -> bool {
19511965
type_contents(cx, t).is_copy(cx)
@@ -2029,7 +2043,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
20292043
}
20302044

20312045
ty_box(mt) => {
2032-
TC_MANAGED + nonowned(tc_mt(cx, mt, cache))
2046+
TC_MANAGED + statically_sized(nonowned(tc_mt(cx, mt, cache)))
20332047
}
20342048

20352049
ty_trait(_, _, UniqTraitStore, _) => {
@@ -2049,28 +2063,35 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
20492063

20502064
ty_rptr(r, mt) => {
20512065
borrowed_contents(r, mt.mutbl) +
2052-
nonowned(tc_mt(cx, mt, cache))
2066+
statically_sized(nonowned(tc_mt(cx, mt, cache)))
20532067
}
20542068

20552069
ty_uniq(mt) => {
2056-
TC_OWNED_POINTER + tc_mt(cx, mt, cache)
2070+
TC_OWNED_POINTER + statically_sized(tc_mt(cx, mt, cache))
20572071
}
20582072

20592073
ty_evec(mt, vstore_uniq) => {
2060-
TC_OWNED_VEC + tc_mt(cx, mt, cache)
2074+
TC_OWNED_VEC + statically_sized(tc_mt(cx, mt, cache))
20612075
}
20622076

20632077
ty_evec(mt, vstore_box) => {
2064-
TC_MANAGED + nonowned(tc_mt(cx, mt, cache))
2078+
TC_MANAGED + statically_sized(nonowned(tc_mt(cx, mt, cache)))
20652079
}
20662080

20672081
ty_evec(mt, vstore_slice(r)) => {
20682082
borrowed_contents(r, mt.mutbl) +
2069-
nonowned(tc_mt(cx, mt, cache))
2083+
statically_sized(nonowned(tc_mt(cx, mt, cache)))
20702084
}
20712085

20722086
ty_evec(mt, vstore_fixed(_)) => {
2073-
tc_mt(cx, mt, cache)
2087+
let contents = tc_mt(cx, mt, cache);
2088+
// FIXME(#6308) Uncomment this when construction of such
2089+
// vectors is prevented earlier in compilation.
2090+
// if !contents.is_sized(cx) {
2091+
// cx.sess.bug("Fixed-length vector of unsized type \
2092+
// should be impossible");
2093+
// }
2094+
contents
20742095
}
20752096

20762097
ty_estr(vstore_box) => {
@@ -2145,7 +2166,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
21452166
}
21462167

21472168
ty_opaque_box => TC_MANAGED,
2148-
ty_unboxed_vec(mt) => tc_mt(cx, mt, cache),
2169+
ty_unboxed_vec(mt) => TC_DYNAMIC_SIZE + tc_mt(cx, mt, cache),
21492170
ty_opaque_closure_ptr(sigil) => {
21502171
match sigil {
21512172
ast::BorrowedSigil => TC_BORROWED_POINTER,
@@ -2212,6 +2233,14 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
22122233
TypeContents {bits: pointee.bits & mask}
22132234
}
22142235

2236+
fn statically_sized(pointee: TypeContents) -> TypeContents {
2237+
/*!
2238+
* If a dynamically-sized type is found behind a pointer, we should
2239+
* restore the 'Sized' kind to the pointer and things that contain it.
2240+
*/
2241+
TypeContents {bits: pointee.bits & !TC_DYNAMIC_SIZE.bits}
2242+
}
2243+
22152244
fn closure_contents(cty: &ClosureTy) -> TypeContents {
22162245
let st = match cty.sigil {
22172246
ast::BorrowedSigil => TC_BORROWED_POINTER,
@@ -2240,6 +2269,8 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
22402269
BoundStatic => TypeContents::nonstatic(cx),
22412270
BoundOwned => TypeContents::nonowned(cx),
22422271
BoundConst => TypeContents::nonconst(cx),
2272+
// The dynamic-size bit can be removed at pointer-level, etc.
2273+
BoundSized => TypeContents::dynamically_sized(cx),
22432274
};
22442275
}
22452276

@@ -2509,6 +2540,21 @@ pub fn type_is_enum(ty: t) -> bool {
25092540
}
25102541
}
25112542

2543+
// Is the type's representation size known at compile time?
2544+
pub fn type_is_sized(cx: ctxt, ty: ty::t) -> bool {
2545+
match get(ty).sty {
2546+
// FIXME(#6308) add trait, vec, str, etc here.
2547+
ty_param(p) => {
2548+
let param_def = cx.ty_param_defs.get(&p.def_id.node);
2549+
if param_def.bounds.builtin_bounds.contains_elem(BoundSized) {
2550+
return true;
2551+
}
2552+
return false;
2553+
},
2554+
_ => return true,
2555+
}
2556+
}
2557+
25122558
// Whether a type is enum like, that is a enum type with only nullary
25132559
// constructors
25142560
pub fn type_is_c_like_enum(cx: ctxt, ty: t) -> bool {

src/librustc/middle/typeck/astconv.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,9 @@ pub fn try_add_builtin_trait(tcx: ty::ctxt,
782782
} else if trait_def_id == li.const_trait() {
783783
builtin_bounds.add(ty::BoundConst);
784784
true
785+
} else if trait_def_id == li.sized_trait() {
786+
builtin_bounds.add(ty::BoundSized);
787+
true
785788
} else {
786789
false
787790
}

0 commit comments

Comments
 (0)