Skip to content

Commit 516ece6

Browse files
committed
auto merge of #18645 : nick29581/rust/coercions-1, r=alexcrichton
r? (I realise this needs a rebase, but I will probably have to chop it up in order to land and I'd like to get r+ first so I can do that quicker)
2 parents fcb1523 + 225de0d commit 516ece6

File tree

10 files changed

+37
-72
lines changed

10 files changed

+37
-72
lines changed

src/librustc/middle/typeck/infer/coercion.rs

-50
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,6 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
9393
a.repr(self.get_ref().infcx.tcx),
9494
b.repr(self.get_ref().infcx.tcx));
9595

96-
// Special case: if the subtype is a sized array literal (`[T, ..n]`),
97-
// then it would get auto-borrowed to `&[T, ..n]` and then DST-ified
98-
// to `&[T]`. Doing it all at once makes the target code a bit more
99-
// efficient and spares us from having to handle multiple coercions.
100-
match ty::get(b).sty {
101-
ty::ty_ptr(mt_b) | ty::ty_rptr(_, mt_b) => {
102-
match ty::get(mt_b.ty).sty {
103-
ty::ty_vec(_, None) => {
104-
let unsize_and_ref = self.unpack_actual_value(a, |sty_a| {
105-
self.coerce_unsized_with_borrow(a, sty_a, b, mt_b.mutbl)
106-
});
107-
if unsize_and_ref.is_ok() {
108-
return unsize_and_ref;
109-
}
110-
}
111-
_ => {}
112-
}
113-
}
114-
_ => {}
115-
}
116-
11796
// Consider coercing the subtype to a DST
11897
let unsize = self.unpack_actual_value(a, |sty_a| {
11998
self.coerce_unsized(a, sty_a, b)
@@ -274,35 +253,6 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
274253
})))
275254
}
276255

277-
// [T, ..n] -> &[T] or &mut [T]
278-
fn coerce_unsized_with_borrow(&self,
279-
a: ty::t,
280-
sty_a: &ty::sty,
281-
b: ty::t,
282-
mutbl_b: ast::Mutability)
283-
-> CoerceResult {
284-
debug!("coerce_unsized_with_borrow(a={}, sty_a={}, b={})",
285-
a.repr(self.get_ref().infcx.tcx), sty_a,
286-
b.repr(self.get_ref().infcx.tcx));
287-
288-
match *sty_a {
289-
ty::ty_vec(t_a, Some(len)) => {
290-
let sub = Sub(self.get_ref().clone());
291-
let coercion = Coercion(self.get_ref().trace.clone());
292-
let r_borrow = self.get_ref().infcx.next_region_var(coercion);
293-
let unsized_ty = ty::mk_slice(self.get_ref().infcx.tcx, r_borrow,
294-
mt {ty: t_a, mutbl: mutbl_b});
295-
try!(self.get_ref().infcx.try(|| sub.tys(unsized_ty, b)));
296-
Ok(Some(AdjustDerefRef(AutoDerefRef {
297-
autoderefs: 0,
298-
autoref: Some(ty::AutoPtr(r_borrow,
299-
mutbl_b,
300-
Some(box AutoUnsize(ty::UnsizeLength(len)))))
301-
})))
302-
}
303-
_ => Err(ty::terr_mismatch)
304-
}
305-
}
306256

307257
// &[T, ..n] or &mut [T, ..n] -> &[T]
308258
// or &mut [T, ..n] -> &mut [T]

src/libstd/os.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub fn getcwd() -> Path {
140140
panic!();
141141
}
142142
}
143-
Path::new(String::from_utf16(::str::truncate_utf16_at_nul(buf))
143+
Path::new(String::from_utf16(::str::truncate_utf16_at_nul(&buf))
144144
.expect("GetCurrentDirectoryW returned invalid UTF-16"))
145145
}
146146

@@ -2065,12 +2065,12 @@ mod tests {
20652065
join_paths(input).unwrap().as_slice() == output.as_bytes()
20662066
}
20672067

2068-
assert!(test_eq([], ""));
2069-
assert!(test_eq([r"c:\windows", r"c:\"],
2068+
assert!(test_eq(&[], ""));
2069+
assert!(test_eq(&[r"c:\windows", r"c:\"],
20702070
r"c:\windows;c:\"));
2071-
assert!(test_eq(["", r"c:\windows", "", "", r"c:\", ""],
2071+
assert!(test_eq(&["", r"c:\windows", "", "", r"c:\", ""],
20722072
r";c:\windows;;;c:\;"));
2073-
assert!(test_eq([r"c:\te;st", r"c:\"],
2073+
assert!(test_eq(&[r"c:\te;st", r"c:\"],
20742074
r#""c:\te;st";c:\"#));
20752075
assert!(join_paths(&[r#"c:\te"st"#]).is_err());
20762076
}

src/libstd/rand/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,12 @@ mod imp {
298298
impl Rng for OsRng {
299299
fn next_u32(&mut self) -> u32 {
300300
let mut v = [0u8, .. 4];
301-
self.fill_bytes(v);
301+
self.fill_bytes(&mut v);
302302
unsafe { mem::transmute(v) }
303303
}
304304
fn next_u64(&mut self) -> u64 {
305305
let mut v = [0u8, .. 8];
306-
self.fill_bytes(v);
306+
self.fill_bytes(&mut v);
307307
unsafe { mem::transmute(v) }
308308
}
309309
fn fill_bytes(&mut self, v: &mut [u8]) {

src/libstd/rt/backtrace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ mod imp {
999999
None => try!(w.write(bytes[..bytes.len()-1])),
10001000
}
10011001
}
1002-
try!(w.write(['\n' as u8]));
1002+
try!(w.write(&['\n' as u8]));
10031003
}
10041004

10051005
Ok(())

src/libstd/sys/windows/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ pub fn readdir(p: &Path) -> IoResult<Vec<Path>> {
263263
let mut more_files = 1 as libc::BOOL;
264264
while more_files != 0 {
265265
{
266-
let filename = str::truncate_utf16_at_nul(wfd.cFileName);
266+
let filename = str::truncate_utf16_at_nul(&wfd.cFileName);
267267
match String::from_utf16(filename) {
268268
Some(filename) => paths.push(Path::new(filename)),
269269
None => {

src/libstd/sys/windows/os.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub fn error_string(errnum: i32) -> String {
7676
return format!("OS Error {} (FormatMessageW() returned error {})", errnum, fm_err);
7777
}
7878

79-
let msg = String::from_utf16(::str::truncate_utf16_at_nul(buf));
79+
let msg = String::from_utf16(::str::truncate_utf16_at_nul(&buf));
8080
match msg {
8181
Some(msg) => format!("OS Error {}: {}", errnum, msg),
8282
None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", errnum),

src/libstd/sys/windows/pipe.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ impl UnixStream {
395395
loop {
396396
// Process a timeout if one is pending
397397
let wait_succeeded = await(self.handle(), self.read_deadline,
398-
[overlapped.hEvent]);
398+
&[overlapped.hEvent]);
399399

400400
let ret = unsafe {
401401
libc::GetOverlappedResult(self.handle(),
@@ -459,7 +459,7 @@ impl UnixStream {
459459
}
460460
// Process a timeout if one is pending
461461
let wait_succeeded = await(self.handle(), self.write_deadline,
462-
[overlapped.hEvent]);
462+
&[overlapped.hEvent]);
463463
let ret = unsafe {
464464
libc::GetOverlappedResult(self.handle(),
465465
&mut overlapped,
@@ -660,8 +660,8 @@ impl UnixAcceptor {
660660
if err == libc::ERROR_IO_PENDING as libc::DWORD {
661661
// Process a timeout if one is pending
662662
let wait_succeeded = await(handle, self.deadline,
663-
[self.inner.abort.handle(),
664-
overlapped.hEvent]);
663+
&[self.inner.abort.handle(),
664+
overlapped.hEvent]);
665665

666666
// This will block until the overlapped I/O is completed. The
667667
// timeout was previously handled, so this will either block in

src/libstd/sys/windows/process.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -487,24 +487,24 @@ mod tests {
487487
}
488488

489489
assert_eq!(
490-
test_wrapper("prog", ["aaa", "bbb", "ccc"]),
490+
test_wrapper("prog", &["aaa", "bbb", "ccc"]),
491491
"prog aaa bbb ccc".to_string()
492492
);
493493

494494
assert_eq!(
495-
test_wrapper("C:\\Program Files\\blah\\blah.exe", ["aaa"]),
495+
test_wrapper("C:\\Program Files\\blah\\blah.exe", &["aaa"]),
496496
"\"C:\\Program Files\\blah\\blah.exe\" aaa".to_string()
497497
);
498498
assert_eq!(
499-
test_wrapper("C:\\Program Files\\test", ["aa\"bb"]),
499+
test_wrapper("C:\\Program Files\\test", &["aa\"bb"]),
500500
"\"C:\\Program Files\\test\" aa\\\"bb".to_string()
501501
);
502502
assert_eq!(
503-
test_wrapper("echo", ["a b c"]),
503+
test_wrapper("echo", &["a b c"]),
504504
"echo \"a b c\"".to_string()
505505
);
506506
assert_eq!(
507-
test_wrapper("\u03c0\u042f\u97f3\u00e6\u221e", []),
507+
test_wrapper("\u03c0\u042f\u97f3\u00e6\u221e", &[]),
508508
"\u03c0\u042f\u97f3\u00e6\u221e".to_string()
509509
);
510510
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Tests that we forbid coercion from `[T, ..n]` to `&[T]`
12+
13+
fn main() {
14+
let _: &[int] = [0i]; //~ERROR: mismatched types: expected `&[int]`, found `[int, ..1]`
15+
}

src/test/run-pass/tcp-stress.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn main() {
4545
}
4646
};
4747
stream.read_byte();
48-
stream.write([2]);
48+
stream.write(&[2]);
4949
}
5050
});
5151
let addr = rx.recv();
@@ -57,9 +57,9 @@ fn main() {
5757
match TcpStream::connect(addr) {
5858
Ok(stream) => {
5959
let mut stream = stream;
60-
stream.write([1]);
60+
stream.write(&[1]);
6161
let mut buf = [0];
62-
stream.read(buf);
62+
stream.read(&mut buf);
6363
},
6464
Err(e) => debug!("{}", e)
6565
}

0 commit comments

Comments
 (0)