-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
comptime: declaring a struct in an 'inline for' fails to compile #4148
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
Comments
Anon list literal cannot coerce into a type if the type is provided explicitly. export fn entry() void {
const T = struct {c: usize, d: usize};
var x: T = T { .c = 546343, .d = 7};
var z: struct {a: usize, b: usize} = .{x.c, x.d};
} Compile error is nonsense and misleading.
|
When using intermediate generic function this code actually works: export fn func() void {
inline for ([_]u8{1,2}) |phase| {
const S = MakeStruct(if (phase == 1) u32 else [2]f32);
const s = S{.val = if (phase== 1) 1 else [2]f32{1,2} };
}
}
fn MakeStruct(comptime ArgType: type) type {
return struct {
val: ArgType,
};
}} |
The compiler error is correct and on point. The expression lhs[0] = x.c;
lhs[1] = x.d; If The correct way to do what you wanted to do is: var z: struct {a: usize, b: usize} = .{.a = x.c, .b = x.d}; |
@LemonBoy Why not allow this coercion? By default it will be struct anyway: export fn entry() void {
const T = struct {c: usize, d: usize};
var x: T = T { .c = 546343, .d = 7};
var z = .{x.c, x.d};
// @TypeOf(z) is struct {@"0": usize, @"1": usize}
var z2: @TypeOf(z) = .{.@"0" = x.c, .@"1" = x.d};
} Anyway the error should be more clear e.g. "use struct literal instead" Related #4335 |
Because it's a time-bomb, if you swap the order of two entries in a Edit: and for non-extern, non-packed struct the field order is unspecified.
Aye, there's already a |
Same can be said about functions - they are all time-bombs - if you swap order of parameters and the types match then the code will compile but may 'blow up'
Order of fields in memory? How is that related? |
fixed in stage2 |
hi.
This fails to compile:
(with error:
6:57: error: array access of non-array type 'u32'
)Looks like the second iteration tries to use the struct from the first iteration or something like this.
If I declare both structs before hand, it works:
The text was updated successfully, but these errors were encountered: