Skip to content

Commit 5eb7238

Browse files
authored
Rollup merge of rust-lang#98276 - compiler-errors:const-format-macro, r=oli-obk
Mention formatting macros when encountering `ArgumentV1` method in const Also open to just closing this if it's overkill. There are a lot of other distracting error messages around, so maybe it's not worth fixing just this one. Fixes rust-lang#93665
2 parents 3e5800b + 5373d73 commit 5eb7238

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use rustc_middle::mir;
1010
use rustc_middle::ty::print::with_no_trimmed_paths;
1111
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
1212
use rustc_middle::ty::{
13-
suggest_constraining_type_param, Adt, Closure, FnDef, FnPtr, Param, TraitPredicate, Ty,
13+
suggest_constraining_type_param, Adt, Closure, DefIdTree, FnDef, FnPtr, Param, TraitPredicate,
14+
Ty,
1415
};
1516
use rustc_middle::ty::{Binder, BoundConstness, ImplPolarity, TraitRef};
1617
use rustc_session::parse::feature_err;
@@ -300,6 +301,15 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
300301
diag_trait(&mut err, self_ty, tcx.lang_items().deref_trait().unwrap());
301302
err
302303
}
304+
_ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::ArgumentV1Methods) => {
305+
struct_span_err!(
306+
ccx.tcx.sess,
307+
span,
308+
E0015,
309+
"cannot call non-const formatting macro in {}s",
310+
ccx.const_kind(),
311+
)
312+
}
303313
_ => struct_span_err!(
304314
ccx.tcx.sess,
305315
span,

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ symbols! {
131131
Arc,
132132
Argument,
133133
ArgumentV1,
134+
ArgumentV1Methods,
134135
Arguments,
135136
AsMut,
136137
AsRef,

library/core/src/fmt/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ macro_rules! arg_new {
320320
};
321321
}
322322

323+
#[rustc_diagnostic_item = "ArgumentV1Methods"]
323324
impl<'a> ArgumentV1<'a> {
324325
#[doc(hidden)]
325326
#[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const fn failure() {
2+
panic!("{:?}", 0);
3+
//~^ ERROR cannot call non-const formatting macro in constant functions
4+
//~| ERROR erroneous constant used
5+
//~| ERROR erroneous constant used
6+
//~| WARN this was previously accepted by the compiler
7+
//~| WARN this was previously accepted by the compiler
8+
}
9+
10+
const fn print() {
11+
println!("{:?}", 0);
12+
//~^ ERROR cannot call non-const formatting macro in constant functions
13+
//~| ERROR `Arguments::<'a>::new_v1` is not yet stable as a const fn
14+
//~| ERROR cannot call non-const fn `_print` in constant functions
15+
//~| ERROR erroneous constant used
16+
//~| ERROR erroneous constant used
17+
//~| WARN this was previously accepted by the compiler
18+
//~| WARN this was previously accepted by the compiler
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
error[E0015]: cannot call non-const formatting macro in constant functions
2+
--> $DIR/format.rs:2:20
3+
|
4+
LL | panic!("{:?}", 0);
5+
| ^
6+
|
7+
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
8+
= note: this error originates in the macro `$crate::const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
9+
10+
error[E0015]: cannot call non-const formatting macro in constant functions
11+
--> $DIR/format.rs:11:22
12+
|
13+
LL | println!("{:?}", 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::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
18+
19+
error: `Arguments::<'a>::new_v1` is not yet stable as a const fn
20+
--> $DIR/format.rs:11:5
21+
|
22+
LL | println!("{:?}", 0);
23+
| ^^^^^^^^^^^^^^^^^^^
24+
|
25+
= help: add `#![feature(const_fmt_arguments_new)]` to the crate attributes to enable
26+
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
27+
28+
error[E0015]: cannot call non-const fn `_print` in constant functions
29+
--> $DIR/format.rs:11: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 `println` (in Nightly builds, run with -Z macro-backtrace for more info)
36+
37+
error: erroneous constant used
38+
--> $DIR/format.rs:2:12
39+
|
40+
LL | panic!("{:?}", 0);
41+
| ^^^^^^ referenced constant has errors
42+
|
43+
= note: `#[deny(const_err)]` on by default
44+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
45+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
46+
47+
error: erroneous constant used
48+
--> $DIR/format.rs:2:20
49+
|
50+
LL | panic!("{:?}", 0);
51+
| ^ referenced constant has errors
52+
|
53+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
54+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
55+
= note: this error originates in the macro `$crate::const_format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
56+
57+
error: erroneous constant used
58+
--> $DIR/format.rs:11:14
59+
|
60+
LL | println!("{:?}", 0);
61+
| ^^^^^^ referenced constant has errors
62+
|
63+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
64+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
65+
66+
error: erroneous constant used
67+
--> $DIR/format.rs:11:22
68+
|
69+
LL | println!("{:?}", 0);
70+
| ^ referenced constant has errors
71+
|
72+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
73+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
74+
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
75+
76+
error: aborting due to 8 previous errors
77+
78+
For more information about this error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)