Skip to content

Commit 57def4d

Browse files
Add builtin function name to add methods error (#59112)
``` julia> Base.throw(x::Int) = 1 ERROR: cannot add methods to builtin function `throw` Stacktrace: [1] top-level scope @ REPL[1]:1 ```
1 parent 853aba5 commit 57def4d

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/method.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
12201220
// jl_value_t **ttypes = { jl_builtin_type, jl_tparam0(jl_anytuple_type) };
12211221
// jl_value_t *invalidt = jl_apply_tuple_type_v(ttypes, 2); // Tuple{Union{Builtin,OpaqueClosure}, Vararg}
12221222
// if (!jl_has_empty_intersection(argtype, invalidt))
1223-
// jl_error("cannot add methods to a builtin function");
1223+
// jl_error("cannot add methods to builtin function");
12241224
//}
12251225

12261226
assert(jl_is_linenode(functionloc));
@@ -1299,7 +1299,7 @@ JL_DLLEXPORT jl_method_t* jl_method_def(jl_svec_t *argdata,
12991299
}
13001300
ft = jl_rewrap_unionall(ft, argtype);
13011301
if (!external_mt && !jl_has_empty_intersection(ft, (jl_value_t*)jl_builtin_type)) // disallow adding methods to Any, Function, Builtin, and subtypes, or Unions of those
1302-
jl_error("cannot add methods to a builtin function");
1302+
jl_errorf("cannot add methods to builtin function `%s`", jl_symbol_name(name));
13031303

13041304
m = jl_new_method_uninit(module);
13051305
m->external_mt = (jl_value_t*)external_mt;

test/core.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,13 +2661,16 @@ struct D14919 <: Function; end
26612661
@test B14919()() == "It's a brand new world"
26622662
@test C14919()() == D14919()() == "Boo."
26632663

2664-
let ex = ErrorException("cannot add methods to a builtin function")
2664+
let ex_t = ErrorException, ex_r = r"cannot add methods to builtin function"
26652665
for f in (:(Core.Any), :(Core.Function), :(Core.Builtin), :(Base.Callable), :(Union{Nothing,F} where F), :(typeof(Core.getfield)), :(Core.IntrinsicFunction))
2666-
@test_throws ex @eval (::$f)() = 1
2666+
@test_throws ex_t @eval (::$f)() = 1
2667+
@test_throws ex_r @eval (::$f)() = 1
26672668
end
2668-
@test_throws ex @eval (::Union{Nothing,F})() where {F<:Function} = 1
2669+
@test_throws ex_t @eval (::Union{Nothing,F})() where {F<:Function} = 1
2670+
@test_throws ex_r @eval (::Union{Nothing,F})() where {F<:Function} = 1
26692671
for f in (:(Core.getfield),)
2670-
@test_throws ex @eval $f() = 1
2672+
@test_throws ex_t @eval $f() = 1
2673+
@test_throws ex_r @eval $f() = 1
26712674
end
26722675
end
26732676

0 commit comments

Comments
 (0)