Skip to content
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

fix #45024, lost expected assignment after const error #45344

Merged
merged 1 commit into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1355,11 +1355,19 @@
(list 'where (rewrap-where x (cadr w)) (caddr w))
x))

(define (parse-struct-field s)
(let ((tok (peek-token s)))
;; allow `const x` only as a struct field
(if (eq? tok 'const)
(begin (take-token s)
`(const ,(parse-eq s)))
(parse-eq s))))

(define (parse-struct-def s mut? word)
(if (reserved-word? (peek-token s))
(error (string "invalid type name \"" (take-token s) "\"")))
(let ((sig (parse-subtype-spec s)))
(begin0 (list 'struct (if mut? '(true) '(false)) sig (parse-block s))
(begin0 (list 'struct (if mut? '(true) '(false)) sig (parse-block s parse-struct-field))
(expect-end s word))))

;; consume any number of line endings from a token stream
Expand Down Expand Up @@ -1456,7 +1464,13 @@
`(const ,expr)
expr)))
((const)
`(const ,(parse-eq s)))
(let ((assgn (parse-eq s)))
(if (not (and (pair? assgn)
(or (eq? (car assgn) '=)
(eq? (car assgn) 'global)
(eq? (car assgn) 'local))))
(error "expected assignment after \"const\"")
`(const ,assgn))))

((function macro)
(let* ((loc (line-number-node s))
Expand Down
10 changes: 10 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3369,3 +3369,13 @@ end
# issue #45162
f45162(f) = f(x=1)
@test first(methods(f45162)).called != 0

# issue #45024
@test_throws ParseError("expected assignment after \"const\"") Meta.parse("const x")
@test_throws ParseError("expected assignment after \"const\"") Meta.parse("const x::Int")
# these cases have always been caught during lowering, since (const (global x)) is not
# ambiguous with the lowered form (const x), but that could probably be changed.
@test Meta.lower(@__MODULE__, :(global const x)) == Expr(:error, "expected assignment after \"const\"")
@test Meta.lower(@__MODULE__, :(global const x::Int)) == Expr(:error, "expected assignment after \"const\"")
@test Meta.lower(@__MODULE__, :(const global x)) == Expr(:error, "expected assignment after \"const\"")
@test Meta.lower(@__MODULE__, :(const global x::Int)) == Expr(:error, "expected assignment after \"const\"")