Skip to content

Commit

Permalink
Prohibit const with non-variable expressions on the left hand side
Browse files Browse the repository at this point in the history
Expressions like

    const x[] = 1

were found in the wild, and worked until 1.12.  Explicitly prohibit this with an
error message:

    error: `const` left hand side "x[]" contains non-variables
  • Loading branch information
xal-0 committed Feb 19, 2025
1 parent 6ca5417 commit 0f5a7aa
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,8 @@
;; because the symbols come out wrong. Sigh... So much effort for such a syntax corner case.
(expand-tuple-destruct (cdr (cadr arg)) (caddr arg) (lambda (assgn) `(,(car e) ,assgn))))
(else
(unless (const-lhs? (cadr arg))
(error (string "`const` left hand side \"" (deparse (cadr arg)) "\" contains non-variables")))
(let* ((vars (filter-not-underscore (lhs-vars (cadr arg))))
(rr (make-ssavalue))
(temp (map (lambda (v) (make-ssavalue)) vars)))
Expand Down Expand Up @@ -2979,6 +2981,15 @@
(define (lhs-vars e)
(map decl-var (lhs-decls e)))

;; Does the assignment LHS only declare new variables (no assignment to (ref ...))
(define (const-lhs? e)
(cond ((symdecl? e) #t)
((and (pair? e)
(or (eq? (car e) 'tuple)
(eq? (car e) 'parameters)))
(every const-lhs? (cdr e)))
(else #f)))

(define (all-decl-vars e) ;; map decl-var over every level of an assignment LHS
(cond ((eventually-call? e) e)
((decl? e) (decl-var e))
Expand Down

0 comments on commit 0f5a7aa

Please sign in to comment.