From 2535804aa0bf2f0b7f230793ece8a5b39b980ec7 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 21 Dec 2017 11:22:18 +0000 Subject: [PATCH 01/21] Allow underscores in camel-case between non-alphabetic characters --- src/Cargo.lock | 8 ++++++++ src/librustc_lint/Cargo.toml | 4 +++- src/librustc_lint/bad_style.rs | 32 +++++++++++++++++++++----------- src/librustc_lint/lib.rs | 3 +++ 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index c22187ee13e8e..26b70f63d6589 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -967,6 +967,11 @@ name = "lazy_static" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lazy_static" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lazycell" version = "0.5.1" @@ -1863,7 +1868,9 @@ dependencies = [ name = "rustc_lint" version = "0.0.0" dependencies = [ + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "rustc 0.0.0", "rustc_const_eval 0.0.0", "syntax 0.0.0", @@ -2813,6 +2820,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum kuchiki 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e03098e8e719c92b7794515dfd5c1724e2b12f5ce1788e61cfa4663f82eba8d8" "checksum languageserver-types 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "773e175c945800aeea4c21c04090bcb9db987b1a566ad9c6f569972299950e3e" "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" +"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" "checksum lazycell 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3b585b7a6811fb03aa10e74b278a0f00f8dd9b45dc681f148bb29fa5cb61859b" "checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0" "checksum libgit2-sys 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "6f74b4959cef96898f5123148724fc7dee043b9a6b99f219d948851bfbe53cb2" diff --git a/src/librustc_lint/Cargo.toml b/src/librustc_lint/Cargo.toml index cebf52d5af7a9..2cc1827f518da 100644 --- a/src/librustc_lint/Cargo.toml +++ b/src/librustc_lint/Cargo.toml @@ -6,7 +6,7 @@ version = "0.0.0" [lib] name = "rustc_lint" path = "lib.rs" -crate-type = ["dylib"] +crate-types = ["rlib", "dylib"] test = false [dependencies] @@ -15,3 +15,5 @@ rustc = { path = "../librustc" } rustc_const_eval = { path = "../librustc_const_eval" } syntax = { path = "../libsyntax" } syntax_pos = { path = "../libsyntax_pos" } +lazy_static = "1.0" +regex = "0.2.3" diff --git a/src/librustc_lint/bad_style.rs b/src/librustc_lint/bad_style.rs index d14a6943fc112..09194d5c7c4ae 100644 --- a/src/librustc_lint/bad_style.rs +++ b/src/librustc_lint/bad_style.rs @@ -21,6 +21,12 @@ use syntax_pos::Span; use rustc::hir::{self, PatKind}; use rustc::hir::intravisit::FnKind; +use regex::Regex; + +lazy_static! { + static ref ALPHABETIC_UNDERSCORE: Regex = Regex::new("([[:alpha:]])_([[:alpha:]])").unwrap(); +} + #[derive(PartialEq)] pub enum MethodLateContext { TraitAutoImpl, @@ -62,20 +68,24 @@ impl NonCamelCaseTypes { // start with a non-lowercase letter rather than non-uppercase // ones (some scripts don't have a concept of upper/lowercase) - !name.is_empty() && !name.chars().next().unwrap().is_lowercase() && !name.contains('_') + !name.is_empty() && !name.chars().next().unwrap().is_lowercase() && + !ALPHABETIC_UNDERSCORE.is_match(name) } fn to_camel_case(s: &str) -> String { - s.split('_') - .flat_map(|word| { - word.chars().enumerate().map(|(i, c)| if i == 0 { - c.to_uppercase().collect::() - } else { - c.to_lowercase().collect() - }) - }) - .collect::>() - .concat() + let s = s.split('_') + .map(|word| { + word.chars().enumerate().map(|(i, c)| if i == 0 { + c.to_uppercase().collect::() + } else { + c.to_lowercase().collect() + }) + .collect::>() + .concat() + }) + .collect::>() + .join("_"); + ALPHABETIC_UNDERSCORE.replace_all(s.as_str(), "$1$2").to_string() } if !is_camel_case(name) { diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 8b41dd62742ce..d285ac91e9fe5 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -41,6 +41,9 @@ extern crate rustc; extern crate log; extern crate rustc_const_eval; extern crate syntax_pos; +#[macro_use] +extern crate lazy_static; +extern crate regex; use rustc::lint; use rustc::middle; From e4c02e551b6c6c94cafc09d4db0b493836673292 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 21 Dec 2017 13:12:36 +0000 Subject: [PATCH 02/21] Adjust the rules for underscores --- src/librustc_lint/bad_style.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/librustc_lint/bad_style.rs b/src/librustc_lint/bad_style.rs index 09194d5c7c4ae..123dbe807fb09 100644 --- a/src/librustc_lint/bad_style.rs +++ b/src/librustc_lint/bad_style.rs @@ -24,7 +24,8 @@ use rustc::hir::intravisit::FnKind; use regex::Regex; lazy_static! { - static ref ALPHABETIC_UNDERSCORE: Regex = Regex::new("([[:alpha:]])_([[:alpha:]])").unwrap(); + static ref ALPHABETIC_UNDERSCORE: Regex = + Regex::new("([[:alpha:]])_+|_+([[:alpha:]])").unwrap(); } #[derive(PartialEq)] @@ -69,11 +70,12 @@ impl NonCamelCaseTypes { // start with a non-lowercase letter rather than non-uppercase // ones (some scripts don't have a concept of upper/lowercase) !name.is_empty() && !name.chars().next().unwrap().is_lowercase() && - !ALPHABETIC_UNDERSCORE.is_match(name) + !name.contains("__") && !ALPHABETIC_UNDERSCORE.is_match(name) } fn to_camel_case(s: &str) -> String { - let s = s.split('_') + let s = s.trim_matches('_') + .split('_') .map(|word| { word.chars().enumerate().map(|(i, c)| if i == 0 { c.to_uppercase().collect::() @@ -83,6 +85,7 @@ impl NonCamelCaseTypes { .collect::>() .concat() }) + .filter(|x| !x.is_empty()) .collect::>() .join("_"); ALPHABETIC_UNDERSCORE.replace_all(s.as_str(), "$1$2").to_string() From c1aa017645925ef1fd80c0d69b08ebca532e6a38 Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 21 Dec 2017 13:12:58 +0000 Subject: [PATCH 03/21] Add tests --- src/test/compile-fail/lint-non-camel-case-types.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/compile-fail/lint-non-camel-case-types.rs b/src/test/compile-fail/lint-non-camel-case-types.rs index f6d3d62d0bf75..5dcdf3a863f87 100644 --- a/src/test/compile-fail/lint-non-camel-case-types.rs +++ b/src/test/compile-fail/lint-non-camel-case-types.rs @@ -45,4 +45,12 @@ struct foo7 { type __ = isize; //~ ERROR type `__` should have a camel case name such as `CamelCase` +struct X86_64; + +struct X86__64; //~ ERROR type `X86__64` should have a camel case name such as `X86_64` + +struct Abc_123; //~ ERROR type `Abc_123` should have a camel case name such as `Abc123` + +struct A1_b2_c3; //~ ERROR type `A1_b2_c3` should have a camel case name such as `A1B2C3` + fn main() { } From a8d107be258514261a9366a8a384e370eb0a9628 Mon Sep 17 00:00:00 2001 From: Oliver Middleton Date: Wed, 27 Dec 2017 14:11:05 +0000 Subject: [PATCH 04/21] Correct a few stability attributes --- src/libcore/mem.rs | 14 +++++++------- src/libcore/str/mod.rs | 8 ++++++-- src/libcore/sync/atomic.rs | 13 ++++++++++++- src/libstd/ffi/c_str.rs | 8 ++++---- src/libstd/ffi/os_str.rs | 8 ++++---- src/libstd/path.rs | 8 ++++---- src/libstd/sync/mutex.rs | 2 +- src/libstd/sync/rwlock.rs | 2 +- 8 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index 5b1a9399c39bf..93f6a0214d77d 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -1024,7 +1024,7 @@ impl ::fmt::Debug for ManuallyDrop { } } -#[stable(feature = "manually_drop", since = "1.20.0")] +#[stable(feature = "manually_drop_impls", since = "1.22.0")] impl Clone for ManuallyDrop { fn clone(&self) -> Self { ManuallyDrop::new(self.deref().clone()) @@ -1035,14 +1035,14 @@ impl Clone for ManuallyDrop { } } -#[stable(feature = "manually_drop", since = "1.20.0")] +#[stable(feature = "manually_drop_impls", since = "1.22.0")] impl Default for ManuallyDrop { fn default() -> Self { ManuallyDrop::new(Default::default()) } } -#[stable(feature = "manually_drop", since = "1.20.0")] +#[stable(feature = "manually_drop_impls", since = "1.22.0")] impl PartialEq for ManuallyDrop { fn eq(&self, other: &Self) -> bool { self.deref().eq(other) @@ -1053,10 +1053,10 @@ impl PartialEq for ManuallyDrop { } } -#[stable(feature = "manually_drop", since = "1.20.0")] +#[stable(feature = "manually_drop_impls", since = "1.22.0")] impl Eq for ManuallyDrop {} -#[stable(feature = "manually_drop", since = "1.20.0")] +#[stable(feature = "manually_drop_impls", since = "1.22.0")] impl PartialOrd for ManuallyDrop { fn partial_cmp(&self, other: &Self) -> Option<::cmp::Ordering> { self.deref().partial_cmp(other) @@ -1079,14 +1079,14 @@ impl PartialOrd for ManuallyDrop { } } -#[stable(feature = "manually_drop", since = "1.20.0")] +#[stable(feature = "manually_drop_impls", since = "1.22.0")] impl Ord for ManuallyDrop { fn cmp(&self, other: &Self) -> ::cmp::Ordering { self.deref().cmp(other) } } -#[stable(feature = "manually_drop", since = "1.20.0")] +#[stable(feature = "manually_drop_impls", since = "1.22.0")] impl ::hash::Hash for ManuallyDrop { fn hash(&self, state: &mut H) { self.deref().hash(state); diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 1ca995cae6d97..765b369e4b25d 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -1997,7 +1997,9 @@ mod traits { } } - #[stable(feature = "str_checked_slicing", since = "1.20.0")] + #[unstable(feature = "inclusive_range", + reason = "recently added, follows RFC", + issue = "28237")] impl SliceIndex for ops::RangeInclusive { type Output = str; #[inline] @@ -2040,7 +2042,9 @@ mod traits { - #[stable(feature = "str_checked_slicing", since = "1.20.0")] + #[unstable(feature = "inclusive_range", + reason = "recently added, follows RFC", + issue = "28237")] impl SliceIndex for ops::RangeToInclusive { type Output = str; #[inline] diff --git a/src/libcore/sync/atomic.rs b/src/libcore/sync/atomic.rs index e334d2014af7c..3da9e9c87dde3 100644 --- a/src/libcore/sync/atomic.rs +++ b/src/libcore/sync/atomic.rs @@ -944,6 +944,7 @@ macro_rules! atomic_int { $stable_cxchg:meta, $stable_debug:meta, $stable_access:meta, + $stable_from:meta, $s_int_type:expr, $int_ref:expr, $int_type:ident $atomic_type:ident $atomic_init:ident) => { /// An integer type which can be safely shared between threads. @@ -978,7 +979,7 @@ macro_rules! atomic_int { } } - #[stable(feature = "atomic_from", since = "1.23.0")] + #[$stable_from] impl From<$int_type> for $atomic_type { #[inline] fn from(v: $int_type) -> Self { Self::new(v) } @@ -1375,6 +1376,7 @@ atomic_int! { unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), + unstable(feature = "integer_atomics", issue = "32976"), "i8", "../../../std/primitive.i8.html", i8 AtomicI8 ATOMIC_I8_INIT } @@ -1384,6 +1386,7 @@ atomic_int! { unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), + unstable(feature = "integer_atomics", issue = "32976"), "u8", "../../../std/primitive.u8.html", u8 AtomicU8 ATOMIC_U8_INIT } @@ -1393,6 +1396,7 @@ atomic_int! { unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), + unstable(feature = "integer_atomics", issue = "32976"), "i16", "../../../std/primitive.i16.html", i16 AtomicI16 ATOMIC_I16_INIT } @@ -1402,6 +1406,7 @@ atomic_int! { unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), + unstable(feature = "integer_atomics", issue = "32976"), "u16", "../../../std/primitive.u16.html", u16 AtomicU16 ATOMIC_U16_INIT } @@ -1411,6 +1416,7 @@ atomic_int! { unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), + unstable(feature = "integer_atomics", issue = "32976"), "i32", "../../../std/primitive.i32.html", i32 AtomicI32 ATOMIC_I32_INIT } @@ -1420,6 +1426,7 @@ atomic_int! { unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), + unstable(feature = "integer_atomics", issue = "32976"), "u32", "../../../std/primitive.u32.html", u32 AtomicU32 ATOMIC_U32_INIT } @@ -1429,6 +1436,7 @@ atomic_int! { unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), + unstable(feature = "integer_atomics", issue = "32976"), "i64", "../../../std/primitive.i64.html", i64 AtomicI64 ATOMIC_I64_INIT } @@ -1438,6 +1446,7 @@ atomic_int! { unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), unstable(feature = "integer_atomics", issue = "32976"), + unstable(feature = "integer_atomics", issue = "32976"), "u64", "../../../std/primitive.u64.html", u64 AtomicU64 ATOMIC_U64_INIT } @@ -1447,6 +1456,7 @@ atomic_int!{ stable(feature = "extended_compare_and_swap", since = "1.10.0"), stable(feature = "atomic_debug", since = "1.3.0"), stable(feature = "atomic_access", since = "1.15.0"), + stable(feature = "atomic_from", since = "1.23.0"), "isize", "../../../std/primitive.isize.html", isize AtomicIsize ATOMIC_ISIZE_INIT } @@ -1456,6 +1466,7 @@ atomic_int!{ stable(feature = "extended_compare_and_swap", since = "1.10.0"), stable(feature = "atomic_debug", since = "1.3.0"), stable(feature = "atomic_access", since = "1.15.0"), + stable(feature = "atomic_from", since = "1.23.0"), "usize", "../../../std/primitive.usize.html", usize AtomicUsize ATOMIC_USIZE_INIT } diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index a2022a2eeb23c..a19fe825f21fa 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -706,7 +706,7 @@ impl From for Box { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl From for Arc { #[inline] fn from(s: CString) -> Arc { @@ -715,7 +715,7 @@ impl From for Arc { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl<'a> From<&'a CStr> for Arc { #[inline] fn from(s: &CStr) -> Arc { @@ -724,7 +724,7 @@ impl<'a> From<&'a CStr> for Arc { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl From for Rc { #[inline] fn from(s: CString) -> Rc { @@ -733,7 +733,7 @@ impl From for Rc { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl<'a> From<&'a CStr> for Rc { #[inline] fn from(s: &CStr) -> Rc { diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index cb902461f39fd..109173d31c501 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -594,7 +594,7 @@ impl From for Box { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl From for Arc { #[inline] fn from(s: OsString) -> Arc { @@ -603,7 +603,7 @@ impl From for Arc { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl<'a> From<&'a OsStr> for Arc { #[inline] fn from(s: &OsStr) -> Arc { @@ -612,7 +612,7 @@ impl<'a> From<&'a OsStr> for Arc { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl From for Rc { #[inline] fn from(s: OsString) -> Rc { @@ -621,7 +621,7 @@ impl From for Rc { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl<'a> From<&'a OsStr> for Rc { #[inline] fn from(s: &OsStr) -> Rc { diff --git a/src/libstd/path.rs b/src/libstd/path.rs index eb125a4737a1c..bed9efcb8469d 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1454,7 +1454,7 @@ impl<'a> From for Cow<'a, Path> { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl From for Arc { #[inline] fn from(s: PathBuf) -> Arc { @@ -1463,7 +1463,7 @@ impl From for Arc { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl<'a> From<&'a Path> for Arc { #[inline] fn from(s: &Path) -> Arc { @@ -1472,7 +1472,7 @@ impl<'a> From<&'a Path> for Arc { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl From for Rc { #[inline] fn from(s: PathBuf) -> Rc { @@ -1481,7 +1481,7 @@ impl From for Rc { } } -#[stable(feature = "shared_from_slice2", since = "1.23.0")] +#[stable(feature = "shared_from_slice2", since = "1.24.0")] impl<'a> From<&'a Path> for Rc { #[inline] fn from(s: &Path) -> Rc { diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index 81f5594bc5231..3b4904c98e871 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -382,7 +382,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Mutex { } } -#[stable(feature = "mutex_from", since = "1.22.0")] +#[stable(feature = "mutex_from", since = "1.24.0")] impl From for Mutex { /// Creates a new mutex in an unlocked state ready for use. /// This is equivalent to [`Mutex::new`]. diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index fd6cff6b69c40..0f3f4e50f7e32 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -457,7 +457,7 @@ impl Default for RwLock { } } -#[stable(feature = "rw_lock_from", since = "1.22.0")] +#[stable(feature = "rw_lock_from", since = "1.24.0")] impl From for RwLock { /// Creates a new instance of an `RwLock` which is unlocked. /// This is equivalent to [`RwLock::new`]. From 838fb4a6a0d11ac2c24189518415d181638af001 Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Wed, 27 Dec 2017 18:38:57 +0100 Subject: [PATCH 05/21] Disable printing of error message on file descriptor 2 on CloudABI. As CloudABI is a capability-based runtime environment, file descriptors are the mechanism that grants rights to a process. These file descriptors may be passed into processes on startup using a utility called cloudabi-run. Unlike the POSIX shell, cloudabi-run does not follow the UNIX model where file descriptors 0, 1 and 2 represent stdin, stdout and stderr. There can be arbitrary many (or few) file descriptors that can be provided. For this reason, CloudABI's C library also doesn't define STD*_FILENO. liblibc should also not declare these. Disable the code in liballoc_system that tries to print error messages over file descriptor 2. For now, let's keep this function quiet. We'll see if we can think of some other way to log this in the future. --- src/liballoc_system/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/liballoc_system/lib.rs b/src/liballoc_system/lib.rs index 27259cc31a5ed..4597175474810 100644 --- a/src/liballoc_system/lib.rs +++ b/src/liballoc_system/lib.rs @@ -213,6 +213,16 @@ mod platform { struct Stderr; impl Write for Stderr { + #[cfg(target_os = "cloudabi")] + fn write_str(&mut self, _: &str) -> fmt::Result { + // CloudABI does not have any reserved file descriptor + // numbers. We should not attempt to write to file + // descriptor #2, as it may be associated with any kind of + // resource. + Ok(()) + } + + #[cfg(not(target_os = "cloudabi"))] fn write_str(&mut self, s: &str) -> fmt::Result { unsafe { libc::write(libc::STDERR_FILENO, From c661e385fd81afef808f414867cc44a6c897195e Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Sun, 31 Dec 2017 13:21:46 +0100 Subject: [PATCH 06/21] Build the right platform module on CloudABI. After #47089 lands, CloudABI will no longer be considered UNIX. We need to pick the right allocator flavour now. --- src/liballoc_system/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/liballoc_system/lib.rs b/src/liballoc_system/lib.rs index 4597175474810..1d5e7b73be557 100644 --- a/src/liballoc_system/lib.rs +++ b/src/liballoc_system/lib.rs @@ -21,7 +21,7 @@ #![feature(core_intrinsics)] #![feature(staged_api)] #![feature(rustc_attrs)] -#![cfg_attr(any(unix, target_os = "redox"), feature(libc))] +#![cfg_attr(any(unix, target_os = "cloudabi", target_os = "redox"), feature(libc))] #![rustc_alloc_kind = "lib"] // The minimum alignment guaranteed by the architecture. This value is used to @@ -116,7 +116,7 @@ unsafe impl Alloc for System { } } -#[cfg(any(unix, target_os = "redox"))] +#[cfg(any(unix, target_os = "cloudabi", target_os = "redox"))] mod platform { extern crate libc; From 4fe167adba47478e1a443c7b82e67e020253eac8 Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Mon, 1 Jan 2018 21:46:22 +0100 Subject: [PATCH 07/21] Use the right TLS model for CloudABI. CloudABI doesn't do dynamic linking. For this reason, there is no need to handle any other TLS model than local-exec. CloudABI's C library doesn't provide a __tls_get_addr() function to do Dynamic TLS. By forcing local-exec to be used here, we ensure that we don't generate function calls to __tls_get_addr(). --- src/librustc_back/target/cloudabi_base.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustc_back/target/cloudabi_base.rs b/src/librustc_back/target/cloudabi_base.rs index 3353e7936f0d5..c41c6b233584b 100644 --- a/src/librustc_back/target/cloudabi_base.rs +++ b/src/librustc_back/target/cloudabi_base.rs @@ -27,6 +27,7 @@ pub fn opts() -> TargetOptions { linker_is_gnu: true, pre_link_args: args, position_independent_executables: true, + tls_model: "local-exec".to_string(), relro_level: RelroLevel::Full, exe_allocation_crate: super::maybe_jemalloc(), .. Default::default() From 3dff918c6c3748104bf83db1b27fd13d51d587be Mon Sep 17 00:00:00 2001 From: varkor Date: Tue, 2 Jan 2018 01:06:17 +0000 Subject: [PATCH 08/21] Remove dependency on regex --- src/Cargo.lock | 8 ----- src/librustc_lint/Cargo.toml | 4 +-- src/librustc_lint/bad_style.rs | 55 ++++++++++++++++++++-------------- src/librustc_lint/lib.rs | 3 -- 4 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index 26b70f63d6589..c22187ee13e8e 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -967,11 +967,6 @@ name = "lazy_static" version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "lazy_static" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "lazycell" version = "0.5.1" @@ -1868,9 +1863,7 @@ dependencies = [ name = "rustc_lint" version = "0.0.0" dependencies = [ - "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "rustc 0.0.0", "rustc_const_eval 0.0.0", "syntax 0.0.0", @@ -2820,7 +2813,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum kuchiki 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e03098e8e719c92b7794515dfd5c1724e2b12f5ce1788e61cfa4663f82eba8d8" "checksum languageserver-types 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "773e175c945800aeea4c21c04090bcb9db987b1a566ad9c6f569972299950e3e" "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" -"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" "checksum lazycell 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3b585b7a6811fb03aa10e74b278a0f00f8dd9b45dc681f148bb29fa5cb61859b" "checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0" "checksum libgit2-sys 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "6f74b4959cef96898f5123148724fc7dee043b9a6b99f219d948851bfbe53cb2" diff --git a/src/librustc_lint/Cargo.toml b/src/librustc_lint/Cargo.toml index 2cc1827f518da..cebf52d5af7a9 100644 --- a/src/librustc_lint/Cargo.toml +++ b/src/librustc_lint/Cargo.toml @@ -6,7 +6,7 @@ version = "0.0.0" [lib] name = "rustc_lint" path = "lib.rs" -crate-types = ["rlib", "dylib"] +crate-type = ["dylib"] test = false [dependencies] @@ -15,5 +15,3 @@ rustc = { path = "../librustc" } rustc_const_eval = { path = "../librustc_const_eval" } syntax = { path = "../libsyntax" } syntax_pos = { path = "../libsyntax_pos" } -lazy_static = "1.0" -regex = "0.2.3" diff --git a/src/librustc_lint/bad_style.rs b/src/librustc_lint/bad_style.rs index 123dbe807fb09..f089ce8f06533 100644 --- a/src/librustc_lint/bad_style.rs +++ b/src/librustc_lint/bad_style.rs @@ -21,13 +21,6 @@ use syntax_pos::Span; use rustc::hir::{self, PatKind}; use rustc::hir::intravisit::FnKind; -use regex::Regex; - -lazy_static! { - static ref ALPHABETIC_UNDERSCORE: Regex = - Regex::new("([[:alpha:]])_+|_+([[:alpha:]])").unwrap(); -} - #[derive(PartialEq)] pub enum MethodLateContext { TraitAutoImpl, @@ -60,6 +53,10 @@ pub struct NonCamelCaseTypes; impl NonCamelCaseTypes { fn check_case(&self, cx: &LateContext, sort: &str, name: ast::Name, span: Span) { + fn char_has_case(c: char) -> bool { + c.is_lowercase() || c.is_uppercase() + } + fn is_camel_case(name: ast::Name) -> bool { let name = name.as_str(); if name.is_empty() { @@ -70,25 +67,37 @@ impl NonCamelCaseTypes { // start with a non-lowercase letter rather than non-uppercase // ones (some scripts don't have a concept of upper/lowercase) !name.is_empty() && !name.chars().next().unwrap().is_lowercase() && - !name.contains("__") && !ALPHABETIC_UNDERSCORE.is_match(name) + !name.contains("__") && !name.chars().collect::>().windows(2).any(|pair| { + // contains a capitalisable character followed by, or preceded by, an underscore + char_has_case(pair[0]) && pair[1] == '_' || + char_has_case(pair[1]) && pair[0] == '_' + }) } fn to_camel_case(s: &str) -> String { - let s = s.trim_matches('_') - .split('_') - .map(|word| { - word.chars().enumerate().map(|(i, c)| if i == 0 { - c.to_uppercase().collect::() - } else { - c.to_lowercase().collect() - }) - .collect::>() - .concat() - }) - .filter(|x| !x.is_empty()) - .collect::>() - .join("_"); - ALPHABETIC_UNDERSCORE.replace_all(s.as_str(), "$1$2").to_string() + s.trim_matches('_') + .split('_') + .map(|word| { + word.chars().enumerate().map(|(i, c)| if i == 0 { + c.to_uppercase().collect::() + } else { + c.to_lowercase().collect() + }) + .collect::>() + .concat() + }) + .filter(|x| !x.is_empty()) + .collect::>() + .iter().fold((String::new(), None), |(acc, prev): (String, Option<&String>), next| { + // separate two components with an underscore if their boundary cannot + // be distinguished using a uppercase/lowercase case distinction + let join = if let Some(prev) = prev { + let l = prev.chars().last().unwrap(); + let f = next.chars().next().unwrap(); + !char_has_case(l) && !char_has_case(f) + } else { false }; + (acc + if join { "_" } else { "" } + next, Some(next)) + }).0 } if !is_camel_case(name) { diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index d285ac91e9fe5..8b41dd62742ce 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -41,9 +41,6 @@ extern crate rustc; extern crate log; extern crate rustc_const_eval; extern crate syntax_pos; -#[macro_use] -extern crate lazy_static; -extern crate regex; use rustc::lint; use rustc::middle; From 2b9add2c165c4e397aeb02e1d11c57621ad11dbe Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 2 Jan 2018 23:37:36 -0800 Subject: [PATCH 09/21] Return None from Span::join if in different files --- src/libproc_macro/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 41ccd88b4a887..386c12ba36ddf 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -247,7 +247,7 @@ impl Span { #[unstable(feature = "proc_macro", issue = "38356")] pub fn join(&self, other: Span) -> Option { let self_loc = __internal::lookup_char_pos(self.0.lo()); - let other_loc = __internal::lookup_char_pos(self.0.lo()); + let other_loc = __internal::lookup_char_pos(other.0.lo()); if self_loc.file.name != other_loc.file.name { return None } From 000e907c1fc73ca249252cba3b2c9b1a20de857d Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 2 Jan 2018 23:29:11 -0800 Subject: [PATCH 10/21] Span::resolved_at and Span::located_at to combine behavior of two spans Proc macro spans serve two mostly unrelated purposes: controlling name resolution and controlling error messages. It can be useful to mix the name resolution behavior of one span with the line/column error message locations of a different span. In particular, consider the case of a trait brought into scope within the def_site of a custom derive. I want to invoke trait methods on the fields of the user's struct. If the field type does not implement the right trait, I want the error message to underline the corresponding struct field. Generating the method call with the def_site span is not ideal -- it compiles and runs but error messages sadly always point to the derive attribute like we saw with Macros 1.1. ``` | 4 | #[derive(HeapSize)] | ^^^^^^^^ ``` Generating the method call with the same span as the struct field's ident or type is not correct -- it shows the right underlines but fails to resolve to the trait in scope at the def_site. ``` | 7 | bad: std::thread::Thread, | ^^^^^^^^^^^^^^^^^^^^^^^^ ``` The correct span for the method call is one that combines the def_site's name resolution with the struct field's line/column. ``` field.span.resolved_at(Span::def_site()) // equivalently Span::def_site().located_at(field.span) ``` Adding both because which one is more natural will depend on context. --- src/libproc_macro/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 41ccd88b4a887..f2936f2bca83d 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -254,6 +254,20 @@ impl Span { Some(Span(self.0.to(other.0))) } + /// Creates a new span with the same line/column information as `self` but + /// that resolves symbols as though it were at `other`. + #[unstable(feature = "proc_macro", issue = "38356")] + pub fn resolved_at(&self, other: Span) -> Span { + Span(self.0.with_ctxt(other.0.ctxt())) + } + + /// Creates a new span with the same name resolution behavior as `self` but + /// with the line/column information of `other`. + #[unstable(feature = "proc_macro", issue = "38356")] + pub fn located_at(&self, other: Span) -> Span { + other.resolved_at(*self) + } + diagnostic_method!(error, Level::Error); diagnostic_method!(warning, Level::Warning); diagnostic_method!(note, Level::Note); From 48f2f711857c3797978f1a4e300a1375672cef84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 3 Jan 2018 00:58:56 +0200 Subject: [PATCH 11/21] Implement TrustedRandomAccess for slice::{Chunks, ChunksMut, Windows} --- src/libcore/slice/mod.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 6b6ec7147b306..ebb1660593137 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -2117,6 +2117,14 @@ impl<'a, T> ExactSizeIterator for Windows<'a, T> {} #[unstable(feature = "fused", issue = "35602")] impl<'a, T> FusedIterator for Windows<'a, T> {} +#[doc(hidden)] +unsafe impl<'a, T> TrustedRandomAccess for Windows<'a, T> { + unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] { + from_raw_parts(self.v.as_ptr().offset(i as isize), self.size) + } + fn may_have_side_effect() -> bool { false } +} + /// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a /// time). /// @@ -2228,6 +2236,16 @@ impl<'a, T> ExactSizeIterator for Chunks<'a, T> {} #[unstable(feature = "fused", issue = "35602")] impl<'a, T> FusedIterator for Chunks<'a, T> {} +#[doc(hidden)] +unsafe impl<'a, T> TrustedRandomAccess for Chunks<'a, T> { + unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] { + let start = i * self.size; + let end = cmp::min(start + self.size, self.v.len()); + from_raw_parts(self.v.as_ptr().offset(start as isize), end - start) + } + fn may_have_side_effect() -> bool { false } +} + /// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size` /// elements at a time). When the slice len is not evenly divided by the chunk /// size, the last slice of the iteration will be the remainder. @@ -2331,6 +2349,16 @@ impl<'a, T> ExactSizeIterator for ChunksMut<'a, T> {} #[unstable(feature = "fused", issue = "35602")] impl<'a, T> FusedIterator for ChunksMut<'a, T> {} +#[doc(hidden)] +unsafe impl<'a, T> TrustedRandomAccess for ChunksMut<'a, T> { + unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut [T] { + let start = i * self.chunk_size; + let end = cmp::min(start + self.chunk_size, self.v.len()); + from_raw_parts_mut(self.v.as_mut_ptr().offset(start as isize), end - start) + } + fn may_have_side_effect() -> bool { false } +} + // // Free functions // From a56a3fc85f466c8488dccdfb78cfcd05740e0b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 3 Jan 2018 12:25:18 +0200 Subject: [PATCH 12/21] Add unit test for zipping slice::{Chunks, ChunksMut, Windows} iterators For testing if the TrustedRandomAccess implementation works. --- src/libcore/tests/slice.rs | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs index fa4c2e9b3736f..d6230e93f998d 100644 --- a/src/libcore/tests/slice.rs +++ b/src/libcore/tests/slice.rs @@ -137,6 +137,18 @@ fn test_chunks_last() { assert_eq!(c2.last().unwrap()[0], 4); } +#[test] +fn test_chunks_zip() { + let v1: &[i32] = &[0, 1, 2, 3, 4]; + let v2: &[i32] = &[6, 7, 8, 9, 10]; + + let res = v1.chunks(2) + .zip(v2.chunks(2)) + .map(|(a, b)| a.iter().sum::() + b.iter().sum::()) + .collect::>(); + assert_eq!(res, vec![14, 22, 14]); +} + #[test] fn test_chunks_mut_count() { let v: &mut [i32] = &mut [0, 1, 2, 3, 4, 5]; @@ -176,6 +188,20 @@ fn test_chunks_mut_last() { assert_eq!(c2.last().unwrap()[0], 4); } +#[test] +fn test_chunks_mut_zip() { + let v1: &mut [i32] = &mut [0, 1, 2, 3, 4]; + let v2: &[i32] = &[6, 7, 8, 9, 10]; + + for (a, b) in v1.chunks_mut(2).zip(v2.chunks(2)) { + let sum = b.iter().sum::(); + for v in a { + *v += sum; + } + } + assert_eq!(v1, [13, 14, 19, 20, 14]); +} + #[test] fn test_windows_count() { let v: &[i32] = &[0, 1, 2, 3, 4, 5]; @@ -215,6 +241,19 @@ fn test_windows_last() { assert_eq!(c2.last().unwrap()[0], 3); } +#[test] +fn test_windows_zip() { + let v1: &[i32] = &[0, 1, 2, 3, 4]; + let v2: &[i32] = &[6, 7, 8, 9, 10]; + + let res = v1.windows(2) + .zip(v2.windows(2)) + .map(|(a, b)| a.iter().sum::() + b.iter().sum::()) + .collect::>(); + + assert_eq!(res, [14, 18, 22, 26]); +} + #[test] fn get_range() { let v: &[i32] = &[0, 1, 2, 3, 4, 5]; From 3f29e2b495a6b875af835f1fff64256e2d58d2b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Wed, 3 Jan 2018 12:26:55 +0200 Subject: [PATCH 13/21] Fix compilation of TrustedRandomAccess impl for slice::Chunks https://github.com/rust-lang/rust/pull/47113 renamed the private size field to chunk_size for consistency. --- src/libcore/slice/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index ebb1660593137..9b8334167f55f 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -2239,8 +2239,8 @@ impl<'a, T> FusedIterator for Chunks<'a, T> {} #[doc(hidden)] unsafe impl<'a, T> TrustedRandomAccess for Chunks<'a, T> { unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] { - let start = i * self.size; - let end = cmp::min(start + self.size, self.v.len()); + let start = i * self.chunk_size; + let end = cmp::min(start + self.chunk_size, self.v.len()); from_raw_parts(self.v.as_ptr().offset(start as isize), end - start) } fn may_have_side_effect() -> bool { false } From 47e18e0db73246cb509d54567a8910954547a1ab Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 3 Jan 2018 11:58:42 -0500 Subject: [PATCH 14/21] This isn't in Rust 1.23 --- RELEASES.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index 8fcd22b0b448f..b54d05ab4ec4c 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -49,7 +49,6 @@ Misc ---- - [Releases now ship with the Cargo book documentation.][45692] - [rustdoc now prints rendering warnings on every run.][45324] -- [Release tarballs now come with rustfmt][45903] Compatibility Notes ------------------- @@ -83,7 +82,6 @@ Compatibility Notes [45852]: https://github.com/rust-lang/rust/issues/45852 [45853]: https://github.com/rust-lang/rust/pull/45853 [45887]: https://github.com/rust-lang/rust/pull/45887 -[45903]: https://github.com/rust-lang/rust/pull/45903 [45920]: https://github.com/rust-lang/rust/pull/45920 [cargo/4506]: https://github.com/rust-lang/cargo/pull/4506 [cargo/4561]: https://github.com/rust-lang/cargo/pull/4561 From 50989cd98dbef60b0b6f5baa0ce4203ce778adaa Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 3 Jan 2018 12:05:57 -0500 Subject: [PATCH 15/21] This is an unstable feature --- RELEASES.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index b54d05ab4ec4c..a4e6f22ba3db4 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -38,7 +38,6 @@ Stabilized APIs Cargo ----- -- [Cargo now supports alternative registries][cargo/4506] - [Cargo now supports uninstallation of multiple packages][cargo/4561] eg. `cargo uninstall foo bar` uninstalls `foo` and `bar`. - [Added unit test checking to `cargo check`][cargo/4592] @@ -83,7 +82,6 @@ Compatibility Notes [45853]: https://github.com/rust-lang/rust/pull/45853 [45887]: https://github.com/rust-lang/rust/pull/45887 [45920]: https://github.com/rust-lang/rust/pull/45920 -[cargo/4506]: https://github.com/rust-lang/cargo/pull/4506 [cargo/4561]: https://github.com/rust-lang/cargo/pull/4561 [cargo/4592]: https://github.com/rust-lang/cargo/pull/4592 [cargo/4637]: https://github.com/rust-lang/cargo/pull/4637 From 05949b09a87e8753601441484e17aecc8438e3ce Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Wed, 3 Jan 2018 18:32:41 +0100 Subject: [PATCH 16/21] Explain why local-exec is used by CloudABI. --- src/librustc_back/target/cloudabi_base.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/librustc_back/target/cloudabi_base.rs b/src/librustc_back/target/cloudabi_base.rs index c41c6b233584b..4cd52ebb26df5 100644 --- a/src/librustc_back/target/cloudabi_base.rs +++ b/src/librustc_back/target/cloudabi_base.rs @@ -27,6 +27,17 @@ pub fn opts() -> TargetOptions { linker_is_gnu: true, pre_link_args: args, position_independent_executables: true, + // As CloudABI only supports static linkage, there is no need + // for dynamic TLS. The C library therefore does not provide + // __tls_get_addr(), which is normally used to perform dynamic + // TLS lookups by programs that make use of dlopen(). Only the + // "local-exec" and "initial-exec" TLS models can be used. + // + // "local-exec" is more efficient than "initial-exec", as the + // latter has one more level of indirection: it accesses the GOT + // (Global Offset Table) to obtain the effective address of a + // thread-local variable. Using a GOT is useful only when doing + // dynamic linking. tls_model: "local-exec".to_string(), relro_level: RelroLevel::Full, exe_allocation_crate: super::maybe_jemalloc(), From d7bbd3042cc674fb1a863c89085044a8a4a8b15e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Fri, 8 Dec 2017 10:53:46 +0100 Subject: [PATCH 17/21] Remove outdated LLVMRustBuildLandingPad() wrapper The function was added as a wrapper to handle compatibility with older LLVM versions that we no longer support, so it can be removed. Refs #46437 --- src/librustc_llvm/ffi.rs | 13 ++++++------- src/librustc_trans/builder.rs | 7 +++---- src/librustc_trans/intrinsic.rs | 2 +- src/librustc_trans/mir/block.rs | 2 +- src/rustllvm/RustWrapper.cpp | 7 ------- 5 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/librustc_llvm/ffi.rs b/src/librustc_llvm/ffi.rs index 7d65349446516..746c5a7cb247e 100644 --- a/src/librustc_llvm/ffi.rs +++ b/src/librustc_llvm/ffi.rs @@ -812,13 +812,12 @@ extern "C" { Bundle: OperandBundleDefRef, Name: *const c_char) -> ValueRef; - pub fn LLVMRustBuildLandingPad(B: BuilderRef, - Ty: TypeRef, - PersFn: ValueRef, - NumClauses: c_uint, - Name: *const c_char, - F: ValueRef) - -> ValueRef; + pub fn LLVMBuildLandingPad(B: BuilderRef, + Ty: TypeRef, + PersFn: ValueRef, + NumClauses: c_uint, + Name: *const c_char) + -> ValueRef; pub fn LLVMBuildResume(B: BuilderRef, Exn: ValueRef) -> ValueRef; pub fn LLVMBuildUnreachable(B: BuilderRef) -> ValueRef; diff --git a/src/librustc_trans/builder.rs b/src/librustc_trans/builder.rs index 5b697d6b99c95..4a0b1381a4008 100644 --- a/src/librustc_trans/builder.rs +++ b/src/librustc_trans/builder.rs @@ -1012,12 +1012,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } pub fn landing_pad(&self, ty: Type, pers_fn: ValueRef, - num_clauses: usize, - llfn: ValueRef) -> ValueRef { + num_clauses: usize) -> ValueRef { self.count_insn("landingpad"); unsafe { - llvm::LLVMRustBuildLandingPad(self.llbuilder, ty.to_ref(), pers_fn, - num_clauses as c_uint, noname(), llfn) + llvm::LLVMBuildLandingPad(self.llbuilder, ty.to_ref(), pers_fn, + num_clauses as c_uint, noname()) } } diff --git a/src/librustc_trans/intrinsic.rs b/src/librustc_trans/intrinsic.rs index 3cd60e7f1bc7f..cfddd99d0dd9a 100644 --- a/src/librustc_trans/intrinsic.rs +++ b/src/librustc_trans/intrinsic.rs @@ -925,7 +925,7 @@ fn trans_gnu_try<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, // rust_try ignores the selector. let lpad_ty = Type::struct_(ccx, &[Type::i8p(ccx), Type::i32(ccx)], false); - let vals = catch.landing_pad(lpad_ty, bcx.ccx.eh_personality(), 1, catch.llfn()); + let vals = catch.landing_pad(lpad_ty, bcx.ccx.eh_personality(), 1); catch.add_clause(vals, C_null(Type::i8p(ccx))); let ptr = catch.extract_value(vals, 0); let ptr_align = bcx.tcx().data_layout.pointer_align; diff --git a/src/librustc_trans/mir/block.rs b/src/librustc_trans/mir/block.rs index 422b8210b3544..8c9fb03954583 100644 --- a/src/librustc_trans/mir/block.rs +++ b/src/librustc_trans/mir/block.rs @@ -753,7 +753,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> { let llpersonality = self.ccx.eh_personality(); let llretty = self.landing_pad_type(); - let lp = bcx.landing_pad(llretty, llpersonality, 1, self.llfn); + let lp = bcx.landing_pad(llretty, llpersonality, 1); bcx.set_cleanup(lp); let slot = self.get_personality_slot(&bcx); diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index 6f51ea67cb1d1..d5095f1f94c3e 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -1144,13 +1144,6 @@ extern "C" void LLVMRustWriteSMDiagnosticToString(LLVMSMDiagnosticRef D, unwrap(D)->print("", OS); } -extern "C" LLVMValueRef -LLVMRustBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty, - LLVMValueRef PersFn, unsigned NumClauses, - const char *Name, LLVMValueRef F) { - return LLVMBuildLandingPad(B, Ty, PersFn, NumClauses, Name); -} - extern "C" LLVMValueRef LLVMRustBuildCleanupPad(LLVMBuilderRef B, LLVMValueRef ParentPad, unsigned ArgCount, From 493c29d35ac7ad1fd1558155b4f5ea056dfc7d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Fri, 8 Dec 2017 11:07:08 +0100 Subject: [PATCH 18/21] Remove unused function LLVMRustGetValueContext() Refs #46437 --- src/librustc_llvm/ffi.rs | 3 --- src/rustllvm/RustWrapper.cpp | 4 ---- 2 files changed, 7 deletions(-) diff --git a/src/librustc_llvm/ffi.rs b/src/librustc_llvm/ffi.rs index 746c5a7cb247e..4e65195626507 100644 --- a/src/librustc_llvm/ffi.rs +++ b/src/librustc_llvm/ffi.rs @@ -538,9 +538,6 @@ extern "C" { /// See llvm::LLVMTypeKind::getTypeID. pub fn LLVMRustGetTypeKind(Ty: TypeRef) -> TypeKind; - /// See llvm::Value::getContext - pub fn LLVMRustGetValueContext(V: ValueRef) -> ContextRef; - // Operations on integer types pub fn LLVMInt1TypeInContext(C: ContextRef) -> TypeRef; pub fn LLVMInt8TypeInContext(C: ContextRef) -> TypeRef; diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index d5095f1f94c3e..d4480002d4000 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -1348,10 +1348,6 @@ extern "C" bool LLVMRustConstInt128Get(LLVMValueRef CV, bool sext, uint64_t *hig return true; } -extern "C" LLVMContextRef LLVMRustGetValueContext(LLVMValueRef V) { - return wrap(&unwrap(V)->getContext()); -} - enum class LLVMRustVisibility { Default = 0, Hidden = 1, From 7e522b2f0ebddd60fb3df467cb755c2de0f37f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Fri, 8 Dec 2017 11:23:23 +0100 Subject: [PATCH 19/21] Simplify LLVMRustModuleCost() --- src/rustllvm/RustWrapper.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index d4480002d4000..96fb05ee06e37 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -1428,11 +1428,6 @@ LLVMRustModuleBufferLen(const LLVMRustModuleBuffer *Buffer) { extern "C" uint64_t LLVMRustModuleCost(LLVMModuleRef M) { - Module &Mod = *unwrap(M); - uint64_t cost = 0; - for (auto &F : Mod.functions()) { - (void)F; - cost += 1; - } - return cost; + auto f = unwrap(M)->functions(); + return std::distance(std::begin(f), std::end(f)); } From b69c124255e25d5d1c16ee88c4f2a75964db9209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 4 Jan 2018 11:34:05 +0200 Subject: [PATCH 20/21] Fix potential overflow in TrustedRandomAccess impl for slice::{Chunks,ChunksMut} --- src/libcore/slice/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 9b8334167f55f..72036d6d3a248 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -2240,7 +2240,10 @@ impl<'a, T> FusedIterator for Chunks<'a, T> {} unsafe impl<'a, T> TrustedRandomAccess for Chunks<'a, T> { unsafe fn get_unchecked(&mut self, i: usize) -> &'a [T] { let start = i * self.chunk_size; - let end = cmp::min(start + self.chunk_size, self.v.len()); + let end = match start.checked_add(self.chunk_size) { + None => self.v.len(), + Some(end) => cmp::min(end, self.v.len()), + }; from_raw_parts(self.v.as_ptr().offset(start as isize), end - start) } fn may_have_side_effect() -> bool { false } @@ -2353,7 +2356,10 @@ impl<'a, T> FusedIterator for ChunksMut<'a, T> {} unsafe impl<'a, T> TrustedRandomAccess for ChunksMut<'a, T> { unsafe fn get_unchecked(&mut self, i: usize) -> &'a mut [T] { let start = i * self.chunk_size; - let end = cmp::min(start + self.chunk_size, self.v.len()); + let end = match start.checked_add(self.chunk_size) { + None => self.v.len(), + Some(end) => cmp::min(end, self.v.len()), + }; from_raw_parts_mut(self.v.as_mut_ptr().offset(start as isize), end - start) } fn may_have_side_effect() -> bool { false } From 922f0618d1b1616f1bb3b8046948b47e4fadf29d Mon Sep 17 00:00:00 2001 From: aheart Date: Thu, 4 Jan 2018 15:55:01 +0200 Subject: [PATCH 21/21] Make examples equivalent The example with the ? operator was missing file.write_all --- src/libcore/macros.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 948ad104cdf2f..f00128a8147de 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -330,6 +330,7 @@ macro_rules! debug_assert_ne { /// // The prefered method of quick returning Errors /// fn write_to_file_question() -> Result<(), MyError> { /// let mut file = File::create("my_best_friends.txt")?; +/// file.write_all(b"This is a list of my best friends.")?; /// Ok(()) /// } ///