You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import "core:fmt"
main :: proc()
{
embed_u32 := transmute([]u32) #load("embed") // 8 bytes file just like literal below
src_u32 := transmute([]u32) []u8{31,32,33,34,35,36,37,38}
fmt.printf( "embed_u32 len %d\nsrc_u32 len %d\n", len(embed_u32) , len(src_u32) )
}
This prints:
embed_u32 len 2
src_u32 len 8
Somehow transmuting []u8 slice returned by #load() readjusts slice length from 8 to 2. But with slice literal this doesn't work. As I understand mem.slice_data_cast should be used here. But it is very confusing why transmute([]u32) works differently on slices returned by #load()
It is a bug, though. Make a file called eight.txt with contents 12345678, run this:
main :: proc() {
DATA :: #load("eight.txt")
D32 :: transmute([]u32)#load("eight.txt")
fmt.println(len(DATA), len(D32))
}
Prints: 8 2
This means that D32 :: transmute([]u32)#load("eight.txt") is treated as #load("eight.txt", []u32), even though if it had really been a transmute bit cast, the length would've remained 8.
Also interesting is that _D32 :: transmute([]u32)DATA gives you the expected error that you can't transmute a constant, but when used directly on #load as above it is allowed (and does the wrong thing).
Here you have the expected behavior that _D32 contains 8 elements despite having a backing for only 2, i.e. a user error. So the bug is either that transmute([]u32)#load is allowed, or that it doesn't evince the same user error.
main :: proc() {
DATA :: #load("eight.txt")
D32 :: transmute([]u32)#load("eight.txt")
fmt.println(len(DATA), len(D32))
data := DATA
_D32 := transmute([]u32)data
fmt.println(D32)
fmt.println(_D32)
}
This prints:
embed_u32 len 2
src_u32 len 8
Somehow transmuting
[]u8
slice returned by#load()
readjusts slice length from 8 to 2. But with slice literal this doesn't work. As I understandmem.slice_data_cast
should be used here. But it is very confusing whytransmute([]u32)
works differently on slices returned by#load()
The text was updated successfully, but these errors were encountered: