Skip to content

Comptime falsy accepted #5458

@DutchGhost

Description

@DutchGhost

The following code compiles, but shouldn't:

const std = @import("std");

fn foo() type {
    return struct {
        var N: usize = 0;

        fn init() @This() {
            comptime {
                N += 1;
            }
            return .{};
        }

        fn num(this: *const @This()) usize {
            comptime {
                N += 1;
            }

            return N;
        }
    };
}

pub fn main() anyerror!void {
    var one = foo().init();
    var two = foo().init();
    std.debug.print("{}  {}  {}  {}\n", .{ one.num(), two.num(), one.num(), two.num() });
}

Notice how it has two comptime { N += 1; } blocks, while in reality it's a runtime add I believe, https://godbolt.org/z/JKrscX

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions