Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Clarify exactly how C is bad at passing arrays to functions #63

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading