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

@typeName fails on TypeInfo.StructField.field_type if the struct field is declared as anytype #7740

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

Comments

@tyrion
Copy link

tyrion commented Jan 10, 2021

TypeInfo.StructField.field_type has type type, so it should be possible to pass it to @typeName. However this is not the case if a struct is declared with a field of type anytype.

You can reproduce the error with the following code:

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

pub fn printType(comptime t: type) void {
    switch(@typeInfo(t)) {
        .Struct => |s| printStruct(s),
        else => @compileError("Only struct supported"),
    }
}

pub fn printStruct(comptime s: std.builtin.TypeInfo.Struct) void {
    print("struct {{\n", .{});
    inline for (s.fields) |field| {
        if (field.is_comptime) {
            print("comptime ", .{});
        }
        print("  ({}) {s}: {s} = {}\n", .{
            field.alignment,
            field.name,
            @typeName(field.field_type),
            field.default_value,
        });
    }
    print("}}", .{});
}

const TestStruct = struct{
    name: []const u8,
    type: type,
    optional: bool = true,
    // Uncomment the following line and it crashes
    // default: anytype = null,
};

pub fn main() !void {
    printType(TestStruct);
}
@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Jan 26, 2021
@Vexu Vexu added this to the 0.9.0 milestone Jan 26, 2021
@ghost
Copy link

ghost commented Feb 1, 2021

I ran the above program through GDB, and got this:

(gdb) bt
#0  0x000000000294ee5b in type_bare_name(ZigType*) ()
#1  0x000000000297e913 in ir_analyze_instruction_base(IrAnalyze*, IrInstSrc*) ()
#2  0x0000000002976a93 in ir_analyze(CodeGen*, IrExecutableSrc*, IrExecutableGen*, ZigType*, AstNode*, ZigValue*) ()
#3  0x000000000293e57b in analyze_fn_body(CodeGen*, ZigFn*) ()
#4  0x000000000293fae7 in semantic_analyze(CodeGen*) ()
#5  0x00000000029043d0 in codegen_build_object(CodeGen*) ()
#6  0x00000000028fc75e in zig_stage1_build_object ()
#7  0x00000000025cc741 in Compilation.performAllTheWork ()
#8  0x00007ffff7ffa670 in ?? ()
#9  0x0000000000000000 in ?? ()

@Vexu
Copy link
Member

Vexu commented Aug 24, 2022

Anytype fields have been removed.

@Vexu Vexu closed this as completed Aug 24, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.10.0 Aug 26, 2022
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

3 participants