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

Array literal shouldn't cause GC allocation for void element types #20668

Open
ljmf00-wekaio opened this issue Jan 9, 2025 · 5 comments
Open

Comments

@ljmf00-wekaio
Copy link

This compiles:

@nogc
void main()
{
	ubyte[1] bar = [0x00];
}

Although, this doesn't, but should:

@nogc
void main()
{
	void[1] bar = [0x00];
}
@Geod24
Copy link
Member

Geod24 commented Jan 9, 2025

Although, this doesn't, but should:

Why does a void static array make sense ? It's size can't be known.

@ljmf00-wekaio
Copy link
Author

ljmf00-wekaio commented Jan 9, 2025

Although, this doesn't, but should:

Why does a void static array make sense ? It's size can't be known.

It does make sense.

The size is known, the length is in bytes of the original slice. See https://dlang.org/spec/arrays.html#void_arrays

13.17.2.1. [...] The .length of a void array is the length of the data in bytes, rather than the number of elements in its original type.

The main difference from ubyte[] to void[] is that GC shouldn't assume no indirections on void[] arrays (on ubyte[] arrays, there's no pointers, on void[] there might be).

@dkorpel
Copy link
Contributor

dkorpel commented Jan 9, 2025

I think @nogc is the least of your problems, it looks like void[] arrays combined with array literals is a mess:

void main()
{
    ubyte[] b = [0x00]; // [cast(ubyte) 0]
    writeln(b.length);  // 1
    
    void[] v = [0x00]; // cast(void[]) [0]
    writeln(v.length); // 4
    
    void[1] bar = [0x00]; // Range violation (4 bytes assigned to 1 byte)
    // void[4] bar = [0x00]; // Error: Mismatched array lengths, 4 and 1
}

We need to decide whether integers in array literals converted to void[] represent 4 bytes or 1.

@ljmf00-wekaio
Copy link
Author

I think @nogc is the least of your problems, it looks like void[] arrays combined with array literals is a mess:

void main()
{
    ubyte[] b = [0x00]; // [cast(ubyte) 0]
    writeln(b.length);  // 1
    
    void[] v = [0x00]; // cast(void[]) [0]
    writeln(v.length); // 4
    
    void[1] bar = [0x00]; // Range violation (4 bytes assigned to 1 byte)
    // void[4] bar = [0x00]; // Error: Mismatched array lengths, 4 and 1
}

We need to decide whether integers in array literals converted to void[] represent 4 bytes or 1.

Luckily, that's dmd specific, on LDC that is ok. Perhaps, you should create another bug report :D

@ljmf00-wekaio
Copy link
Author

Btw, there is no problem regarding GC with D optimisations in LDC, as we benefit from the lowering from GC to stack transformation passes, see https://godbolt.org/z/1c77P8xY6 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants