Skip to content

Cannot return B coerced in-place to B!T, if function return type is A!B!T. #11890

@InKryption

Description

@InKryption

Zig Version

0.10.0-dev.2624+d506275a0

Steps to Reproduce

Note: This behavior applies to both stage1 and stage2.

Example:

const A = error{A};
const B = error{B};

fn failsToCompile0() A!B!void {
    return B.B;
}
fn failsToCompile1() A!B!void {
    return @as(B!void, B.B);
}
// NOTE: the following two compile.
fn succeedsCompilation0() A!B!void {
    const inner: B!void = B.B;
    return inner;
}
// NOTE: the following does not compile on stage2.
fn succeedsCompilation1() void {
    _ = @as(A!B!void, @as(B!void, B.B)) catch {} catch {};
}

test {
    failsToCompile0() catch {} catch {};
    failsToCompile1() catch {} catch {};
    succeedsCompilation0() catch {} catch {};
    succeedsCompilation1();
}

Expected Behavior

Would expect the terminal to output:

All 1 tests passed.

Actual Behavior

stage1:

./test.zig:5:13: error: expected type 'A', found 'B'
    return B.B;
            ^
./test.zig:2:17: note: 'error.B' not a member of destination error set
const B = error{B};
                ^
./test.zig:8:25: error: expected type 'A', found 'B'
    return @as(B!void, B.B);
                        ^
./test.zig:2:17: note: 'error.B' not a member of destination error set
const B = error{B};

stage2:

test.zig:5:13: error: expected type 'error{A}', found type 'error{B}'
    return B.B;
            ^
test.zig:8:12: error: expected type 'error{A}', found type 'error{B}'
    return @as(B!void, B.B);
           ^
test.zig:13:12: error: expected type 'error{A}', found type 'error{B}'
    return inner;
           ^
test.zig:17:23: error: expected type 'error{A}', found type 'error{B}'
    _ = @as(A!B!void, @as(B!void, B.B)) catch {} catch {};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions