diff --git a/README.md b/README.md index 4ba069810..080e1f251 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,6 @@ Currently, the `@compat` macro supports the following syntaxes: `CartesianRange` now has two type parameters, so using them as fields in other `struct`s requires manual intervention. -* Required keyword arguments ([#25830]). For example, `@compat foo(; x, y)` makes `x` and `y` required keyword arguments: when calling `foo`, an error is thrown if `x` or `y` is not explicitly provided. - ## Module Aliases ## New functions, macros, and methods diff --git a/src/compatmacro.jl b/src/compatmacro.jl index 09234c613..97a78771b 100644 --- a/src/compatmacro.jl +++ b/src/compatmacro.jl @@ -4,27 +4,9 @@ using Base.Meta export @compat -if !isdefined(Base, :UndefKeywordError) - struct UndefKeywordError <: Exception - kw - end - Base.showerror(io::IO, e::UndefKeywordError) = print(io, "UndefKeywordError: keyword argument $(e.kw) not assigned") - export UndefKeywordError -end - -"Convert a functions symbol argument to the corresponding required keyword argument." -function symbol2kw(sym::Symbol) - Expr(:kw, sym, Expr(:call, throw, UndefKeywordError(sym))) -end -symbol2kw(arg) = arg - function _compat(ex::Expr) if ex.head === :call f = ex.args[1] - if !isdefined(Base, :UndefKeywordError) && length(ex.args) > 1 && isexpr(ex.args[2], :parameters) - params = ex.args[2] - params.args = map(symbol2kw, params.args) - end elseif ex.head === :quote && isa(ex.args[1], Symbol) # Passthrough return ex diff --git a/test/old.jl b/test/old.jl index 771f941c0..a91c7e63a 100644 --- a/test/old.jl +++ b/test/old.jl @@ -1277,3 +1277,21 @@ if VERSION < v"0.7.0-beta2.143" @test_throws UndefKeywordError dropdims(a) end end + +let + # test required keyword arguments + @compat func1() = 1 + @test func1() == 1 # using the function works + @compat func2(x) = x + @test func2(3) == 3 # using the function works + @compat func3(;y) = y + @test func3(y=2) == 2 # using the function works + @test_throws UndefKeywordError func3() + @compat func4(x; z) = x*z + @test func4(2,z=3) == 6 # using the function works + @test_throws UndefKeywordError func4(2) + @compat func5(;x=1, y) = x*y + @test func5(y=3) == 3 + @test func5(y=3, x=2) == 6 + @test_throws UndefKeywordError func5(x=2) +end diff --git a/test/runtests.jl b/test/runtests.jl index 167733fc0..ba72fa9da 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -80,24 +80,6 @@ let A = [1] @test x == 1 end -let - # test required keyword arguments - @compat func1() = 1 - @test func1() == 1 # using the function works - @compat func2(x) = x - @test func2(3) == 3 # using the function works - @compat func3(;y) = y - @test func3(y=2) == 2 # using the function works - @test_throws UndefKeywordError func3() - @compat func4(x; z) = x*z - @test func4(2,z=3) == 6 # using the function works - @test_throws UndefKeywordError func4(2) - @compat func5(;x=1, y) = x*y - @test func5(y=3) == 3 - @test func5(y=3, x=2) == 6 - @test_throws UndefKeywordError func5(x=2) -end - # Support for positional `stop` @test Compat.range(0, 5, length = 6) == 0.0:1.0:5.0 @test Compat.range(0, 10, step = 2) == 0:2:10