Skip to content

Comptime function calls being memoised or not depending on return type #5674

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
ghost opened this issue Jun 23, 2020 · 0 comments
Closed

Comptime function calls being memoised or not depending on return type #5674

ghost opened this issue Jun 23, 2020 · 0 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@ghost
Copy link

ghost commented Jun 23, 2020

(N.B.: Some details may be incorrect. I can't compile atm, so i can't check.)

The following code prints 1:

const std = @import("std");

usingnamespace blk: {
    comptime var counter: u64 = 0;

    break :blk struct {
        pub fn getValue() type {
            counter += 1;

            return struct { value: u64 = counter };
        }
    };
};

pub fn main() !void {
    _ = getValue();
    _ = getValue();

    std.debug.print("{}\n", .{ (getValue(){}).value });
}

This is because the call to getValue at comptime is memoised.

The following code prints 3:

const std = @import("std");

fn ComptimeOnly(comptime T: type) type {
    return struct { value: T, _: var = undefined, };
}

usingnamespace blk: {
    comptime var counter: u64 = 0;

    break :blk struct {
        pub fn getValue() ComptimeOnly(u64) {
            counter += 1;

            return .{ .value = counter };
        }
    };
};

pub fn main() !void {
    _ = getValue();
    _ = getValue();

    std.debug.print("{}\n", .{ getValue().value });
}

This indicates that getValue is not being memoised.

Not sure if this is a bug or overzealous special casing, but either way it's not right. Either both getValue's should be memoised, or neither should. (Personally I think comptime closures are a mistake -- see #5718.)

@Vexu Vexu added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jun 23, 2020
@Vexu Vexu added this to the 0.7.0 milestone Jun 23, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 26, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk changed the title Bug: Comptime function calls being memoised or not depending on return type Comptime function calls being memoised or not depending on return type Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

2 participants