Skip to content

Commit 7b865aa

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

12 files changed

+106
-33
lines changed

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

Lines changed: 6 additions & 4 deletions
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

Lines changed: 9 additions & 1 deletion
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.rs

Lines changed: 3 additions & 3 deletions
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

Lines changed: 11 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 11 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 11 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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() {}
Lines changed: 11 additions & 2 deletions
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

0 commit comments

Comments
 (0)