The zig doc treats `i += 1` as an expression, but is it?

While loops support a continue expression which is executed when the loop is continued. The continue keyword respects this expression.

…

const expectEqual = @import("std").testing.expectEqual;
const expect = @import("std").testing.expect;

test "while loop continue expression" {
    var i: usize = 0;
    while (i < 10) : (i += 1) {}
    try expectEqual(10, i);
}

Here, the doc says (i += 1) is a continue expression.

I think (although I don’t know) that this is a typo. “continue statement” would be more precise, since the statement i += 1; does not produce a value in the way that an expression can. that being said, it might be true that even now i am mixing up what Zig means by “expression” vs “statement”.

I think it’s correct to say that the continue expression is an expression, but the expression must return void so in practice it’s more like a statement. In Zig, the line between a statement and an expression isn’t as clear as in other languages.

As for assignments, the grammar refers to expressions like a = b or a += b as “assignment expressions”, but unlike other non-assignment expressions like a + b they are a special case and are only allowed in specific contexts, one of them being the continue expression.

WhilePrefix <- KEYWORD_while LPAREN Expr RPAREN PtrPayload? WhileContinueExpr?

WhileContinueExpr <- COLON LPAREN AssignExpr RPAREN

The reason for these restrictions is probably originally because languages like C that allow nesting assignments within other expressions, such as a = b += c or if (a = b), make it easy to make mistakes or write confusing code, and Zig wanted to disarm that footgun.

However, in Zig, an assignment expression returns void instead of the assigned value, so these confusing situations couldn’t occur in the first place. Therefore, there is an accepted proposal to turn assignment expressions into proper first-class expressions and allow them everywhere a void-returning expression is allowed:

and in the doc:

AssignExpr ← Expr (AssignOp Expr / (COMMA Expr)+ EQUAL Expr / !AssignOp !COMMA)
…
Expr ← PrefixExpr (OrElseExpr / CatchExpr / BoolExpr)
…
PrefixExpr ← PrefixOp* !PrefixOp PrimaryExpr

I don’t understand these well. Does it mean continue-expressions must be AssignExpr? It looks functions calls can also be used as continue-expressions now. Are function calls AssignExpr?

TL;DR: AssignExpr doesn’t necessarily mean the expression actaully has any assignment operators, might just be an integer literal or function call.

I’m pretty sure Zig makes the distinction of Expr and AssignExpr because of types in variable declarations. For example, if you have

const i: usize = 0;

Then, the parser would go through functions something like this:
parseContainerMember (the var decl)
...
parseExpr (for the usize)

Because the form a = b is considered an ‘expression’ in Zig, then the parser could consume usize = 0 as the type expression, instead of just usize. The AssignExpr is just there so this sort of ambiguity doesn’t occur.

Function calls are AssignExpr because function calls lie under the PrimaryExpr term. It is confusing, because just because a term is called TypeExpr doesn’t mean it actually is a type, same with AssignExpr. Zig splits it into something like this, with the higher functions calling the lower functions:

parseAssignExpr → assignment operators
parseExpr → infix operators and precedence
parsePrefixExpr → prefix operators
parsePrimaryExpr → blocks, ifs, switches, whiles, control flow
parseTypeExpr → error ! and || operators
parsePrimaryTypeExpr → identifiers, literals

As you can see, the identifiers or variables might not be types, but they are still contained in the parsePrimaryTypeExpr because they might be types, and more general expr parsing functions could call them.

Most of the time, Zig uses parseExpr for general expressions in say functions and stuff, uses parseTypeExpr where a type is expected (like fn ident () T) and parseAssignExpr, the most general expr parsing function, I think only when parsing while continue expressions and expressions blocks.