Skip to content

Commit 49b4f15

Browse files
committed
Auto merge of #15022 - HKalbasi:nightly-mir-eval-panic, r=HKalbasi
Fix panic in displaying unsized structs
2 parents 5f8a6f6 + 1dd76e8 commit 49b4f15

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

crates/hir-ty/src/display.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
//! HIR back into source code, and just displaying them for debugging/testing
33
//! purposes.
44
5-
use std::fmt::{self, Debug};
5+
use std::{
6+
fmt::{self, Debug},
7+
mem::size_of,
8+
};
69

710
use base_db::CrateId;
811
use chalk_ir::{BoundVar, TyKind};
@@ -552,6 +555,16 @@ fn render_const_scalar(
552555
f.write_str("&")?;
553556
render_const_scalar(f, bytes, memory_map, t)
554557
}
558+
TyKind::Adt(adt, _) if b.len() == 2 * size_of::<usize>() => match adt.0 {
559+
hir_def::AdtId::StructId(s) => {
560+
let data = f.db.struct_data(s);
561+
write!(f, "&{}", data.name.display(f.db.upcast()))?;
562+
Ok(())
563+
}
564+
_ => {
565+
return f.write_str("<unsized-enum-or-union>");
566+
}
567+
},
555568
_ => {
556569
let addr = usize::from_le_bytes(match b.try_into() {
557570
Ok(b) => b,

crates/ide/src/hover/tests.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4525,6 +4525,26 @@ const FOO$0: Tree = {
45254525
```
45264526
"#]],
45274527
);
4528+
// FIXME: Show the data of unsized structs
4529+
check(
4530+
r#"
4531+
//- minicore: slice, index, coerce_unsized, transmute
4532+
#[repr(transparent)]
4533+
struct S<T: ?Sized>(T);
4534+
const FOO$0: &S<[u8]> = core::mem::transmute::<&[u8], _>(&[1, 2, 3]);
4535+
"#,
4536+
expect![[r#"
4537+
*FOO*
4538+
4539+
```rust
4540+
test
4541+
```
4542+
4543+
```rust
4544+
const FOO: &S<[u8]> = &S
4545+
```
4546+
"#]],
4547+
);
45284548
}
45294549

45304550
#[test]

0 commit comments

Comments
 (0)