Skip to content

Commit ade234d

Browse files
committed
Auto merge of #125144 - fmease:rollup-4uft293, r=fmease
Rollup of 6 pull requests Successful merges: - #124307 (Optimize character escaping.) - #124975 (Use an helper to move the files) - #125027 (Migrate `run-make/c-link-to-rust-staticlib` to `rmake`) - #125038 (Invert comparison in `uN::checked_sub`) - #125104 (Migrate `run-make/no-cdylib-as-rdylib` to `rmake`) - #125137 (MIR operators: clarify Shl/Shr handling of negative offsets) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3cb0030 + 8d38f2f commit ade234d

15 files changed

Lines changed: 190 additions & 125 deletions

File tree

‎compiler/rustc_middle/src/mir/syntax.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,13 +1480,17 @@ pub enum BinOp {
14801480
BitOr,
14811481
/// The `<<` operator (shift left)
14821482
///
1483-
/// The offset is truncated to the size of the first operand and made unsigned before shifting.
1483+
/// The offset is (uniquely) determined as follows:
1484+
/// - it is "equal modulo LHS::BITS" to the RHS
1485+
/// - it is in the range `0..LHS::BITS`
14841486
Shl,
14851487
/// Like `Shl`, but is UB if the RHS >= LHS::BITS or RHS < 0
14861488
ShlUnchecked,
14871489
/// The `>>` operator (shift right)
14881490
///
1489-
/// The offset is truncated to the size of the first operand and made unsigned before shifting.
1491+
/// The offset is (uniquely) determined as follows:
1492+
/// - it is "equal modulo LHS::BITS" to the RHS
1493+
/// - it is in the range `0..LHS::BITS`
14901494
///
14911495
/// This is an arithmetic shift if the LHS is signed
14921496
/// and a logical shift if the LHS is unsigned.

‎library/core/src/ascii.rs‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,21 @@ pub struct EscapeDefault(escape::EscapeIterInner<4>);
9191
/// ```
9292
#[stable(feature = "rust1", since = "1.0.0")]
9393
pubfnescape_default(c:u8) -> EscapeDefault{
94-
letmut data = [Char::Null;4];
95-
let range = escape::escape_ascii_into(&mut data, c);
96-
EscapeDefault(escape::EscapeIterInner::new(data, range))
94+
EscapeDefault::new(c)
9795
}
9896

9997
implEscapeDefault{
98+
#[inline]
99+
pub(crate)constfnnew(c:u8) -> Self{
100+
Self(escape::EscapeIterInner::ascii(c))
101+
}
102+
103+
#[inline]
100104
pub(crate)fnempty() -> Self{
101-
let data = [Char::Null;4];
102-
EscapeDefault(escape::EscapeIterInner::new(data,0..0))
105+
Self(escape::EscapeIterInner::empty())
103106
}
104107

108+
#[inline]
105109
pub(crate)fnas_str(&self) -> &str{
106110
self.0.as_str()
107111
}

‎library/core/src/char/methods.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,10 @@ impl char {
449449
'\"'if args.escape_double_quote => EscapeDebug::backslash(ascii::Char::QuotationMark),
450450
'\''if args.escape_single_quote => EscapeDebug::backslash(ascii::Char::Apostrophe),
451451
_ if args.escape_grapheme_extended && self.is_grapheme_extended() => {
452-
EscapeDebug::from_unicode(self.escape_unicode())
452+
EscapeDebug::unicode(self)
453453
}
454454
_ ifis_printable(self) => EscapeDebug::printable(self),
455-
_ => EscapeDebug::from_unicode(self.escape_unicode()),
455+
_ => EscapeDebug::unicode(self),
456456
}
457457
}
458458

@@ -555,9 +555,9 @@ impl char {
555555
'\t' => EscapeDefault::backslash(ascii::Char::SmallT),
556556
'\r' => EscapeDefault::backslash(ascii::Char::SmallR),
557557
'\n' => EscapeDefault::backslash(ascii::Char::SmallN),
558-
'\\' | '\'' | '"' => EscapeDefault::backslash(self.as_ascii().unwrap()),
558+
'\\' | '\'' | '\"' => EscapeDefault::backslash(self.as_ascii().unwrap()),
559559
'\x20'..='\x7e' => EscapeDefault::printable(self.as_ascii().unwrap()),
560-
_ => EscapeDefault::from_unicode(self.escape_unicode()),
560+
_ => EscapeDefault::unicode(self),
561561
}
562562
}
563563

‎library/core/src/char/mod.rs‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,9 @@ pub const fn from_digit(num: u32, radix: u32) -> Option<char> {
152152
pubstructEscapeUnicode(escape::EscapeIterInner<10>);
153153

154154
implEscapeUnicode{
155-
fnnew(chr:char) -> Self{
156-
letmut data = [ascii::Char::Null;10];
157-
let range = escape::escape_unicode_into(&mut data, chr);
158-
Self(escape::EscapeIterInner::new(data, range))
155+
#[inline]
156+
constfnnew(c:char) -> Self{
157+
Self(escape::EscapeIterInner::unicode(c))
159158
}
160159
}
161160

@@ -219,18 +218,19 @@ impl fmt::Display for EscapeUnicode {
219218
pubstructEscapeDefault(escape::EscapeIterInner<10>);
220219

221220
implEscapeDefault{
222-
fnprintable(chr: ascii::Char) -> Self{
223-
let data = [chr];
224-
Self(escape::EscapeIterInner::from_array(data))
221+
#[inline]
222+
constfnprintable(c: ascii::Char) -> Self{
223+
Self(escape::EscapeIterInner::ascii(c.to_u8()))
225224
}
226225

227-
fnbackslash(chr: ascii::Char) -> Self{
228-
let data = [ascii::Char::ReverseSolidus, chr];
229-
Self(escape::EscapeIterInner::from_array(data))
226+
#[inline]
227+
constfnbackslash(c:ascii::Char) -> Self{
228+
Self(escape::EscapeIterInner::backslash(c))
230229
}
231230

232-
fnfrom_unicode(esc:EscapeUnicode) -> Self{
233-
Self(esc.0)
231+
#[inline]
232+
constfnunicode(c:char) -> Self{
233+
Self(escape::EscapeIterInner::unicode(c))
234234
}
235235
}
236236

@@ -304,23 +304,24 @@ enum EscapeDebugInner {
304304
}
305305

306306
implEscapeDebug{
307-
fnprintable(chr:char) -> Self{
307+
#[inline]
308+
constfnprintable(chr:char) -> Self{
308309
Self(EscapeDebugInner::Char(chr))
309310
}
310311

311-
fnbackslash(chr: ascii::Char) -> Self{
312-
let data = [ascii::Char::ReverseSolidus, chr];
313-
let iter = escape::EscapeIterInner::from_array(data);
314-
Self(EscapeDebugInner::Bytes(iter))
312+
#[inline]
313+
constfnbackslash(c: ascii::Char) -> Self{
314+
Self(EscapeDebugInner::Bytes(escape::EscapeIterInner::backslash(c)))
315315
}
316316

317-
fnfrom_unicode(esc:EscapeUnicode) -> Self{
318-
Self(EscapeDebugInner::Bytes(esc.0))
317+
#[inline]
318+
constfnunicode(c:char) -> Self{
319+
Self(EscapeDebugInner::Bytes(escape::EscapeIterInner::unicode(c)))
319320
}
320321

322+
#[inline]
321323
fnclear(&mutself){
322-
let bytes = escape::EscapeIterInner::from_array([]);
323-
self.0 = EscapeDebugInner::Bytes(bytes);
324+
self.0 = EscapeDebugInner::Bytes(escape::EscapeIterInner::empty());
324325
}
325326
}
326327

@@ -339,6 +340,7 @@ impl Iterator for EscapeDebug {
339340
}
340341
}
341342

343+
#[inline]
342344
fnsize_hint(&self) -> (usize,Option<usize>){
343345
let n = self.len();
344346
(n,Some(n))

‎library/core/src/escape.rs‎

Lines changed: 90 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,79 @@ use crate::ops::Range;
66

77
constHEX_DIGITS:[ascii::Char;16] = *b"0123456789abcdef".as_ascii().unwrap();
88

9-
/// Escapes a byte into provided buffer; returns length of escaped
10-
/// representation.
11-
pub(crate)fnescape_ascii_into(output:&mut[ascii::Char;4],byte:u8) -> Range<u8>{
12-
#[inline]
13-
fnbackslash(a: ascii::Char) -> ([ascii::Char;4],u8){
14-
([ascii::Char::ReverseSolidus, a, ascii::Char::Null, ascii::Char::Null],2)
15-
}
9+
#[inline]
10+
constfnbackslash<constN:usize>(a: ascii::Char) -> ([ascii::Char;N],Range<u8>){
11+
const{assert!(N >= 2)};
12+
13+
letmut output = [ascii::Char::Null;N];
14+
15+
output[0] = ascii::Char::ReverseSolidus;
16+
output[1] = a;
17+
18+
(output,0..2)
19+
}
1620

17-
let(data, len) = match byte {
21+
/// Escapes an ASCII character.
22+
///
23+
/// Returns a buffer and the length of the escaped representation.
24+
constfnescape_ascii<constN:usize>(byte:u8) -> ([ascii::Char;N],Range<u8>){
25+
const{assert!(N >= 4)};
26+
27+
match byte {
1828
b'\t' => backslash(ascii::Char::SmallT),
1929
b'\r' => backslash(ascii::Char::SmallR),
2030
b'\n' => backslash(ascii::Char::SmallN),
2131
b'\\' => backslash(ascii::Char::ReverseSolidus),
2232
b'\'' => backslash(ascii::Char::Apostrophe),
2333
b'\"' => backslash(ascii::Char::QuotationMark),
24-
_ => {
25-
ifletSome(a) = byte.as_ascii()
34+
byte => {
35+
letmut output = [ascii::Char::Null;N];
36+
37+
ifletSome(c) = byte.as_ascii()
2638
&& !byte.is_ascii_control()
2739
{
28-
([a, ascii::Char::Null, ascii::Char::Null, ascii::Char::Null],1)
40+
output[0] = c;
41+
(output,0..1)
2942
}else{
30-
let hi = HEX_DIGITS[usize::from(byte >> 4)];
31-
let lo = HEX_DIGITS[usize::from(byte &0xf)];
32-
([ascii::Char::ReverseSolidus, ascii::Char::SmallX, hi, lo],4)
43+
let hi = HEX_DIGITS[(byte >> 4)asusize];
44+
let lo = HEX_DIGITS[(byte &0xf)asusize];
45+
46+
output[0] = ascii::Char::ReverseSolidus;
47+
output[1] = ascii::Char::SmallX;
48+
output[2] = hi;
49+
output[3] = lo;
50+
51+
(output,0..4)
3352
}
3453
}
35-
};
36-
*output = data;
37-
0..len
54+
}
3855
}
3956

40-
/// Escapes a character into provided buffer using `\u{NNNN}` representation.
41-
pub(crate)fnescape_unicode_into(output:&mut[ascii::Char;10],ch:char) -> Range<u8>{
57+
/// Escapes a character `\u{NNNN}` representation.
58+
///
59+
/// Returns a buffer and the length of the escaped representation.
60+
constfnescape_unicode<constN:usize>(c:char) -> ([ascii::Char;N],Range<u8>){
61+
const{assert!(N >= 10 && N < u8::MAXasusize)};
62+
63+
let c = u32::from(c);
64+
65+
// OR-ing `1` ensures that for `c == 0` the code computes that
66+
// one digit should be printed.
67+
let start = (c | 1).leading_zeros()asusize / 4 - 2;
68+
69+
letmut output = [ascii::Char::Null;N];
70+
output[3] = HEX_DIGITS[((c >> 20)&15)asusize];
71+
output[4] = HEX_DIGITS[((c >> 16)&15)asusize];
72+
output[5] = HEX_DIGITS[((c >> 12)&15)asusize];
73+
output[6] = HEX_DIGITS[((c >> 8)&15)asusize];
74+
output[7] = HEX_DIGITS[((c >> 4)&15)asusize];
75+
output[8] = HEX_DIGITS[((c >> 0)&15)asusize];
4276
output[9] = ascii::Char::RightCurlyBracket;
77+
output[start + 0] = ascii::Char::ReverseSolidus;
78+
output[start + 1] = ascii::Char::SmallU;
79+
output[start + 2] = ascii::Char::LeftCurlyBracket;
4380

44-
let ch = ch asu32;
45-
output[3] = HEX_DIGITS[((ch >> 20)&15)asusize];
46-
output[4] = HEX_DIGITS[((ch >> 16)&15)asusize];
47-
output[5] = HEX_DIGITS[((ch >> 12)&15)asusize];
48-
output[6] = HEX_DIGITS[((ch >> 8)&15)asusize];
49-
output[7] = HEX_DIGITS[((ch >> 4)&15)asusize];
50-
output[8] = HEX_DIGITS[((ch >> 0)&15)asusize];
51-
52-
// or-ing 1 ensures that for ch==0 the code computes that one digit should
53-
// be printed.
54-
let start = (ch | 1).leading_zeros()asusize / 4 - 2;
55-
constUNICODE_ESCAPE_PREFIX:&[ascii::Char;3] = b"\\u{".as_ascii().unwrap();
56-
output[start..][..3].copy_from_slice(UNICODE_ESCAPE_PREFIX);
57-
58-
(start asu8)..10
81+
(output,(start asu8)..(Nasu8))
5982
}
6083

6184
/// An iterator over an fixed-size array.
@@ -65,45 +88,63 @@ pub(crate) fn escape_unicode_into(output: &mut [ascii::Char; 10], ch: char) -> R
6588
#[derive(Clone,Debug)]
6689
pub(crate)structEscapeIterInner<constN:usize>{
6790
// The element type ensures this is always ASCII, and thus also valid UTF-8.
68-
pub(crate)data:[ascii::Char;N],
91+
data:[ascii::Char;N],
6992

70-
// Invariant: alive.start <= alive.end <= N.
71-
pub(crate)alive:Range<u8>,
93+
// Invariant: `alive.start <= alive.end <= N`
94+
alive:Range<u8>,
7295
}
7396

7497
impl<constN:usize>EscapeIterInner<N>{
75-
pubfnnew(data:[ascii::Char;N],alive:Range<u8>) -> Self{
76-
const{assert!(N < 256)};
77-
debug_assert!(alive.start <= alive.end && usize::from(alive.end) <= N,"{alive:?}");
78-
Self{ data, alive }
98+
pubconstfnbackslash(c: ascii::Char) -> Self{
99+
let(data, range) = backslash(c);
100+
Self{ data,alive: range }
101+
}
102+
103+
pubconstfnascii(c:u8) -> Self{
104+
let(data, range) = escape_ascii(c);
105+
Self{ data,alive: range }
79106
}
80107

81-
pubfnfrom_array<constM:usize>(array:[ascii::Char;M]) -> Self{
82-
const{assert!(M <= N)};
108+
pubconstfnunicode(c:char) -> Self{
109+
let(data, range) = escape_unicode(c);
110+
Self{ data,alive: range }
111+
}
83112

84-
letmut data = [ascii::Char::Null;N];
85-
data[..M].copy_from_slice(&array);
86-
Self::new(data,0..Masu8)
113+
#[inline]
114+
pubconstfnempty() -> Self{
115+
Self{data:[ascii::Char::Null;N],alive:0..0}
87116
}
88117

118+
#[inline]
89119
pubfnas_ascii(&self) -> &[ascii::Char]{
90-
&self.data[usize::from(self.alive.start)..usize::from(self.alive.end)]
120+
// SAFETY: `self.alive` is guaranteed to be a valid range for indexing `self.data`.
121+
unsafe{
122+
self.data.get_unchecked(usize::from(self.alive.start)..usize::from(self.alive.end))
123+
}
91124
}
92125

126+
#[inline]
93127
pubfnas_str(&self) -> &str{
94128
self.as_ascii().as_str()
95129
}
96130

131+
#[inline]
97132
pubfnlen(&self) -> usize{
98133
usize::from(self.alive.end - self.alive.start)
99134
}
100135

101136
pubfnnext(&mutself) -> Option<u8>{
102-
self.alive.next().map(|i| self.data[usize::from(i)].to_u8())
137+
let i = self.alive.next()?;
138+
139+
// SAFETY: `i` is guaranteed to be a valid index for `self.data`.
140+
unsafe{Some(self.data.get_unchecked(usize::from(i)).to_u8())}
103141
}
104142

105143
pubfnnext_back(&mutself) -> Option<u8>{
106-
self.alive.next_back().map(|i| self.data[usize::from(i)].to_u8())
144+
let i = self.alive.next_back()?;
145+
146+
// SAFETY: `i` is guaranteed to be a valid index for `self.data`.
147+
unsafe{Some(self.data.get_unchecked(usize::from(i)).to_u8())}
107148
}
108149

109150
pubfnadvance_by(&mutself,n:usize) -> Result<(),NonZero<usize>>{

‎library/core/src/num/uint_macros.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,11 @@ macro_rules! uint_impl {
584584
// Thus, rather than using `overflowing_sub` that produces a wrapping
585585
// subtraction, check it ourself so we can use an unchecked one.
586586

587-
ifself >= rhs {
587+
ifself < rhs {
588+
None
589+
} else {
588590
// SAFETY: just checked this can't overflow
589591
Some(unsafe{ intrinsics::unchecked_sub(self, rhs)})
590-
} else {
591-
None
592592
}
593593
}
594594

‎src/bootstrap/src/core/build_steps/dist.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ use crate::core::build_steps::tool::{self, Tool};
2626
usecrate::core::builder::{Builder,Kind,RunConfig,ShouldRun,Step};
2727
usecrate::core::config::TargetSelection;
2828
usecrate::utils::channel::{self,Info};
29-
usecrate::utils::helpers::{exe, is_dylib, output, t, target_supports_cranelift_backend, timeit};
29+
usecrate::utils::helpers::{
30+
exe, is_dylib, move_file, output, t, target_supports_cranelift_backend, timeit,
31+
};
3032
usecrate::utils::tarball::{GeneratedTarball,OverlayKind,Tarball};
3133
usecrate::{Compiler,DependencyType,Mode,LLVM_TOOLS};
3234

@@ -2024,7 +2026,7 @@ impl Step for Extended {
20242026
builder.run(&mut cmd);
20252027

20262028
if !builder.config.dry_run(){
2027-
t!(fs::rename(exe.join(&filename), distdir(builder).join(&filename)));
2029+
t!(move_file(exe.join(&filename), distdir(builder).join(&filename)));
20282030
}
20292031
}
20302032
}

0 commit comments

Comments
 (0)