Skip to content

Commit

Permalink
fix #28593, macro hygiene problems in type definitions (#28706)
Browse files Browse the repository at this point in the history
(cherry picked from commit cd850de)
  • Loading branch information
JeffBezanson authored and KristofferC committed Aug 24, 2018
1 parent 930bc5f commit 0caae7c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
18 changes: 13 additions & 5 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,17 @@
(let ((bounds (map analyze-typevar params)))
(values (map car bounds) bounds)))

(define (unmangled-name v)
(if (eq? v '||)
v
(let ((s (string v)))
(if (eqv? (string.char s 0) #\#)
(symbol (last (string-split s "#")))
v))))

;; construct expression to allocate a TypeVar
(define (bounds-to-TypeVar v)
(let ((v (car v))
(define (bounds-to-TypeVar v (unmangle #f))
(let ((v ((if unmangle unmangled-name identity) (car v)))
(lb (cadr v))
(ub (caddr v)))
`(call (core TypeVar) ',v
Expand Down Expand Up @@ -836,7 +844,7 @@
(block
(global ,name) (const ,name)
,@(map (lambda (v) `(local ,v)) params)
,@(map (lambda (n v) (make-assignment n (bounds-to-TypeVar v))) params bounds)
,@(map (lambda (n v) (make-assignment n (bounds-to-TypeVar v #t))) params bounds)
(struct_type ,name (call (core svec) ,@params)
(call (core svec) ,@(map quotify field-names))
,super (call (core svec) ,@field-types) ,mut ,min-initialized)))
Expand Down Expand Up @@ -877,7 +885,7 @@
(scope-block
(block
,@(map (lambda (v) `(local ,v)) params)
,@(map (lambda (n v) (make-assignment n (bounds-to-TypeVar v))) params bounds)
,@(map (lambda (n v) (make-assignment n (bounds-to-TypeVar v #t))) params bounds)
(abstract_type ,name (call (core svec) ,@params) ,super))))))

(define (primitive-type-def-expr n name params super)
Expand All @@ -888,7 +896,7 @@
(scope-block
(block
,@(map (lambda (v) `(local ,v)) params)
,@(map (lambda (n v) (make-assignment n (bounds-to-TypeVar v))) params bounds)
,@(map (lambda (n v) (make-assignment n (bounds-to-TypeVar v #t))) params bounds)
(primitive_type ,name (call (core svec) ,@params) ,n ,super))))))

;; take apart a type signature, e.g. T{X} <: S{Y}
Expand Down
36 changes: 26 additions & 10 deletions src/macroexpand.scm
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,21 @@
(if var (list 'varlist var) '()))

;; type definition
(pattern-lambda (struct mut (<: (curly tn . tvars) super) body)
(list* 'varlist (cons (unescape tn) (unescape tn)) '(new . new)
(typevar-names tvars)))
(pattern-lambda (struct mut (curly tn . tvars) body)
(list* 'varlist (cons (unescape tn) (unescape tn)) '(new . new)
(typevar-names tvars)))
(pattern-lambda (struct mut (<: tn super) body)
(list 'varlist (cons (unescape tn) (unescape tn)) '(new . new)))
(pattern-lambda (struct mut tn body)
(list 'varlist (cons (unescape tn) (unescape tn)) '(new . new)))
(pattern-lambda (struct mut spec body)
(let ((tn (typedef-expr-name spec))
(tv (typedef-expr-tvars spec)))
(list* 'varlist (cons (unescape tn) (unescape tn)) '(new . new)
(typevar-names tv))))
(pattern-lambda (abstract spec)
(let ((tn (typedef-expr-name spec))
(tv (typedef-expr-tvars spec)))
(list* 'varlist (cons (unescape tn) (unescape tn))
(typevar-names tv))))
(pattern-lambda (primitive spec nb)
(let ((tn (typedef-expr-name spec))
(tv (typedef-expr-tvars spec)))
(list* 'varlist (cons (unescape tn) (unescape tn))
(typevar-names tv))))

)) ; vars-introduced-by-patterns

Expand Down Expand Up @@ -178,6 +183,17 @@
(cadr e)
e))

(define (typedef-expr-name e)
(cond ((atom? e) e)
((or (eq? (car e) 'curly) (eq? (car e) '<:)) (typedef-expr-name (cadr e)))
(else e)))

(define (typedef-expr-tvars e)
(cond ((atom? e) '())
((eq? (car e) '<:) (typedef-expr-tvars (cadr e)))
((eq? (car e) 'curly) (cddr e))
(else '())))

(define (typevar-expr-name e) (car (analyze-typevar e)))

;; get the list of names from a list of `where` variable expressions
Expand Down
27 changes: 27 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1646,3 +1646,30 @@ end
# issue #28576
@test Meta.isexpr(Meta.parse("1 == 2 ?"), :incomplete)
@test Meta.isexpr(Meta.parse("1 == 2 ? 3 :"), :incomplete)

# issue #28593
macro a28593()
quote
abstract type A28593{S<:Real, V<:AbstractVector{S}} end
end
end

macro b28593()
quote
struct B28593{S<:Real, V<:AbstractVector{S}} end
end
end

macro c28593()
quote
primitive type C28593{S<:Real, V<:AbstractVector{S}} 32 end
end
end

@a28593
@b28593
@c28593

@test A28593.var.name === :S
@test B28593.var.name === :S
@test C28593.var.name === :S

0 comments on commit 0caae7c

Please sign in to comment.