Skip to content

Commit b8d158b

Browse files
Rollup merge of #77160 - ecstatic-morse:const-fn-transmute-suggestion, r=oli-obk
Suggest `const_fn_transmute`, not `const_fn` More fallout from #76850 in the vein of #77134. The fix is the same. I looked through the structured errors file and didn't see any more of this kind of diagnostics bug. r? @oli-obk
2 parents 473ae22 + 0f59469 commit b8d158b

File tree

5 files changed

+60
-43
lines changed

5 files changed

+60
-43
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,14 @@ impl NonConstOp for Transmute {
489489
}
490490

491491
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
492-
mcf_emit_error(ccx, span, "can only call `transmute` from const items, not `const fn`");
492+
feature_err(
493+
&ccx.tcx.sess.parse_sess,
494+
sym::const_fn_transmute,
495+
span,
496+
&format!("`transmute` is not allowed in {}s", ccx.const_kind()),
497+
)
498+
.note("`transmute` is only allowed in constants and statics for now")
499+
.emit();
493500
}
494501
}
495502

src/test/ui/feature-gates/feature-gate-const_fn_transmute.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@ struct Foo(u32);
66
const TRANSMUTED_U32: u32 = unsafe { mem::transmute(Foo(3)) };
77

88
const fn transmute_fn() -> u32 { unsafe { mem::transmute(Foo(3)) } }
9-
//~^ ERROR can only call `transmute` from const items, not `const fn`
9+
//~^ ERROR `transmute`
1010

1111
const fn transmute_fn_intrinsic() -> u32 { unsafe { std::intrinsics::transmute(Foo(3)) } }
12-
//~^ ERROR can only call `transmute` from const items, not `const fn`
12+
//~^ ERROR `transmute`
1313

1414
const fn transmute_fn_core_intrinsic() -> u32 { unsafe { core::intrinsics::transmute(Foo(3)) } }
15-
//~^ ERROR can only call `transmute` from const items, not `const fn`
15+
//~^ ERROR `transmute`
1616

1717
const unsafe fn unsafe_transmute_fn() -> u32 { mem::transmute(Foo(3)) }
18-
//~^ ERROR can only call `transmute` from const items, not `const fn`
18+
//~^ ERROR `transmute`
1919

2020
const unsafe fn unsafe_transmute_fn_intrinsic() -> u32 { std::intrinsics::transmute(Foo(3)) }
21-
//~^ ERROR can only call `transmute` from const items, not `const fn`
21+
//~^ ERROR `transmute`
2222

2323
const unsafe fn unsafe_transmute_fn_core_intrinsic() -> u32 { core::intrinsics::transmute(Foo(3)) }
24-
//~^ ERROR can only call `transmute` from const items, not `const fn`
24+
//~^ ERROR `transmute`
2525

2626
const fn safe_transmute_fn() -> u32 { mem::transmute(Foo(3)) }
27-
//~^ ERROR can only call `transmute` from const items, not `const fn`
27+
//~^ ERROR `transmute`
2828
//~| ERROR call to unsafe function is unsafe and requires unsafe function or block
2929

3030
const fn safe_transmute_fn_intrinsic() -> u32 { std::intrinsics::transmute(Foo(3)) }
31-
//~^ ERROR can only call `transmute` from const items, not `const fn`
31+
//~^ ERROR `transmute`
3232
//~| ERROR call to unsafe function is unsafe and requires unsafe function or block
3333

3434
const fn safe_transmute_fn_core_intrinsic() -> u32 { core::intrinsics::transmute(Foo(3)) }
35-
//~^ ERROR can only call `transmute` from const items, not `const fn`
35+
//~^ ERROR `transmute`
3636
//~| ERROR call to unsafe function is unsafe and requires unsafe function or block
3737

3838
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,92 @@
1-
error[E0723]: can only call `transmute` from const items, not `const fn`
1+
error[E0658]: `transmute` is not allowed in constant functions
22
--> $DIR/feature-gate-const_fn_transmute.rs:8:43
33
|
44
LL | const fn transmute_fn() -> u32 { unsafe { mem::transmute(Foo(3)) } }
55
| ^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
8-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
7+
= note: see issue #53605 <https://github.com/rust-lang/rust/issues/53605> for more information
8+
= help: add `#![feature(const_fn_transmute)]` to the crate attributes to enable
9+
= note: `transmute` is only allowed in constants and statics for now
910

10-
error[E0723]: can only call `transmute` from const items, not `const fn`
11+
error[E0658]: `transmute` is not allowed in constant functions
1112
--> $DIR/feature-gate-const_fn_transmute.rs:11:53
1213
|
1314
LL | const fn transmute_fn_intrinsic() -> u32 { unsafe { std::intrinsics::transmute(Foo(3)) } }
1415
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1516
|
16-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
17-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
17+
= note: see issue #53605 <https://github.com/rust-lang/rust/issues/53605> for more information
18+
= help: add `#![feature(const_fn_transmute)]` to the crate attributes to enable
19+
= note: `transmute` is only allowed in constants and statics for now
1820

19-
error[E0723]: can only call `transmute` from const items, not `const fn`
21+
error[E0658]: `transmute` is not allowed in constant functions
2022
--> $DIR/feature-gate-const_fn_transmute.rs:14:58
2123
|
2224
LL | const fn transmute_fn_core_intrinsic() -> u32 { unsafe { core::intrinsics::transmute(Foo(3)) } }
2325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2426
|
25-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
26-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
27+
= note: see issue #53605 <https://github.com/rust-lang/rust/issues/53605> for more information
28+
= help: add `#![feature(const_fn_transmute)]` to the crate attributes to enable
29+
= note: `transmute` is only allowed in constants and statics for now
2730

28-
error[E0723]: can only call `transmute` from const items, not `const fn`
31+
error[E0658]: `transmute` is not allowed in constant functions
2932
--> $DIR/feature-gate-const_fn_transmute.rs:17:48
3033
|
3134
LL | const unsafe fn unsafe_transmute_fn() -> u32 { mem::transmute(Foo(3)) }
3235
| ^^^^^^^^^^^^^^^^^^^^^^
3336
|
34-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
35-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
37+
= note: see issue #53605 <https://github.com/rust-lang/rust/issues/53605> for more information
38+
= help: add `#![feature(const_fn_transmute)]` to the crate attributes to enable
39+
= note: `transmute` is only allowed in constants and statics for now
3640

37-
error[E0723]: can only call `transmute` from const items, not `const fn`
41+
error[E0658]: `transmute` is not allowed in constant functions
3842
--> $DIR/feature-gate-const_fn_transmute.rs:20:58
3943
|
4044
LL | const unsafe fn unsafe_transmute_fn_intrinsic() -> u32 { std::intrinsics::transmute(Foo(3)) }
4145
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4246
|
43-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
44-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
47+
= note: see issue #53605 <https://github.com/rust-lang/rust/issues/53605> for more information
48+
= help: add `#![feature(const_fn_transmute)]` to the crate attributes to enable
49+
= note: `transmute` is only allowed in constants and statics for now
4550

46-
error[E0723]: can only call `transmute` from const items, not `const fn`
51+
error[E0658]: `transmute` is not allowed in constant functions
4752
--> $DIR/feature-gate-const_fn_transmute.rs:23:63
4853
|
4954
LL | const unsafe fn unsafe_transmute_fn_core_intrinsic() -> u32 { core::intrinsics::transmute(Foo(3)) }
5055
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5156
|
52-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
53-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
57+
= note: see issue #53605 <https://github.com/rust-lang/rust/issues/53605> for more information
58+
= help: add `#![feature(const_fn_transmute)]` to the crate attributes to enable
59+
= note: `transmute` is only allowed in constants and statics for now
5460

55-
error[E0723]: can only call `transmute` from const items, not `const fn`
61+
error[E0658]: `transmute` is not allowed in constant functions
5662
--> $DIR/feature-gate-const_fn_transmute.rs:26:39
5763
|
5864
LL | const fn safe_transmute_fn() -> u32 { mem::transmute(Foo(3)) }
5965
| ^^^^^^^^^^^^^^^^^^^^^^
6066
|
61-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
62-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
67+
= note: see issue #53605 <https://github.com/rust-lang/rust/issues/53605> for more information
68+
= help: add `#![feature(const_fn_transmute)]` to the crate attributes to enable
69+
= note: `transmute` is only allowed in constants and statics for now
6370

64-
error[E0723]: can only call `transmute` from const items, not `const fn`
71+
error[E0658]: `transmute` is not allowed in constant functions
6572
--> $DIR/feature-gate-const_fn_transmute.rs:30:49
6673
|
6774
LL | const fn safe_transmute_fn_intrinsic() -> u32 { std::intrinsics::transmute(Foo(3)) }
6875
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6976
|
70-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
71-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
77+
= note: see issue #53605 <https://github.com/rust-lang/rust/issues/53605> for more information
78+
= help: add `#![feature(const_fn_transmute)]` to the crate attributes to enable
79+
= note: `transmute` is only allowed in constants and statics for now
7280

73-
error[E0723]: can only call `transmute` from const items, not `const fn`
81+
error[E0658]: `transmute` is not allowed in constant functions
7482
--> $DIR/feature-gate-const_fn_transmute.rs:34:54
7583
|
7684
LL | const fn safe_transmute_fn_core_intrinsic() -> u32 { core::intrinsics::transmute(Foo(3)) }
7785
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7886
|
79-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
80-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
87+
= note: see issue #53605 <https://github.com/rust-lang/rust/issues/53605> for more information
88+
= help: add `#![feature(const_fn_transmute)]` to the crate attributes to enable
89+
= note: `transmute` is only allowed in constants and statics for now
8190

8291
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
8392
--> $DIR/feature-gate-const_fn_transmute.rs:26:39
@@ -105,5 +114,5 @@ LL | const fn safe_transmute_fn_core_intrinsic() -> u32 { core::intrinsics::tran
105114

106115
error: aborting due to 12 previous errors
107116

108-
Some errors have detailed explanations: E0133, E0723.
117+
Some errors have detailed explanations: E0133, E0658.
109118
For more information about an error, try `rustc --explain E0133`.

src/test/ui/internal/internal-unstable-const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#[stable(feature = "rust1", since = "1.0.0")]
99
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
1010
pub const fn foo() -> i32 {
11-
unsafe { std::mem::transmute(4u32) } //~ ERROR can only call `transmute` from const items
11+
unsafe { std::mem::transmute(4u32) } //~ ERROR `transmute`
1212
}
1313

1414
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
error[E0723]: can only call `transmute` from const items, not `const fn`
1+
error[E0658]: `transmute` is not allowed in constant functions
22
--> $DIR/internal-unstable-const.rs:11:14
33
|
44
LL | unsafe { std::mem::transmute(4u32) }
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
8-
= help: add `#![feature(const_fn)]` to the crate attributes to enable
7+
= note: see issue #53605 <https://github.com/rust-lang/rust/issues/53605> for more information
8+
= help: add `#![feature(const_fn_transmute)]` to the crate attributes to enable
9+
= note: `transmute` is only allowed in constants and statics for now
910

1011
error: aborting due to previous error
1112

12-
For more information about this error, try `rustc --explain E0723`.
13+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)