Skip to content

Commit 7b199eb

Browse files
committed
Say what unstable feature is being used in a const-stable function
1 parent a9b03ff commit 7b199eb

File tree

7 files changed

+92
-19
lines changed

7 files changed

+92
-19
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -323,17 +323,19 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
323323
let gate = match op.status_in_item(self.ccx) {
324324
Status::Allowed => return,
325325

326-
Status::Unstable(gate) if self.tcx.features().active(gate) => {
326+
Status::Unstable(gate) => {
327327
let unstable_in_stable = self.ccx.is_const_stable_const_fn()
328328
&& !super::rustc_allow_const_fn_unstable(self.tcx, self.def_id(), gate);
329329
if unstable_in_stable {
330330
emit_unstable_in_stable_error(self.ccx, span, gate);
331+
return;
332+
} else if self.tcx.features().declared(gate) && self.tcx.features().active(gate) {
333+
return;
334+
} else {
335+
Some(gate)
331336
}
332-
333-
return;
334337
}
335338

336-
Status::Unstable(gate) => Some(gate),
337339
Status::Forbidden => None,
338340
};
339341

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
332332
pub struct FnCallUnstable(pub DefId, pub Option<Symbol>);
333333

334334
impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
335+
fn status_in_item(&self, _: &ConstCx<'_, 'tcx>) -> Status {
336+
if let Some(symbol) = self.1 { Status::Unstable(symbol) } else { Status::Forbidden }
337+
}
338+
335339
fn build_error(
336340
&self,
337341
ccx: &ConstCx<'_, 'tcx>,
@@ -345,7 +349,11 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
345349
.create_err(errors::UnstableConstFn { span, def_path: ccx.tcx.def_path_str(def_id) });
346350

347351
if ccx.is_const_stable_const_fn() {
348-
err.help("const-stable functions can only call other const-stable functions");
352+
if self.1.is_some() {
353+
bug!("this should be triggering the UnstableInStable lint instead");
354+
} else {
355+
err.help("const-stable functions can only call other const-stable functions");
356+
}
349357
} else if ccx.tcx.sess.is_nightly_build() {
350358
if let Some(feature) = feature {
351359
err.help(format!("add `#![feature({feature})]` to the crate attributes to enable"));

tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.stderr

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
error: `foo` is not yet stable as a const fn
1+
error: const-stable function cannot use `#[feature(foo)]`
22
--> $DIR/min_const_fn_libstd_stability.rs:16:25
33
|
44
LL | const fn bar() -> u32 { foo() }
55
| ^^^^^
66
|
7-
= help: const-stable functions can only call other const-stable functions
7+
help: if it is not part of the public API, make this function unstably const
8+
|
9+
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
10+
LL | const fn bar() -> u32 { foo() }
11+
|
12+
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
13+
|
14+
LL + #[rustc_allow_const_fn_unstable(foo)]
15+
LL | const fn bar() -> u32 { foo() }
16+
|
817

918
error: `foo2` is not yet stable as a const fn
1019
--> $DIR/min_const_fn_libstd_stability.rs:24:26

tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability.stderr

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
error: `foo` is not yet stable as a const fn
1+
error: const-stable function cannot use `#[feature(foo)]`
22
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:16:41
33
|
44
LL | const unsafe fn bar() -> u32 { unsafe { foo() } }
55
| ^^^^^
66
|
7-
= help: const-stable functions can only call other const-stable functions
7+
help: if it is not part of the public API, make this function unstably const
8+
|
9+
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
10+
LL | const unsafe fn bar() -> u32 { unsafe { foo() } }
11+
|
12+
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
13+
|
14+
LL + #[rustc_allow_const_fn_unstable(foo)]
15+
LL | const unsafe fn bar() -> u32 { unsafe { foo() } }
16+
|
817

918
error: `foo2` is not yet stable as a const fn
1019
--> $DIR/min_const_unsafe_fn_libstd_stability.rs:24:42

tests/ui/consts/min_const_fn/min_const_unsafe_fn_libstd_stability2.stderr

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
error: `foo` is not yet stable as a const fn
1+
error: const-stable function cannot use `#[feature(foo)]`
22
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:16:32
33
|
44
LL | const unsafe fn bar() -> u32 { foo() }
55
| ^^^^^
66
|
7-
= help: const-stable functions can only call other const-stable functions
7+
help: if it is not part of the public API, make this function unstably const
8+
|
9+
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
10+
LL | const unsafe fn bar() -> u32 { foo() }
11+
|
12+
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
13+
|
14+
LL + #[rustc_allow_const_fn_unstable(foo)]
15+
LL | const unsafe fn bar() -> u32 { foo() }
16+
|
817

918
error: `foo2` is not yet stable as a const fn
1019
--> $DIR/min_const_unsafe_fn_libstd_stability2.rs:24:33
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
error: `const_eval_select` is not yet stable as a const fn
1+
error: const-stable function cannot use `#[feature(const_eval_select)]`
22
--> $DIR/const-eval-select-stability.rs:17:5
33
|
44
LL | const_eval_select((), nothing, log);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= help: const-stable functions can only call other const-stable functions
7+
help: if it is not part of the public API, make this function unstably const
8+
|
9+
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
10+
LL | pub const unsafe fn hey() {
11+
|
12+
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
13+
|
14+
LL + #[rustc_allow_const_fn_unstable(const_eval_select)]
15+
LL | pub const unsafe fn hey() {
16+
|
817

918
error: aborting due to previous error
1019

tests/ui/rfcs/rfc-2632-const-trait-impl/staged-api.unstable.stderr

+33-6
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,56 @@ LL | Foo::func();
1414
|
1515
= help: add `#![feature(foo)]` to the crate attributes to enable
1616

17-
error: `<staged_api::Unstable as staged_api::MyTrait>::func` is not yet stable as a const fn
17+
error: const-stable function cannot use `#[feature(unstable)]`
1818
--> $DIR/staged-api.rs:55:5
1919
|
2020
LL | Unstable::func();
2121
| ^^^^^^^^^^^^^^^^
2222
|
23-
= help: const-stable functions can only call other const-stable functions
23+
help: if it is not part of the public API, make this function unstably const
24+
|
25+
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
26+
LL | const fn stable_const_context() {
27+
|
28+
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
29+
|
30+
LL + #[rustc_allow_const_fn_unstable(unstable)]
31+
LL | const fn stable_const_context() {
32+
|
2433

25-
error: `<Foo as staged_api::MyTrait>::func` is not yet stable as a const fn
34+
error: const-stable function cannot use `#[feature(foo)]`
2635
--> $DIR/staged-api.rs:57:5
2736
|
2837
LL | Foo::func();
2938
| ^^^^^^^^^^^
3039
|
31-
= help: const-stable functions can only call other const-stable functions
40+
help: if it is not part of the public API, make this function unstably const
41+
|
42+
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
43+
LL | const fn stable_const_context() {
44+
|
45+
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
46+
|
47+
LL + #[rustc_allow_const_fn_unstable(foo)]
48+
LL | const fn stable_const_context() {
49+
|
3250

33-
error: `const_context_not_const_stable` is not yet stable as a const fn
51+
error: const-stable function cannot use `#[feature(foo)]`
3452
--> $DIR/staged-api.rs:59:5
3553
|
3654
LL | const_context_not_const_stable()
3755
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3856
|
39-
= help: const-stable functions can only call other const-stable functions
57+
help: if it is not part of the public API, make this function unstably const
58+
|
59+
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
60+
LL | const fn stable_const_context() {
61+
|
62+
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
63+
|
64+
LL + #[rustc_allow_const_fn_unstable(foo)]
65+
LL | const fn stable_const_context() {
66+
|
4067

4168
error: aborting due to 5 previous errors
4269

0 commit comments

Comments
 (0)