Skip to content

Commit 54be1ce

Browse files
authored
Fix static show for Base.Colon (JuliaLang#44726)
As noted in 44635, we current static show `Base.Colon()` as `Base.Any`, which is very confusing. Try to improve the situation by only using the special function printing if the binding actually matches the value we're trying to print.
1 parent 7c2edc5 commit 54be1ce

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/rtutils.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -628,21 +628,27 @@ JL_DLLEXPORT jl_value_t *jl_argument_datatype(jl_value_t *argt JL_PROPAGATES_ROO
628628
return (jl_value_t*)dt;
629629
}
630630

631-
static int is_globfunction(jl_value_t *v, jl_datatype_t *dv, jl_sym_t **globname_out)
631+
static int is_globname_binding(jl_value_t *v, jl_datatype_t *dv)
632632
{
633633
jl_sym_t *globname = dv->name->mt != NULL ? dv->name->mt->name : NULL;
634-
*globname_out = globname;
635-
int globfunc = 0;
636-
if (globname && !strchr(jl_symbol_name(globname), '#') &&
637-
!strchr(jl_symbol_name(globname), '@') && dv->name->module &&
638-
jl_binding_resolved_p(dv->name->module, globname)) {
634+
if (globname && dv->name->module && jl_binding_resolved_p(dv->name->module, globname)) {
639635
jl_binding_t *b = jl_get_module_binding(dv->name->module, globname);
640636
// The `||` makes this function work for both function instances and function types.
641637
if (b && b->value && (b->value == v || jl_typeof(b->value) == v)) {
642-
globfunc = 1;
638+
return 1;
643639
}
644640
}
645-
return globfunc;
641+
return 0;
642+
}
643+
644+
static int is_globfunction(jl_value_t *v, jl_datatype_t *dv, jl_sym_t **globname_out)
645+
{
646+
jl_sym_t *globname = dv->name->mt != NULL ? dv->name->mt->name : NULL;
647+
*globname_out = globname;
648+
if (globname && !strchr(jl_symbol_name(globname), '#') && !strchr(jl_symbol_name(globname), '@')) {
649+
return 1;
650+
}
651+
return 0;
646652
}
647653

648654
static size_t jl_static_show_x_sym_escaped(JL_STREAM *out, jl_sym_t *name) JL_NOTSAFEPOINT
@@ -773,7 +779,7 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
773779
// `Base.Set{Int}`, and function types are printed as e.g. `typeof(Main.f)`
774780
jl_datatype_t *dv = (jl_datatype_t*)v;
775781
jl_sym_t *globname;
776-
int globfunc = is_globfunction(v, dv, &globname);
782+
int globfunc = is_globname_binding(v, dv) && is_globfunction(v, dv, &globname);
777783
jl_sym_t *sym = globfunc ? globname : dv->name->name;
778784
char *sn = jl_symbol_name(sym);
779785
size_t quote = 0;
@@ -1065,20 +1071,18 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
10651071
n += jl_static_show_x(out, *(jl_value_t**)v, depth);
10661072
n += jl_printf(out, ")");
10671073
}
1068-
else if (jl_static_is_function_(vt)) {
1074+
else if (jl_static_is_function_(vt) && is_globname_binding(v, (jl_datatype_t*)vt)) {
10691075
// v is function instance (an instance of a Function type).
10701076
jl_datatype_t *dv = (jl_datatype_t*)vt;
1071-
jl_sym_t *sym = dv->name->mt->name;
1072-
char *sn = jl_symbol_name(sym);
1073-
1074-
jl_sym_t *globname;
1075-
int globfunc = is_globfunction(v, dv, &globname);
1077+
jl_sym_t *sym;
1078+
int globfunc = is_globfunction(v, dv, &sym);
10761079
int quote = 0;
10771080
if (jl_core_module && (dv->name->module != jl_core_module || !jl_module_exports_p(jl_core_module, sym))) {
10781081
n += jl_static_show_x(out, (jl_value_t*)dv->name->module, depth);
10791082
n += jl_printf(out, ".");
10801083

10811084
size_t i = 0;
1085+
char *sn = jl_symbol_name(sym);
10821086
if (globfunc && !jl_id_start_char(u8_nextchar(sn, &i))) {
10831087
n += jl_printf(out, ":(");
10841088
quote = 1;

test/show.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,9 @@ struct var"%X%" end # Invalid name without '#'
14531453
end
14541454
end
14551455

1456+
# Test that static show prints something reasonable for `<:Function` types
1457+
@test static_shown(:) == "Base.Colon()"
1458+
14561459
# Test @show
14571460
let fname = tempname()
14581461
try

0 commit comments

Comments
 (0)