Skip to content

Commit 3f465bc

Browse files
authored
Merge pull request #84 from TuringLang/ref_begin_end
Use `replace_ref_begin_end!` on Julia >= 1.5.0-DEV.666
2 parents b7159cb + 28ac83f commit 3f465bc

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/varname.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ _issubrange(i::Colon, j::ConcreteIndex) = true
139139
140140
A macro that returns an instance of `VarName` given the symbol or expression of a Julia variable,
141141
e.g. `@varname x[1,2][1+5][45][3]` returns `VarName{:x}(((1, 2), (6,), (45,), (3,)))`.
142+
143+
!!! compat "Julia 1.5"
144+
Using `begin` in an indexing expression to refer to the first index requires at least
145+
Julia 1.5.
142146
"""
143147
macro varname(expr::Union{Expr, Symbol})
144148
return esc(varname(expr))
@@ -177,8 +181,12 @@ end
177181
"""
178182
@vinds(expr)
179183
180-
Returns a tuple of tuples of the indices in `expr`. For example, `@vinds x[1, :][2]` returns
184+
Returns a tuple of tuples of the indices in `expr`. For example, `@vinds x[1, :][2]` returns
181185
`((1, Colon()), (2,))`.
186+
187+
!!! compat "Julia 1.5"
188+
Using `begin` in an indexing expression to refer to the first index requires at least
189+
Julia 1.5.
182190
"""
183191
macro vinds(expr::Union{Expr, Symbol})
184192
return esc(vinds(expr))
@@ -188,7 +196,11 @@ vinds(expr::Symbol) = Expr(:tuple)
188196
function vinds(expr::Expr)
189197
if Meta.isexpr(expr, :ref)
190198
ex = copy(expr)
191-
Base.replace_ref_end!(ex)
199+
@static if VERSION < v"1.5.0-DEV.666"
200+
Base.replace_ref_end!(ex)
201+
else
202+
Base.replace_ref_begin_end!(ex)
203+
end
192204
last = Expr(:tuple, ex.args[2:end]...)
193205
init = vinds(ex.args[1]).args
194206
return Expr(:tuple, init..., last)
@@ -197,7 +209,6 @@ function vinds(expr::Expr)
197209
end
198210
end
199211

200-
201212
@generated function inargnames(::VarName{s}, ::Model{_F, argnames}) where {s, argnames, _F}
202213
return s in argnames
203214
end

test/compiler.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,14 @@ end
543543
var_expr = :(x[2:3,2:3][[1,2],[1,2]])
544544
@test vsym(var_expr) == :x
545545
@test vinds(var_expr) == :(((2:3, 2:3), ([1, 2], [1, 2])))
546+
547+
var_expr = :(x[end])
548+
@test vsym(var_expr) == :x
549+
@test vinds(var_expr) == :((($lastindex(x),),))
550+
551+
var_expr = :(x[1, end])
552+
@test vsym(var_expr) == :x
553+
@test vinds(var_expr) == :(((1, $lastindex(x, 2)),))
546554
end
547555
@testset "user-defined variable name" begin
548556
@model f1() = begin

0 commit comments

Comments
 (0)