Skip to content

Commit f24fc19

Browse files
author
Lukas Markeffsky
committed
unify inherent impls of CompileTimeEvalContext
1 parent 9a1a753 commit f24fc19

File tree

1 file changed

+44
-46
lines changed
  • compiler/rustc_const_eval/src/const_eval

1 file changed

+44
-46
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,6 @@ use crate::interpret::{
2323

2424
use super::error::*;
2525

26-
impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
27-
/// "Intercept" a function call to a panic-related function
28-
/// because we have something special to do for it.
29-
/// If this returns successfully (`Ok`), the function should just be evaluated normally.
30-
fn hook_special_const_fn(
31-
&mut self,
32-
instance: ty::Instance<'tcx>,
33-
args: &[OpTy<'tcx>],
34-
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
35-
// All `#[rustc_do_not_const_check]` functions should be hooked here.
36-
let def_id = instance.def_id();
37-
38-
if Some(def_id) == self.tcx.lang_items().panic_display()
39-
|| Some(def_id) == self.tcx.lang_items().begin_panic_fn()
40-
{
41-
// &str or &&str
42-
assert!(args.len() == 1);
43-
44-
let mut msg_place = self.deref_operand(&args[0])?;
45-
while msg_place.layout.ty.is_ref() {
46-
msg_place = self.deref_operand(&msg_place.into())?;
47-
}
48-
49-
let msg = Symbol::intern(self.read_str(&msg_place)?);
50-
let span = self.find_closest_untracked_caller_location();
51-
let (file, line, col) = self.location_triple_for_span(span);
52-
return Err(ConstEvalErrKind::Panic { msg, file, line, col }.into());
53-
} else if Some(def_id) == self.tcx.lang_items().panic_fmt() {
54-
// For panic_fmt, call const_panic_fmt instead.
55-
if let Some(const_panic_fmt) = self.tcx.lang_items().const_panic_fmt() {
56-
return Ok(Some(
57-
ty::Instance::resolve(
58-
*self.tcx,
59-
ty::ParamEnv::reveal_all(),
60-
const_panic_fmt,
61-
self.tcx.intern_substs(&[]),
62-
)
63-
.unwrap()
64-
.unwrap(),
65-
));
66-
}
67-
}
68-
Ok(None)
69-
}
70-
}
71-
7226
/// Extra machine state for CTFE, and the Machine instance
7327
pub struct CompileTimeInterpreter<'mir, 'tcx> {
7428
/// For now, the number of terminators that can be evaluated before we throw a resource
@@ -191,6 +145,50 @@ impl interpret::MayLeak for ! {
191145
}
192146

193147
impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> {
148+
/// "Intercept" a function call to a panic-related function
149+
/// because we have something special to do for it.
150+
/// If this returns successfully (`Ok`), the function should just be evaluated normally.
151+
fn hook_special_const_fn(
152+
&mut self,
153+
instance: ty::Instance<'tcx>,
154+
args: &[OpTy<'tcx>],
155+
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
156+
// All `#[rustc_do_not_const_check]` functions should be hooked here.
157+
let def_id = instance.def_id();
158+
159+
if Some(def_id) == self.tcx.lang_items().panic_display()
160+
|| Some(def_id) == self.tcx.lang_items().begin_panic_fn()
161+
{
162+
// &str or &&str
163+
assert!(args.len() == 1);
164+
165+
let mut msg_place = self.deref_operand(&args[0])?;
166+
while msg_place.layout.ty.is_ref() {
167+
msg_place = self.deref_operand(&msg_place.into())?;
168+
}
169+
170+
let msg = Symbol::intern(self.read_str(&msg_place)?);
171+
let span = self.find_closest_untracked_caller_location();
172+
let (file, line, col) = self.location_triple_for_span(span);
173+
return Err(ConstEvalErrKind::Panic { msg, file, line, col }.into());
174+
} else if Some(def_id) == self.tcx.lang_items().panic_fmt() {
175+
// For panic_fmt, call const_panic_fmt instead.
176+
if let Some(const_panic_fmt) = self.tcx.lang_items().const_panic_fmt() {
177+
return Ok(Some(
178+
ty::Instance::resolve(
179+
*self.tcx,
180+
ty::ParamEnv::reveal_all(),
181+
const_panic_fmt,
182+
self.tcx.intern_substs(&[]),
183+
)
184+
.unwrap()
185+
.unwrap(),
186+
));
187+
}
188+
}
189+
Ok(None)
190+
}
191+
194192
/// See documentation on the `ptr_guaranteed_cmp` intrinsic.
195193
fn guaranteed_cmp(&mut self, a: Scalar, b: Scalar) -> InterpResult<'tcx, u8> {
196194
Ok(match (a, b) {

0 commit comments

Comments
 (0)