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 #37890, fix new error checks with varargs #37894

Merged
merged 1 commit into from
Oct 6, 2020
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
14 changes: 9 additions & 5 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -746,13 +746,17 @@
,@locs
(call (curly ,name ,@params) ,@field-names)))))

(define (num-non-varargs args)
(count (lambda (a) (not (vararg? a))) args))

(define (new-call Tname type-params sparams params args field-names field-types)
(if (any kwarg? args)
(error "\"new\" does not accept keyword arguments"))
(if (length> params (length type-params))
(error "too few type parameters specified in \"new{...}\""))
(if (length> type-params (length params))
(error "too many type parameters specified in \"new{...}\""))
(let ((nnv (num-non-varargs type-params)))
(if (and (not (any vararg? type-params)) (length> params nnv))
(error "too few type parameters specified in \"new{...}\""))
(if (> nnv (length params))
(error "too many type parameters specified in \"new{...}\"")))
(let* ((Texpr (if (null? type-params)
`(outerref ,Tname)
`(curly (outerref ,Tname)
Expand All @@ -767,7 +771,7 @@
; local variable (currently just handles sparam) for the bijection of params to type-params
`(call (core fieldtype) ,tn ,(+ fld 1)))
,val)))))
(cond ((length> (filter (lambda (a) (not (vararg? a))) args) (length field-names))
(cond ((> (num-non-varargs args) (length field-names))
`(call (core throw) (call (top ArgumentError)
,(string "new: too many arguments (expected " (length field-names) ")"))))
((any vararg? args)
Expand Down
17 changes: 17 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2345,3 +2345,20 @@ end

# issue #37656
@test :(if true 'a' else 1 end) == Expr(:if, true, quote 'a' end, quote 1 end)

# issue #37890
struct A37890{A, B}
a
b
A37890(args::Tuple) = return new{typeof.(args)...}(args...)
end
@test A37890((1, "")) isa A37890{Int, String}
@test_throws ErrorException A37890((1,1,1))
@test_throws TypeError A37890((1,))

struct B37890{A, B}
a
b
B37890(a, b) = new{Int, ()..., Int8}(a, b)
end
@test B37890(1.0, 2.0f0) isa B37890{Int, Int8}