From 3e626375d8d2226a203bf6ea6e98dab14774c59f Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 4 Aug 2014 14:20:11 +0200 Subject: DST coercions and DST structs [breaking-change] 1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code. 2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible. 3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string. --- src/libstd/path/posix.rs | 93 ++++++++++++++++++++++++++-------------------- src/libstd/path/windows.rs | 85 ++++++++++++++++++++++++------------------ 2 files changed, 100 insertions(+), 78 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index 0a7817c3e00..06eab31d7bf 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -464,6 +464,7 @@ static dot_dot_static: &'static [u8] = b".."; mod tests { use prelude::*; use super::*; + use mem; use str; use str::StrSlice; @@ -621,8 +622,10 @@ mod tests { macro_rules! t( (s: $path:expr, $op:ident, $exp:expr) => ( { - let path = Path::new($path); - assert!(path.$op() == ($exp).as_bytes()); + unsafe { + let path = Path::new($path); + assert!(path.$op() == mem::transmute(($exp).as_bytes())); + } } ); (s: $path:expr, $op:ident, $exp:expr, opt) => ( @@ -634,9 +637,11 @@ mod tests { ); (v: $path:expr, $op:ident, $exp:expr) => ( { - let arg = $path; - let path = Path::new(arg); - assert!(path.$op() == $exp); + unsafe { + let arg = $path; + let path = Path::new(arg); + assert!(path.$op() == mem::transmute($exp)); + } } ); ) @@ -684,8 +689,9 @@ mod tests { t!(v: b"hi/there.txt", extension, Some(b"txt")); t!(v: b"hi/there\x80.txt", extension, Some(b"txt")); t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt")); - t!(v: b"hi/there", extension, None); - t!(v: b"hi/there\x80", extension, None); + let no: Option<&'static [u8]> = None; + t!(v: b"hi/there", extension, no); + t!(v: b"hi/there\x80", extension, no); t!(s: "hi/there.txt", extension, Some("txt"), opt); t!(s: "hi/there", extension, None, opt); t!(s: "there.txt", extension, Some("txt"), opt); @@ -974,57 +980,62 @@ mod tests { macro_rules! t( (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { - let path = $path; - let filename = $filename; - assert!(path.filename_str() == filename, - "{}.filename_str(): Expected `{:?}`, found {:?}", - path.as_str().unwrap(), filename, path.filename_str()); - let dirname = $dirname; - assert!(path.dirname_str() == dirname, - "`{}`.dirname_str(): Expected `{:?}`, found `{:?}`", - path.as_str().unwrap(), dirname, path.dirname_str()); - let filestem = $filestem; - assert!(path.filestem_str() == filestem, - "`{}`.filestem_str(): Expected `{:?}`, found `{:?}`", - path.as_str().unwrap(), filestem, path.filestem_str()); - let ext = $ext; - assert!(path.extension_str() == ext, - "`{}`.extension_str(): Expected `{:?}`, found `{:?}`", - path.as_str().unwrap(), ext, path.extension_str()); + unsafe { + let path = $path; + let filename = $filename; + assert!(path.filename_str() == filename, + "{}.filename_str(): Expected `{:?}`, found {:?}", + path.as_str().unwrap(), filename, path.filename_str()); + let dirname = $dirname; + assert!(path.dirname_str() == dirname, + "`{}`.dirname_str(): Expected `{:?}`, found `{:?}`", + path.as_str().unwrap(), dirname, path.dirname_str()); + let filestem = $filestem; + assert!(path.filestem_str() == filestem, + "`{}`.filestem_str(): Expected `{:?}`, found `{:?}`", + path.as_str().unwrap(), filestem, path.filestem_str()); + let ext = $ext; + assert!(path.extension_str() == mem::transmute(ext), + "`{}`.extension_str(): Expected `{:?}`, found `{:?}`", + path.as_str().unwrap(), ext, path.extension_str()); + } } ); (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { - let path = $path; - assert!(path.filename() == $filename); - assert!(path.dirname() == $dirname); - assert!(path.filestem() == $filestem); - assert!(path.extension() == $ext); + unsafe { + let path = $path; + assert!(path.filename() == mem::transmute($filename)); + assert!(path.dirname() == mem::transmute($dirname)); + assert!(path.filestem() == mem::transmute($filestem)); + assert!(path.extension() == mem::transmute($ext)); + } } ) ) - t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None); - t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None); + let no: Option<&'static str> = None; + t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), no); + t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), no); t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi", Some(b"there"), Some(b"\xFF")); - t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None); - t!(s: Path::new("."), None, Some("."), None, None); - t!(s: Path::new("/"), None, Some("/"), None, None); - t!(s: Path::new(".."), None, Some(".."), None, None); - t!(s: Path::new("../.."), None, Some("../.."), None, None); + t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), no); + t!(s: Path::new("."), None, Some("."), None, no); + t!(s: Path::new("/"), None, Some("/"), None, no); + t!(s: Path::new(".."), None, Some(".."), None, no); + t!(s: Path::new("../.."), None, Some("../.."), None, no); t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"), Some("there"), Some("txt")); - t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), None); + t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), no); t!(s: Path::new("hi/there."), Some("there."), Some("hi"), Some("there"), Some("")); - t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None); + t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), no); t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"), Some("."), Some("there")); - t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, None); + t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, no); t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt")); - t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), None); - t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), None); + t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), no); + t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), no); } #[test] diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index dd87e214c2c..d9864cfaa61 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -1141,6 +1141,7 @@ fn prefix_len(p: Option) -> uint { #[cfg(test)] mod tests { + use mem; use prelude::*; use super::*; use super::parse_prefix; @@ -1383,9 +1384,11 @@ mod tests { macro_rules! t( (s: $path:expr, $op:ident, $exp:expr) => ( { - let path = $path; - let path = Path::new(path); - assert!(path.$op() == Some($exp)); + unsafe { + let path = $path; + let path = Path::new(path); + assert!(path.$op() == Some(mem::transmute($exp))); + } } ); (s: $path:expr, $op:ident, $exp:expr, opt) => ( @@ -1398,9 +1401,11 @@ mod tests { ); (v: $path:expr, $op:ident, $exp:expr) => ( { - let path = $path; - let path = Path::new(path); - assert!(path.$op() == $exp); + unsafe { + let path = $path; + let path = Path::new(path); + assert!(path.$op() == mem::transmute($exp)); + } } ) ) @@ -1485,7 +1490,8 @@ mod tests { // filestem is based on filename, so we don't need the full set of prefix tests t!(v: b"hi\\there.txt", extension, Some(b"txt")); - t!(v: b"hi\\there", extension, None); + let no: Option<&'static [u8]> = None; + t!(v: b"hi\\there", extension, no); t!(s: "hi\\there.txt", extension_str, Some("txt"), opt); t!(s: "hi\\there", extension_str, None, opt); t!(s: "there.txt", extension_str, Some("txt"), opt); @@ -1892,48 +1898,53 @@ mod tests { macro_rules! t( (s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { - let path = $path; - let filename = $filename; - assert!(path.filename_str() == filename, - "`{}`.filename_str(): Expected `{:?}`, found `{:?}`", - path.as_str().unwrap(), filename, path.filename_str()); - let dirname = $dirname; - assert!(path.dirname_str() == dirname, - "`{}`.dirname_str(): Expected `{:?}`, found `{:?}`", - path.as_str().unwrap(), dirname, path.dirname_str()); - let filestem = $filestem; - assert!(path.filestem_str() == filestem, - "`{}`.filestem_str(): Expected `{:?}`, found `{:?}`", - path.as_str().unwrap(), filestem, path.filestem_str()); - let ext = $ext; - assert!(path.extension_str() == ext, - "`{}`.extension_str(): Expected `{:?}`, found `{:?}`", - path.as_str().unwrap(), ext, path.extension_str()); + unsafe { + let path = $path; + let filename = $filename; + assert!(path.filename_str() == filename, + "`{}`.filename_str(): Expected `{:?}`, found `{:?}`", + path.as_str().unwrap(), filename, path.filename_str()); + let dirname = $dirname; + assert!(path.dirname_str() == dirname, + "`{}`.dirname_str(): Expected `{:?}`, found `{:?}`", + path.as_str().unwrap(), dirname, path.dirname_str()); + let filestem = $filestem; + assert!(path.filestem_str() == filestem, + "`{}`.filestem_str(): Expected `{:?}`, found `{:?}`", + path.as_str().unwrap(), filestem, path.filestem_str()); + let ext = $ext; + assert!(path.extension_str() == mem::transmute(ext), + "`{}`.extension_str(): Expected `{:?}`, found `{:?}`", + path.as_str().unwrap(), ext, path.extension_str()); + } } ); (v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => ( { - let path = $path; - assert!(path.filename() == $filename); - assert!(path.dirname() == $dirname); - assert!(path.filestem() == $filestem); - assert!(path.extension() == $ext); + unsafe { + let path = $path; + assert!(path.filename() == mem::transmute($filename)); + assert!(path.dirname() == mem::transmute($dirname)); + assert!(path.filestem() == mem::transmute($filestem)); + assert!(path.extension() == mem::transmute($ext)); + } } ) ) - t!(v: Path::new(b"a\\b\\c"), Some(b"c"), b"a\\b", Some(b"c"), None); - t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), None); - t!(s: Path::new("."), None, Some("."), None, None); - t!(s: Path::new("\\"), None, Some("\\"), None, None); - t!(s: Path::new(".."), None, Some(".."), None, None); - t!(s: Path::new("..\\.."), None, Some("..\\.."), None, None); + let no: Option<&'static str> = None; + t!(v: Path::new(b"a\\b\\c"), Some(b"c"), b"a\\b", Some(b"c"), no); + t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), no); + t!(s: Path::new("."), None, Some("."), None, no); + t!(s: Path::new("\\"), None, Some("\\"), None, no); + t!(s: Path::new(".."), None, Some(".."), None, no); + t!(s: Path::new("..\\.."), None, Some("..\\.."), None, no); t!(s: Path::new("hi\\there.txt"), Some("there.txt"), Some("hi"), Some("there"), Some("txt")); - t!(s: Path::new("hi\\there"), Some("there"), Some("hi"), Some("there"), None); + t!(s: Path::new("hi\\there"), Some("there"), Some("hi"), Some("there"), no); t!(s: Path::new("hi\\there."), Some("there."), Some("hi"), Some("there"), Some("")); - t!(s: Path::new("hi\\.there"), Some(".there"), Some("hi"), Some(".there"), None); + t!(s: Path::new("hi\\.there"), Some(".there"), Some("hi"), Some(".there"), no); t!(s: Path::new("hi\\..there"), Some("..there"), Some("hi"), Some("."), Some("there")); -- cgit 1.4.1-3-g733a5