Skip to content

Commit

Permalink
docs: Clarify exactly how C is bad at passing arrays to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
belkadan authored and Gankra committed Jul 7, 2024
1 parent 483d6fe commit 10e4e98
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions docs/src/kdl-script/types/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ KDLScript array types like `[u32; 4]` have the layout/repr you would expect from

But there's a problem with passing them by-value: C is supremely fucking weird about passing arrays by value if they're not wrapped in a struct.

This is actually sugar for pass-by-reference (and largely decays into `u32*`):
This is actually sugar for pass-by-reference (and largely decays into `uint32_t*`):

```C
void blah(u32[4] array);
void blah(uint32_t array[4]);
```
And this doesn't even compile:
```C
u32[4] blah();
uint32_t[4] blah(); // invalid syntax
uint32_t blah()[4]; // valid syntax, but still disallowed
```

To avoid trying to squish weird square pegs in round holes, passing an array by-value like this in KDLScript should indeed mean passing it by-value! C/C++ backends should *simply refuse to lower such a KDLScript program and produce an error*. Rust backends are free to lower it in the obvious way. If you want to test the C way, use this:
Expand Down

0 comments on commit 10e4e98

Please sign in to comment.