Skip to content

Commit b5f2df4

Browse files
vtjnashsimeonschaub
authored andcommitted
refine method overwrite warning (JuliaLang#36609)
Always show the warning for anonymous functions, but update the verbiage to give additional information. This warning can still be avoided by explicitly calling delete_method first. fixes JuliaLang#32635 fixes JuliaLang#35140 refs JuliaLang#15602
1 parent fef8377 commit b5f2df4

File tree

5 files changed

+46
-22
lines changed

5 files changed

+46
-22
lines changed

src/datatype.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,10 @@ void jl_compute_field_offsets(jl_datatype_t *st)
521521

522522
static int is_anonfn_typename(char *name)
523523
{
524-
if (name[0] != '#')
524+
if (name[0] != '#' || name[1] == '#')
525525
return 0;
526526
char *other = strrchr(name, '#');
527-
return (name[1] != '#' && other > &name[1] && is10digit(other[1]));
527+
return other > &name[1] && is10digit(other[1]);
528528
}
529529

530530
JL_DLLEXPORT jl_datatype_t *jl_new_datatype(

src/dump.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -2341,8 +2341,7 @@ static void jl_recache_types(void) JL_GC_DISABLED
23412341
}
23422342
}
23432343

2344-
// repeatedly look up older methods until we come to one that existed
2345-
// at the time this module was serialized (e.g. ignoring deletion)
2344+
// look up a method from a previously deserialized dependent module
23462345
static jl_method_t *jl_lookup_method(jl_methtable_t *mt, jl_datatype_t *sig, size_t world)
23472346
{
23482347
if (world < jl_main_module->primary_world)

src/gf.c

+27-14
Original file line numberDiff line numberDiff line change
@@ -1197,15 +1197,6 @@ static jl_method_instance_t *jl_mt_assoc_by_type(jl_methtable_t *mt, jl_datatype
11971197
return nf;
11981198
}
11991199

1200-
void print_func_loc(JL_STREAM *s, jl_method_t *m)
1201-
{
1202-
long lno = m->line;
1203-
if (lno > 0) {
1204-
char *fname = jl_symbol_name((jl_sym_t*)m->file);
1205-
jl_printf(s, " at %s:%ld", fname, lno);
1206-
}
1207-
}
1208-
12091200
struct shadowed_matches_env {
12101201
struct typemap_intersection_env match;
12111202
jl_typemap_entry_t *newentry;
@@ -1291,14 +1282,33 @@ static jl_value_t *check_shadowed_matches(jl_typemap_t *defs, jl_typemap_entry_t
12911282
return env.shadowed;
12921283
}
12931284

1285+
void print_func_loc(JL_STREAM *s, jl_method_t *m)
1286+
{
1287+
long lno = m->line;
1288+
if (lno > 0) {
1289+
char *fname = jl_symbol_name((jl_sym_t*)m->file);
1290+
jl_printf(s, " at %s:%ld", fname, lno);
1291+
}
1292+
}
1293+
1294+
static int is_anonfn_typename(char *name)
1295+
{
1296+
if (name[0] != '#' || name[1] == '#')
1297+
return 0;
1298+
char *other = strrchr(name, '#');
1299+
return other > &name[1] && other[1] > '0' && other[1] <= '9';
1300+
}
1301+
12941302
static void method_overwrite(jl_typemap_entry_t *newentry, jl_method_t *oldvalue)
12951303
{
12961304
// method overwritten
1305+
jl_method_t *method = (jl_method_t*)newentry->func.method;
1306+
jl_module_t *newmod = method->module;
1307+
jl_module_t *oldmod = oldvalue->module;
1308+
jl_datatype_t *dt = jl_first_argument_datatype(oldvalue->sig);
1309+
int anon = dt && is_anonfn_typename(jl_symbol_name(dt->name->name));
12971310
if ((jl_options.warn_overwrite == JL_OPTIONS_WARN_OVERWRITE_ON) ||
1298-
(jl_options.incremental && jl_generating_output())) {
1299-
jl_method_t *method = (jl_method_t*)newentry->func.method;
1300-
jl_module_t *newmod = method->module;
1301-
jl_module_t *oldmod = oldvalue->module;
1311+
(jl_options.incremental && jl_generating_output()) || anon) {
13021312
JL_STREAM *s = JL_STDERR;
13031313
jl_printf(s, "WARNING: Method definition ");
13041314
jl_static_show_func_sig(s, (jl_value_t*)newentry->sig);
@@ -1307,7 +1317,10 @@ static void method_overwrite(jl_typemap_entry_t *newentry, jl_method_t *oldvalue
13071317
jl_printf(s, " overwritten");
13081318
if (oldmod != newmod)
13091319
jl_printf(s, " in module %s", jl_symbol_name(newmod->name));
1310-
print_func_loc(s, method);
1320+
if (method->line > 0 && method->line == oldvalue->line && method->file == oldvalue->file)
1321+
jl_printf(s, anon ? " on the same line" : " on the same line (check for duplicate calls to `include`)");
1322+
else
1323+
print_func_loc(s, method);
13111324
jl_printf(s, ".\n");
13121325
jl_uv_flush(s);
13131326
}

sysimage.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ RELBUILDROOT := $(call rel_path,$(JULIAHOME)/base,$(BUILDROOT)/base)/ # <-- make
6060
$(build_private_libdir)/corecompiler.ji: $(COMPILER_SRCS)
6161
@$(call PRINT_JULIA, cd $(JULIAHOME)/base && \
6262
$(call spawn,$(JULIA_EXECUTABLE)) -C "$(JULIA_CPU_TARGET)" --output-ji $(call cygpath_w,$@).tmp \
63-
--startup-file=no -g0 -O0 compiler/compiler.jl)
63+
--startup-file=no --warn-overwrite=yes -g0 -O0 compiler/compiler.jl)
6464
@mv $@.tmp $@
6565

6666
$(build_private_libdir)/sys.ji: $(build_private_libdir)/corecompiler.ji $(JULIAHOME)/VERSION $(BASE_SRCS) $(STDLIB_SRCS)

test/misc.jl

+15-3
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,25 @@ end
7878
@test GC.enable(true)
7979

8080
# PR #10984
81-
# Disable on windows because of issue (missing flush) when redirecting stderr.
8281
let
8382
redir_err = "redirect_stderr(stdout)"
8483
exename = Base.julia_cmd()
85-
script = "$redir_err; module A; f() = 1; end; A.f() = 1"
84+
script = """
85+
$redir_err
86+
module A; f() = 1; end; A.f() = 1
87+
A.f() = 1
88+
outer() = (g() = 1; g() = 2; g)
89+
"""
8690
warning_str = read(`$exename --warn-overwrite=yes --startup-file=no -e $script`, String)
87-
@test occursin("f()", warning_str)
91+
@test warning_str == """
92+
WARNING: Method definition f() in module A at none:2 overwritten in module Main on the same line (check for duplicate calls to `include`).
93+
WARNING: Method definition f() in module Main at none:2 overwritten at none:3.
94+
WARNING: Method definition g() in module Main at none:4 overwritten on the same line.
95+
"""
96+
warning_str = read(`$exename --startup-file=no -e $script`, String)
97+
@test warning_str == """
98+
WARNING: Method definition g() in module Main at none:4 overwritten on the same line.
99+
"""
88100
end
89101

90102
# lock / unlock

0 commit comments

Comments
 (0)