Skip to content

Commit 80d95f9

Browse files
committed
---
yaml --- r: 148814 b: refs/heads/try2 c: 2bcd951 h: refs/heads/master v: v3
1 parent 181eec1 commit 80d95f9

File tree

12 files changed

+141
-86
lines changed

12 files changed

+141
-86
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 212507413a2768ec4b6a072dde73d60527c2beee
8+
refs/heads/try2: 2bcd951749b67402ccaa31f1bb0349656f880fe2
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libextra/ringbuf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> {
303303
&mut self.remaining2
304304
};
305305
self.nelts -= 1;
306-
Some(r.mut_shift_ref().get_mut_ref())
306+
Some(r.mut_shift_ref().unwrap().get_mut_ref())
307307
}
308308

309309
#[inline]
@@ -325,7 +325,7 @@ impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> {
325325
&mut self.remaining1
326326
};
327327
self.nelts -= 1;
328-
Some(r.mut_pop_ref().get_mut_ref())
328+
Some(r.mut_pop_ref().unwrap().get_mut_ref())
329329
}
330330
}
331331

branches/try2/src/librustc/metadata/common.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub static tag_items_data_item_variant: uint = 0x0eu;
3535

3636
pub static tag_items_data_parent_item: uint = 0x0fu;
3737

38+
pub static tag_items_data_item_is_tuple_struct_ctor: uint = 0x10u;
39+
3840
pub static tag_index: uint = 0x11u;
3941

4042
pub static tag_index_buckets: uint = 0x12u;

branches/try2/src/librustc/metadata/decoder.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,26 @@ pub fn get_static_methods_if_impl(intr: @IdentInterner,
980980
return Some(static_impl_methods);
981981
}
982982

983+
/// If node_id is the constructor of a tuple struct, retrieve the NodeId of
984+
/// the actual type definition, otherwise, return None
985+
pub fn get_tuple_struct_definition_if_ctor(cdata: Cmd,
986+
node_id: ast::NodeId) -> Option<ast::NodeId> {
987+
let item = lookup_item(node_id, cdata.data());
988+
let mut ret = None;
989+
reader::tagged_docs(item, tag_items_data_item_is_tuple_struct_ctor, |_| {
990+
ret = Some(item_reqd_and_translated_parent_item(cdata.cnum, item));
991+
false
992+
});
993+
ret.map(|x| x.node)
994+
}
995+
983996
pub fn get_item_attrs(cdata: Cmd,
984997
node_id: ast::NodeId,
985998
f: |~[@ast::MetaItem]|) {
999+
// The attributes for a tuple struct are attached to the definition, not the ctor;
1000+
// we assume that someone passing in a tuple struct ctor is actually wanting to
1001+
// look at the definition
1002+
let node_id = get_tuple_struct_definition_if_ctor(cdata, node_id).unwrap_or(node_id);
9861003
let item = lookup_item(node_id, cdata.data());
9871004
reader::tagged_docs(item, tag_attributes, |attributes| {
9881005
reader::tagged_docs(attributes, tag_attribute, |attribute| {

branches/try2/src/librustc/metadata/encoder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,12 @@ fn encode_info_for_struct_ctor(ecx: &EncodeContext,
777777
encode_symbol(ecx, ebml_w, ctor_id);
778778
}
779779

780+
// indicate that this is a tuple struct ctor, because downstream users will normally want
781+
// the tuple struct definition, but without this there is no way for them to tell that
782+
// they actually have a ctor rather than a normal function
783+
ebml_w.start_tag(tag_items_data_item_is_tuple_struct_ctor);
784+
ebml_w.end_tag();
785+
780786
ebml_w.end_tag();
781787
}
782788

branches/try2/src/librustc/middle/trans/cleanup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ impl<'a> CleanupHelperMethods<'a> for FunctionContext<'a> {
662662
// Check if a landing pad block exists; if not, create one.
663663
{
664664
let mut scopes = self.scopes.borrow_mut();
665-
let last_scope = scopes.get().mut_last();
665+
let last_scope = scopes.get().mut_last().unwrap();
666666
match last_scope.cached_landing_pad {
667667
Some(llbb) => { return llbb; }
668668
None => {

branches/try2/src/libstd/io/extensions.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,16 @@ impl<'r, R: Reader> Iterator<u8> for Bytes<'r, R> {
5151
}
5252

5353
pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
54+
use unstable::intrinsics::{to_le16, to_le32, to_le64};
55+
use cast::transmute;
56+
57+
// LLVM fails to properly optimize this when using shifts instead of the to_le* intrinsics
5458
assert!(size <= 8u);
5559
match size {
5660
1u => f(&[n as u8]),
57-
2u => f(&[n as u8,
58-
(n >> 8) as u8]),
59-
4u => f(&[n as u8,
60-
(n >> 8) as u8,
61-
(n >> 16) as u8,
62-
(n >> 24) as u8]),
63-
8u => f(&[n as u8,
64-
(n >> 8) as u8,
65-
(n >> 16) as u8,
66-
(n >> 24) as u8,
67-
(n >> 32) as u8,
68-
(n >> 40) as u8,
69-
(n >> 48) as u8,
70-
(n >> 56) as u8]),
61+
2u => f(unsafe { transmute::<i16, [u8, ..2]>(to_le16(n as i16)) }),
62+
4u => f(unsafe { transmute::<i32, [u8, ..4]>(to_le32(n as i32)) }),
63+
8u => f(unsafe { transmute::<i64, [u8, ..8]>(to_le64(n as i64)) }),
7164
_ => {
7265

7366
let mut bytes: ~[u8] = ~[];
@@ -84,23 +77,16 @@ pub fn u64_to_le_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
8477
}
8578

8679
pub fn u64_to_be_bytes<T>(n: u64, size: uint, f: |v: &[u8]| -> T) -> T {
80+
use unstable::intrinsics::{to_be16, to_be32, to_be64};
81+
use cast::transmute;
82+
83+
// LLVM fails to properly optimize this when using shifts instead of the to_be* intrinsics
8784
assert!(size <= 8u);
8885
match size {
8986
1u => f(&[n as u8]),
90-
2u => f(&[(n >> 8) as u8,
91-
n as u8]),
92-
4u => f(&[(n >> 24) as u8,
93-
(n >> 16) as u8,
94-
(n >> 8) as u8,
95-
n as u8]),
96-
8u => f(&[(n >> 56) as u8,
97-
(n >> 48) as u8,
98-
(n >> 40) as u8,
99-
(n >> 32) as u8,
100-
(n >> 24) as u8,
101-
(n >> 16) as u8,
102-
(n >> 8) as u8,
103-
n as u8]),
87+
2u => f(unsafe { transmute::<i16, [u8, ..2]>(to_be16(n as i16)) }),
88+
4u => f(unsafe { transmute::<i32, [u8, ..4]>(to_be32(n as i32)) }),
89+
8u => f(unsafe { transmute::<i64, [u8, ..8]>(to_be64(n as i64)) }),
10490
_ => {
10591
let mut bytes: ~[u8] = ~[];
10692
let mut i = size;

branches/try2/src/libstd/vec.rs

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -953,14 +953,15 @@ pub trait ImmutableVector<'a, T> {
953953
* Equivalent to:
954954
*
955955
* ```
956+
* if self.len() == 0 { return None }
956957
* let head = &self[0];
957958
* *self = self.slice_from(1);
958-
* head
959+
* Some(head)
959960
* ```
960961
*
961-
* Fails if slice is empty.
962+
* Returns `None` if vector is empty
962963
*/
963-
fn shift_ref(&mut self) -> &'a T;
964+
fn shift_ref(&mut self) -> Option<&'a T>;
964965

965966
/**
966967
* Returns a mutable reference to the last element in this slice
@@ -970,14 +971,15 @@ pub trait ImmutableVector<'a, T> {
970971
* Equivalent to:
971972
*
972973
* ```
974+
* if self.len() == 0 { return None; }
973975
* let tail = &self[self.len() - 1];
974976
* *self = self.slice_to(self.len() - 1);
975-
* tail
977+
* Some(tail)
976978
* ```
977979
*
978-
* Fails if slice is empty.
980+
* Returns `None` if slice is empty.
979981
*/
980-
fn pop_ref(&mut self) -> &'a T;
982+
fn pop_ref(&mut self) -> Option<&'a T>;
981983
}
982984

983985
impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
@@ -1136,17 +1138,19 @@ impl<'a,T> ImmutableVector<'a, T> for &'a [T] {
11361138
self.iter().map(f).collect()
11371139
}
11381140

1139-
fn shift_ref(&mut self) -> &'a T {
1141+
fn shift_ref(&mut self) -> Option<&'a T> {
1142+
if self.len() == 0 { return None; }
11401143
unsafe {
11411144
let s: &mut Slice<T> = cast::transmute(self);
1142-
&*raw::shift_ptr(s)
1145+
Some(&*raw::shift_ptr(s))
11431146
}
11441147
}
11451148

1146-
fn pop_ref(&mut self) -> &'a T {
1149+
fn pop_ref(&mut self) -> Option<&'a T> {
1150+
if self.len() == 0 { return None; }
11471151
unsafe {
11481152
let s: &mut Slice<T> = cast::transmute(self);
1149-
&*raw::pop_ptr(s)
1153+
Some(&*raw::pop_ptr(s))
11501154
}
11511155
}
11521156
}
@@ -1981,7 +1985,7 @@ pub trait MutableVector<'a, T> {
19811985
fn mut_iter(self) -> MutItems<'a, T>;
19821986

19831987
/// Returns a mutable pointer to the last item in the vector.
1984-
fn mut_last(self) -> &'a mut T;
1988+
fn mut_last(self) -> Option<&'a mut T>;
19851989

19861990
/// Returns a reversed iterator that allows modifying each value
19871991
fn mut_rev_iter(self) -> RevMutItems<'a, T>;
@@ -2011,14 +2015,15 @@ pub trait MutableVector<'a, T> {
20112015
* Equivalent to:
20122016
*
20132017
* ```
2018+
* if self.len() == 0 { return None; }
20142019
* let head = &mut self[0];
20152020
* *self = self.mut_slice_from(1);
2016-
* head
2021+
* Some(head)
20172022
* ```
20182023
*
2019-
* Fails if slice is empty.
2024+
* Returns `None` if slice is empty
20202025
*/
2021-
fn mut_shift_ref(&mut self) -> &'a mut T;
2026+
fn mut_shift_ref(&mut self) -> Option<&'a mut T>;
20222027

20232028
/**
20242029
* Returns a mutable reference to the last element in this slice
@@ -2028,14 +2033,15 @@ pub trait MutableVector<'a, T> {
20282033
* Equivalent to:
20292034
*
20302035
* ```
2036+
* if self.len() == 0 { return None; }
20312037
* let tail = &mut self[self.len() - 1];
20322038
* *self = self.mut_slice_to(self.len() - 1);
2033-
* tail
2039+
* Some(tail)
20342040
* ```
20352041
*
2036-
* Fails if slice is empty.
2042+
* Returns `None` if slice is empty.
20372043
*/
2038-
fn mut_pop_ref(&mut self) -> &'a mut T;
2044+
fn mut_pop_ref(&mut self) -> Option<&'a mut T>;
20392045

20402046
/// Swaps two elements in a vector.
20412047
///
@@ -2246,10 +2252,10 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
22462252
}
22472253

22482254
#[inline]
2249-
fn mut_last(self) -> &'a mut T {
2255+
fn mut_last(self) -> Option<&'a mut T> {
22502256
let len = self.len();
2251-
if len == 0 { fail!("mut_last: empty vector") }
2252-
&mut self[len - 1]
2257+
if len == 0 { return None; }
2258+
Some(&mut self[len - 1])
22532259
}
22542260

22552261
#[inline]
@@ -2268,17 +2274,19 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
22682274
MutChunks { v: self, chunk_size: chunk_size }
22692275
}
22702276

2271-
fn mut_shift_ref(&mut self) -> &'a mut T {
2277+
fn mut_shift_ref(&mut self) -> Option<&'a mut T> {
2278+
if self.len() == 0 { return None; }
22722279
unsafe {
22732280
let s: &mut Slice<T> = cast::transmute(self);
2274-
cast::transmute_mut(&*raw::shift_ptr(s))
2281+
Some(cast::transmute_mut(&*raw::shift_ptr(s)))
22752282
}
22762283
}
22772284

2278-
fn mut_pop_ref(&mut self) -> &'a mut T {
2285+
fn mut_pop_ref(&mut self) -> Option<&'a mut T> {
2286+
if self.len() == 0 { return None; }
22792287
unsafe {
22802288
let s: &mut Slice<T> = cast::transmute(self);
2281-
cast::transmute_mut(&*raw::pop_ptr(s))
2289+
Some(cast::transmute_mut(&*raw::pop_ptr(s)))
22822290
}
22832291
}
22842292

@@ -4135,34 +4143,26 @@ mod tests {
41354143
fn test_shift_ref() {
41364144
let mut x: &[int] = [1, 2, 3, 4, 5];
41374145
let h = x.shift_ref();
4138-
assert_eq!(*h, 1);
4146+
assert_eq!(*h.unwrap(), 1);
41394147
assert_eq!(x.len(), 4);
41404148
assert_eq!(x[0], 2);
41414149
assert_eq!(x[3], 5);
4142-
}
41434150

4144-
#[test]
4145-
#[should_fail]
4146-
fn test_shift_ref_empty() {
4147-
let mut x: &[int] = [];
4148-
x.shift_ref();
4151+
let mut y: &[int] = [];
4152+
assert_eq!(y.shift_ref(), None);
41494153
}
41504154

41514155
#[test]
41524156
fn test_pop_ref() {
41534157
let mut x: &[int] = [1, 2, 3, 4, 5];
41544158
let h = x.pop_ref();
4155-
assert_eq!(*h, 5);
4159+
assert_eq!(*h.unwrap(), 5);
41564160
assert_eq!(x.len(), 4);
41574161
assert_eq!(x[0], 1);
41584162
assert_eq!(x[3], 4);
4159-
}
41604163

4161-
#[test]
4162-
#[should_fail]
4163-
fn test_pop_ref_empty() {
4164-
let mut x: &[int] = [];
4165-
x.pop_ref();
4164+
let mut y: &[int] = [];
4165+
assert!(y.pop_ref().is_none());
41664166
}
41674167

41684168
#[test]
@@ -4225,34 +4225,36 @@ mod tests {
42254225
fn test_mut_shift_ref() {
42264226
let mut x: &mut [int] = [1, 2, 3, 4, 5];
42274227
let h = x.mut_shift_ref();
4228-
assert_eq!(*h, 1);
4228+
assert_eq!(*h.unwrap(), 1);
42294229
assert_eq!(x.len(), 4);
42304230
assert_eq!(x[0], 2);
42314231
assert_eq!(x[3], 5);
4232-
}
42334232

4234-
#[test]
4235-
#[should_fail]
4236-
fn test_mut_shift_ref_empty() {
4237-
let mut x: &mut [int] = [];
4238-
x.mut_shift_ref();
4233+
let mut y: &mut [int] = [];
4234+
assert!(y.mut_shift_ref().is_none());
42394235
}
42404236

42414237
#[test]
42424238
fn test_mut_pop_ref() {
42434239
let mut x: &mut [int] = [1, 2, 3, 4, 5];
42444240
let h = x.mut_pop_ref();
4245-
assert_eq!(*h, 5);
4241+
assert_eq!(*h.unwrap(), 5);
42464242
assert_eq!(x.len(), 4);
42474243
assert_eq!(x[0], 1);
42484244
assert_eq!(x[3], 4);
4245+
4246+
let mut y: &mut [int] = [];
4247+
assert!(y.mut_pop_ref().is_none());
42494248
}
42504249

42514250
#[test]
4252-
#[should_fail]
4253-
fn test_mut_pop_ref_empty() {
4254-
let mut x: &mut [int] = [];
4255-
x.mut_pop_ref();
4251+
fn test_mut_last() {
4252+
let mut x = [1, 2, 3, 4, 5];
4253+
let h = x.mut_last();
4254+
assert_eq!(*h.unwrap(), 5);
4255+
4256+
let mut y: &mut [int] = [];
4257+
assert!(y.mut_last().is_none());
42564258
}
42574259
}
42584260

branches/try2/src/libsyntax/opt_vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ impl<T> OptVec<T> {
6262
}
6363
}
6464

65-
pub fn mut_last<'a>(&'a mut self) -> &'a mut T {
65+
pub fn mut_last<'a>(&'a mut self) -> Option<&'a mut T> {
6666
match *self {
6767
Vec(ref mut v) => v.mut_last(),
68-
Empty => fail!("mut_last on empty opt_vec")
68+
Empty => None
6969
}
7070
}
7171

0 commit comments

Comments
 (0)