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

fn memoized with incorrect value from parent scope when value is from inline else of a switch #19586

Closed
Jarred-Sumner opened this issue Apr 8, 2024 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@Jarred-Sumner
Copy link
Contributor

Jarred-Sumner commented Apr 8, 2024

Zig Version

0.12.0-dev.1828+225fe6ddb

Steps to Reproduce and Observed Behavior

  1. Code
const std = @import("std");
const LogLevel = enum {
    verbose,
    default,
};

pub fn main() void {
    const argv = std.os.argv;
    const last = argv[argv.len - 1];

    std.debug.print("       Last arg: {s}\n", .{last});
    var log_level: LogLevel = .default;
    if (last[0] == '1') {
        log_level = .verbose;
    }

    switch (log_level) {
        inline else => |level| {
            std.debug.print(" before closure: {s}\n", .{@tagName(level)});
            const Closure = struct {
                pub fn run() void {
                    std.debug.print(" within closure: {s}\n", .{@tagName(level)});
                }
            };
            Closure.run();
        },
    }
}
  1. Run:
> zig run repro.zig -- 0
       Last arg: 0
 before closure: default
 within closure: default

> zig run repro.zig -- 1
       Last arg: 1
 before closure: verbose
 within closure: default

Expected Behavior

It should output the following:

> zig run repro.zig -- 0
       Last arg: 0
 before closure: default
 within closure: default

> zig run repro.zig -- 1
       Last arg: 1
 before closure: verbose
 within closure: verbose

It should not choose the last value from the inline else for each invocation. It also shouldn't be a compiler error since log_level is unambiguously comptime-known due to the inline else.

@Jarred-Sumner Jarred-Sumner added the bug Observed behavior contradicts documented or intended behavior label Apr 8, 2024
Jarred-Sumner added a commit to oven-sh/bun that referenced this issue Apr 8, 2024
@rohlem
Copy link
Contributor

rohlem commented Apr 8, 2024

Data point: Does not reproduce for me on old version 0.12.0-dev.3182+f3227598e.
That means the AST-based type deduplication #18816 , implemented in #19190 , didn't cause this initially; it must have broken since then.

@Jarred-Sumner
Copy link
Contributor Author

@rohlem that version is newer than the one I tested with 0.12.0-dev.1828+225fe6ddb

@mlugg
Copy link
Member

mlugg commented Apr 8, 2024

Fixed on master (tested by @rohlem, plus I tested on 0.12.0-dev.3563+129de47a7). This was probably an old bug with capture scopes, which have since been eliminated.

@mlugg mlugg closed this as completed Apr 8, 2024
Jarred-Sumner added a commit to oven-sh/bun that referenced this issue Apr 8, 2024
…emory usage (#10037)

* [bundows] Make bun install 60% faster

* [autofix.ci] apply automated fixes

* Do not keep node_modules folder open between async tasks. Make sure we call runTasks on every event loop wakeup.

* Update install.zig

* Fix deadlock

* Make that deadlock impossible

* a little less repetitive

* Fix test failure with local tarball

* Get those tests to pass

* Normalize absolutely

* lets see how many times we call GetFinalPathNameByHandle

* Workaround ziglang/zig#19586

ziglang/zig#19586

* Is the dev-server-100 test failure a hash table collision?

* Give it its own cache dir

* We cannot change the git task ids

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
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
Projects
None yet
Development

No branches or pull requests

3 participants