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

function call with pointer to in-scope comptime var causes a miscompilation #2480

Closed
EEVV opened this issue May 12, 2019 · 5 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. miscompilation The compiler reports success but produces semantically incorrect code.
Milestone

Comments

@EEVV
Copy link

EEVV commented May 12, 2019

This code:

const Foo = struct {
    modify_me_i_dare_you: usize,

    pub fn exit(self: *Foo) void {
        self.modify_me_i_dare_you += 1;
    }
};

pub fn main() void {
    comptime var arb = Foo {
        .modify_me_i_dare_you = 0
    };

    arb.exit();
}

results in Segmentation fault.
However this code:

pub fn main() void {
    comptime var modify_me_i_dare_you = 0;
    modify_me_i_dare_you += 1;
}

doesn't result in a Segmentation fault.

$ zig version

0.4.0+24ee7653
@andrewrk
Copy link
Member

Thanks for the report. This is a duplicate of #1487

@andrewrk
Copy link
Member

Oops, that's not right. Sorry about that. I think your original example should actually compile correctly and not segfault.

@andrewrk andrewrk reopened this May 12, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone May 12, 2019
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label May 12, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 20, 2019
@andrewrk andrewrk added the stage1 The process of building from source via WebAssembly and the C backend. label Jan 5, 2020
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Jan 5, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@nektro
Copy link
Contributor

nektro commented Oct 16, 2022

this affects stage2 on 0.10.0-dev.4369+1087e6776 as well

changing exit() to be an inline fn makes the code run fine, but the source code as-is still segfaults on the += 1

@andrewrk andrewrk added frontend Tokenization, parsing, AstGen, Sema, and Liveness. and removed stage1 The process of building from source via WebAssembly and the C backend. labels Dec 27, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 27, 2022
@andrewrk andrewrk changed the title Mixing comptime and noncomptime results in seg fault. function call with pointer to in-scope comptime var needs to inline the call, or convert the pointer to const Dec 27, 2022
@andrewrk andrewrk changed the title function call with pointer to in-scope comptime var needs to inline the call, or convert the pointer to const function call with pointer to in-scope comptime var causes a miscompilation Dec 27, 2022
@andrewrk andrewrk added the miscompilation The compiler reports success but produces semantically incorrect code. label Dec 27, 2022
@andrewrk
Copy link
Member

One of two things needs to happen:

@Vexu Vexu modified the milestones: 0.11.0, 0.12.0 Apr 23, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.11.0 Jul 22, 2023
@andrewrk andrewrk modified the milestones: 0.11.0, 0.11.1 Jul 31, 2023
@andrewrk andrewrk modified the milestones: 0.11.1, 0.12.0, 0.13.0 Jan 29, 2024
@mlugg
Copy link
Member

mlugg commented Feb 5, 2025

This fails correctly now thanks to our new comptime var pointer rules (which already have good test coverage):

foo.zig:12:8: error: runtime value contains reference to comptime var
    arb.exit();
    ~~~^~~~~
foo.zig:12:8: note: comptime var pointers are not available at runtime
foo.zig:10:27: note: 'runtime_value' points to comptime var declared here
    comptime var arb = Foo{ .modify_me_i_dare_you = 0 };
                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@mlugg mlugg closed this as completed Feb 5, 2025
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 frontend Tokenization, parsing, AstGen, Sema, and Liveness. miscompilation The compiler reports success but produces semantically incorrect code.
Projects
None yet
Development

No branches or pull requests

5 participants