Auto merge of #15019 - HKalbasi:nightly-mir-eval-panic, r=HKalbasi

Fix panic in displaying const trait objects

I hope this fixes the panic on recent nightly stdlib, but I didn't test it locally.
This commit is contained in:
bors 2023-06-09 22:43:58 +00:00
commit 993299ee3f
3 changed files with 52 additions and 5 deletions

View File

@ -536,6 +536,22 @@ fn render_const_scalar(
}
f.write_str("]")
}
TyKind::Dyn(_) => {
let addr = usize::from_le_bytes(b[0..b.len() / 2].try_into().unwrap());
let ty_id = usize::from_le_bytes(b[b.len() / 2..].try_into().unwrap());
let Ok(t) = memory_map.vtable.ty(ty_id) else {
return f.write_str("<ty-missing-in-vtable-map>");
};
let Ok(layout) = f.db.layout_of_ty(t.clone(), krate) else {
return f.write_str("<layout-error>");
};
let size = layout.size.bytes_usize();
let Some(bytes) = memory_map.get(addr, size) else {
return f.write_str("<ref-data-not-available>");
};
f.write_str("&")?;
render_const_scalar(f, bytes, memory_map, t)
}
_ => {
let addr = usize::from_le_bytes(b.try_into().unwrap());
let Ok(layout) = f.db.layout_of_ty(t.clone(), krate) else {

View File

@ -77,7 +77,7 @@ fn id(&mut self, ty: Ty) -> usize {
id
}
fn ty(&self, id: usize) -> Result<&Ty> {
pub(crate) fn ty(&self, id: usize) -> Result<&Ty> {
self.id_to_ty.get(id).ok_or(MirEvalError::InvalidVTableId(id))
}
@ -1571,16 +1571,24 @@ fn rec(
}
None => {
let mut check_inner = None;
let (addr, meta) = bytes.split_at(bytes.len() / 2);
let element_size = match t.kind(Interner) {
TyKind::Str => 1,
TyKind::Slice(t) => {
check_inner = Some(t);
this.size_of_sized(t, locals, "slice inner type")?
}
_ => return Ok(()), // FIXME: support other kind of unsized types
TyKind::Dyn(_) => {
let t = this.vtable_map.ty_of_bytes(meta)?;
check_inner = Some(t);
this.size_of_sized(t, locals, "dyn concrete type")?
}
_ => return Ok(()),
};
let count = match t.kind(Interner) {
TyKind::Dyn(_) => 1,
_ => from_bytes!(usize, meta),
};
let (addr, meta) = bytes.split_at(bytes.len() / 2);
let count = from_bytes!(usize, meta);
let size = element_size * count;
let addr = Address::from_bytes(addr)?;
let b = this.read_memory(addr, size)?;
@ -1588,7 +1596,7 @@ fn rec(
if let Some(ty) = check_inner {
for i in 0..count {
let offset = element_size * i;
rec(this, &b[offset..offset + element_size], ty, locals, mm)?;
rec(this, &b[offset..offset + element_size], &ty, locals, mm)?;
}
}
}

View File

@ -4422,6 +4422,29 @@ enum Enum {
);
}
#[test]
fn hover_const_eval_dyn_trait() {
check(
r#"
//- minicore: fmt, coerce_unsized, builtin_impls
use core::fmt::Debug;
const FOO$0: &dyn Debug = &2i32;
"#,
expect![[r#"
*FOO*
```rust
test
```
```rust
const FOO: &dyn Debug = &2
```
"#]],
);
}
#[test]
fn hover_const_eval_slice() {
check(