Skip to content

Commit c3ca854

Browse files
committed
lower new() to reference the called object instead of re-creating it with apply_type
addresses #36384
1 parent b9b2a3c commit c3ca854

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/julia-syntax.scm

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,9 @@
791791
(define (num-non-varargs args)
792792
(count (lambda (a) (not (vararg? a))) args))
793793

794-
(define (new-call Tname type-params sparams params args field-names field-types)
794+
;; selftype?: tells us whether the called object is the type being constructed,
795+
;; i.e. `new()` and not `new{...}()`.
796+
(define (new-call Tname type-params sparams params args field-names field-types selftype?)
795797
(if (any kwarg? args)
796798
(error "\"new\" does not accept keyword arguments"))
797799
(let ((nnv (num-non-varargs type-params)))
@@ -801,9 +803,11 @@
801803
(error "too many type parameters specified in \"new{...}\"")))
802804
(let* ((Texpr (if (null? type-params)
803805
`(outerref ,Tname)
804-
`(curly (outerref ,Tname)
805-
,@type-params)))
806-
(tn (make-ssavalue))
806+
(if selftype?
807+
'|#self#|
808+
`(curly (outerref ,Tname)
809+
,@type-params))))
810+
(tn (if (symbol? Texpr) Texpr (make-ssavalue)))
807811
(field-convert (lambda (fld fty val)
808812
(if (equal? fty '(core Any))
809813
val
@@ -823,7 +827,7 @@
823827
(let ((argt (make-ssavalue))
824828
(nf (make-ssavalue)))
825829
`(block
826-
(= ,tn ,Texpr)
830+
,@(if (symbol? tn) '() `((= ,tn ,Texpr)))
827831
(= ,argt (call (core tuple) ,@args))
828832
(= ,nf (call (core nfields) ,argt))
829833
(if (call (top ult_int) ,nf ,(length field-names))
@@ -835,9 +839,9 @@
835839
(new ,tn ,@(map (lambda (fld fty) (field-convert fld fty `(call (core getfield) ,argt ,(+ fld 1) (false))))
836840
(iota (length field-names)) (list-head field-types (length field-names))))))))
837841
(else
838-
`(block
839-
(= ,tn ,Texpr)
840-
(new ,tn ,@(map field-convert (iota (length args)) (list-head field-types (length args)) args)))))))
842+
`(block
843+
,@(if (symbol? tn) '() `((= ,tn ,Texpr)))
844+
(new ,tn ,@(map field-convert (iota (length args)) (list-head field-types (length args)) args)))))))
841845

842846
;; insert item at start of arglist
843847
(define (arglist-unshift sig item)
@@ -881,12 +885,12 @@
881885
(call (-/ new) . args)
882886
(new-call Tname type-params sparams params
883887
(map (lambda (a) (ctor-body a type-params sparams)) args)
884-
field-names field-types))
888+
field-names field-types #t))
885889
(pattern-lambda
886890
(call (curly (-/ new) . p) . args)
887891
(new-call Tname p sparams params
888892
(map (lambda (a) (ctor-body a type-params sparams)) args)
889-
field-names field-types)))
893+
field-names field-types #f)))
890894
body))
891895
(pattern-replace
892896
(pattern-set

0 commit comments

Comments
 (0)