Skip to content

Optional should not coerce to optional optional as null, if it is null #10555

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

Closed
ArborealAnole opened this issue Jan 9, 2022 · 2 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@ArborealAnole
Copy link

Zig Version

0.10.0-dev.108+7c1e17a84

Steps to Reproduce

const std = @import("std");
const print = std.debug.print;
const builtin = std.builtin;

const A = @Type(.{
    .Struct = .{
        .layout = .Auto,
        .fields = &[_]builtin.TypeInfo.StructField{
            .{ .name = "one", .field_type = ?i32, .default_value = @as(?i32, null), .is_comptime = false, .alignment = 0 },
        },
        .decls = &[_]builtin.TypeInfo.Declaration{},
        .is_tuple = false,
    },
});
pub fn main() !void {
    const a: A = .{ }; // error
    // const a: A = .{ .one = null }; // ok
    print("{}\n", .{a});
    print("{}\n", .{@TypeOf(@typeInfo(A).Struct.fields[0].default_value)});
    print("{}\n", .{@typeInfo(A).Struct.fields[0].default_value});
    print("{}\n", .{@TypeOf(@as(?i32, null))});
}

Expected Behavior

The optional ?i32 should coerce to a ??i32 but it should not be a null ??i32. Therefore, the default value of the one field of A should be null.

Actual Behavior

There is no default value for the one field in the reified A.

@ArborealAnole ArborealAnole added the bug Observed behavior contradicts documented or intended behavior label Jan 9, 2022
@Vexu Vexu added the stage1 The process of building from source via WebAssembly and the C backend. label Jan 9, 2022
@Vexu Vexu added this to the 0.11.0 milestone Jan 9, 2022
@InKryption
Copy link
Contributor

InKryption commented Jan 10, 2022

#7091 Possibly related? Which also looks at not being able to represent a default value when creating a struct, but with undefined (with or without using @Type).

@Vexu
Copy link
Member

Vexu commented Jan 19, 2023

The example in the issue no longer reproduces the issue due to changes to std.builtin.Type but the bug has been fixed in self-hosted.

@Vexu Vexu closed this as completed Jan 19, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.11.0 Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

4 participants