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

add tuple type printing with Vararg, and port to static_show #44970

Merged
merged 2 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 19 additions & 7 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -993,19 +993,31 @@ function show_datatype(io::IO, x::DataType, wheres::Vector{TypeVar}=TypeVar[])
istuple = x.name === Tuple.name
n = length(parameters)

# Print homogeneous tuples with more than 3 elements compactly as NTuple{N, T}
# Print tuple types with homogeneous tails longer than max_n compactly using `NTuple` or `Vararg`
max_n = 3
if istuple
if n > 3 && all(@nospecialize(i) -> (parameters[1] === i), parameters)
taillen = 1
for i in (n-1):-1:1
if parameters[i] === parameters[n]
taillen += 1
else
break
end
end
if n == taillen > max_n
print(io, "NTuple{", n, ", ")
show(io, parameters[1])
print(io, "}")
else
print(io, "Tuple{")
# join(io, params, ", ") params but `show` it
first = true
for param in parameters
first ? (first = false) : print(io, ", ")
show(io, param)
for i = 1:(taillen > max_n ? n-taillen : n)
i > 1 && print(io, ", ")
show(io, parameters[i])
end
if taillen > max_n
print(io, ", Vararg{")
show(io, parameters[n])
print(io, ", ", taillen, "}")
end
print(io, "}")
end
Expand Down
38 changes: 32 additions & 6 deletions src/rtutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,37 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
jl_sym_t *sym = globfunc ? globname : dv->name->name;
char *sn = jl_symbol_name(sym);
size_t quote = 0;
if (dv->name == jl_tuple_typename) {
if (dv == jl_tuple_type)
return jl_printf(out, "Tuple");
int taillen = 1, tlen = jl_nparams(dv), i;
for (i = tlen-2; i >= 0; i--) {
if (jl_tparam(dv, i) == jl_tparam(dv, tlen-1))
taillen++;
else
break;
}
if (taillen == tlen && taillen > 3) {
n += jl_printf(out, "NTuple{%d, ", tlen);
n += jl_static_show_x(out, jl_tparam0(dv), depth);
n += jl_printf(out, "}");
}
else {
n += jl_printf(out, "Tuple{");
for (i = 0; i < (taillen > 3 ? tlen-taillen : tlen); i++) {
if (i > 0)
n += jl_printf(out, ", ");
n += jl_static_show_x(out, jl_tparam(dv, i), depth);
}
if (taillen > 3) {
n += jl_printf(out, ", Vararg{");
n += jl_static_show_x(out, jl_tparam(dv, tlen-1), depth);
n += jl_printf(out, ", %d}", taillen);
}
n += jl_printf(out, "}");
}
return n;
}
if (globfunc) {
n += jl_printf(out, "typeof(");
}
Expand All @@ -804,9 +835,7 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
n += jl_printf(out, ")");
}
}
if (dv->parameters && (jl_value_t*)dv != dv->name->wrapper &&
(jl_has_free_typevars(v) ||
(jl_value_t*)dv != (jl_value_t*)jl_tuple_type)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the jl_tuple_type check was to make sure Tuple is printed as Tuple, but I don't know how the free_typevars check makes sense 🤷

if (dv->parameters && (jl_value_t*)dv != dv->name->wrapper) {
size_t j, tlen = jl_nparams(dv);
if (tlen > 0) {
n += jl_printf(out, "{");
Expand All @@ -818,9 +847,6 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
}
n += jl_printf(out, "}");
}
else if (dv->name == jl_tuple_typename) {
n += jl_printf(out, "{}");
}
}
}
else if (vt == jl_intrinsic_type) {
Expand Down
2 changes: 2 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,8 @@ test_repr("(:).a")
@test repr(NTuple{7,Int64}) == "NTuple{7, Int64}"
@test repr(Tuple{Float64, Float64, Float64, Float64}) == "NTuple{4, Float64}"
@test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32, Float32, Float32}"
@test repr(Tuple{String, Int64, Int64, Int64}) == "Tuple{String, Int64, Int64, Int64}"
@test repr(Tuple{String, Int64, Int64, Int64, Int64}) == "Tuple{String, Vararg{Int64, 4}}"

@testset "issue #42931" begin
@test repr(NTuple{4, :A}) == "NTuple{4, :A}"
Expand Down