Skip to content

Commit e4d2f0f

Browse files
JeffBezansonKristofferC
authored andcommitted
fix #45024, lost expected assignment after const error (#45344)
(cherry picked from commit 2d40898)
1 parent 57c884c commit e4d2f0f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/julia-parser.scm

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,11 +1355,19 @@
13551355
(list 'where (rewrap-where x (cadr w)) (caddr w))
13561356
x))
13571357

1358+
(define (parse-struct-field s)
1359+
(let ((tok (peek-token s)))
1360+
;; allow `const x` only as a struct field
1361+
(if (eq? tok 'const)
1362+
(begin (take-token s)
1363+
`(const ,(parse-eq s)))
1364+
(parse-eq s))))
1365+
13581366
(define (parse-struct-def s mut? word)
13591367
(if (reserved-word? (peek-token s))
13601368
(error (string "invalid type name \"" (take-token s) "\"")))
13611369
(let ((sig (parse-subtype-spec s)))
1362-
(begin0 (list 'struct (if mut? '(true) '(false)) sig (parse-block s))
1370+
(begin0 (list 'struct (if mut? '(true) '(false)) sig (parse-block s parse-struct-field))
13631371
(expect-end s word))))
13641372

13651373
;; consume any number of line endings from a token stream
@@ -1456,7 +1464,13 @@
14561464
`(const ,expr)
14571465
expr)))
14581466
((const)
1459-
`(const ,(parse-eq s)))
1467+
(let ((assgn (parse-eq s)))
1468+
(if (not (and (pair? assgn)
1469+
(or (eq? (car assgn) '=)
1470+
(eq? (car assgn) 'global)
1471+
(eq? (car assgn) 'local))))
1472+
(error "expected assignment after \"const\"")
1473+
`(const ,assgn))))
14601474

14611475
((function macro)
14621476
(let* ((loc (line-number-node s))

test/syntax.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,3 +3278,13 @@ demo44723()::Any = Base.Experimental.@opaque () -> true ? 1 : 2
32783278
# issue #45162
32793279
f45162(f) = f(x=1)
32803280
@test first(methods(f45162)).called != 0
3281+
3282+
# issue #45024
3283+
@test_throws ParseError("expected assignment after \"const\"") Meta.parse("const x")
3284+
@test_throws ParseError("expected assignment after \"const\"") Meta.parse("const x::Int")
3285+
# these cases have always been caught during lowering, since (const (global x)) is not
3286+
# ambiguous with the lowered form (const x), but that could probably be changed.
3287+
@test Meta.lower(@__MODULE__, :(global const x)) == Expr(:error, "expected assignment after \"const\"")
3288+
@test Meta.lower(@__MODULE__, :(global const x::Int)) == Expr(:error, "expected assignment after \"const\"")
3289+
@test Meta.lower(@__MODULE__, :(const global x)) == Expr(:error, "expected assignment after \"const\"")
3290+
@test Meta.lower(@__MODULE__, :(const global x::Int)) == Expr(:error, "expected assignment after \"const\"")

0 commit comments

Comments
 (0)