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

Compiler crash when unpacking and returning optionals #3242

Closed
Tetralux opened this issue Sep 17, 2019 · 2 comments
Closed

Compiler crash when unpacking and returning optionals #3242

Tetralux opened this issue Sep 17, 2019 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@Tetralux
Copy link
Contributor

const std = @import("std");
const warn = std.debug.warn;

const T = struct{};

const U = struct {
    str: []const u8,

    fn f(self: *U) ?T {
        var str = self.str;
        return str[0..1];
    }

    fn g(self: *U) ?T {
        if (self.f()) |e| return e;
        return null;
    }
};

pub fn main() void { // return type can also be 'anyerror!void' as well -- still crashes.
    var u = U{ .str = "" };

    // Either of these crashes the compiler.
    // if (u.f()) |e| return e; 
    // if (u.g()) |e| warn("{}\n", e);
    
    warn("done\n");
}
@Tetralux
Copy link
Contributor Author

Tetralux commented Sep 17, 2019

There's a possibly related problem that causes invalid LLVM IR to be generated, which is what I was originally trying to reproduce when I came across this; this outright crashes rather than generating bad IR though.

@andrewrk andrewrk added this to the 0.6.0 milestone Sep 19, 2019
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Sep 19, 2019
@Vexu
Copy link
Member

Vexu commented Feb 10, 2020

Fixed by 5ea79bf

./build/a.zig:45:27: error: expected type 'void', found 'T'
    if (u.f()) |e| return e; 
                          ^
./build/a.zig:25:11: note: T declared here
const T = struct{};
          ^
./build/a.zig:32:19: error: expected type 'T', found '[]const u8'
        return str[0..1];
                  ^
./build/a.zig:25:11: note: T declared here
const T = struct{};
          ^

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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

4 participants