Skip to content

Commit

Permalink
Improve inference broadly throughout REPL (#37081)
Browse files Browse the repository at this point in the history
* REPL: move Options to improve inference

Moving this earlier in the load sequence allows one to annotate the
return type of some methods.

* REPL: add interfaces for abstract types and change one field type

Since MIState and others have containers with abstract typing, these interfaces
fix inference problems broadly.

* IO: improve inferrability of write and unsafe_write

* REPL: add argument typing and improve implementations

Since entire modules are marked `@nospecialize`, we need
to declare argument types anywhere we want good inference.
This also improves numerous implementations to ensure
inferrability.

For the completion methods, notice this changes typeof(ret[2]) from
UnitRange{Int64} to UnitRange{Int}. Internally, the methods are using
Int rather than Int64, so consistently supporting Int64 would require
more extensive changes.

* REPL: add argtypes to keymap functions

* REPL: test inference in LineEdit
  • Loading branch information
timholy committed Aug 26, 2020
1 parent a9f99b5 commit 69eadbc
Show file tree
Hide file tree
Showing 15 changed files with 557 additions and 459 deletions.
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function pipe_reader end
function pipe_writer end

write(io::AbstractPipe, byte::UInt8) = write(pipe_writer(io)::IO, byte)
unsafe_write(io::AbstractPipe, p::Ptr{UInt8}, nb::UInt) = unsafe_write(pipe_writer(io)::IO, p, nb)
unsafe_write(io::AbstractPipe, p::Ptr{UInt8}, nb::UInt) = unsafe_write(pipe_writer(io)::IO, p, nb)::Union{Int,UInt}
buffer_writes(io::AbstractPipe, args...) = buffer_writes(pipe_writer(io)::IO, args...)
flush(io::AbstractPipe) = flush(pipe_writer(io)::IO)

Expand Down
2 changes: 1 addition & 1 deletion base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ end
function unsafe_write(to::GenericIOBuffer, p::Ptr{UInt8}, nb::UInt)
ensureroom(to, nb)
ptr = (to.append ? to.size+1 : to.ptr)
written = Int(min(nb, length(to.data) - ptr + 1))
written = Int(min(nb, Int(length(to.data))::Int - ptr + 1))
towrite = written
d = to.data
while towrite > 0
Expand Down
4 changes: 2 additions & 2 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ string(a::Symbol) = String(a)
# note: print uses an encoding determined by `io` (defaults to UTF-8), whereas
# write uses an encoding determined by `s` (UTF-8 for `String`)
print(io::IO, s::AbstractString) = for c in s; print(io, c); end
write(io::IO, s::AbstractString) = (len = 0; for c in s; len += write(io, c); end; len)
write(io::IO, s::AbstractString) = (len = 0; for c in s; len += Int(write(io, c))::Int; end; len)
show(io::IO, s::AbstractString) = print_quoted(io, s)

# optimized methods to avoid iterating over chars
write(io::IO, s::Union{String,SubString{String}}) =
GC.@preserve s unsafe_write(io, pointer(s), reinterpret(UInt, sizeof(s)))
GC.@preserve s Int(unsafe_write(io, pointer(s), reinterpret(UInt, sizeof(s))))::Int
print(io::IO, s::Union{String,SubString{String}}) = (write(io, s); nothing)

## printing literal quoted string data ##
Expand Down
Loading

0 comments on commit 69eadbc

Please sign in to comment.