Skip to content
/ rust Public
forked from rust-lang/rust

Commit 3836aa7

Browse files
authored
Rollup merge of rust-lang#135139 - c410-f3r:8-years-rfc, r=jhpratt
[generic_assert] Constify methods used by the formatting system cc rust-lang#44838 Starts the "constification" of all the elements required to allow the execution of the formatting system in constant environments. ```rust const _: () = { panic!("{:?}", 1i32); }; ``` Further stuff is blocked by rust-lang#133999.
2 parents b5cf02f + db17be8 commit 3836aa7

File tree

4 files changed

+11
-31
lines changed

4 files changed

+11
-31
lines changed

library/core/src/fmt/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ impl<'a> Arguments<'a> {
596596
/// When using the format_args!() macro, this function is used to generate the
597597
/// Arguments structure.
598598
#[inline]
599-
pub fn new_v1<const P: usize, const A: usize>(
599+
pub const fn new_v1<const P: usize, const A: usize>(
600600
pieces: &'a [&'static str; P],
601601
args: &'a [rt::Argument<'a>; A],
602602
) -> Arguments<'a> {
@@ -612,7 +612,7 @@ impl<'a> Arguments<'a> {
612612
/// 2. Every `rt::Placeholder::position` value within `fmt` must be a valid index of `args`.
613613
/// 3. Every `rt::Count::Param` within `fmt` must contain a valid index of `args`.
614614
#[inline]
615-
pub fn new_v1_formatted(
615+
pub const fn new_v1_formatted(
616616
pieces: &'a [&'static str],
617617
args: &'a [rt::Argument<'a>],
618618
fmt: &'a [rt::Placeholder],

library/core/src/fmt/rt.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ pub struct Argument<'a> {
9696
#[rustc_diagnostic_item = "ArgumentMethods"]
9797
impl Argument<'_> {
9898
#[inline]
99-
fn new<'a, T>(x: &'a T, f: fn(&T, &mut Formatter<'_>) -> Result) -> Argument<'a> {
99+
const fn new<'a, T>(x: &'a T, f: fn(&T, &mut Formatter<'_>) -> Result) -> Argument<'a> {
100100
Argument {
101101
// INVARIANT: this creates an `ArgumentType<'a>` from a `&'a T` and
102102
// a `fn(&T, ...)`, so the invariant is maintained.
103103
ty: ArgumentType::Placeholder {
104-
value: NonNull::from(x).cast(),
104+
value: NonNull::from_ref(x).cast(),
105105
// SAFETY: function pointers always have the same layout.
106106
formatter: unsafe { mem::transmute(f) },
107107
_lifetime: PhantomData,
@@ -150,7 +150,7 @@ impl Argument<'_> {
150150
Self::new(x, UpperExp::fmt)
151151
}
152152
#[inline]
153-
pub fn from_usize(x: &usize) -> Argument<'_> {
153+
pub const fn from_usize(x: &usize) -> Argument<'_> {
154154
Argument { ty: ArgumentType::Count(*x) }
155155
}
156156

@@ -181,7 +181,7 @@ impl Argument<'_> {
181181
}
182182

183183
#[inline]
184-
pub(super) fn as_usize(&self) -> Option<usize> {
184+
pub(super) const fn as_usize(&self) -> Option<usize> {
185185
match self.ty {
186186
ArgumentType::Count(count) => Some(count),
187187
ArgumentType::Placeholder { .. } => None,
@@ -199,7 +199,7 @@ impl Argument<'_> {
199199
/// println!("{f}");
200200
/// ```
201201
#[inline]
202-
pub fn none() -> [Self; 0] {
202+
pub const fn none() -> [Self; 0] {
203203
[]
204204
}
205205
}
@@ -216,7 +216,7 @@ impl UnsafeArg {
216216
/// See documentation where `UnsafeArg` is required to know when it is safe to
217217
/// create and use `UnsafeArg`.
218218
#[inline]
219-
pub unsafe fn new() -> Self {
219+
pub const unsafe fn new() -> Self {
220220
Self { _private: () }
221221
}
222222
}

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

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

76
const fn print() {
87
println!("{:?}", 0);
98
//~^ ERROR cannot call non-const formatting macro in constant functions
10-
//~| ERROR cannot call non-const associated function `Arguments::<'_>::new_v1::<2, 1>` in constant functions
119
//~| ERROR cannot call non-const function `_print` in constant functions
1210
}
1311

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

+3-21
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,24 @@ 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 associated function `Arguments::<'_>::new_v1::<1, 1>` in constant functions
11-
--> $DIR/format.rs:2:5
12-
|
13-
LL | panic!("{:?}", 0);
14-
| ^^^^^^^^^^^^^^^^^
15-
|
16-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
17-
= 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)
18-
1910
error[E0015]: cannot call non-const formatting macro in constant functions
20-
--> $DIR/format.rs:8:15
11+
--> $DIR/format.rs:7:15
2112
|
2213
LL | println!("{:?}", 0);
2314
| ^^^^
2415
|
2516
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2617
= 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)
2718

28-
error[E0015]: cannot call non-const associated function `Arguments::<'_>::new_v1::<2, 1>` in constant functions
29-
--> $DIR/format.rs:8:5
30-
|
31-
LL | println!("{:?}", 0);
32-
| ^^^^^^^^^^^^^^^^^^^
33-
|
34-
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
35-
= 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)
36-
3719
error[E0015]: cannot call non-const function `_print` in constant functions
38-
--> $DIR/format.rs:8:5
20+
--> $DIR/format.rs:7:5
3921
|
4022
LL | println!("{:?}", 0);
4123
| ^^^^^^^^^^^^^^^^^^^
4224
|
4325
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
4426
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
4527

46-
error: aborting due to 5 previous errors
28+
error: aborting due to 3 previous errors
4729

4830
For more information about this error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)