Skip to content

Commit

Permalink
Improve location information for @qstruct etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
cstjean committed Dec 11, 2023
1 parent df2eb2c commit 2ed4967
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/QuickTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ narrow_typeof(t::T) where {T} = T

# Helper for @qmutable/@qstruct
# narrow_types means that
function qexpansion(def::Expr, mutable::Bool, fully_parametric::Bool, narrow_types::Bool)
function qexpansion(__source__, def::Expr, mutable::Bool, fully_parametric::Bool,narrow_types::Bool)
if !@capture(def, typ_def_ <: parent_type_)
typ_def = def
parent_type = :Any
Expand Down Expand Up @@ -226,18 +226,21 @@ function qexpansion(def::Expr, mutable::Bool, fully_parametric::Bool, narrow_typ
else
given_types = type_vars
end
inner_constr = quote
inner_constr = @q begin
function $type_with_vars($(constr_args...);
$(constr_kwargs...)) where {$(type_params...)}
$__source__
$constraints
return new{$(type_vars...)}($(new_args...))
end
end
straight_constr = :($name($(args...); $(reg_kwargs...)) where {$(type_vars...)} =
$name{$(given_types...)}($(o_constr_args...);
$(o_constr_kwargs...)))
straight_constr = @q function $name($(args...); $(reg_kwargs...)) where {$(type_vars...)}
$__source__
$name{$(given_types...)}($(o_constr_args...); $(o_constr_kwargs...))
end
type_def =
quote
@q begin
$__source__
Base.@__doc__ $(Expr(:struct,
mutable, Expr(:<:, typ, parent_type),
Expr(:block, fields..., kwfields...,
Expand Down Expand Up @@ -301,12 +304,12 @@ Note: `@qstruct` automatically defines a `Base.show` method for the new type,
unless `_define_show=false` (eg. `@qstruct(x, y; _define_show=false)`).
"""
macro qstruct(def)
return qexpansion(def, false, false, false)
return qexpansion(__source__, def, false, false, false)
end

""" Quick mutable struct definition. See ?@qstruct """
macro qmutable(def)
return qexpansion(def, true, false, false)
return qexpansion(__source__, def, true, false, false)
end

# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -353,23 +356,23 @@ end
""" Fully-parametric version of `@qstruct`. `@qstruct_fp Foo(a, b=2)` is like
`@qstruct Foo{T, U}(a::T, B::U=2)` """
macro qstruct_fp(def)
return qexpansion(def, false, true, false)
return qexpansion(__source__, def, false, true, false)
end
""" Fully-parametric version of `@qmutable`. `@qmutable_fp Foo(a, b=2)` is like
`@qmutable Foo{T, U}(a::T, B::U=2)` """
macro qmutable_fp(def)
return qexpansion(def, true, true, false)
return qexpansion(__source__, def, true, true, false)
end


""" Narrowly-parametric version of `@qstruct`. `@qstruct_np Foo(a, b=2)` is like
`@qstruct Foo{T, U}(a::T, B::U=2)`, but it will additionally specialize on types:
`Foo(Int, 2.0) ==> Foo{Type{Int64},Float64}(Int64, 2.0)` """
macro qstruct_np(def)
return qexpansion(def, false, true, true)
return qexpansion(__source__, def, false, true, true)
end
macro qmutable_np(def)
return qexpansion(def, true, true, true)
return qexpansion(__source__, def, true, true, true)
end

################################################################################
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ end
@test_throws AssertionError Foo(11; y=10.0)
@test_throws AssertionError construct(Foo, 11, 10.0)

@test is_defined_in_runtest(which(Foo, Tuple{Int}))

################################################################################
# Fully-parametric

Expand Down

0 comments on commit 2ed4967

Please sign in to comment.