Skip to content

Commit dd757b9

Browse files
committed
Auto merge of #90273 - nbdd0121:const, r=fee1-dead
Clean up special function const checks Mark them as const and `#[rustc_do_not_const_check]` instead of hard-coding them in const-eval checks. r? `@oli-obk` `@rustbot` label A-const-eval T-compiler
2 parents 337e156 + 223f580 commit dd757b9

File tree

8 files changed

+53
-93
lines changed

8 files changed

+53
-93
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

+19-37
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,25 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
3030
&mut self,
3131
instance: ty::Instance<'tcx>,
3232
args: &[OpTy<'tcx>],
33-
is_const_fn: bool,
3433
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
35-
// The list of functions we handle here must be in sync with
36-
// `is_lang_special_const_fn` in `transform/check_consts/mod.rs`.
34+
// All `#[rustc_do_not_const_check]` functions should be hooked here.
3735
let def_id = instance.def_id();
3836

39-
if is_const_fn {
40-
if Some(def_id) == self.tcx.lang_items().const_eval_select() {
41-
// redirect to const_eval_select_ct
42-
if let Some(const_eval_select) = self.tcx.lang_items().const_eval_select_ct() {
43-
return Ok(Some(
44-
ty::Instance::resolve(
45-
*self.tcx,
46-
ty::ParamEnv::reveal_all(),
47-
const_eval_select,
48-
instance.substs,
49-
)
50-
.unwrap()
51-
.unwrap(),
52-
));
53-
}
37+
if Some(def_id) == self.tcx.lang_items().const_eval_select() {
38+
// redirect to const_eval_select_ct
39+
if let Some(const_eval_select) = self.tcx.lang_items().const_eval_select_ct() {
40+
return Ok(Some(
41+
ty::Instance::resolve(
42+
*self.tcx,
43+
ty::ParamEnv::reveal_all(),
44+
const_eval_select,
45+
instance.substs,
46+
)
47+
.unwrap()
48+
.unwrap(),
49+
));
5450
}
55-
return Ok(None);
56-
}
57-
58-
if Some(def_id) == self.tcx.lang_items().panic_fn()
59-
|| Some(def_id) == self.tcx.lang_items().panic_str()
60-
|| Some(def_id) == self.tcx.lang_items().panic_display()
51+
} else if Some(def_id) == self.tcx.lang_items().panic_display()
6152
|| Some(def_id) == self.tcx.lang_items().begin_panic_fn()
6253
{
6354
// &str or &&str
@@ -274,31 +265,22 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
274265

275266
// Only check non-glue functions
276267
if let ty::InstanceDef::Item(def) = instance.def {
277-
let mut is_const_fn = true;
278-
279268
// Execution might have wandered off into other crates, so we cannot do a stability-
280269
// sensitive check here. But we can at least rule out functions that are not const
281270
// at all.
282271
if !ecx.tcx.is_const_fn_raw(def.did) {
283272
// allow calling functions marked with #[default_method_body_is_const].
284273
if !ecx.tcx.has_attr(def.did, sym::default_method_body_is_const) {
285-
is_const_fn = false;
274+
// We certainly do *not* want to actually call the fn
275+
// though, so be sure we return here.
276+
throw_unsup_format!("calling non-const function `{}`", instance)
286277
}
287278
}
288279

289-
// Some functions we support even if they are non-const -- but avoid testing
290-
// that for const fn!
291-
// `const_eval_select` is a const fn because it must use const trait bounds.
292-
if let Some(new_instance) = ecx.hook_special_const_fn(instance, args, is_const_fn)? {
280+
if let Some(new_instance) = ecx.hook_special_const_fn(instance, args)? {
293281
// We call another const fn instead.
294282
return Self::find_mir_or_eval_fn(ecx, new_instance, _abi, args, _ret, _unwind);
295283
}
296-
297-
if !is_const_fn {
298-
// We certainly do *not* want to actually call the fn
299-
// though, so be sure we return here.
300-
throw_unsup_format!("calling non-const function `{}`", instance)
301-
}
302284
}
303285
// This is a const fn. Call it.
304286
Ok(Some(ecx.load_mir(instance.def, None)?))

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

+19-23
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::ops::Deref;
2424
use super::ops::{self, NonConstOp, Status};
2525
use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop, NeedsNonConstDrop};
2626
use super::resolver::FlowSensitiveAnalysis;
27-
use super::{is_lang_panic_fn, is_lang_special_const_fn, ConstCx, Qualif};
27+
use super::{ConstCx, Qualif};
2828
use crate::const_eval::is_unstable_const_fn;
2929

3030
// We are using `MaybeMutBorrowedLocals` as a proxy for whether an item may have been mutated
@@ -918,31 +918,27 @@ impl Visitor<'tcx> for Checker<'mir, 'tcx> {
918918
}
919919

920920
// At this point, we are calling a function, `callee`, whose `DefId` is known...
921-
if is_lang_special_const_fn(tcx, callee) {
922-
// `begin_panic` and `panic_display` are generic functions that accept
923-
// types other than str. Check to enforce that only str can be used in
924-
// const-eval.
925-
926-
// const-eval of the `begin_panic` fn assumes the argument is `&str`
927-
if Some(callee) == tcx.lang_items().begin_panic_fn() {
928-
match args[0].ty(&self.ccx.body.local_decls, tcx).kind() {
929-
ty::Ref(_, ty, _) if ty.is_str() => (),
930-
_ => self.check_op(ops::PanicNonStr),
931-
}
932-
}
933921

934-
// const-eval of the `panic_display` fn assumes the argument is `&&str`
935-
if Some(callee) == tcx.lang_items().panic_display() {
936-
match args[0].ty(&self.ccx.body.local_decls, tcx).kind() {
937-
ty::Ref(_, ty, _) if matches!(ty.kind(), ty::Ref(_, ty, _) if ty.is_str()) =>
938-
{}
939-
_ => self.check_op(ops::PanicNonStr),
940-
}
922+
// `begin_panic` and `panic_display` are generic functions that accept
923+
// types other than str. Check to enforce that only str can be used in
924+
// const-eval.
925+
926+
// const-eval of the `begin_panic` fn assumes the argument is `&str`
927+
if Some(callee) == tcx.lang_items().begin_panic_fn() {
928+
match args[0].ty(&self.ccx.body.local_decls, tcx).kind() {
929+
ty::Ref(_, ty, _) if ty.is_str() => return,
930+
_ => self.check_op(ops::PanicNonStr),
941931
}
932+
}
942933

943-
if is_lang_panic_fn(tcx, callee) {
944-
// run stability check on non-panic special const fns.
945-
return;
934+
// const-eval of the `panic_display` fn assumes the argument is `&&str`
935+
if Some(callee) == tcx.lang_items().panic_display() {
936+
match args[0].ty(&self.ccx.body.local_decls, tcx).kind() {
937+
ty::Ref(_, ty, _) if matches!(ty.kind(), ty::Ref(_, ty, _) if ty.is_str()) =>
938+
{
939+
return;
940+
}
941+
_ => self.check_op(ops::PanicNonStr),
946942
}
947943
}
948944

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

-18
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,6 @@ impl ConstCx<'mir, 'tcx> {
7272
}
7373
}
7474

75-
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
76-
pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
77-
Some(def_id) == tcx.lang_items().panic_fn()
78-
|| Some(def_id) == tcx.lang_items().panic_str()
79-
|| Some(def_id) == tcx.lang_items().panic_display()
80-
|| Some(def_id) == tcx.lang_items().begin_panic_fn()
81-
|| Some(def_id) == tcx.lang_items().panic_fmt()
82-
}
83-
84-
/// Returns `true` if this `DefId` points to one of the lang items that will be handled differently
85-
/// in const_eval.
86-
pub fn is_lang_special_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
87-
// We can allow calls to these functions because `hook_special_const_fn` in
88-
// `const_eval/machine.rs` ensures the calls are handled specially.
89-
// Keep in sync with what that function handles!
90-
is_lang_panic_fn(tcx, def_id) || Some(def_id) == tcx.lang_items().const_eval_select()
91-
}
92-
9375
pub fn rustc_allow_const_fn_unstable(
9476
tcx: TyCtxt<'tcx>,
9577
def_id: DefId,

compiler/rustc_const_eval/src/transform/promote_consts.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_index::vec::{Idx, IndexVec};
2626
use std::cell::Cell;
2727
use std::{cmp, iter, mem};
2828

29-
use crate::transform::check_consts::{is_lang_special_const_fn, qualifs, ConstCx};
29+
use crate::transform::check_consts::{qualifs, ConstCx};
3030
use crate::transform::MirPass;
3131

3232
/// A `MirPass` for promotion.
@@ -656,9 +656,7 @@ impl<'tcx> Validator<'_, 'tcx> {
656656
}
657657

658658
let is_const_fn = match *fn_ty.kind() {
659-
ty::FnDef(def_id, _) => {
660-
self.tcx.is_const_fn_raw(def_id) || is_lang_special_const_fn(self.tcx, def_id)
661-
}
659+
ty::FnDef(def_id, _) => self.tcx.is_const_fn_raw(def_id),
662660
_ => false,
663661
};
664662
if !is_const_fn {

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
#![feature(const_discriminant)]
108108
#![feature(const_float_bits_conv)]
109109
#![feature(const_float_classify)]
110+
#![feature(const_fmt_arguments_new)]
110111
#![feature(const_heap)]
111112
#![feature(const_inherent_unchecked_arith)]
112113
#![feature(const_int_unchecked_arith)]

library/core/src/panicking.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@ use crate::panic::{Location, PanicInfo};
3434
// never inline unless panic_immediate_abort to avoid code
3535
// bloat at the call sites as much as possible
3636
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
37+
#[cfg_attr(feature = "panic_immediate_abort", inline)]
3738
#[track_caller]
3839
#[lang = "panic"] // needed by codegen for panic on overflow and other `Assert` MIR terminators
39-
pub fn panic(expr: &'static str) -> ! {
40-
if cfg!(feature = "panic_immediate_abort") {
41-
super::intrinsics::abort()
42-
}
43-
40+
pub const fn panic(expr: &'static str) -> ! {
4441
// Use Arguments::new_v1 instead of format_args!("{}", expr) to potentially
4542
// reduce size overhead. The format_args! macro uses str's Display trait to
4643
// write expr, which calls Formatter::pad, which must accommodate string
@@ -52,15 +49,16 @@ pub fn panic(expr: &'static str) -> ! {
5249

5350
#[inline]
5451
#[track_caller]
55-
#[lang = "panic_str"] // needed for const-evaluated panics
56-
pub fn panic_str(expr: &str) -> ! {
57-
panic_fmt(format_args!("{}", expr));
52+
#[lang = "panic_str"] // needed for `non-fmt-panics` lint
53+
pub const fn panic_str(expr: &str) -> ! {
54+
panic_display(&expr);
5855
}
5956

6057
#[inline]
6158
#[track_caller]
6259
#[lang = "panic_display"] // needed for const-evaluated panics
63-
pub fn panic_display<T: fmt::Display>(x: &T) -> ! {
60+
#[rustc_do_not_const_check] // hooked by const-eval
61+
pub const fn panic_display<T: fmt::Display>(x: &T) -> ! {
6462
panic_fmt(format_args!("{}", *x));
6563
}
6664

@@ -89,7 +87,8 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
8987
#[cfg_attr(feature = "panic_immediate_abort", inline)]
9088
#[track_caller]
9189
#[lang = "panic_fmt"] // needed for const-evaluated panics
92-
pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
90+
#[rustc_do_not_const_check] // hooked by const-eval
91+
pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
9392
if cfg!(feature = "panic_immediate_abort") {
9493
super::intrinsics::abort()
9594
}

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
#![feature(const_cstr_unchecked)]
258258
#![feature(const_fn_floating_point_arithmetic)]
259259
#![feature(const_fn_fn_ptr_basics)]
260+
#![feature(const_fn_trait_bound)]
260261
#![feature(const_format_args)]
261262
#![feature(const_io_structs)]
262263
#![feature(const_ip)]

library/std/src/panicking.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,8 @@ pub fn begin_panic_handler(info: &PanicInfo<'_>) -> ! {
512512
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
513513
#[cold]
514514
#[track_caller]
515-
pub fn begin_panic<M: Any + Send>(msg: M) -> ! {
515+
#[rustc_do_not_const_check] // hooked by const-eval
516+
pub const fn begin_panic<M: Any + Send>(msg: M) -> ! {
516517
if cfg!(feature = "panic_immediate_abort") {
517518
intrinsics::abort()
518519
}

0 commit comments

Comments
 (0)