Skip to content

Commit

Permalink
Avoid several bad show_default invalidations (JuliaLang#37125)
Browse files Browse the repository at this point in the history
This fixes ~50 quite critical invalidations from loading StaticArrays,
which manages to hit the package-loading code, the docstring code,
and the `MethodError` display code. Since particularly the first two
are used by *everything*, these invalidations are about as bad as it
gets given that this is not a huge number of invalidations (at least, by
our old standards).

The invalidation of the package-loading code is due to a rather
hilarious chain of events.
[This method definition](https://github.com/JuliaArrays/StaticArrays.jl/blob/ad583c99768f3a381ba20b1a0782c9ca50890b50/src/SArray.jl#L125-L126)
ends up invalidating [`show_default`](https://github.com/JuliaLang/julia/blob/89d6b469b633dde8f92b4d245ca3d8e5606e229c/base/show.jl#L417),
which got called because `stale_cachefile` used an outdated variable
name in a [`@debug` macro](https://github.com/JuliaLang/julia/blob/89d6b469b633dde8f92b4d245ca3d8e5606e229c/base/loading.jl#L1518)
that ended up grabbing the global `mod` function. Since
`show(::IO, ::Function)` can call `show_default`, this ended up
invalidating basically all the package-loading code.

The docstring code got invalidated because
`show(::IO, ::Pair{Symbol,Any})`
[calls `show_default`](https://github.com/JuliaLang/julia/blob/89d6b469b633dde8f92b4d245ca3d8e5606e229c/base/show.jl#L907).
This PR provides a dedicated path that circumvents `show_default`.

Finally, an innocuous-seeming function-print in
`showerror(::IO, ::MethodError)` also led to quite a chain of invalidation.

* Specify that getaddrinfo(::AbstractString) returns a `Union{IPv4,IPv6}`

These are the only two types supported by `Sockets`, so this seems
reasonable, although it does block further extension.
  • Loading branch information
timholy authored Aug 25, 2020
1 parent 304efd9 commit 6aee988
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ function showerror(io::IO, ex::MethodError)
)
varnames = ("scalar", "array")
first, second = arg_types_param[1] <: Number ? varnames : reverse(varnames)
print(io, "\nFor element-wise $(nouns[f]), use broadcasting with dot syntax: $first .$f $second")
fstring = f === Base.:+ ? "+" : "-" # avoid depending on show_default for functions (invalidation)
print(io, "\nFor element-wise $(nouns[f]), use broadcasting with dot syntax: $first .$fstring $second")
end
end
if ft <: AbstractArray
Expand Down
2 changes: 1 addition & 1 deletion base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ function stale_cachefile(modpath::String, cachefile::String)
skip_timecheck = true
break
end
@debug "Rejecting cache file $cachefile because it provides the wrong uuid (got $build_id) for $mod (want $req_build_id)"
@debug "Rejecting cache file $cachefile because it provides the wrong uuid (got $build_id) for $req_key (want $req_build_id)"
return true # cachefile doesn't provide the required version of the dependency
end
end
Expand Down
8 changes: 7 additions & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ function gettypeinfos(io::IO, p::Pair)
end

function show(io::IO, p::Pair)
isdelimited(io, p) && return show_default(io, p)
isdelimited(io, p) && return show_pairtyped(io, p)
typeinfos = gettypeinfos(io, p)
for i = (1, 2)
io_i = IOContext(io, :typeinfo => typeinfos[i])
Expand All @@ -938,6 +938,11 @@ function show(io::IO, p::Pair)
end
end

function show_pairtyped(io::IO, p::Pair{K,V}) where {K,V}
show(io, typeof(p))
show(io, (p.first, p.second))
end

function show(io::IO, m::Module)
if is_root_module(m)
print(io, nameof(m))
Expand Down Expand Up @@ -2424,6 +2429,7 @@ end

const undef_ref_str = "#undef"

show(io::IO, ::UndefInitializer) = print(io, "UndefInitializer()")

"""
summary(io::IO, x)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Sockets/src/addrinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ getaddrinfo(host::AbstractString, T::Type{<:IPAddr}) = getaddrinfo(String(host),
function getaddrinfo(host::AbstractString)
addrs = getalladdrinfo(String(host))
if !isempty(addrs)
return addrs[begin]
return addrs[begin]::Union{IPv4,IPv6}
end
throw(DNSError(host, UV_EAI_NONAME))
end
Expand Down

0 comments on commit 6aee988

Please sign in to comment.