Skip to content

improve error message for unable to evaluate constant expression in inline while #1732

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

Closed
oconnor0 opened this issue Nov 16, 2018 · 6 comments
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. error message This issue points out an error message that is unhelpful and should be improved.
Milestone

Comments

@oconnor0
Copy link
Contributor

The following code works as I would expect.


pub fn inclusive_range(comptime Int: type, comptime lower: Int, comptime upper: Int) [upper-lower+1]Int {
  var values: [upper-lower+1]Int = undefined;
  var i: Int = lower;
  while (i <= upper) : (i += 1) {
    values[i - lower] = i;
  }
  return values;
}

pub fn main() void {
    for (inclusive_range(u64, 2, 72)) |i| {
        warn("i {}\n", i);
    }
}

However changing line 6 to inline while (i <= upper) : (i += 1) { produces the following error:

E:\Source\langdev\inclusive_range.zig:6:19: error: unable to evaluate constant expression
  inline while (i <= upper) : (i += 1) {
                  ^
E:\Source\langdev\inclusive_range.zig:13:25: note: called from here
    for (inclusive_range(u64, 2, 72)) |i| {
                        ^

2 thoughts:

  1. Is this a bug in zig?
  2. If not an error messages that involves the inline while vs while or whatever the underlying issue is would be greatly appreciated.
@andrewrk
Copy link
Member

You need to make i a comptime var.

That said I think we can improve the error message.

@andrewrk andrewrk added this to the 0.5.0 milestone Nov 17, 2018
@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Nov 17, 2018
@andrewrk andrewrk changed the title inline while improve error message for unable to evaluate constant expression in inline while Nov 17, 2018
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Jul 3, 2019
@SimonN
Copy link

SimonN commented Aug 11, 2019

Zig's C++ codebase calls ir_resolve_const from 98 places. Each time, ir_resolve_const generates the error message itself, and the call site does not add its own error. It will be hard to change the error message for a single specific error. What should the high-level strategy be to improve this error message throughout the codebase?

Example: Last week, in usercode, I mistakenly wrote std.debug.warn(s) with a runtime string s, even though warn needs its first argument fmt to be comptime. The error again was "unable to evaluate constant expression".

  • My first interpretation: s is constant, but cannot be evaluated.
  • Desired interpretation: s is not constant.

The inline while example from November 2018 should get a very different error message than my example here; my example should get e.g.: "argument fmt of warn requires a comptime parameter, but s is not comptime".

@BarabasGitHub
Copy link
Contributor

Ran into this today. It's very confusing.

    var i : u32 = 0;
    inline while(i < 10) : (i += 1) {
    }

will say:

error: unable to evaluate constant expression
    inline while(i < 10) : (i += 1) {
                   ^

And my first thought is: "What?! Why? If the expression is constant, then why can't you evaluate it? Should be easy, right?"

Better would be something like: "Runtime variable used in comptime expression."

@jsyrjala
Copy link
Contributor

I ran into this error message with this bit of code:

const value: f32 = 1. + get_value();
_ = std.math.clamp(value, 0.0, 255.0);

Here the problem is that value is type f32 and constants comptime_float. Casting constants to f32 fixes it, but it was also confusing.

@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 14, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@SpexGuy SpexGuy added the error message This issue points out an error message that is unhelpful and should be improved. label Apr 5, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@jibal
Copy link

jibal commented Apr 4, 2022

I believe that the error message actually means "expression cannot be evaluated at comptime" or something to that effect. The wording of the message says something completely different (and wrong).

@Vexu
Copy link
Member

Vexu commented Jan 10, 2023

Now results in:

a.zig:4:21: error: unable to resolve comptime value
    inline while (i <= upper) : (i += 1) {
                  ~~^~~~~~~~
a.zig:4:21: note: condition in comptime branch must be comptime-known

Tested in:

$ git grep "condition in comptime branch must be comptime-known" test   
test/cases/compile_errors/branch_in_comptime_only_scope_uses_condbr_inline.zig:// :4:15: note: condition in comptime branch must be comptime-known
test/cases/compile_errors/branch_in_comptime_only_scope_uses_condbr_inline.zig:// :11:11: note: condition in comptime branch must be comptime-known
test/cases/compile_errors/condition_comptime_reason_explained.zig:// :8:9: note: condition in comptime branch must be comptime-known
test/cases/x86_64-linux/assert_function.8.zig:// :3:21: note: condition in comptime branch must be comptime-known
test/cases/x86_64-macos/assert_function.8.zig:// :5:21: note: condition in comptime branch must be comptime-known

@Vexu Vexu closed this as completed Jan 10, 2023
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. error message This issue points out an error message that is unhelpful and should be improved.
Projects
None yet
Development

No branches or pull requests

8 participants