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

@floatToInt with u0 gives misleading error #9415

Closed
travisstaloch opened this issue Jul 18, 2021 · 2 comments
Closed

@floatToInt with u0 gives misleading error #9415

travisstaloch opened this issue Jul 18, 2021 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior error message This issue points out an error message that is unhelpful and should be improved. stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@travisstaloch
Copy link
Contributor

travisstaloch commented Jul 18, 2021

// test.zig
const std = @import("std");

test "reproduce @floatToInt(u0, ..) error: unable to evaluate constant expression" {
    var float: f128 = 1.23;
    _ = @floatToInt(u0, float);
}
$ zig version
0.9.0-dev.482+44c50fef6

$ zig test test.zig 
./test.zig:5:25: error: unable to evaluate constant expression
    _ = @floatToInt(u0, float);
                        ^
./test.zig:5:9: note: referenced here
    _ = @floatToInt(u0, float);

This was discovered by @greenfork when trying to call std.json.parse passing a union which contains a u0 field. Doing so caused an error here in std.json:

@g-w1 on discord suggested the error message should be: "error: integer value '1' cannot be stored in type 'u0'"

@travisstaloch
Copy link
Contributor Author

travisstaloch commented Jul 19, 2021

Here is a reproduction which demonstrates how this was discovered. The error message makes it very difficult to tell what caused the error.

test "repro std.json.parse error" {
    const U = union(enum) { a: u8, b: u0 };
    const text = "42";
    var tokens = std.json.TokenStream.init(text);
    const u = try std.json.parse(U, &tokens, .{});
    try std.testing.expectEqual(U{ .a = 42 }, u);
}
..zig/build/lib/zig/std/json.zig:1637:35: error: unable to evaluate constant expression
            return @floatToInt(T, float);
                                  ^
..zig/build/lib/zig/std/json.zig:1676:38: note: called from here
                    if (parseInternal(u_field.field_type, token, &tokens_copy, options)) |value| {
                                     ^
..zig/build/lib/zig/std/json.zig:1881:32: note: called from here
    const r = try parseInternal(T, token, tokens, options);
                               ^
./test.zig:12:33: note: called from here
    const u = try std.json.parse(U, &tokens, .{});
                                ^
./test.zig:8:35: note: called from here
test "repro std.json.parse error" {
                                  ^
..zig/build/lib/zig/std/json.zig:1637:20: note: referenced here
            return @floatToInt(T, float);

@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior error message This issue points out an error message that is unhelpful and should be improved. stage1 The process of building from source via WebAssembly and the C backend. labels Aug 6, 2021
@Vexu Vexu added this to the 0.10.0 milestone Aug 6, 2021
@matu3ba
Copy link
Contributor

matu3ba commented Feb 17, 2022

In stage1 there are a pile of error messages just giving output "unable to evaluate constant expression":

// comptime side effects
static Error ir_exec_scan_for_side_effects(CodeGen *codegen, Stage1Air *exec) {
...
// checking if inlining is not possible
static bool ir_emit_global_runtime_side_effect(IrAnalyze *ira, Stage1ZirInst* source_instruction) {
...
//  
static Error ir_resolve_const_val(CodeGen *codegen, Stage1Air *exec, AstNode *source_node,
        ZigValue *val, UndefAllowed undef_allowed)
...
//
        if (!fn_entry || fn_entry->body_node == nullptr) {
            ir_add_error(ira, fn_ref, buf_sprintf("unable to evaluate constant expression"));
...
//
    if (is_comptime) {
        ir_add_error(ira, first_non_const_instruction,
            buf_sprintf("unable to evaluate constant expression"));
        return ira->codegen->invalid_inst_gen;
    }

Although there is a pile of those error message, this requires special casing each of them which is likely not worth the effort in stage1.

Vexu added a commit to Vexu/zig that referenced this issue Oct 28, 2022
@Vexu Vexu closed this as completed in 1ea1228 Nov 4, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Nov 4, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.10.1 Jan 9, 2023
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 error message This issue points out an error message that is unhelpful and should be improved. stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

4 participants