Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 75e2c5d

Browse files
committedMay 26, 2024
Auto merge of #125518 - saethlin:check-arguments-new-in-const, r=joboet
Move the checks for Arguments constructors to inline const Thanks `@Skgland` for pointing out this opportunity: #117804 (comment)
2 parents 0a59f11 + 9763222 commit 75e2c5d

File tree

6 files changed

+75
-87
lines changed

6 files changed

+75
-87
lines changed
 

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

+7-11
Original file line numberDiff line numberDiff line change
@@ -338,23 +338,19 @@ pub struct Arguments<'a> {
338338
impl<'a> Arguments<'a> {
339339
#[inline]
340340
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
341-
pub const fn new_const(pieces: &'a [&'static str]) -> Self {
342-
if pieces.len() > 1 {
343-
// Since panic!() expands to panic_fmt(format_args!()), using panic! here is both a
344-
// bit silly and also significantly increases the amount of MIR generated by panics.
345-
crate::panicking::panic_nounwind("invalid args");
346-
}
341+
pub const fn new_const<const N: usize>(pieces: &'a [&'static str; N]) -> Self {
342+
const { assert!(N <= 1) };
347343
Arguments { pieces, fmt: None, args: &[] }
348344
}
349345

350346
/// When using the format_args!() macro, this function is used to generate the
351347
/// Arguments structure.
352348
#[inline]
353-
pub fn new_v1(pieces: &'a [&'static str], args: &'a [rt::Argument<'a>]) -> Arguments<'a> {
354-
if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
355-
// See Arguments::new_const for why we don't use panic!.
356-
crate::panicking::panic_nounwind("invalid args");
357-
}
349+
pub fn new_v1<const P: usize, const A: usize>(
350+
pieces: &'a [&'static str; P],
351+
args: &'a [rt::Argument<'a>; A],
352+
) -> Arguments<'a> {
353+
const { assert!(P >= A && P <= A + 1, "invalid args") }
358354
Arguments { pieces, fmt: None, args }
359355
}
360356

‎tests/mir-opt/sroa/lifetimes.foo.ScalarReplacementOfAggregates.diff

+61-69
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,28 @@
1111
let _9: ();
1212
let _10: ();
1313
let mut _11: std::fmt::Arguments<'_>;
14-
let mut _12: &[&str];
15-
let mut _13: &[&str; 3];
16-
let _14: &[&str; 3];
17-
let _15: [&str; 3];
18-
let mut _16: &[core::fmt::rt::Argument<'_>];
19-
let mut _17: &[core::fmt::rt::Argument<'_>; 2];
20-
let _18: &[core::fmt::rt::Argument<'_>; 2];
21-
let _19: [core::fmt::rt::Argument<'_>; 2];
22-
let mut _20: core::fmt::rt::Argument<'_>;
23-
let mut _21: &std::boxed::Box<dyn std::fmt::Display>;
24-
let _22: &std::boxed::Box<dyn std::fmt::Display>;
25-
let mut _23: core::fmt::rt::Argument<'_>;
26-
let mut _24: &u32;
27-
let _25: &u32;
28-
let mut _27: bool;
14+
let mut _12: &[&str; 3];
15+
let _13: &[&str; 3];
16+
let _14: [&str; 3];
17+
let mut _15: &[core::fmt::rt::Argument<'_>; 2];
18+
let _16: &[core::fmt::rt::Argument<'_>; 2];
19+
let _17: [core::fmt::rt::Argument<'_>; 2];
20+
let mut _18: core::fmt::rt::Argument<'_>;
21+
let mut _19: &std::boxed::Box<dyn std::fmt::Display>;
22+
let _20: &std::boxed::Box<dyn std::fmt::Display>;
23+
let mut _21: core::fmt::rt::Argument<'_>;
24+
let mut _22: &u32;
25+
let _23: &u32;
26+
let mut _25: bool;
27+
let mut _26: isize;
28+
let mut _27: isize;
2929
let mut _28: isize;
30-
let mut _29: isize;
31-
let mut _30: isize;
32-
+ let _31: std::result::Result<std::boxed::Box<dyn std::fmt::Display>, <T as Err>::Err>;
33-
+ let _32: u32;
30+
+ let _29: std::result::Result<std::boxed::Box<dyn std::fmt::Display>, <T as Err>::Err>;
31+
+ let _30: u32;
3432
scope 1 {
3533
- debug foo => _1;
36-
+ debug ((foo: Foo<T>).0: std::result::Result<std::boxed::Box<dyn std::fmt::Display>, <T as Err>::Err>) => _31;
37-
+ debug ((foo: Foo<T>).1: u32) => _32;
34+
+ debug ((foo: Foo<T>).0: std::result::Result<std::boxed::Box<dyn std::fmt::Display>, <T as Err>::Err>) => _29;
35+
+ debug ((foo: Foo<T>).1: u32) => _30;
3836
let _5: std::result::Result<std::boxed::Box<dyn std::fmt::Display>, <T as Err>::Err>;
3937
scope 2 {
4038
debug x => _5;
@@ -44,17 +42,17 @@
4442
scope 4 {
4543
debug x => _8;
4644
let _8: std::boxed::Box<dyn std::fmt::Display>;
47-
let mut _26: &[&str; 3];
45+
let mut _24: &[&str; 3];
4846
}
4947
}
5048
}
5149
}
5250

5351
bb0: {
54-
_27 = const false;
52+
_25 = const false;
5553
- StorageLive(_1);
56-
+ StorageLive(_31);
57-
+ StorageLive(_32);
54+
+ StorageLive(_29);
55+
+ StorageLive(_30);
5856
+ nop;
5957
StorageLive(_2);
6058
StorageLive(_3);
@@ -68,83 +66,77 @@
6866
_2 = Result::<Box<dyn std::fmt::Display>, <T as Err>::Err>::Ok(move _3);
6967
StorageDead(_3);
7068
- _1 = Foo::<T> { x: move _2, y: const 7_u32 };
71-
+ _31 = move _2;
72-
+ _32 = const 7_u32;
69+
+ _29 = move _2;
70+
+ _30 = const 7_u32;
7371
+ nop;
7472
StorageDead(_2);
7573
StorageLive(_5);
76-
_27 = const true;
74+
_25 = const true;
7775
- _5 = move (_1.0: std::result::Result<std::boxed::Box<dyn std::fmt::Display>, <T as Err>::Err>);
78-
+ _5 = move _31;
76+
+ _5 = move _29;
7977
StorageLive(_6);
8078
- _6 = (_1.1: u32);
81-
+ _6 = _32;
79+
+ _6 = _30;
8280
_7 = discriminant(_5);
8381
switchInt(move _7) -> [0: bb2, otherwise: bb7];
8482
}
8583

8684
bb2: {
8785
StorageLive(_8);
88-
_27 = const false;
86+
_25 = const false;
8987
_8 = move ((_5 as Ok).0: std::boxed::Box<dyn std::fmt::Display>);
9088
StorageLive(_9);
9189
StorageLive(_10);
9290
StorageLive(_11);
9391
StorageLive(_12);
9492
StorageLive(_13);
95-
StorageLive(_14);
96-
_26 = const foo::<T>::promoted[0];
97-
_14 = &(*_26);
98-
_13 = &(*_14);
99-
_12 = move _13 as &[&str] (PointerCoercion(Unsize));
100-
StorageDead(_13);
93+
_24 = const foo::<T>::promoted[0];
94+
_13 = &(*_24);
95+
_12 = &(*_13);
96+
StorageLive(_15);
10197
StorageLive(_16);
10298
StorageLive(_17);
10399
StorageLive(_18);
104100
StorageLive(_19);
105101
StorageLive(_20);
106-
StorageLive(_21);
107-
StorageLive(_22);
108-
_22 = &_8;
109-
_21 = &(*_22);
110-
_20 = core::fmt::rt::Argument::<'_>::new_display::<Box<dyn std::fmt::Display>>(move _21) -> [return: bb3, unwind unreachable];
102+
_20 = &_8;
103+
_19 = &(*_20);
104+
_18 = core::fmt::rt::Argument::<'_>::new_display::<Box<dyn std::fmt::Display>>(move _19) -> [return: bb3, unwind unreachable];
111105
}
112106

113107
bb3: {
114-
StorageDead(_21);
108+
StorageDead(_19);
109+
StorageLive(_21);
110+
StorageLive(_22);
115111
StorageLive(_23);
116-
StorageLive(_24);
117-
StorageLive(_25);
118-
_25 = &_6;
119-
_24 = &(*_25);
120-
_23 = core::fmt::rt::Argument::<'_>::new_display::<u32>(move _24) -> [return: bb4, unwind unreachable];
112+
_23 = &_6;
113+
_22 = &(*_23);
114+
_21 = core::fmt::rt::Argument::<'_>::new_display::<u32>(move _22) -> [return: bb4, unwind unreachable];
121115
}
122116

123117
bb4: {
124-
StorageDead(_24);
125-
_19 = [move _20, move _23];
126-
StorageDead(_23);
127-
StorageDead(_20);
128-
_18 = &_19;
129-
_17 = &(*_18);
130-
_16 = move _17 as &[core::fmt::rt::Argument<'_>] (PointerCoercion(Unsize));
131-
StorageDead(_17);
132-
_11 = Arguments::<'_>::new_v1(move _12, move _16) -> [return: bb5, unwind unreachable];
118+
StorageDead(_22);
119+
_17 = [move _18, move _21];
120+
StorageDead(_21);
121+
StorageDead(_18);
122+
_16 = &_17;
123+
_15 = &(*_16);
124+
_11 = Arguments::<'_>::new_v1::<3, 2>(move _12, move _15) -> [return: bb5, unwind unreachable];
133125
}
134126

135127
bb5: {
136-
StorageDead(_16);
128+
StorageDead(_15);
137129
StorageDead(_12);
138130
_10 = _eprint(move _11) -> [return: bb6, unwind unreachable];
139131
}
140132

141133
bb6: {
142134
StorageDead(_11);
143-
StorageDead(_25);
144-
StorageDead(_22);
145-
StorageDead(_19);
146-
StorageDead(_18);
147-
StorageDead(_14);
135+
StorageDead(_23);
136+
StorageDead(_20);
137+
StorageDead(_17);
138+
StorageDead(_16);
139+
StorageDead(_13);
148140
StorageDead(_10);
149141
_9 = const ();
150142
StorageDead(_9);
@@ -164,22 +156,22 @@
164156

165157
bb9: {
166158
StorageDead(_6);
167-
_28 = discriminant(_5);
168-
switchInt(move _28) -> [0: bb11, otherwise: bb13];
159+
_26 = discriminant(_5);
160+
switchInt(move _26) -> [0: bb11, otherwise: bb13];
169161
}
170162

171163
bb10: {
172-
_27 = const false;
164+
_25 = const false;
173165
StorageDead(_5);
174166
- StorageDead(_1);
175-
+ StorageDead(_31);
176-
+ StorageDead(_32);
167+
+ StorageDead(_29);
168+
+ StorageDead(_30);
177169
+ nop;
178170
return;
179171
}
180172

181173
bb11: {
182-
switchInt(_27) -> [0: bb10, otherwise: bb12];
174+
switchInt(_25) -> [0: bb10, otherwise: bb12];
183175
}
184176

185177
bb12: {

‎tests/pretty/issue-4264.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
((::alloc::fmt::format as
3535
for<'a> fn(Arguments<'a>) -> String {format})(((format_arguments::new_const
3636
as
37-
fn(&[&'static str]) -> Arguments<'_> {Arguments::<'_>::new_const})((&([("test"
37+
fn(&[&'static str; 1]) -> Arguments<'_> {Arguments::<'_>::new_const::<1>})((&([("test"
3838
as &str)] as [&str; 1]) as &[&str; 1])) as Arguments<'_>))
3939
as String);
4040
(res as String)

‎tests/run-make/symbol-mangling-hashed/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ all:
3535
# Check hashed symbol name
3636

3737
[ "$$($(NM) $(TMPDIR)/$(DYLIB_NAME) | grep -c hello)" -eq "0" ]
38-
[ "$$($(NM) $(TMPDIR)/$(DYLIB_NAME) | grep _RNxC7a_dylib | grep -c ' T ')" -eq "1" ]
38+
[ "$$($(NM) $(TMPDIR)/$(DYLIB_NAME) | grep _RNxC7a_dylib | grep -c ' T ')" -eq "2" ]
3939

4040
[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep b_dylib | grep -c hello)" -eq "1" ]
41-
[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep _RNxC6a_rlib | grep -c ' T ')" -eq "1" ]
41+
[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep _RNxC6a_rlib | grep -c ' T ')" -eq "2" ]
4242
[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep _RNxC7a_dylib | grep -c ' U ')" -eq "1" ]
4343

4444
[ "$$($(NM) $(TMPDIR)/$(BIN_NAME) | grep _RNxC6a_rlib | grep -c ' U ')" -eq "1" ]

‎tests/ui/consts/const-eval/format.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const fn failure() {
22
panic!("{:?}", 0);
33
//~^ ERROR cannot call non-const formatting macro in constant functions
4-
//~| ERROR cannot call non-const fn `Arguments::<'_>::new_v1` in constant functions
4+
//~| ERROR cannot call non-const fn `Arguments::<'_>::new_v1::<1, 1>` in constant functions
55
}
66

77
const fn print() {
88
println!("{:?}", 0);
99
//~^ ERROR cannot call non-const formatting macro in constant functions
10-
//~| ERROR cannot call non-const fn `Arguments::<'_>::new_v1` in constant functions
10+
//~| ERROR cannot call non-const fn `Arguments::<'_>::new_v1::<2, 1>` in constant functions
1111
//~| ERROR cannot call non-const fn `_print` in constant functions
1212
}
1313

‎tests/ui/consts/const-eval/format.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | panic!("{:?}", 0);
77
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
88
= note: this error originates in the macro `$crate::const_format_args` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
99

10-
error[E0015]: cannot call non-const fn `Arguments::<'_>::new_v1` in constant functions
10+
error[E0015]: cannot call non-const fn `Arguments::<'_>::new_v1::<1, 1>` in constant functions
1111
--> $DIR/format.rs:2:5
1212
|
1313
LL | panic!("{:?}", 0);
@@ -25,7 +25,7 @@ LL | println!("{:?}", 0);
2525
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2626
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
2727

28-
error[E0015]: cannot call non-const fn `Arguments::<'_>::new_v1` in constant functions
28+
error[E0015]: cannot call non-const fn `Arguments::<'_>::new_v1::<2, 1>` in constant functions
2929
--> $DIR/format.rs:8:5
3030
|
3131
LL | println!("{:?}", 0);

0 commit comments

Comments
 (0)
Failed to load comments.