Skip to content

Commit

Permalink
repr: improve layout table printing
Browse files Browse the repository at this point in the history
This commit improves the layout table printing by removing the empty gap
at the end of a row for non-single element rows. This means that previously
the layout table would print for a struct like this:

```rs
\#layout_of Boon := enum(
    Foo(x: i32, y: i32),
    Faz(z: f64),
);
```

Before:

```
info: Layout of `Boon` (size=16b align=8b):
┌──────────────┬─────────────────────────┐
│ 4b tag (u32) │                         │
├──────────────┼────────────┬────────────┤
│     Foo      │   x: i32   │   y: i32   │
│              │            │            │
│      0       │   size: 4b │   size: 4b │
│              │ offset: 4b │ offset: 8b │
│              │  align: 4b │  align: 4b │
├──────────────┼────────┬───┴────────┬───┤
│     Faz      │ 4b pad │   z: f64   │   │
│              │        │            │   │
│      1       │   ##   │   size: 8b │   │
│              │   ##   │ offset: 8b │   │
│              │   ##   │  align: 8b │   │
└──────────────┴────────┴────────────┴───┘
```

After the change, we now print the following:

```
info: Layout of `Boon` (size=16b align=8b):
┌──────────────┬─────────────────────────┐
│ 4b tag (u32) │                         │
├──────────────┼────────────┬────────────┤
│     Foo      │   x: i32   │   y: i32   │
│              │            │            │
│      0       │   size: 4b │   size: 4b │
│              │ offset: 4b │ offset: 8b │
│              │  align: 4b │  align: 4b │
├──────────────┼────────┬───┴────────────┤
│     Faz      │ 4b pad │     z: f64     │
│              │        │                │
│      1       │   ##   │     size: 8b   │
│              │   ##   │   offset: 8b   │
│              │   ##   │    align: 8b   │
└──────────────┴────────┴────────────────┘
```
  • Loading branch information
feds01 committed Sep 28, 2024
1 parent 9854702 commit a85ac18
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
6 changes: 4 additions & 2 deletions compiler/hash-repr/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,10 @@ impl BoxRow {
return;
}

// Insert an empty box to fill in the missing gap.
if width - current_width > 2 {
// For singletons, insert an empty box to fill in the missing gap.
let singleton = self.contents.len() == 1;

if singleton && width - current_width > 2 {
let empty_width = width - current_width - 1;
self.widths.push(empty_width);
self.contents.push(BoxContent::new_empty(empty_width));
Expand Down
6 changes: 5 additions & 1 deletion tests/cases/typecheck/enums/discriminants/valid.hash
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Variants := enum(
#layout_of
Empty := enum();


#layout_of
Boon := enum(
Foo(x: i32, y: i32),
Faz(z: f64),
);

main := () => {}
17 changes: 17 additions & 0 deletions tests/cases/typecheck/enums/discriminants/valid.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,20 @@ info: Layout of `!` (size=0b align=1b):
│ align: 1b │
└────────────┘

info: Layout of `Boon` (size=16b align=8b):
┌──────────────┬─────────────────────────┐
│ 4b tag (u32) │ │
├──────────────┼────────────┬────────────┤
│ Foo │ x: i32 │ y: i32 │
│ │ │ │
│ 0 │ size: 4b │ size: 4b │
│ │ offset: 4b │ offset: 8b │
│ │ align: 4b │ align: 4b │
├──────────────┼────────┬───┴────────────┤
│ Faz │ 4b pad │ z: f64 │
│ │ │ │
│ 1 │ ## │ size: 8b │
│ │ ## │ offset: 8b │
│ │ ## │ align: 8b │
└──────────────┴────────┴────────────────┘

0 comments on commit a85ac18

Please sign in to comment.