Skip to content

Possibility for more helpful error message upon attempt to use assignment operators in an expression. #16136

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

Open
amcrjlnv opened this issue Jun 21, 2023 · 3 comments
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Milestone

Comments

@amcrjlnv
Copy link

amcrjlnv commented Jun 21, 2023

Zig Version

0.11.0-dev.3771+128fd7dd0

Steps to Reproduce and Observed Output

In C and C-like languages, assignment operators can be used as parts of expressions. So, it is allowed to have code like this:

int x = 3;
while (x--) {
    printf("%d\n", x);
}

and it would output:

2
1
0

So, someone coming to zig might try something similar like:

var x: u32 = 0;
while ((x += 1) < 5) {
     try format(stdout, "{}", .{x});
}

Since such trickery is not allowed in zig, the compiler gives an error:

test.zig:7:15: error: expected ')', found '+='
    while ((x += 1) < 5) {
              ^~

Expected Output

Since the compiler correctly points out where the problem is, the current error message is probably good enough, but it could be improved. It would be more helpful if it could say something like:

test.zig:7:15: error: assignment operators such as '+=' cannot be used as part of an expression
    while ((x += 1) < 5) {
            ~~~^~~
test.zig:7:15: note: consider placing 'x += 1' elsewhere.

I'm not sure if it's worth the effort to change this, but I figured I could just point it out in case.

Edit: as per some suggestions, the message could instead look like:

test.zig:7:15: error: expected ')', found '+='
    while ((x += 1) < 5) {
              ^~
test.zig:7:15: note: assignment operations such as '+=' cannot be used as expressions
@amcrjlnv amcrjlnv added the error message This issue points out an error message that is unhelpful and should be improved. label Jun 21, 2023
@Vexu Vexu added this to the 0.13.0 milestone Jun 21, 2023
@nektro
Copy link
Contributor

nektro commented Jun 21, 2023

related #11805

@rohlem
Copy link
Contributor

rohlem commented Jun 22, 2023

I like the idea, just have a small (maybe subjective) nitpick about the suggested phrasing.

The old error reads more clearly like a syntax error to me, and is structured like all other syntax errors (currently).
The new phrasing of "assignment operators ... cannot be used" looks different, and to me suggests that the syntax were valid but the compiler explicitly filtered out the construction.
I think keeping the old phrasing and making the suggested error text instead be an additional note would keep uniformity while still being equally helpful.

The new note "consider placing ... elsewhere" on the other hand seems less helpful, and maybe a bit misleading if you take it too literally.
Especially in the given example of a loop, you would actually need to find two places, one just before the loop and one at the end of the iteration, + before every continue.
Experienced users who understand their code would obviously reach this conclusion themselves, but newcomers might not sufficiently reason about the code change. Think of someone trying to port a C program 1:1 - this change would break the behavior without them realizing it.
Therefore I think this additional note is not worth being added.

@amcrjlnv
Copy link
Author

amcrjlnv commented Jun 22, 2023

@rohlem
After some thought, I agree that consistency should be kept and that to the compiler it is a syntax error, so I agree that moving the suggested error to a note is a good idea, and I also agree that deleting the extra note would help with consistency. With those suggestions, the message would look like:

test.zig:7:15: error: expected ')', found '+='
    while ((x += 1) < 5) {
              ^~
test.zig:7:15: note: assignment operators such as '+=' cannot be used as parts of expressions

With this, I've thought more about the wording, and here are some other possibilities:

  1. assignment operations are not expressions
  2. assignment operations such as ... are not expressions
  3. assignment operations such as ... do not form expressions
  4. an assignment operator such as ... cannot be used in an expression
  5. an assignment operator such as ... cannot be used as part of an expression
  6. assignment operations such as ... cannot be used as expressions

My main issue with 4-5 and my original suggestion is that they can be confusing in the context of block expressions. And, a slight problem with all of them is that they don't really give a solution. For now, I've probably been staring at this for too long, so I'll come back later after I have had time to think.

Edit: I now think 6 is the best, so I'm updating the issue to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

4 participants