Skip to content

Commit

Permalink
refine method overwrite warning
Browse files Browse the repository at this point in the history
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 #32635
fixes #35140
refs #15602
  • Loading branch information
vtjnash committed Jul 10, 2020
1 parent 0d5efa8 commit df2265b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/datatype.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,10 @@ void jl_compute_field_offsets(jl_datatype_t *st)

static int is_anonfn_typename(char *name)
{
if (name[0] != '#')
if (name[0] != '#' || name[1] == '#')
return 0;
char *other = strrchr(name, '#');
return (name[1] != '#' && other > &name[1] && is10digit(other[1]));
return other > &name[1] && is10digit(other[1]);
}

JL_DLLEXPORT jl_datatype_t *jl_new_datatype(
Expand Down
3 changes: 1 addition & 2 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -2341,8 +2341,7 @@ static void jl_recache_types(void) JL_GC_DISABLED
}
}

// repeatedly look up older methods until we come to one that existed
// at the time this module was serialized (e.g. ignoring deletion)
// look up a method from a previously deserialized dependent module
static jl_method_t *jl_lookup_method(jl_methtable_t *mt, jl_datatype_t *sig, size_t world)
{
if (world < jl_main_module->primary_world)
Expand Down
41 changes: 27 additions & 14 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1197,15 +1197,6 @@ static jl_method_instance_t *jl_mt_assoc_by_type(jl_methtable_t *mt, jl_datatype
return nf;
}

void print_func_loc(JL_STREAM *s, jl_method_t *m)
{
long lno = m->line;
if (lno > 0) {
char *fname = jl_symbol_name((jl_sym_t*)m->file);
jl_printf(s, " at %s:%ld", fname, lno);
}
}

struct shadowed_matches_env {
struct typemap_intersection_env match;
jl_typemap_entry_t *newentry;
Expand Down Expand Up @@ -1291,14 +1282,33 @@ static jl_value_t *check_shadowed_matches(jl_typemap_t *defs, jl_typemap_entry_t
return env.shadowed;
}

void print_func_loc(JL_STREAM *s, jl_method_t *m)
{
long lno = m->line;
if (lno > 0) {
char *fname = jl_symbol_name((jl_sym_t*)m->file);
jl_printf(s, " at %s:%ld", fname, lno);
}
}

static int is_anonfn_typename(char *name)
{
if (name[0] != '#' || name[1] == '#')
return 0;
char *other = strrchr(name, '#');
return other > &name[1] && other[1] > '0' && other[1] <= '9';
}

static void method_overwrite(jl_typemap_entry_t *newentry, jl_method_t *oldvalue)
{
// method overwritten
jl_method_t *method = (jl_method_t*)newentry->func.method;
jl_module_t *newmod = method->module;
jl_module_t *oldmod = oldvalue->module;
jl_datatype_t *dt = jl_first_argument_datatype(oldvalue->sig);
int anon = dt && is_anonfn_typename(jl_symbol_name(dt->name->name));
if ((jl_options.warn_overwrite == JL_OPTIONS_WARN_OVERWRITE_ON) ||
(jl_options.incremental && jl_generating_output())) {
jl_method_t *method = (jl_method_t*)newentry->func.method;
jl_module_t *newmod = method->module;
jl_module_t *oldmod = oldvalue->module;
(jl_options.incremental && jl_generating_output()) || anon) {
JL_STREAM *s = JL_STDERR;
jl_printf(s, "WARNING: Method definition ");
jl_static_show_func_sig(s, (jl_value_t*)newentry->sig);
Expand All @@ -1307,7 +1317,10 @@ static void method_overwrite(jl_typemap_entry_t *newentry, jl_method_t *oldvalue
jl_printf(s, " overwritten");
if (oldmod != newmod)
jl_printf(s, " in module %s", jl_symbol_name(newmod->name));
print_func_loc(s, method);
if (method->line > 0 && method->line == oldvalue->line && method->file == oldvalue->file)
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_uv_flush(s);
}
Expand Down
18 changes: 15 additions & 3 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,25 @@ end
@test GC.enable(true)

# PR #10984
# Disable on windows because of issue (missing flush) when redirecting stderr.
let
redir_err = "redirect_stderr(stdout)"
exename = Base.julia_cmd()
script = "$redir_err; module A; f() = 1; end; A.f() = 1"
script = """
$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 occursin("f()", warning_str)
@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.
"""
end

# lock / unlock
Expand Down

0 comments on commit df2265b

Please sign in to comment.