Skip to content

Commit ecd618b

Browse files
committed
parser: fix an edge case where expression of a variable declaration assignment placed on the next statement may cause segfault
1 parent fc53370 commit ecd618b

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

std/jule/parser/scope.jule

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,13 +1191,29 @@ impl scopeParser {
11911191
self.pushErr(token, "invalid syntax")
11921192
ret nil, false
11931193
} else if i+1 < len(tokens) {
1194+
// Expression is in the same statement,
1195+
// following the assignment operator.
11941196
assign.Op = tokens[i]
11951197
if assign.Op.ID != token::ASSIGN {
11961198
self.pushErr(assign.Op, "assignment operation @ requires single-valued expressions", assign.Op.Kind)
11971199
}
11981200
i++
11991201
mut exprTokens := tokens[i:]
12001202
assign.Y = self.p.buildExpr(exprTokens)
1203+
} else {
1204+
// The assignment operator exist, but expression is not.
1205+
// Probably the following statement is the expression.
1206+
// We must try to handle next statement as expression.
1207+
assign.Op = tokens[i]
1208+
if assign.Op.ID != token::ASSIGN {
1209+
self.pushErr(assign.Op, "assignment operation @ requires single-valued expressions", assign.Op.Kind)
1210+
}
1211+
if self.isLastSt() {
1212+
self.pushErr(assign.Op, "expected expression")
1213+
ret nil, false
1214+
}
1215+
mut exprTokens := self.next().tokens
1216+
assign.Y = self.p.buildExpr(exprTokens)
12011217
}
12021218

12031219
ok := self.buildDeclAssign1(rang, assign)

0 commit comments

Comments
 (0)