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

Consistently parse A <: B as a Expr(:comparison, A, :(<:), B) #9582

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ New language features
Language changes
----------------

* `A <: B` is parsed consistently as `Expr(:comparison, :A, :(<:), :B)` ([#9582]).

* `Uint` et al. are now spelled `UInt` ([#8905]).

* `String` has been renamed to `AbstractString` ([#8872]).
Expand Down
2 changes: 1 addition & 1 deletion base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function gen_cartesian(N::Int)
if !in(N,implemented)
fieldnames = [symbol("I_$i") for i = 1:N]
fields = [Expr(:(::), fieldnames[i], :Int) for i = 1:N]
extype = Expr(:type, false, Expr(:(<:), indextype, Expr(:curly, :CartesianIndex, N)), Expr(:block, fields...))
extype = Expr(:type, false, Expr(:comparison, indextype, :(<:), Expr(:curly, :CartesianIndex, N)), Expr(:block, fields...))
exindices = Expr[:(index[$i]) for i = 1:N]
totalex = quote
# type definition of state
Expand Down
11 changes: 2 additions & 9 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -881,13 +881,6 @@
(else
ex)))))

;; convert (comparison a <: b) to (<: a b)
(define (subtype-syntax e)
(if (and (pair? e) (eq? (car e) 'comparison)
(length= e 4) (eq? (caddr e) '|<:|))
`(<: ,(cadr e) ,(cadddr e))
e))

(define (parse-unary-prefix s)
(let ((op (peek-token s)))
(if (syntactic-unary-op? op)
Expand Down Expand Up @@ -987,7 +980,7 @@
(loop (list t ex)))
((#\{ ) (take-token s)
(loop (list* 'curly ex
(map subtype-syntax (parse-arglist s #\} )))))
(parse-arglist s #\} ))))
((#\")
(if (and (symbol? ex) (not (operator? ex))
(not (ts:space? s)))
Expand Down Expand Up @@ -1022,7 +1015,7 @@
" expected \"end\", got \"" t "\""))))))

(define (parse-subtype-spec s)
(subtype-syntax (parse-comparison s)))
(parse-comparison s))

; parse expressions or blocks introduced by syntactic reserved words
(define (parse-resword s word)
Expand Down
26 changes: 14 additions & 12 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,9 @@
(define (sparam-name sp)
(cond ((symbol? sp)
sp)
((and (length= sp 3)
(eq? (car sp) '|<:|)
((and (length= sp 4)
(eq? (car sp) 'comparison)
(eq? (undot-name (caddr sp)) '<:)
(symbol? (cadr sp)))
(cadr sp))
(else (error "malformed type parameter list"))))
Expand All @@ -416,11 +417,12 @@
((symbol? (car sparams))
(sparam-name-bounds (cdr sparams) (cons (car sparams) names)
(cons '(top Any) bounds)))
((and (length= (car sparams) 3)
(eq? (caar sparams) '|<:|)
((and (length= (car sparams) 4)
(eq? (caar sparams) 'comparison)
(eq? (undot-name (caddar sparams)) '<:)
(symbol? (cadar sparams)))
(sparam-name-bounds (cdr sparams) (cons (cadr (car sparams)) names)
(cons (caddr (car sparams)) bounds)))
(cons (cadddr (car sparams)) bounds)))
(else
(error "malformed type parameter list"))))

Expand Down Expand Up @@ -746,7 +748,7 @@
(define (default-outer-ctor name field-names field-types params bounds)
(let ((field-names (safe-field-names field-names field-types)))
`(function (call (curly ,name
,@(map (lambda (p b) `(<: ,p ,b))
,@(map (lambda (p b) `(comparison ,p <: ,b))
params bounds))
,@(map make-decl field-names field-types))
(block
Expand Down Expand Up @@ -804,15 +806,15 @@
,@(arglist-unshift sig `(|::| ,(gensy) (curly Type ,name))))
params))
(if (null? method-params)
(cons `(call (curly call ,@(map (lambda (p b) `(<: ,p ,b)) params bounds))
(cons `(call (curly call ,@(map (lambda (p b) `(comparison ,p <: ,b)) params bounds))
,@(arglist-unshift sig `(|::| ,(gensy) (curly Type (curly ,name ,@params)))))
params)
;; rename parameters that conflict with user-written method parameters
(let ((new-params (map (lambda (p) (if (memq p method-params)
(gensy)
p))
params)))
(cons `(call (curly call ,@(map (lambda (p b) `(<: ,p ,b)) new-params bounds)
(cons `(call (curly call ,@(map (lambda (p b) `(comparison ,p <: ,b)) new-params bounds)
,@method-params)
,@(arglist-unshift sig `(|::| ,(gensy) (curly Type (curly ,name ,@new-params)))))
new-params)))))
Expand Down Expand Up @@ -972,9 +974,9 @@
(values name '() 'Any)) ex)
((pattern-lambda (curly (-- name (-s)) . params)
(values name params 'Any)) ex)
((pattern-lambda (|<:| (-- name (-s)) super)
((pattern-lambda (comparison (-- name (-s)) <: super)
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be (-/ <:) to make it match a literal <:. Otherwise <: is a pattern variable like other symbols.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, failure to match this pattern is the reason this worked at all. Thanks.

(values name '() super)) ex)
((pattern-lambda (|<:| (curly (-- name (-s)) . params) super)
((pattern-lambda (comparison (curly (-- name (-s)) . params) <: super)
(values name params super)) ex)
(error "invalid type signature")))

Expand Down Expand Up @@ -1360,13 +1362,13 @@
(if var (list 'varlist var) '()))

;; type definition
(pattern-lambda (type mut (<: (curly tn . tvars) super) body)
(pattern-lambda (type mut (comparison (curly tn . tvars) <: super) body)
(list* 'varlist (cons (unescape tn) (unescape tn)) '(new . new)
(map typevar-expr-name tvars)))
(pattern-lambda (type mut (curly tn . tvars) body)
(list* 'varlist (cons (unescape tn) (unescape tn)) '(new . new)
(map typevar-expr-name tvars)))
(pattern-lambda (type mut (<: tn super) body)
(pattern-lambda (type mut (comparison tn <: super) body)
(list 'varlist (cons (unescape tn) (unescape tn)) '(new . new)))
(pattern-lambda (type mut tn body)
(list 'varlist (cons (unescape tn) (unescape tn)) '(new . new)))
Expand Down