Skip to content

stage2: comptime-known value isn't comptime-known through generic function #11179

@mitchellh

Description

@mitchellh

Zig Version

0.10.0-dev.1335+c64279b15

Steps to Reproduce

The test program is below: Note:

  • The switch return type is required for the function sig.
  • The switch return value is required for the function body.
  • The function call is required
  • If you wrap the whole test in a comptime {} block it works.
const std = @import("std");
const Type = std.builtin.Type;

test "Tuple" {
    const fields_list = fields(@TypeOf(.{}));
    if (fields_list.len != 0)
        @compileError("Argument count mismatch");
}

pub fn fields(comptime T: type) switch (@typeInfo(T)) {
    .Struct => []const Type.StructField,
    else => unreachable,
} {
    return switch (@typeInfo(T)) {
        .Struct => |info| info.fields,
        else => unreachable,
    };
}

Expected Behavior

Should not have any errors (stage1 works), since fields.len should be comptime known.

The reason: this is how std.meta tests Tuple and ArgsTuple, so this is exercised in the stdlib.

Actual Behavior

Triggers @compileError

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions