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

consistent quoting of import Base.:operator and warnings thereof #33158

Merged
merged 7 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ New language features

* Structs with all isbits and isbitsunion fields are now stored inline in arrays ([#32448]).

* `import` now allows quoted symbols, e.g. `import Base.:+` ([#33158]).

Language changes
----------------

Expand Down
10 changes: 4 additions & 6 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,14 @@ function showerror(io::IO, ex::MethodError)
if f_is_function && isdefined(Base, name)
basef = getfield(Base, name)
if basef !== ex.f && hasmethod(basef, arg_types)
println(io)
print(io, "You may have intended to import Base.", name)
print(io, "\nYou may have intended to import ")
show_unquoted(io, Expr(:., :Base, QuoteNode(name)))
end
end
if (ex.world != typemax(UInt) && hasmethod(ex.f, arg_types) &&
!hasmethod(ex.f, arg_types, world = ex.world))
curworld = get_world_counter()
println(io)
print(io, "The applicable method may be too new: running in world age $(ex.world), while current world is $(curworld).")
print(io, "\nThe applicable method may be too new: running in world age $(ex.world), while current world is $(curworld).")
end
if !is_arg_types
# Check for row vectors used where a column vector is intended.
Expand Down Expand Up @@ -455,8 +454,7 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()

if !isempty(lines) # Display up to three closest candidates
Base.with_output_color(:normal, io) do io
println(io)
print(io, "Closest candidates are:")
print(io, "\nClosest candidates are:")
sort!(lines, by = x -> -x[2])
i = 0
for line in lines
Expand Down
4 changes: 4 additions & 0 deletions src/ast.scm
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@

(define (quoted? e) (memq (car e) '(quote top core globalref outerref line break inert meta)))
(define (quotify e) `',e)
(define (unquote e)
(if (and (pair? e) (memq (car e) '(quote inert)))
(cadr e)
e))

(define (lam:args x) (cadr x))
(define (lam:vars x) (llist-vars (lam:args x)))
Expand Down
2 changes: 1 addition & 1 deletion src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,7 @@
((eq? nxt '|.|)
(if (ts:space? s) (disallowed-space word nxt))
(take-token s)
(loop (cons (macrocall-to-atsym (parse-unary-prefix s)) path)))
(loop (cons (unquote (macrocall-to-atsym (parse-unary-prefix s))) path)))
((or (memv nxt '(#\newline #\; #\, :))
(eof-object? nxt))
(cons '|.| (reverse path)))
Expand Down
18 changes: 17 additions & 1 deletion test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ import ..@except_str
global +
+() = nothing
err_str = @except_str 1 + 2 MethodError
@test occursin("import Base.+", err_str)
@test occursin("import Base.:+", err_str)

err_str = @except_str Float64[](1) MethodError
@test !occursin("import Base.Array", err_str)
Expand Down Expand Up @@ -578,3 +578,19 @@ let t1 = @async(error(1)),
@test length(findall("Stacktrace:", s)) == 2
@test occursin("[1] error(::Int", s)
end

module TestMethodShadow
struct Foo; x; end
+(a::Foo, b::Foo) = Foo(a.x + b.x)
==(a::Foo, b::Foo) = Foo(a.x == b.x)
div(a::Foo, b::Foo) = Foo(div(a.x, b.x))
end
for (func,str) in ((TestMethodShadow.:+,":+"), (TestMethodShadow.:(==),":(==)"), (TestMethodShadow.:div,"div"))
ex = try
foo = TestMethodShadow.Foo(3)
func(foo,foo)
catch e
e
end::MethodError
@test occursin("You may have intended to import Base.$str", sprint(Base.showerror, ex))
end
5 changes: 5 additions & 0 deletions test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1928,10 +1928,15 @@ end
# Stream positioning after parsing var
@test Meta.parse("var'", 1, greedy=false) == (:var, 4)

# quoted names in import (#33158)
@test Meta.parse("import Base.:+") == :(import Base.+)
@test Meta.parse("import Base.Foo.:(==).bar") == :(import Base.Foo.==.bar)

# issue #33135
function f33135(x::T) where {C1, T}
let C1 = 1, C2 = 2
C1
end
end
@test f33135(0) == 1