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

Make overriding closure methods an error #39498

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 16 additions & 2 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,15 @@ static void method_overwrite(jl_typemap_entry_t *newentry, jl_method_t *oldvalue
if ((jl_options.warn_overwrite == JL_OPTIONS_WARN_OVERWRITE_ON) ||
(jl_options.incremental && jl_generating_output()) || anon) {
JL_STREAM *s = JL_STDERR;
jl_printf(s, "WARNING: Method definition ");
ios_t memstream;
if (anon) {
ios_mem(&memstream, 300);
s = (JL_STREAM*)&memstream;
}
else {
jl_printf(s, "WARNING: ");
}
jl_printf(s, "Method definition ");
jl_static_show_func_sig(s, (jl_value_t*)newentry->sig);
jl_printf(s, " in module %s", jl_symbol_name(oldmod->name));
print_func_loc(s, oldvalue);
Expand All @@ -1287,7 +1295,13 @@ static void method_overwrite(jl_typemap_entry_t *newentry, jl_method_t *oldvalue
jl_printf(s, anon ? " on the same line" : " on the same line (check for duplicate calls to `include`)");
else
print_func_loc(s, method);
jl_printf(s, ".\n");
jl_printf(s, ".");
if (anon) {
jl_value_t *msg = jl_pchar_to_string(memstream.buf, memstream.size);
JL_GC_PUSH1(&msg);
jl_throw(jl_new_struct(jl_errorexception_type, msg));
}
jl_printf(s, "\n");
jl_uv_flush(s);
}
if (jl_options.incremental && jl_generating_output())
Expand Down
17 changes: 12 additions & 5 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,25 @@ let
$redir_err
module A; f() = 1; end; A.f() = 1
A.f() = 1
outer() = (g() = 1; g() = 2; g)
"""
warning_str = read(`$exename --warn-overwrite=yes --startup-file=no -e $script`, String)
@test warning_str == """
WARNING: Method definition f() in module A at none:2 overwritten in module Main on the same line (check for duplicate calls to `include`).
WARNING: Method definition f() in module Main at none:2 overwritten at none:3.
WARNING: Method definition g() in module Main at none:4 overwritten on the same line.
"""
warning_str = read(`$exename --startup-file=no -e $script`, String)
@test warning_str == """
WARNING: Method definition g() in module Main at none:4 overwritten on the same line.
"""
@test warning_str == ""
end

# Overriding closure methods is an error (#15602)
let g_line = (@__LINE__) + 3,
closure_method_overwrite =
:(function closure_method_overrwrite_g()
g() = 1
g() = 2
end)
@test_throws ErrorException("Method definition g() in module $(nameof(@__MODULE__)) at $(@__FILE__):$g_line overwritten at $(@__FILE__):$(g_line+1).") #=
=# eval(closure_method_overwrite)
end

# Debugging tool: return the current state of the enable_finalizers counter.
Expand Down
22 changes: 9 additions & 13 deletions test/specificity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,15 @@ _bound_vararg_specificity_1(::Type{Array{T,1}}, d::Int) where {T} = 1
@test args_morespecific(Tuple{Matrix}, Tuple{AbstractVector})

# Method specificity
begin
local f, A
f(dims::Tuple{}, A::AbstractArray{T,0}) where {T} = 1
f(dims::NTuple{N,Int}, A::AbstractArray{T,N}) where {T,N} = 2
f(dims::NTuple{M,Int}, A::AbstractArray{T,N}) where {T,M,N} = 3
A = zeros(2,2)
@test f((1,2,3), A) == 3
@test f((1,2), A) == 2
@test f((), reshape([1])) == 1
f(dims::NTuple{N,Int}, A::AbstractArray{T,N}) where {T,N} = 4
@test f((1,2), A) == 4
@test f((1,2,3), A) == 3
end
f22162(dims::Tuple{}, A::AbstractArray{T,0}) where {T} = 1
f22162(dims::NTuple{N,Int}, A::AbstractArray{T,N}) where {T,N} = 2
f22162(dims::NTuple{M,Int}, A::AbstractArray{T,N}) where {T,M,N} = 3
@test f22162((1,2,3), zeros(2,2)) == 3
@test f22162((1,2), zeros(2,2)) == 2
@test f22162((), reshape([1])) == 1
f22162(dims::NTuple{N,Int}, A::AbstractArray{T,N}) where {T,N} = 4
@test f22162((1,2), zeros(2,2)) == 4
@test f22162((1,2,3), zeros(2,2)) == 3

# a method specificity issue
c99991(::Type{T},x::T) where {T} = 0
Expand Down
6 changes: 3 additions & 3 deletions test/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,9 @@ f38837(xs) = map((F,x)->F(x), (Float32, Float64), xs)
@test @inferred(f(Tuple(1:10))) === Tuple(3:8)
@test @inferred(f((true, 2., 3, 4f0, 0x05, 6, 7.))) === (3, 4f0, 0x05)

f(t) = t[Base.OneTo(5)]
@test @inferred(f(Tuple(1:10))) === Tuple(1:5)
@test @inferred(f((true, 2., 3, 4f0, 0x05, 6, 7.))) === (true, 2., 3, 4f0, 0x05)
g(t) = t[Base.OneTo(5)]
@test @inferred(g(Tuple(1:10))) === Tuple(1:5)
@test @inferred(g((true, 2., 3, 4f0, 0x05, 6, 7.))) === (true, 2., 3, 4f0, 0x05)

@test @inferred((t -> t[1:end])(Tuple(1:15))) === Tuple(1:15)
@test @inferred((t -> t[2:end])(Tuple(1:15))) === Tuple(2:15)
Expand Down