From e3fc97be2b90add55bbe48362cba902c6a0675be Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 23 Apr 2024 22:16:29 +0200 Subject: [PATCH 01/10] Inline `EscapeDebug::size_hint`. --- library/core/src/char/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/char/mod.rs b/library/core/src/char/mod.rs index a860c7c6aaadc..bf9f633eef370 100644 --- a/library/core/src/char/mod.rs +++ b/library/core/src/char/mod.rs @@ -339,6 +339,7 @@ impl Iterator for EscapeDebug { } } + #[inline] fn size_hint(&self) -> (usize, Option) { let n = self.len(); (n, Some(n)) From 16981ba40607ba87bc3767c0e810622455dc0d85 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Thu, 25 Apr 2024 01:26:32 +0200 Subject: [PATCH 02/10] Avoid panicking branch in `EscapeIterInner`. --- library/core/src/ascii.rs | 14 +-- library/core/src/char/methods.rs | 8 +- library/core/src/char/mod.rs | 48 +++++----- library/core/src/escape.rs | 148 ++++++++++++++++++++----------- 4 files changed, 137 insertions(+), 81 deletions(-) diff --git a/library/core/src/ascii.rs b/library/core/src/ascii.rs index c29e5565d514a..17fbb19d0397a 100644 --- a/library/core/src/ascii.rs +++ b/library/core/src/ascii.rs @@ -91,17 +91,21 @@ pub struct EscapeDefault(escape::EscapeIterInner<4>); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn escape_default(c: u8) -> EscapeDefault { - let mut data = [Char::Null; 4]; - let range = escape::escape_ascii_into(&mut data, c); - EscapeDefault(escape::EscapeIterInner::new(data, range)) + EscapeDefault::new(c) } impl EscapeDefault { + #[inline] + pub(crate) const fn new(c: u8) -> Self { + Self(escape::EscapeIterInner::ascii(c)) + } + + #[inline] pub(crate) fn empty() -> Self { - let data = [Char::Null; 4]; - EscapeDefault(escape::EscapeIterInner::new(data, 0..0)) + EscapeDefault(escape::EscapeIterInner::empty()) } + #[inline] pub(crate) fn as_str(&self) -> &str { self.0.as_str() } diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index a93b94867ce4c..458be49fb152a 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -449,10 +449,10 @@ impl char { '\"' if args.escape_double_quote => EscapeDebug::backslash(ascii::Char::QuotationMark), '\'' if args.escape_single_quote => EscapeDebug::backslash(ascii::Char::Apostrophe), _ if args.escape_grapheme_extended && self.is_grapheme_extended() => { - EscapeDebug::from_unicode(self.escape_unicode()) + EscapeDebug::unicode(self) } _ if is_printable(self) => EscapeDebug::printable(self), - _ => EscapeDebug::from_unicode(self.escape_unicode()), + _ => EscapeDebug::unicode(self), } } @@ -555,9 +555,9 @@ impl char { '\t' => EscapeDefault::backslash(ascii::Char::SmallT), '\r' => EscapeDefault::backslash(ascii::Char::SmallR), '\n' => EscapeDefault::backslash(ascii::Char::SmallN), - '\\' | '\'' | '"' => EscapeDefault::backslash(self.as_ascii().unwrap()), + '\\' | '\'' | '\"' => EscapeDefault::backslash(self.as_ascii().unwrap()), '\x20'..='\x7e' => EscapeDefault::printable(self.as_ascii().unwrap()), - _ => EscapeDefault::from_unicode(self.escape_unicode()), + _ => EscapeDefault::unicode(self), } } diff --git a/library/core/src/char/mod.rs b/library/core/src/char/mod.rs index bf9f633eef370..d99ac226da005 100644 --- a/library/core/src/char/mod.rs +++ b/library/core/src/char/mod.rs @@ -152,10 +152,9 @@ pub const fn from_digit(num: u32, radix: u32) -> Option { pub struct EscapeUnicode(escape::EscapeIterInner<10>); impl EscapeUnicode { - fn new(chr: char) -> Self { - let mut data = [ascii::Char::Null; 10]; - let range = escape::escape_unicode_into(&mut data, chr); - Self(escape::EscapeIterInner::new(data, range)) + #[inline] + const fn new(c: char) -> Self { + Self(escape::EscapeIterInner::unicode(c)) } } @@ -219,18 +218,24 @@ impl fmt::Display for EscapeUnicode { pub struct EscapeDefault(escape::EscapeIterInner<10>); impl EscapeDefault { - fn printable(chr: ascii::Char) -> Self { - let data = [chr]; - Self(escape::EscapeIterInner::from_array(data)) + #[inline] + const fn printable(c: ascii::Char) -> Self { + Self::ascii(c.to_u8()) } - fn backslash(chr: ascii::Char) -> Self { - let data = [ascii::Char::ReverseSolidus, chr]; - Self(escape::EscapeIterInner::from_array(data)) + #[inline] + const fn backslash(c: ascii::Char) -> Self { + Self(escape::EscapeIterInner::backslash(c)) } - fn from_unicode(esc: EscapeUnicode) -> Self { - Self(esc.0) + #[inline] + const fn ascii(c: u8) -> Self { + Self(escape::EscapeIterInner::ascii(c)) + } + + #[inline] + const fn unicode(c: char) -> Self { + Self(escape::EscapeIterInner::unicode(c)) } } @@ -304,23 +309,24 @@ enum EscapeDebugInner { } impl EscapeDebug { - fn printable(chr: char) -> Self { + #[inline] + const fn printable(chr: char) -> Self { Self(EscapeDebugInner::Char(chr)) } - fn backslash(chr: ascii::Char) -> Self { - let data = [ascii::Char::ReverseSolidus, chr]; - let iter = escape::EscapeIterInner::from_array(data); - Self(EscapeDebugInner::Bytes(iter)) + #[inline] + const fn backslash(c: ascii::Char) -> Self { + Self(EscapeDebugInner::Bytes(escape::EscapeIterInner::backslash(c))) } - fn from_unicode(esc: EscapeUnicode) -> Self { - Self(EscapeDebugInner::Bytes(esc.0)) + #[inline] + const fn unicode(c: char) -> Self { + Self(EscapeDebugInner::Bytes(escape::EscapeIterInner::unicode(c))) } + #[inline] fn clear(&mut self) { - let bytes = escape::EscapeIterInner::from_array([]); - self.0 = EscapeDebugInner::Bytes(bytes); + self.0 = EscapeDebugInner::Bytes(escape::EscapeIterInner::empty()); } } diff --git a/library/core/src/escape.rs b/library/core/src/escape.rs index 143e277283e2c..a71fe59e50a13 100644 --- a/library/core/src/escape.rs +++ b/library/core/src/escape.rs @@ -6,56 +6,85 @@ use crate::ops::Range; const HEX_DIGITS: [ascii::Char; 16] = *b"0123456789abcdef".as_ascii().unwrap(); -/// Escapes a byte into provided buffer; returns length of escaped -/// representation. -pub(crate) fn escape_ascii_into(output: &mut [ascii::Char; 4], byte: u8) -> Range { - #[inline] - fn backslash(a: ascii::Char) -> ([ascii::Char; 4], u8) { - ([ascii::Char::ReverseSolidus, a, ascii::Char::Null, ascii::Char::Null], 2) - } +#[inline] +const fn backslash(a: ascii::Char) -> ([ascii::Char; N], u8) { + const { assert!(N >= 2) }; + + let mut output = [ascii::Char::Null; N]; + + output[0] = ascii::Char::ReverseSolidus; + output[1] = a; + + (output, 2) +} + +/// Escapes an ASCII character. +/// +/// Returns a buffer and the length of the escaped representation. +const fn escape_ascii(byte: u8) -> ([ascii::Char; N], u8) { + const { assert!(N >= 4) }; - let (data, len) = match byte { + match byte { b'\t' => backslash(ascii::Char::SmallT), b'\r' => backslash(ascii::Char::SmallR), b'\n' => backslash(ascii::Char::SmallN), b'\\' => backslash(ascii::Char::ReverseSolidus), b'\'' => backslash(ascii::Char::Apostrophe), b'\"' => backslash(ascii::Char::QuotationMark), - _ => { - if let Some(a) = byte.as_ascii() + byte => { + let mut output = [ascii::Char::Null; N]; + + if let Some(c) = byte.as_ascii() && !byte.is_ascii_control() { - ([a, ascii::Char::Null, ascii::Char::Null, ascii::Char::Null], 1) + output[0] = c; + (output, 1) } else { - let hi = HEX_DIGITS[usize::from(byte >> 4)]; - let lo = HEX_DIGITS[usize::from(byte & 0xf)]; - ([ascii::Char::ReverseSolidus, ascii::Char::SmallX, hi, lo], 4) + let hi = HEX_DIGITS[(byte >> 4) as usize]; + let lo = HEX_DIGITS[(byte & 0xf) as usize]; + + output[0] = ascii::Char::ReverseSolidus; + output[1] = ascii::Char::SmallX; + output[2] = hi; + output[3] = lo; + + (output, 4) } } - }; - *output = data; - 0..len + } } -/// Escapes a character into provided buffer using `\u{NNNN}` representation. -pub(crate) fn escape_unicode_into(output: &mut [ascii::Char; 10], ch: char) -> Range { - output[9] = ascii::Char::RightCurlyBracket; - - let ch = ch as u32; - output[3] = HEX_DIGITS[((ch >> 20) & 15) as usize]; - output[4] = HEX_DIGITS[((ch >> 16) & 15) as usize]; - output[5] = HEX_DIGITS[((ch >> 12) & 15) as usize]; - output[6] = HEX_DIGITS[((ch >> 8) & 15) as usize]; - output[7] = HEX_DIGITS[((ch >> 4) & 15) as usize]; - output[8] = HEX_DIGITS[((ch >> 0) & 15) as usize]; - - // or-ing 1 ensures that for ch==0 the code computes that one digit should - // be printed. - let start = (ch | 1).leading_zeros() as usize / 4 - 2; - const UNICODE_ESCAPE_PREFIX: &[ascii::Char; 3] = b"\\u{".as_ascii().unwrap(); - output[start..][..3].copy_from_slice(UNICODE_ESCAPE_PREFIX); - - (start as u8)..10 +/// Escapes a character `\u{NNNN}` representation. +/// +/// Returns a buffer and the length of the escaped representation. +const fn escape_unicode(c: char) -> ([ascii::Char; N], u8) { + const { assert!(N >= 10) }; + + let c = c as u32; + + // OR-ing `1` ensures that for `c == 0` the code computes that + // one digit should be printed. + let u_len = (8 - (c | 1).leading_zeros() / 4) as usize; + + let closing_paren_offset = 3 + u_len; + + let mut output = [ascii::Char::Null; N]; + + output[0] = ascii::Char::ReverseSolidus; + output[1] = ascii::Char::SmallU; + output[2] = ascii::Char::LeftCurlyBracket; + + output[3 + u_len.saturating_sub(6)] = HEX_DIGITS[((c >> 20) & 0x0f) as usize]; + output[3 + u_len.saturating_sub(5)] = HEX_DIGITS[((c >> 16) & 0x0f) as usize]; + output[3 + u_len.saturating_sub(4)] = HEX_DIGITS[((c >> 12) & 0x0f) as usize]; + output[3 + u_len.saturating_sub(3)] = HEX_DIGITS[((c >> 8) & 0x0f) as usize]; + output[3 + u_len.saturating_sub(2)] = HEX_DIGITS[((c >> 4) & 0x0f) as usize]; + output[3 + u_len.saturating_sub(1)] = HEX_DIGITS[((c >> 0) & 0x0f) as usize]; + + output[closing_paren_offset] = ascii::Char::RightCurlyBracket; + + let len = (closing_paren_offset + 1) as u8; + (output, len) } /// An iterator over an fixed-size array. @@ -65,45 +94,62 @@ pub(crate) fn escape_unicode_into(output: &mut [ascii::Char; 10], ch: char) -> R #[derive(Clone, Debug)] pub(crate) struct EscapeIterInner { // The element type ensures this is always ASCII, and thus also valid UTF-8. - pub(crate) data: [ascii::Char; N], + data: [ascii::Char; N], - // Invariant: alive.start <= alive.end <= N. - pub(crate) alive: Range, + // Invariant: `alive.start <= alive.end <= N` + alive: Range, } impl EscapeIterInner { - pub fn new(data: [ascii::Char; N], alive: Range) -> Self { - const { assert!(N < 256) }; - debug_assert!(alive.start <= alive.end && usize::from(alive.end) <= N, "{alive:?}"); - Self { data, alive } + pub const fn backslash(c: ascii::Char) -> Self { + let (data, len) = backslash(c); + Self { data, alive: 0..len } } - pub fn from_array(array: [ascii::Char; M]) -> Self { - const { assert!(M <= N) }; + pub const fn ascii(c: u8) -> Self { + let (data, len) = escape_ascii(c); + Self { data, alive: 0..len } + } - let mut data = [ascii::Char::Null; N]; - data[..M].copy_from_slice(&array); - Self::new(data, 0..M as u8) + pub const fn unicode(c: char) -> Self { + let (data, len) = escape_unicode(c); + Self { data, alive: 0..len } + } + + #[inline] + pub const fn empty() -> Self { + Self { data: [ascii::Char::Null; N], alive: 0..0 } } pub fn as_ascii(&self) -> &[ascii::Char] { - &self.data[usize::from(self.alive.start)..usize::from(self.alive.end)] + // SAFETY: `self.alive` is guaranteed to be a valid range for indexing `self.data`. + unsafe { + self.data.get_unchecked(usize::from(self.alive.start)..usize::from(self.alive.end)) + } } + #[inline] pub fn as_str(&self) -> &str { self.as_ascii().as_str() } + #[inline] pub fn len(&self) -> usize { usize::from(self.alive.end - self.alive.start) } pub fn next(&mut self) -> Option { - self.alive.next().map(|i| self.data[usize::from(i)].to_u8()) + let i = self.alive.next()?; + + // SAFETY: `i` is guaranteed to be a valid index for `self.data`. + unsafe { Some(self.data.get_unchecked(usize::from(i)).to_u8()) } } pub fn next_back(&mut self) -> Option { - self.alive.next_back().map(|i| self.data[usize::from(i)].to_u8()) + let i = self.alive.next_back()?; + + // SAFETY: `i` is guaranteed to be a valid index for `self.data`. + unsafe { Some(self.data.get_unchecked(usize::from(i)).to_u8()) } } pub fn advance_by(&mut self, n: usize) -> Result<(), NonZero> { From 4edf12d33e0aa67a82e6587e94469f3761cb1f53 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 1 May 2024 16:14:37 +0200 Subject: [PATCH 03/10] Improve escape methods. --- library/core/src/ascii.rs | 2 +- library/core/src/char/mod.rs | 7 +---- library/core/src/escape.rs | 61 +++++++++++++++++------------------- 3 files changed, 30 insertions(+), 40 deletions(-) diff --git a/library/core/src/ascii.rs b/library/core/src/ascii.rs index 17fbb19d0397a..e9f4d0f93ed49 100644 --- a/library/core/src/ascii.rs +++ b/library/core/src/ascii.rs @@ -102,7 +102,7 @@ impl EscapeDefault { #[inline] pub(crate) fn empty() -> Self { - EscapeDefault(escape::EscapeIterInner::empty()) + Self(escape::EscapeIterInner::empty()) } #[inline] diff --git a/library/core/src/char/mod.rs b/library/core/src/char/mod.rs index d99ac226da005..f3683fe3f9c83 100644 --- a/library/core/src/char/mod.rs +++ b/library/core/src/char/mod.rs @@ -220,7 +220,7 @@ pub struct EscapeDefault(escape::EscapeIterInner<10>); impl EscapeDefault { #[inline] const fn printable(c: ascii::Char) -> Self { - Self::ascii(c.to_u8()) + Self(escape::EscapeIterInner::ascii(c.to_u8())) } #[inline] @@ -228,11 +228,6 @@ impl EscapeDefault { Self(escape::EscapeIterInner::backslash(c)) } - #[inline] - const fn ascii(c: u8) -> Self { - Self(escape::EscapeIterInner::ascii(c)) - } - #[inline] const fn unicode(c: char) -> Self { Self(escape::EscapeIterInner::unicode(c)) diff --git a/library/core/src/escape.rs b/library/core/src/escape.rs index a71fe59e50a13..f6ec30b9f793a 100644 --- a/library/core/src/escape.rs +++ b/library/core/src/escape.rs @@ -7,7 +7,7 @@ use crate::ops::Range; const HEX_DIGITS: [ascii::Char; 16] = *b"0123456789abcdef".as_ascii().unwrap(); #[inline] -const fn backslash(a: ascii::Char) -> ([ascii::Char; N], u8) { +const fn backslash(a: ascii::Char) -> ([ascii::Char; N], Range) { const { assert!(N >= 2) }; let mut output = [ascii::Char::Null; N]; @@ -15,13 +15,13 @@ const fn backslash(a: ascii::Char) -> ([ascii::Char; N], u8) { output[0] = ascii::Char::ReverseSolidus; output[1] = a; - (output, 2) + (output, 0..2) } /// Escapes an ASCII character. /// /// Returns a buffer and the length of the escaped representation. -const fn escape_ascii(byte: u8) -> ([ascii::Char; N], u8) { +const fn escape_ascii(byte: u8) -> ([ascii::Char; N], Range) { const { assert!(N >= 4) }; match byte { @@ -38,7 +38,7 @@ const fn escape_ascii(byte: u8) -> ([ascii::Char; N], u8) { && !byte.is_ascii_control() { output[0] = c; - (output, 1) + (output, 0..1) } else { let hi = HEX_DIGITS[(byte >> 4) as usize]; let lo = HEX_DIGITS[(byte & 0xf) as usize]; @@ -48,7 +48,7 @@ const fn escape_ascii(byte: u8) -> ([ascii::Char; N], u8) { output[2] = hi; output[3] = lo; - (output, 4) + (output, 0..4) } } } @@ -57,34 +57,28 @@ const fn escape_ascii(byte: u8) -> ([ascii::Char; N], u8) { /// Escapes a character `\u{NNNN}` representation. /// /// Returns a buffer and the length of the escaped representation. -const fn escape_unicode(c: char) -> ([ascii::Char; N], u8) { - const { assert!(N >= 10) }; +const fn escape_unicode(c: char) -> ([ascii::Char; N], Range) { + const { assert!(N >= 10 && N < u8::MAX as usize) }; - let c = c as u32; + let c = u32::from(c); // OR-ing `1` ensures that for `c == 0` the code computes that // one digit should be printed. - let u_len = (8 - (c | 1).leading_zeros() / 4) as usize; - - let closing_paren_offset = 3 + u_len; + let start = (c | 1).leading_zeros() as usize / 4 - 2; let mut output = [ascii::Char::Null; N]; - - output[0] = ascii::Char::ReverseSolidus; - output[1] = ascii::Char::SmallU; - output[2] = ascii::Char::LeftCurlyBracket; - - output[3 + u_len.saturating_sub(6)] = HEX_DIGITS[((c >> 20) & 0x0f) as usize]; - output[3 + u_len.saturating_sub(5)] = HEX_DIGITS[((c >> 16) & 0x0f) as usize]; - output[3 + u_len.saturating_sub(4)] = HEX_DIGITS[((c >> 12) & 0x0f) as usize]; - output[3 + u_len.saturating_sub(3)] = HEX_DIGITS[((c >> 8) & 0x0f) as usize]; - output[3 + u_len.saturating_sub(2)] = HEX_DIGITS[((c >> 4) & 0x0f) as usize]; - output[3 + u_len.saturating_sub(1)] = HEX_DIGITS[((c >> 0) & 0x0f) as usize]; - - output[closing_paren_offset] = ascii::Char::RightCurlyBracket; - - let len = (closing_paren_offset + 1) as u8; - (output, len) + output[3] = HEX_DIGITS[((c >> 20) & 15) as usize]; + output[4] = HEX_DIGITS[((c >> 16) & 15) as usize]; + output[5] = HEX_DIGITS[((c >> 12) & 15) as usize]; + output[6] = HEX_DIGITS[((c >> 8) & 15) as usize]; + output[7] = HEX_DIGITS[((c >> 4) & 15) as usize]; + output[8] = HEX_DIGITS[((c >> 0) & 15) as usize]; + output[9] = ascii::Char::RightCurlyBracket; + output[start + 0] = ascii::Char::ReverseSolidus; + output[start + 1] = ascii::Char::SmallU; + output[start + 2] = ascii::Char::LeftCurlyBracket; + + (output, (start as u8)..(N as u8)) } /// An iterator over an fixed-size array. @@ -102,18 +96,18 @@ pub(crate) struct EscapeIterInner { impl EscapeIterInner { pub const fn backslash(c: ascii::Char) -> Self { - let (data, len) = backslash(c); - Self { data, alive: 0..len } + let (data, range) = backslash(c); + Self { data, alive: range } } pub const fn ascii(c: u8) -> Self { - let (data, len) = escape_ascii(c); - Self { data, alive: 0..len } + let (data, range) = escape_ascii(c); + Self { data, alive: range } } pub const fn unicode(c: char) -> Self { - let (data, len) = escape_unicode(c); - Self { data, alive: 0..len } + let (data, range) = escape_unicode(c); + Self { data, alive: range } } #[inline] @@ -121,6 +115,7 @@ impl EscapeIterInner { Self { data: [ascii::Char::Null; N], alive: 0..0 } } + #[inline] pub fn as_ascii(&self) -> &[ascii::Char] { // SAFETY: `self.alive` is guaranteed to be a valid range for indexing `self.data`. unsafe { From 7fde7308bf751315e0f4fabe32258916b166e34e Mon Sep 17 00:00:00 2001 From: ivan-shrimp <70307174+ivan-shrimp@users.noreply.github.com> Date: Sun, 12 May 2024 11:29:24 +0800 Subject: [PATCH 04/10] reverse condition in `uN::checked_sub` --- library/core/src/num/uint_macros.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 48a96c5792c64..446d0658c1262 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -584,11 +584,11 @@ macro_rules! uint_impl { // Thus, rather than using `overflowing_sub` that produces a wrapping // subtraction, check it ourself so we can use an unchecked one. - if self >= rhs { + if self < rhs { + None + } else { // SAFETY: just checked this can't overflow Some(unsafe { intrinsics::unchecked_sub(self, rhs) }) - } else { - None } } From 4db00fe229f08b06feeee552ae53af9f49c25048 Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Fri, 10 May 2024 16:38:19 +0200 Subject: [PATCH 05/10] Use an helper to move the files In case the source is not in the same filesystem. --- src/bootstrap/src/core/build_steps/dist.rs | 6 ++++-- src/bootstrap/src/core/download.rs | 6 +++--- src/bootstrap/src/utils/helpers.rs | 15 +++++++++++++++ src/bootstrap/src/utils/tarball.rs | 4 ++-- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/dist.rs b/src/bootstrap/src/core/build_steps/dist.rs index 5bc9d7615e2bc..1f006e1453f4b 100644 --- a/src/bootstrap/src/core/build_steps/dist.rs +++ b/src/bootstrap/src/core/build_steps/dist.rs @@ -26,7 +26,9 @@ use crate::core::build_steps::tool::{self, Tool}; use crate::core::builder::{Builder, Kind, RunConfig, ShouldRun, Step}; use crate::core::config::TargetSelection; use crate::utils::channel::{self, Info}; -use crate::utils::helpers::{exe, is_dylib, output, t, target_supports_cranelift_backend, timeit}; +use crate::utils::helpers::{ + exe, is_dylib, move_file, output, t, target_supports_cranelift_backend, timeit, +}; use crate::utils::tarball::{GeneratedTarball, OverlayKind, Tarball}; use crate::{Compiler, DependencyType, Mode, LLVM_TOOLS}; @@ -2024,7 +2026,7 @@ impl Step for Extended { builder.run(&mut cmd); if !builder.config.dry_run() { - t!(fs::rename(exe.join(&filename), distdir(builder).join(&filename))); + t!(move_file(exe.join(&filename), distdir(builder).join(&filename))); } } } diff --git a/src/bootstrap/src/core/download.rs b/src/bootstrap/src/core/download.rs index a074d53aa36e6..60f48c5923e1c 100644 --- a/src/bootstrap/src/core/download.rs +++ b/src/bootstrap/src/core/download.rs @@ -12,7 +12,7 @@ use build_helper::ci::CiEnv; use build_helper::stage0_parser::VersionMetadata; use xz2::bufread::XzDecoder; -use crate::utils::helpers::{check_run, exe, program_out_of_date}; +use crate::utils::helpers::{check_run, exe, move_file, program_out_of_date}; use crate::{core::build_steps::llvm::detect_llvm_sha, utils::helpers::hex_encode}; use crate::{t, Config}; @@ -209,7 +209,7 @@ impl Config { None => panic!("no protocol in {url}"), } t!( - std::fs::rename(&tempfile, dest_path), + move_file(&tempfile, dest_path), format!("failed to rename {tempfile:?} to {dest_path:?}") ); } @@ -313,7 +313,7 @@ impl Config { if src_path.is_dir() && dst_path.exists() { continue; } - t!(fs::rename(src_path, dst_path)); + t!(move_file(src_path, dst_path)); } let dst_dir = dst.join(directory_prefix); if dst_dir.exists() { diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs index 0d2ff4f951b61..278359cb08e39 100644 --- a/src/bootstrap/src/utils/helpers.rs +++ b/src/bootstrap/src/utils/helpers.rs @@ -150,6 +150,21 @@ pub fn symlink_dir(config: &Config, original: &Path, link: &Path) -> io::Result< } } +/// Rename a file if from and to are in the same filesystem or +/// copy and remove the file otherwise +pub fn move_file, Q: AsRef>(from: P, to: Q) -> io::Result<()> { + match fs::rename(&from, &to) { + // FIXME: Once `ErrorKind::CrossesDevices` is stabilized use + // if e.kind() == io::ErrorKind::CrossesDevices { + #[cfg(unix)] + Err(e) if e.raw_os_error() == Some(libc::EXDEV) => { + std::fs::copy(&from, &to)?; + std::fs::remove_file(&from) + } + r => r, + } +} + pub fn forcing_clang_based_tests() -> bool { if let Some(var) = env::var_os("RUSTBUILD_FORCE_CLANG_BASED_TESTS") { match &var.to_string_lossy().to_lowercase()[..] { diff --git a/src/bootstrap/src/utils/tarball.rs b/src/bootstrap/src/utils/tarball.rs index 2a950e669b923..57cdf7473a191 100644 --- a/src/bootstrap/src/utils/tarball.rs +++ b/src/bootstrap/src/utils/tarball.rs @@ -13,7 +13,7 @@ use std::{ use crate::core::builder::Builder; use crate::core::{build_steps::dist::distdir, builder::Kind}; use crate::utils::channel; -use crate::utils::helpers::t; +use crate::utils::helpers::{move_file, t}; #[derive(Copy, Clone)] pub(crate) enum OverlayKind { @@ -284,7 +284,7 @@ impl<'a> Tarball<'a> { // name, not "image". We rename the image directory just before passing // into rust-installer. let dest = self.temp_dir.join(self.package_name()); - t!(std::fs::rename(&self.image_dir, &dest)); + t!(move_file(&self.image_dir, &dest)); self.run(|this, cmd| { let distdir = distdir(this.builder); From 1f5837ae2506d5439b31195b7fcf784b9ee90d2b Mon Sep 17 00:00:00 2001 From: Oneirical Date: Sat, 11 May 2024 17:20:31 -0400 Subject: [PATCH 06/10] rewrite c-link-to-rust-staticlib --- .../tidy/src/allowed_run_make_makefiles.txt | 1 - .../c-link-to-rust-staticlib/Makefile | 16 --------------- .../c-link-to-rust-staticlib/rmake.rs | 20 +++++++++++++++++++ 3 files changed, 20 insertions(+), 17 deletions(-) delete mode 100644 tests/run-make/c-link-to-rust-staticlib/Makefile create mode 100644 tests/run-make/c-link-to-rust-staticlib/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 5f68f779c4eb0..37acd768593eb 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -8,7 +8,6 @@ run-make/branch-protection-check-IBT/Makefile run-make/c-dynamic-dylib/Makefile run-make/c-dynamic-rlib/Makefile run-make/c-link-to-rust-dylib/Makefile -run-make/c-link-to-rust-staticlib/Makefile run-make/c-static-dylib/Makefile run-make/c-static-rlib/Makefile run-make/c-unwind-abi-catch-lib-panic/Makefile diff --git a/tests/run-make/c-link-to-rust-staticlib/Makefile b/tests/run-make/c-link-to-rust-staticlib/Makefile deleted file mode 100644 index d36cc421c468a..0000000000000 --- a/tests/run-make/c-link-to-rust-staticlib/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -# This test checks that C linking with Rust does not encounter any errors, with static libraries. -# See https://github.com/rust-lang/rust/issues/10434 - -# ignore-cross-compile -include ../tools.mk - -# ignore-freebsd -# FIXME - -all: - $(RUSTC) foo.rs - $(CC) bar.c $(call STATICLIB,foo) $(call OUT_EXE,bar) \ - $(EXTRACFLAGS) $(EXTRACXXFLAGS) - $(call RUN,bar) - rm $(call STATICLIB,foo) - $(call RUN,bar) diff --git a/tests/run-make/c-link-to-rust-staticlib/rmake.rs b/tests/run-make/c-link-to-rust-staticlib/rmake.rs new file mode 100644 index 0000000000000..d73ca413777d2 --- /dev/null +++ b/tests/run-make/c-link-to-rust-staticlib/rmake.rs @@ -0,0 +1,20 @@ +// This test checks that C linking with Rust does not encounter any errors, with a static library. +// See https://github.com/rust-lang/rust/issues/10434 + +//@ ignore-cross-compile + +use run_make_support::{cc, extra_c_flags, extra_cxx_flags, run, rustc, static_lib}; +use std::fs; + +fn main() { + rustc().input("foo.rs").run(); + cc().input("bar.c") + .input(static_lib("foo")) + .out_exe("bar") + .args(&extra_c_flags()) + .args(&extra_cxx_flags()) + .run(); + run("bar"); + fs::remove_file(static_lib("foo")); + run("bar"); +} From b1e5e5161a2ef27852aab40cf3472187bdda5fee Mon Sep 17 00:00:00 2001 From: Julien <96022417+Oneirical@users.noreply.github.com> Date: Tue, 14 May 2024 16:43:39 -0400 Subject: [PATCH 07/10] remove cxx_flags --- tests/run-make/c-link-to-rust-staticlib/rmake.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/run-make/c-link-to-rust-staticlib/rmake.rs b/tests/run-make/c-link-to-rust-staticlib/rmake.rs index d73ca413777d2..762d7953a9a0f 100644 --- a/tests/run-make/c-link-to-rust-staticlib/rmake.rs +++ b/tests/run-make/c-link-to-rust-staticlib/rmake.rs @@ -3,7 +3,7 @@ //@ ignore-cross-compile -use run_make_support::{cc, extra_c_flags, extra_cxx_flags, run, rustc, static_lib}; +use run_make_support::{cc, extra_c_flags, run, rustc, static_lib}; use std::fs; fn main() { @@ -12,7 +12,6 @@ fn main() { .input(static_lib("foo")) .out_exe("bar") .args(&extra_c_flags()) - .args(&extra_cxx_flags()) .run(); run("bar"); fs::remove_file(static_lib("foo")); From 1f61cc3078ce6bcca8095778310f73fc45f7193e Mon Sep 17 00:00:00 2001 From: Oneirical Date: Mon, 13 May 2024 23:30:50 -0400 Subject: [PATCH 08/10] port no-cdylib-as-rdylib test --- .../tidy/src/allowed_run_make_makefiles.txt | 1 - tests/run-make/no-cdylib-as-rdylib/Makefile | 16 ---------------- tests/run-make/no-cdylib-as-rdylib/rmake.rs | 16 ++++++++++++++++ 3 files changed, 16 insertions(+), 17 deletions(-) delete mode 100644 tests/run-make/no-cdylib-as-rdylib/Makefile create mode 100644 tests/run-make/no-cdylib-as-rdylib/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index d742368292035..fc2ba589d2442 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -185,7 +185,6 @@ run-make/native-link-modifier-whole-archive/Makefile run-make/no-alloc-shim/Makefile run-make/no-builtins-attribute/Makefile run-make/no-builtins-lto/Makefile -run-make/no-cdylib-as-rdylib/Makefile run-make/no-duplicate-libs/Makefile run-make/no-intermediate-extras/Makefile run-make/obey-crate-type-flag/Makefile diff --git a/tests/run-make/no-cdylib-as-rdylib/Makefile b/tests/run-make/no-cdylib-as-rdylib/Makefile deleted file mode 100644 index 4d2be0aea913d..0000000000000 --- a/tests/run-make/no-cdylib-as-rdylib/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -# ignore-cross-compile -include ../tools.mk - -# Test that rustc will not attempt to link against a cdylib as if -# it is a rust dylib when an rlib for the same crate is available. -# Previously rustc didn't actually check if any further formats of -# a crate which has been loaded are of the same version and if -# they are actually valid. This caused a cdylib to be interpreted -# as rust dylib as soon as the corresponding rlib was loaded. As -# cdylibs don't export any rust symbols, linking would fail if -# rustc decides to link against the cdylib rather than the rlib. - -all: - $(RUSTC) bar.rs --crate-type=rlib --crate-type=cdylib - $(RUSTC) foo.rs -C prefer-dynamic - $(call RUN,foo) diff --git a/tests/run-make/no-cdylib-as-rdylib/rmake.rs b/tests/run-make/no-cdylib-as-rdylib/rmake.rs new file mode 100644 index 0000000000000..42e89df6c2b61 --- /dev/null +++ b/tests/run-make/no-cdylib-as-rdylib/rmake.rs @@ -0,0 +1,16 @@ +// This test produces an rlib and a cdylib from bar.rs. +// Then, foo.rs attempts to link to the bar library. +// If the test passes, that means rustc favored the rlib and ignored the cdylib. +// If the test fails, that is because the cdylib was picked, which does not export +// any Rust symbols. +// See https://github.com/rust-lang/rust/pull/113695 + +//@ ignore-cross-compile + +use run_make_support::{run, rustc}; + +fn main() { + rustc().input("bar.rs").crate_type("rlib").crate_type("cdylib").run(); + rustc().input("foo.rs").arg("-Cprefer-dynamic").run(); + run("foo"); +} From 91a3f04a3f9d6927e09c5ca6e57d337255cf1886 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Tue, 14 May 2024 20:06:23 -0400 Subject: [PATCH 09/10] fix the test --- tests/run-make/c-link-to-rust-staticlib/rmake.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/run-make/c-link-to-rust-staticlib/rmake.rs b/tests/run-make/c-link-to-rust-staticlib/rmake.rs index 762d7953a9a0f..63d5eb78c6987 100644 --- a/tests/run-make/c-link-to-rust-staticlib/rmake.rs +++ b/tests/run-make/c-link-to-rust-staticlib/rmake.rs @@ -8,11 +8,7 @@ use std::fs; fn main() { rustc().input("foo.rs").run(); - cc().input("bar.c") - .input(static_lib("foo")) - .out_exe("bar") - .args(&extra_c_flags()) - .run(); + cc().input("bar.c").input(static_lib("foo")).out_exe("bar").args(&extra_c_flags()).run(); run("bar"); fs::remove_file(static_lib("foo")); run("bar"); From 0afd50e8524aee0e9f23d4881bc7cac687c23ddf Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 15 May 2024 08:22:53 +0200 Subject: [PATCH 10/10] MIR operators: clarify Shl/Shr handling of negative offsets --- compiler/rustc_middle/src/mir/syntax.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 4278ce823d0e0..e124b478f4193 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -1480,13 +1480,17 @@ pub enum BinOp { BitOr, /// The `<<` operator (shift left) /// - /// The offset is truncated to the size of the first operand and made unsigned before shifting. + /// The offset is (uniquely) determined as follows: + /// - it is "equal modulo LHS::BITS" to the RHS + /// - it is in the range `0..LHS::BITS` Shl, /// Like `Shl`, but is UB if the RHS >= LHS::BITS or RHS < 0 ShlUnchecked, /// The `>>` operator (shift right) /// - /// The offset is truncated to the size of the first operand and made unsigned before shifting. + /// The offset is (uniquely) determined as follows: + /// - it is "equal modulo LHS::BITS" to the RHS + /// - it is in the range `0..LHS::BITS` /// /// This is an arithmetic shift if the LHS is signed /// and a logical shift if the LHS is unsigned.