Skip to content

Commit f174c31

Browse files
Rollup merge of rust-lang#123935 - tstsrt:fix-115423, r=oli-obk
Don't inline integer literals when they overflow - new attempt Basically rust-lang#116633 but I implemented the suggested changes. Fixes rust-lang#115423. Fixes rust-lang#116631. This is my first contribution to this repo so please let me know if I'm supposed to change something :)
2 parents 0a0a5a9 + 4c8d210 commit f174c31

File tree

3 files changed

+187
-77
lines changed

3 files changed

+187
-77
lines changed

compiler/rustc_ast_lowering/src/format.rs

+117-77
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,126 @@ impl<'hir> LoweringContext<'_, 'hir> {
2020
let mut fmt = Cow::Borrowed(fmt);
2121
if self.tcx.sess.opts.unstable_opts.flatten_format_args {
2222
fmt = flatten_format_args(fmt);
23-
fmt = inline_literals(fmt);
23+
fmt = self.inline_literals(fmt);
2424
}
2525
expand_format_args(self, sp, &fmt, allow_const)
2626
}
27+
28+
/// Try to convert a literal into an interned string
29+
fn try_inline_lit(&self, lit: token::Lit) -> Option<Symbol> {
30+
match LitKind::from_token_lit(lit) {
31+
Ok(LitKind::Str(s, _)) => Some(s),
32+
Ok(LitKind::Int(n, ty)) => {
33+
match ty {
34+
// unsuffixed integer literals are assumed to be i32's
35+
LitIntType::Unsuffixed => {
36+
(n <= i32::MAX as u128).then_some(Symbol::intern(&n.to_string()))
37+
}
38+
LitIntType::Signed(int_ty) => {
39+
let max_literal = self.int_ty_max(int_ty);
40+
(n <= max_literal).then_some(Symbol::intern(&n.to_string()))
41+
}
42+
LitIntType::Unsigned(uint_ty) => {
43+
let max_literal = self.uint_ty_max(uint_ty);
44+
(n <= max_literal).then_some(Symbol::intern(&n.to_string()))
45+
}
46+
}
47+
}
48+
_ => None,
49+
}
50+
}
51+
52+
/// Get the maximum value of int_ty. It is platform-dependent due to the byte size of isize
53+
fn int_ty_max(&self, int_ty: IntTy) -> u128 {
54+
match int_ty {
55+
IntTy::Isize => self.tcx.data_layout.pointer_size.signed_int_max() as u128,
56+
IntTy::I8 => i8::MAX as u128,
57+
IntTy::I16 => i16::MAX as u128,
58+
IntTy::I32 => i32::MAX as u128,
59+
IntTy::I64 => i64::MAX as u128,
60+
IntTy::I128 => i128::MAX as u128,
61+
}
62+
}
63+
64+
/// Get the maximum value of uint_ty. It is platform-dependent due to the byte size of usize
65+
fn uint_ty_max(&self, uint_ty: UintTy) -> u128 {
66+
match uint_ty {
67+
UintTy::Usize => self.tcx.data_layout.pointer_size.unsigned_int_max(),
68+
UintTy::U8 => u8::MAX as u128,
69+
UintTy::U16 => u16::MAX as u128,
70+
UintTy::U32 => u32::MAX as u128,
71+
UintTy::U64 => u64::MAX as u128,
72+
UintTy::U128 => u128::MAX as u128,
73+
}
74+
}
75+
76+
/// Inline literals into the format string.
77+
///
78+
/// Turns
79+
///
80+
/// `format_args!("Hello, {}! {} {}", "World", 123, x)`
81+
///
82+
/// into
83+
///
84+
/// `format_args!("Hello, World! 123 {}", x)`.
85+
fn inline_literals<'fmt>(&self, mut fmt: Cow<'fmt, FormatArgs>) -> Cow<'fmt, FormatArgs> {
86+
let mut was_inlined = vec![false; fmt.arguments.all_args().len()];
87+
let mut inlined_anything = false;
88+
89+
for i in 0..fmt.template.len() {
90+
let FormatArgsPiece::Placeholder(placeholder) = &fmt.template[i] else { continue };
91+
let Ok(arg_index) = placeholder.argument.index else { continue };
92+
93+
let mut literal = None;
94+
95+
if let FormatTrait::Display = placeholder.format_trait
96+
&& placeholder.format_options == Default::default()
97+
&& let arg = fmt.arguments.all_args()[arg_index].expr.peel_parens_and_refs()
98+
&& let ExprKind::Lit(lit) = arg.kind
99+
{
100+
literal = self.try_inline_lit(lit);
101+
}
102+
103+
if let Some(literal) = literal {
104+
// Now we need to mutate the outer FormatArgs.
105+
// If this is the first time, this clones the outer FormatArgs.
106+
let fmt = fmt.to_mut();
107+
// Replace the placeholder with the literal.
108+
fmt.template[i] = FormatArgsPiece::Literal(literal);
109+
was_inlined[arg_index] = true;
110+
inlined_anything = true;
111+
}
112+
}
113+
114+
// Remove the arguments that were inlined.
115+
if inlined_anything {
116+
let fmt = fmt.to_mut();
117+
118+
let mut remove = was_inlined;
119+
120+
// Don't remove anything that's still used.
121+
for_all_argument_indexes(&mut fmt.template, |index| remove[*index] = false);
122+
123+
// Drop all the arguments that are marked for removal.
124+
let mut remove_it = remove.iter();
125+
fmt.arguments.all_args_mut().retain(|_| remove_it.next() != Some(&true));
126+
127+
// Calculate the mapping of old to new indexes for the remaining arguments.
128+
let index_map: Vec<usize> = remove
129+
.into_iter()
130+
.scan(0, |i, remove| {
131+
let mapped = *i;
132+
*i += !remove as usize;
133+
Some(mapped)
134+
})
135+
.collect();
136+
137+
// Correct the indexes that refer to arguments that have shifted position.
138+
for_all_argument_indexes(&mut fmt.template, |index| *index = index_map[*index]);
139+
}
140+
141+
fmt
142+
}
27143
}
28144

29145
/// Flattens nested `format_args!()` into one.
@@ -103,82 +219,6 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
103219
fmt
104220
}
105221

106-
/// Inline literals into the format string.
107-
///
108-
/// Turns
109-
///
110-
/// `format_args!("Hello, {}! {} {}", "World", 123, x)`
111-
///
112-
/// into
113-
///
114-
/// `format_args!("Hello, World! 123 {}", x)`.
115-
fn inline_literals(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
116-
let mut was_inlined = vec![false; fmt.arguments.all_args().len()];
117-
let mut inlined_anything = false;
118-
119-
for i in 0..fmt.template.len() {
120-
let FormatArgsPiece::Placeholder(placeholder) = &fmt.template[i] else { continue };
121-
let Ok(arg_index) = placeholder.argument.index else { continue };
122-
123-
let mut literal = None;
124-
125-
if let FormatTrait::Display = placeholder.format_trait
126-
&& placeholder.format_options == Default::default()
127-
&& let arg = fmt.arguments.all_args()[arg_index].expr.peel_parens_and_refs()
128-
&& let ExprKind::Lit(lit) = arg.kind
129-
{
130-
if let token::LitKind::Str | token::LitKind::StrRaw(_) = lit.kind
131-
&& let Ok(LitKind::Str(s, _)) = LitKind::from_token_lit(lit)
132-
{
133-
literal = Some(s);
134-
} else if let token::LitKind::Integer = lit.kind
135-
&& let Ok(LitKind::Int(n, _)) = LitKind::from_token_lit(lit)
136-
{
137-
literal = Some(Symbol::intern(&n.to_string()));
138-
}
139-
}
140-
141-
if let Some(literal) = literal {
142-
// Now we need to mutate the outer FormatArgs.
143-
// If this is the first time, this clones the outer FormatArgs.
144-
let fmt = fmt.to_mut();
145-
// Replace the placeholder with the literal.
146-
fmt.template[i] = FormatArgsPiece::Literal(literal);
147-
was_inlined[arg_index] = true;
148-
inlined_anything = true;
149-
}
150-
}
151-
152-
// Remove the arguments that were inlined.
153-
if inlined_anything {
154-
let fmt = fmt.to_mut();
155-
156-
let mut remove = was_inlined;
157-
158-
// Don't remove anything that's still used.
159-
for_all_argument_indexes(&mut fmt.template, |index| remove[*index] = false);
160-
161-
// Drop all the arguments that are marked for removal.
162-
let mut remove_it = remove.iter();
163-
fmt.arguments.all_args_mut().retain(|_| remove_it.next() != Some(&true));
164-
165-
// Calculate the mapping of old to new indexes for the remaining arguments.
166-
let index_map: Vec<usize> = remove
167-
.into_iter()
168-
.scan(0, |i, remove| {
169-
let mapped = *i;
170-
*i += !remove as usize;
171-
Some(mapped)
172-
})
173-
.collect();
174-
175-
// Correct the indexes that refer to arguments that have shifted position.
176-
for_all_argument_indexes(&mut fmt.template, |index| *index = index_map[*index]);
177-
}
178-
179-
fmt
180-
}
181-
182222
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
183223
enum ArgumentType {
184224
Format(FormatTrait),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ only-64bit
2+
3+
fn main() {
4+
format_args!("{}", 0x8f_i8); // issue #115423
5+
//~^ ERROR literal out of range for `i8`
6+
format_args!("{}", 0xffff_ffff_u8); // issue #116633
7+
//~^ ERROR literal out of range for `u8`
8+
format_args!("{}", 0xffff_ffff_ffff_ffff_ffff_usize);
9+
//~^ ERROR literal out of range for `usize`
10+
format_args!("{}", 0x8000_0000_0000_0000_isize);
11+
//~^ ERROR literal out of range for `isize`
12+
format_args!("{}", 0xffff_ffff); // treat unsuffixed literals as i32
13+
//~^ ERROR literal out of range for `i32`
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error: literal out of range for `i8`
2+
--> $DIR/no-inline-literals-out-of-range.rs:4:24
3+
|
4+
LL | format_args!("{}", 0x8f_i8); // issue #115423
5+
| ^^^^^^^
6+
|
7+
= note: the literal `0x8f_i8` (decimal `143`) does not fit into the type `i8` and will become `-113i8`
8+
= note: `#[deny(overflowing_literals)]` on by default
9+
help: consider using the type `u8` instead
10+
|
11+
LL | format_args!("{}", 0x8f_u8); // issue #115423
12+
| ~~~~~~~
13+
help: to use as a negative number (decimal `-113`), consider using the type `u8` for the literal and cast it to `i8`
14+
|
15+
LL | format_args!("{}", 0x8f_u8 as i8); // issue #115423
16+
| ~~~~~~~~~~~~~
17+
18+
error: literal out of range for `u8`
19+
--> $DIR/no-inline-literals-out-of-range.rs:6:24
20+
|
21+
LL | format_args!("{}", 0xffff_ffff_u8); // issue #116633
22+
| ^^^^^^^^^^^^^^ help: consider using the type `u32` instead: `0xffff_ffff_u32`
23+
|
24+
= note: the literal `0xffff_ffff_u8` (decimal `4294967295`) does not fit into the type `u8` and will become `255u8`
25+
26+
error: literal out of range for `usize`
27+
--> $DIR/no-inline-literals-out-of-range.rs:8:24
28+
|
29+
LL | format_args!("{}", 0xffff_ffff_ffff_ffff_ffff_usize);
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= note: the literal `0xffff_ffff_ffff_ffff_ffff_usize` (decimal `1208925819614629174706175`) does not fit into the type `usize` and will become `18446744073709551615usize`
33+
34+
error: literal out of range for `isize`
35+
--> $DIR/no-inline-literals-out-of-range.rs:10:24
36+
|
37+
LL | format_args!("{}", 0x8000_0000_0000_0000_isize);
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
|
40+
= note: the literal `0x8000_0000_0000_0000_isize` (decimal `9223372036854775808`) does not fit into the type `isize` and will become `-9223372036854775808isize`
41+
42+
error: literal out of range for `i32`
43+
--> $DIR/no-inline-literals-out-of-range.rs:12:24
44+
|
45+
LL | format_args!("{}", 0xffff_ffff); // treat unsuffixed literals as i32
46+
| ^^^^^^^^^^^
47+
|
48+
= note: the literal `0xffff_ffff` (decimal `4294967295`) does not fit into the type `i32` and will become `-1i32`
49+
= help: consider using the type `u32` instead
50+
help: to use as a negative number (decimal `-1`), consider using the type `u32` for the literal and cast it to `i32`
51+
|
52+
LL | format_args!("{}", 0xffff_ffffu32 as i32); // treat unsuffixed literals as i32
53+
| ~~~~~~~~~~~~~~~~~~~~~
54+
55+
error: aborting due to 5 previous errors
56+

0 commit comments

Comments
 (0)