Unnamed repository; edit this file 'description' to name the repository.
Implement BeginPanic for mir eval
Lukas Wirth 2024-04-19
parent a9140e1 · commit 6de838c
-rw-r--r--crates/hir-ty/src/chalk_ext.rs5
-rw-r--r--crates/hir-ty/src/mir/eval.rs2
-rw-r--r--crates/hir-ty/src/mir/eval/shim.rs43
-rw-r--r--crates/hir-ty/src/mir/eval/tests.rs1
4 files changed, 44 insertions, 7 deletions
diff --git a/crates/hir-ty/src/chalk_ext.rs b/crates/hir-ty/src/chalk_ext.rs
index 0bf01b0bc6..d99ef6679e 100644
--- a/crates/hir-ty/src/chalk_ext.rs
+++ b/crates/hir-ty/src/chalk_ext.rs
@@ -27,6 +27,7 @@ pub trait TyExt {
fn is_scalar(&self) -> bool;
fn is_floating_point(&self) -> bool;
fn is_never(&self) -> bool;
+ fn is_str(&self) -> bool;
fn is_unknown(&self) -> bool;
fn contains_unknown(&self) -> bool;
fn is_ty_var(&self) -> bool;
@@ -87,6 +88,10 @@ impl TyExt for Ty {
matches!(self.kind(Interner), TyKind::Never)
}
+ fn is_str(&self) -> bool {
+ matches!(self.kind(Interner), TyKind::Str)
+ }
+
fn is_unknown(&self) -> bool {
matches!(self.kind(Interner), TyKind::Error)
}
diff --git a/crates/hir-ty/src/mir/eval.rs b/crates/hir-ty/src/mir/eval.rs
index 045ffb418c..17ad85b6c5 100644
--- a/crates/hir-ty/src/mir/eval.rs
+++ b/crates/hir-ty/src/mir/eval.rs
@@ -1138,7 +1138,7 @@ impl Evaluator<'_> {
let mut ty = self.operand_ty(lhs, locals)?;
while let TyKind::Ref(_, _, z) = ty.kind(Interner) {
ty = z.clone();
- let size = if ty.kind(Interner) == &TyKind::Str {
+ let size = if ty.is_str() {
if *op != BinOp::Eq {
never!("Only eq is builtin for `str`");
}
diff --git a/crates/hir-ty/src/mir/eval/shim.rs b/crates/hir-ty/src/mir/eval/shim.rs
index 33fae866ec..e2ff67a092 100644
--- a/crates/hir-ty/src/mir/eval/shim.rs
+++ b/crates/hir-ty/src/mir/eval/shim.rs
@@ -132,9 +132,7 @@ impl Evaluator<'_> {
return Ok(true);
}
if let Some(it) = self.detect_lang_function(def) {
- let arg_bytes =
- args.iter().map(|it| Ok(it.get(self)?.to_owned())).collect::<Result<Vec<_>>>()?;
- let result = self.exec_lang_item(it, generic_args, &arg_bytes, locals, span)?;
+ let result = self.exec_lang_item(it, generic_args, args, locals, span)?;
destination.write_from_bytes(self, &result)?;
return Ok(true);
}
@@ -333,18 +331,52 @@ impl Evaluator<'_> {
&mut self,
it: LangItem,
generic_args: &Substitution,
- args: &[Vec<u8>],
+ args: &[IntervalAndTy],
locals: &Locals,
span: MirSpan,
) -> Result<Vec<u8>> {
use LangItem::*;
let mut args = args.iter();
match it {
- BeginPanic => Err(MirEvalError::Panic("<unknown-panic-payload>".to_owned())),
+ BeginPanic => {
+ let mut arg = args
+ .next()
+ .ok_or(MirEvalError::InternalError(
+ "argument of BeginPanic is not provided".into(),
+ ))?
+ .clone();
+ while let TyKind::Ref(_, _, ty) = arg.ty.kind(Interner) {
+ if ty.is_str() {
+ let (pointee, metadata) = arg.interval.get(self)?.split_at(self.ptr_size());
+ let len = from_bytes!(usize, metadata);
+
+ return {
+ Err(MirEvalError::Panic(
+ std::str::from_utf8(
+ self.read_memory(Address::from_bytes(pointee)?, len)?,
+ )
+ .unwrap()
+ .to_owned(),
+ ))
+ };
+ }
+ let size = self.size_of_sized(&ty, locals, "begin panic arg")?;
+ let pointee = arg.interval.get(self)?;
+ arg = IntervalAndTy {
+ interval: Interval::new(Address::from_bytes(pointee)?, size),
+ ty: ty.clone(),
+ };
+ }
+ Err(MirEvalError::Panic(format!(
+ "unknown-panic-payload: {:?}",
+ arg.ty.kind(Interner)
+ )))
+ }
SliceLen => {
let arg = args.next().ok_or(MirEvalError::InternalError(
"argument of <[T]>::len() is not provided".into(),
))?;
+ let arg = arg.get(self)?;
let ptr_size = arg.len() / 2;
Ok(arg[ptr_size..].into())
}
@@ -358,6 +390,7 @@ impl Evaluator<'_> {
let arg = args.next().ok_or(MirEvalError::InternalError(
"argument of drop_in_place is not provided".into(),
))?;
+ let arg = arg.interval.get(self)?.to_owned();
self.run_drop_glue_deep(
ty.clone(),
locals,
diff --git a/crates/hir-ty/src/mir/eval/tests.rs b/crates/hir-ty/src/mir/eval/tests.rs
index 031ab51ed7..e88fbd19a4 100644
--- a/crates/hir-ty/src/mir/eval/tests.rs
+++ b/crates/hir-ty/src/mir/eval/tests.rs
@@ -104,7 +104,6 @@ fn main() {
}
"#,
);
- panic!("a");
}
#[test]