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

using try on a function with an empty inferred errorset, within a function that does not return an error union, doesn't cause a compile error #7541

Closed
tgschultz opened this issue Dec 24, 2020 · 3 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

@tgschultz
Copy link
Contributor

tgschultz commented Dec 24, 2020

pub fn isPtr(comptime T: type) bool {
    return std.meta.trait.is(.Pointer)(T);
}

pub fn foo(thing: anytype) !void {
    if(comptime isPtr(@TypeOf(thing))) {
        return error.IsPtr;
    }
}

pub fn main() void {
    const x = @as(usize, 10);
    
    //this causes a compile error, as it should
    //try foo(&x); 

    try foo(x); //this doesn't!
}

I believe this should cause a compile error, as an inferred empty errorset is not the same thing as no errorset.

Related: #5226

@Vexu
Copy link
Member

Vexu commented Dec 24, 2020

Also related #1386.

@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Dec 24, 2020
@Vexu Vexu added this to the 0.9.0 milestone Dec 24, 2020
@LemonBoy
Copy link
Contributor

This is a side-effect of the aggressive constant-folding and the lazy analysis: ir_analyze_instruction_test_err folds the testErr instruction into a comptime-known false if the error set is empty, making the try essentially a no-op.

Removing the following lines makes the compiler emit an error for this test case (and uncovers a bunch of dead switch arms in the stdlib).

             if (!type_is_global_error_set(err_set_type))
                 err_set_type->data.error_set.err_count == 0)

Is this a good solution? IMO it's not, optimizations should not mask problems in the compiler (unless this is intended, but I highly doubt so).

@nektro
Copy link
Contributor

nektro commented Jun 5, 2022

correct behavior imo

andrewrk added a commit that referenced this issue Dec 18, 2022
andrewrk added a commit that referenced this issue Dec 18, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 19, 2022
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

5 participants