Skip to content

Commit d834ea0

Browse files
committed
Say what unstable feature is being used in a const-stable function
1 parent c5f69bd commit d834ea0

12 files changed

+108
-36
lines changed

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

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

298-
Status::Unstable(gate) if self.tcx.features().active(gate) => {
298+
Status::Unstable(gate) => {
299299
let unstable_in_stable = self.ccx.is_const_stable_const_fn()
300300
&& !super::rustc_allow_const_fn_unstable(self.tcx, self.def_id(), gate);
301301
if unstable_in_stable {
302302
emit_unstable_in_stable_error(self.ccx, span, gate);
303+
return;
304+
} else if self.tcx.features().declared(gate) && self.tcx.features().active(gate) {
305+
return;
306+
} else {
307+
Some(gate)
303308
}
304-
305-
return;
306309
}
307310

308-
Status::Unstable(gate) => Some(gate),
309311
Status::Forbidden => None,
310312
};
311313

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

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

319319
impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
320+
fn status_in_item(&self, _: &ConstCx<'_, 'tcx>) -> Status {
321+
if let Some(symbol) = self.1 { Status::Unstable(symbol) } else { Status::Forbidden }
322+
}
323+
320324
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
321325
let FnCallUnstable(def_id, feature) = *self;
322326

@@ -327,7 +331,11 @@ impl<'tcx> NonConstOp<'tcx> for FnCallUnstable {
327331
// FIXME: make this translatable
328332
#[allow(rustc::untranslatable_diagnostic)]
329333
if ccx.is_const_stable_const_fn() {
330-
err.help("const-stable functions can only call other const-stable functions");
334+
if self.1.is_some() {
335+
bug!("this should be triggering the UnstableInStable lint instead");
336+
} else {
337+
err.help("const-stable functions can only call other const-stable functions");
338+
}
331339
} else if ccx.tcx.sess.is_nightly_build() {
332340
if let Some(feature) = feature {
333341
err.help(format!("add `#![feature({feature})]` to the crate attributes to enable"));

tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ const fn foo() -> u32 { 42 }
1313
#[stable(feature = "rust1", since = "1.0.0")]
1414
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
1515
// can't call non-min_const_fn
16-
const fn bar() -> u32 { foo() } //~ ERROR not yet stable as a const fn
16+
const fn bar() -> u32 { foo() } //~ ERROR const-stable function cannot use `#[feature(foo)]`
1717

1818
#[unstable(feature = "foo2", issue = "none")]
1919
const fn foo2() -> u32 { 42 }
2020

2121
#[stable(feature = "rust1", since = "1.0.0")]
2222
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
2323
// can't call non-min_const_fn
24-
const fn bar2() -> u32 { foo2() } //~ ERROR not yet stable as a const fn
24+
const fn bar2() -> u32 { foo2() } //~ ERROR const-stable function cannot use `#[feature(foo2)]`
2525

2626
#[stable(feature = "rust1", since = "1.0.0")]
2727
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
@@ -36,6 +36,6 @@ const fn foo2_gated() -> u32 { 42 }
3636
#[stable(feature = "rust1", since = "1.0.0")]
3737
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
3838
// can't call non-min_const_fn
39-
const fn bar2_gated() -> u32 { foo2_gated() } //~ ERROR not yet stable as a const fn
39+
const fn bar2_gated() -> u32 { foo2_gated() } //~ ERROR const-stable function cannot use `#[feature(foo2)]`
4040

4141
fn main() {}

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.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ const unsafe fn foo() -> u32 { 42 }
1313
#[stable(feature = "rust1", since = "1.0.0")]
1414
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
1515
// can't call non-min_const_fn
16-
const unsafe fn bar() -> u32 { unsafe { foo() } } //~ ERROR not yet stable as a const fn
16+
const unsafe fn bar() -> u32 { unsafe { foo() } } //~ ERROR const-stable function cannot use `#[feature(foo)]`
1717

1818
#[unstable(feature = "foo2", issue = "none")]
1919
const unsafe fn foo2() -> u32 { 42 }
2020

2121
#[stable(feature = "rust1", since = "1.0.0")]
2222
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
2323
// can't call non-min_const_fn
24-
const unsafe fn bar2() -> u32 { unsafe { foo2() } } //~ ERROR not yet stable as a const fn
24+
const unsafe fn bar2() -> u32 { unsafe { foo2() } } //~ ERROR const-stable function cannot use `#[feature(foo2)]`
2525

2626
#[stable(feature = "rust1", since = "1.0.0")]
2727
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
@@ -37,6 +37,6 @@ const unsafe fn foo2_gated() -> u32 { 42 }
3737
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
3838
// can't call non-min_const_fn
3939
const unsafe fn bar2_gated() -> u32 { unsafe { foo2_gated() } }
40-
//~^ ERROR not yet stable as a const fn
40+
//~^ ERROR const-stable function cannot use `#[feature(foo2)]`
4141

4242
fn main() {}

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.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ const fn foo() -> u32 { 42 }
1313
#[stable(feature = "rust1", since = "1.0.0")]
1414
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
1515
// can't call non-min_const_fn
16-
const unsafe fn bar() -> u32 { foo() } //~ ERROR not yet stable as a const fn
16+
const unsafe fn bar() -> u32 { foo() } //~ ERROR const-stable function cannot use `#[feature(foo)]`
1717

1818
#[unstable(feature = "foo2", issue = "none")]
1919
const fn foo2() -> u32 { 42 }
2020

2121
#[stable(feature = "rust1", since = "1.0.0")]
2222
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
2323
// can't call non-min_const_fn
24-
const unsafe fn bar2() -> u32 { foo2() } //~ ERROR not yet stable as a const fn
24+
const unsafe fn bar2() -> u32 { foo2() } //~ ERROR const-stable function cannot use `#[feature(foo)]`
2525

2626
// check whether this function cannot be called even with the feature gate active
2727
#[unstable(feature = "foo2", issue = "none")]
@@ -30,6 +30,6 @@ const fn foo2_gated() -> u32 { 42 }
3030
#[stable(feature = "rust1", since = "1.0.0")]
3131
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
3232
// can't call non-min_const_fn
33-
const unsafe fn bar2_gated() -> u32 { foo2_gated() } //~ ERROR not yet stable as a const fn
33+
const unsafe fn bar2_gated() -> u32 { foo2_gated() } //~ ERROR const-stable function cannot use `#[feature(foo)]`
3434

3535
fn main() {}

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

tests/ui/intrinsics/const-eval-select-stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const fn nothing(){}
1515
#[rustc_const_stable(since = "1.0", feature = "const_hey")]
1616
pub const unsafe fn hey() {
1717
const_eval_select((), nothing, log);
18-
//~^ ERROR `const_eval_select` is not yet stable as a const fn
18+
//~^ ERROR const-stable function cannot use `#[feature(const_eval_select)]`
1919
}
2020

2121
fn main() {}
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 1 previous error
1019

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ pub const fn const_context_not_const_stable() {
5151
#[rustc_const_stable(feature = "cheese", since = "1.0.0")]
5252
const fn stable_const_context() {
5353
Unstable::func();
54-
//~^ ERROR not yet stable as a const fn
54+
//~^ ERROR const-stable function cannot use `#[feature(unstable)]`
5555
Foo::func();
56-
//[unstable]~^ ERROR not yet stable as a const fn
56+
//[unstable]~^ ERROR const-stable function cannot use `#[feature(foo)]`
5757
const_context_not_const_stable()
58-
//[unstable]~^ ERROR not yet stable as a const fn
58+
//[unstable]~^ ERROR const-stable function cannot use `#[feature(foo)]`
5959
}
6060

6161
fn main() {}

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

+36-10
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,55 @@ 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
18-
--> $DIR/staged-api.rs:53:5
17+
error: const-stable function cannot use `#[feature(unstable)]`
18+
--> $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
26-
--> $DIR/staged-api.rs:55:5
34+
error: const-stable function cannot use `#[feature(foo)]`
35+
--> $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
34-
--> $DIR/staged-api.rs:57:5
51+
error: const-stable function cannot use `#[feature(foo)]`
52+
--> $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
42-

0 commit comments

Comments
 (0)