forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorshow.jl
640 lines (558 loc) · 26.1 KB
/
errorshow.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Random, LinearAlgebra
# For curmod_*
include("testenv.jl")
@testset "SystemError" begin
err = try; systemerror("reason", Cint(0)); false; catch ex; ex; end::SystemError
errs = sprint(Base.showerror, err)
@test startswith(errs, "SystemError: reason: ")
err = try; systemerror("reason", Cint(0), extrainfo="addend"); false; catch ex; ex; end::SystemError
errs = sprint(Base.showerror, err)
@test startswith(errs, "SystemError (with addend): reason: ")
err = try
Libc.errno(0xc0ffee)
systemerror("reason", true)
false
catch ex
ex
end::SystemError
errs = sprint(Base.showerror, err)
@test startswith(errs, "SystemError: reason: ")
err = try; Base.windowserror("reason", UInt32(0)); false; catch ex; ex; end::SystemError
errs = sprint(Base.showerror, err)
@test startswith(errs, Sys.iswindows() ? "SystemError: reason: " :
"SystemError (with Base.WindowsErrorInfo(0x00000000, nothing)): reason: ")
err = try; Base.windowserror("reason", UInt32(0); extrainfo="addend"); false; catch ex; ex; end::SystemError
errs = sprint(Base.showerror, err)
@test startswith(errs, Sys.iswindows() ? "SystemError (with addend): reason: " :
"SystemError (with Base.WindowsErrorInfo(0x00000000, \"addend\")): reason: ")
@static if Sys.iswindows()
err = try
ccall(:SetLastError, stdcall, Cvoid, (UInt32,), 0x00000000)
Base.windowserror("reason", true)
false
catch ex
ex
end::SystemError
errs = sprint(Base.showerror, err)
@test startswith(errs, "SystemError: reason: ")
end
end
cfile = " at $(@__FILE__):"
c1line = @__LINE__() + 1
method_c1(x::Float64, s::AbstractString...) = true
buf = IOBuffer()
Base.show_method_candidates(buf, Base.MethodError(method_c1,(1, 1, "")))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c1(!Matched::Float64, !Matched::AbstractString...)$cfile$c1line"
@test length(methods(method_c1)) <= 3 # because of '...' in candidate printing
Base.show_method_candidates(IOContext(buf, :color => true), Base.MethodError(method_c1,(1, 1, "")))
@test String(take!(buf)) == "\n\e[0mClosest candidates are:\n\e[0m method_c1(\e[91m::Float64\e[39m, \e[91m::AbstractString...\e[39m)$cfile$c1line"
Base.show_method_candidates(buf, Base.MethodError(method_c1,(1, "", "")))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c1(!Matched::Float64, ::AbstractString...)$cfile$c1line"
# should match
Base.show_method_candidates(buf, Base.MethodError(method_c1,(1., "", "")))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c1(::Float64, ::AbstractString...)$cfile$c1line"
# Have no matches so should return empty
Base.show_method_candidates(buf, Base.MethodError(method_c1,(1, 1, 1)))
@test String(take!(buf)) == ""
# matches the implicit constructor -> convert method
Base.show_method_candidates(buf, Base.MethodError(Tuple{}, (1, 1, 1)))
let mc = String(take!(buf))
@test occursin("\nClosest candidates are:\n Tuple{}", mc)
@test !occursin(cfile, mc)
end
c2line = @__LINE__
method_c2(x::Int32, args...) = true
method_c2(x::Int32, y::Float64, args...) = true
method_c2(x::Int32, y::Float64) = true
method_c2(x::Int32, y::Int32, z::Int32) = true
method_c2(x::T, y::T, z::T) where {T<:Real} = true
Base.show_method_candidates(buf, Base.MethodError(method_c2,(1., 1., 2)))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c2(!Matched::Int32, ::Float64, ::Any...)$cfile$(c2line+2)\n method_c2(!Matched::Int32, ::Any...)$cfile$(c2line+1)\n method_c2(::T, ::T, !Matched::T) where T<:Real$cfile$(c2line+5)\n ..."
c3line = @__LINE__() + 1
method_c3(x::Float64, y::Float64) = true
Base.show_method_candidates(buf, Base.MethodError(method_c3,(1.,)))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c3(::Float64, !Matched::Float64)$cfile$c3line"
# Test for the method error in issue #8651
c4line = @__LINE__
method_c4() = true
method_c4(x::AbstractString) = false
Base.show_method_candidates(buf, MethodError(method_c4,("",)))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c4(::AbstractString)$cfile$(c4line+2)\n method_c4()$cfile$(c4line+1)"
c5line = @__LINE__() + 1
method_c5(::Type{Float64}) = true
Base.show_method_candidates(buf, MethodError(method_c5,(Float64,)))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c5(::Type{Float64})$cfile$c5line"
Base.show_method_candidates(buf, MethodError(method_c5,(Int32,)))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c5(!Matched::Type{Float64})$cfile$c5line"
mutable struct Test_type end
test_type = Test_type()
for f in [getindex, setindex!]
Base.show_method_candidates(buf, MethodError(f,(test_type, 1,1)))
@test String(take!(buf)) == ""
end
PR16155line = @__LINE__() + 2
mutable struct PR16155
a::Int64
b
end
PR16155line2 = @__LINE__() + 1
(::Type{T})(arg::Any) where {T<:PR16155} = "replace call-to-convert method from sysimg"
Base.show_method_candidates(buf, MethodError(PR16155,(1.0, 2.0, Int64(3))))
@test String(take!(buf)) == "\nClosest candidates are:\n $(curmod_prefix)PR16155(::Any, ::Any)$cfile$PR16155line\n $(curmod_prefix)PR16155(!Matched::Int64, ::Any)$cfile$PR16155line\n $(curmod_prefix)PR16155(::Any) where T<:$(curmod_prefix)PR16155$cfile$PR16155line2"
Base.show_method_candidates(buf, MethodError(PR16155,(Int64(3), 2.0, Int64(3))))
@test String(take!(buf)) == "\nClosest candidates are:\n $(curmod_prefix)PR16155(::Int64, ::Any)$cfile$PR16155line\n $(curmod_prefix)PR16155(::Any, ::Any)$cfile$PR16155line\n $(curmod_prefix)PR16155(::Any) where T<:$(curmod_prefix)PR16155$cfile$PR16155line2"
c6line = @__LINE__
method_c6(; x=1) = x
method_c6(a; y=1) = y
m_error = try method_c6(y=1) catch e; e; end
showerror(buf, m_error)
error_out = String(take!(buf))
m_error = try method_c6(1, x=1) catch e; e; end
showerror(buf, m_error)
error_out1 = String(take!(buf))
c6mline = @__LINE__
module TestKWError
method_c6_in_module(; x=1) = x
method_c6_in_module(a; y=1) = y
end
m_error = try TestKWError.method_c6_in_module(y=1) catch e; e; end
showerror(buf, m_error)
error_out2 = String(take!(buf))
m_error = try TestKWError.method_c6_in_module(1, x=1) catch e; e; end
showerror(buf, m_error)
error_out3 = String(take!(buf))
@test occursin("method_c6(; x)$cfile$(c6line + 1) got unsupported keyword argument \"y\"", error_out)
@test occursin("method_c6(!Matched::Any; y)$cfile$(c6line + 2)", error_out)
@test occursin("method_c6(::Any; y)$cfile$(c6line + 2) got unsupported keyword argument \"x\"", error_out1)
@test occursin("method_c6_in_module(; x)$cfile$(c6mline + 2) got unsupported keyword argument \"y\"", error_out2)
@test occursin("method_c6_in_module(!Matched::Any; y)$cfile$(c6mline + 3)", error_out2)
@test occursin("method_c6_in_module(::Any; y)$cfile$(c6mline + 3) got unsupported keyword argument \"x\"", error_out3)
c7line = @__LINE__() + 1
method_c7(a, b; kargs...) = a
Base.show_method_candidates(buf, MethodError(method_c7, (1, 1)), pairs((x = 1, y = 2)))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c7(::Any, ::Any; kargs...)$cfile$c7line"
c8line = @__LINE__() + 1
method_c8(a, b; y=1, w=1) = a
Base.show_method_candidates(buf, MethodError(method_c8, (1, 1)), pairs((x = 1, y = 2, z = 1, w = 1)))
@test String(take!(buf)) == "\nClosest candidates are:\n method_c8(::Any, ::Any; y, w)$cfile$c8line got unsupported keyword arguments \"x\", \"z\""
let no_kwsorter_match, e
no_kwsorter_match() = 0
no_kwsorter_match(a;y=1) = y
e = try no_kwsorter_match(y=1) catch ex; ex; end
@test occursin(r"no method matching.+\(; y=1\)", sprint(showerror, e))
end
ac15639line = @__LINE__
addConstraint_15639(c::Int32) = c
addConstraint_15639(c::Int64; uncset=nothing) = addConstraint_15639(Int32(c), uncset=uncset)
Base.show_method_candidates(buf, MethodError(addConstraint_15639, (Int32(1),)), pairs((uncset = nothing,)))
@test String(take!(buf)) == "\nClosest candidates are:\n addConstraint_15639(::Int32)$cfile$(ac15639line + 1) got unsupported keyword argument \"uncset\"\n addConstraint_15639(!Matched::Int64; uncset)$cfile$(ac15639line + 2)"
macro except_str(expr, err_type)
return quote
let err = nothing
try
$(esc(expr))
catch err
end
err === nothing && error("expected failure, but no exception thrown")
@test typeof(err) === $(esc(err_type))
buf = IOBuffer()
showerror(buf, err)
String(take!(buf))
end
end
end
macro except_strbt(expr, err_type)
errmsg = "expected failure, but no exception thrown for $expr"
return quote
let err = nothing
try
$(esc(expr))
catch err
end
err === nothing && error($errmsg)
@test typeof(err) === $(esc(err_type))
buf = IOBuffer()
showerror(buf, err, catch_backtrace())
String(take!(buf))
end
end
end
macro except_stackframe(expr, err_type)
return quote
let err = nothing
local st
try
$(esc(expr))
catch err
st = stacktrace(catch_backtrace())
end
err === nothing && error("expected failure, but no exception thrown")
@test typeof(err) === $(esc(err_type))
sprint(show, st[1])
end
end
end
# Pull Request 11007
abstract type InvokeType11007 end
abstract type MethodType11007 <: InvokeType11007 end
mutable struct InstanceType11007 <: MethodType11007
end
let
f11007(::MethodType11007) = nothing
err_str = @except_str(invoke(f11007, Tuple{InvokeType11007},
InstanceType11007()), MethodError)
@test !occursin("::$(curmod_prefix)InstanceType11007", err_str)
@test occursin("::$(curmod_prefix)InvokeType11007", err_str)
end
module __tmp_replutil
using Test
import ..@except_str
global +
+() = nothing
err_str = @except_str 1 + 2 MethodError
@test occursin("import Base.:+", err_str)
err_str = @except_str Float64[](1) MethodError
@test !occursin("import Base.Array", err_str)
Array() = 1
err_str = @except_str Array([1]) MethodError
@test occursin("import Base.Array", err_str)
end
let
g11007(::AbstractVector) = nothing
err_str = @except_str g11007([[1] [1]]) MethodError
@test occursin("row vector", err_str)
@test occursin("column vector", err_str)
end
struct TypeWithIntParam{T <: Integer} end
struct Bounded # not an AbstractArray
bound::Int
end
Base.getindex(b::Bounded, i) = checkindex(Bool, 1:b.bound, i) || throw(BoundsError(b, i))
Base.summary(io::IO, b::Bounded) = print(io, "$(b.bound)-size Bounded")
let undefvar
err_str = @except_strbt sqrt(-1) DomainError
@test occursin("Try sqrt(Complex(x)).", err_str)
err_str = @except_strbt 2^(1-2) DomainError
@test occursin("Cannot raise an integer x to a negative power -1", err_str)
err_str = @except_strbt (-1)^0.25 DomainError
@test occursin("Exponentiation yielding a complex result requires a complex argument", err_str)
A = zeros(10, 10)
A[2,1] = 1
A[1,2] = -1
err_str = @except_strbt eigmax(A) DomainError
@test occursin("DomainError with [0.0 -1.0 …", err_str)
err_str = @except_str (1, 2, 3)[4] BoundsError
@test err_str == "BoundsError: attempt to access (1, 2, 3)\n at index [4]"
err_str = @except_str [5, 4, 3][-2, 1] BoundsError
@test err_str == "BoundsError: attempt to access 3-element Array{$Int,1} at index [-2, 1]"
err_str = @except_str [5, 4, 3][1:5] BoundsError
@test err_str == "BoundsError: attempt to access 3-element Array{$Int,1} at index [1:5]"
err_str = @except_str Bounded(2)[3] BoundsError
@test err_str == "BoundsError: attempt to access 2-size Bounded\n at index [3]"
err_str = @except_str 0::Bool TypeError
@test err_str == "TypeError: non-boolean ($Int) used in boolean context"
err_str = @except_str 0::AbstractFloat TypeError
@test err_str == "TypeError: in typeassert, expected AbstractFloat, got $Int"
err_str = @except_str 0::7 TypeError
@test err_str == "TypeError: in typeassert, expected Type, got $Int"
err_str = @except_str "" <: AbstractString TypeError
@test err_str == "TypeError: in <:, expected Type, got String"
err_str = @except_str AbstractString <: "" TypeError
@test err_str == "TypeError: in <:, expected Type, got String"
err_str = @except_str Type{""} TypeError
@test err_str == "TypeError: in Type, in parameter, expected Type, got String"
err_str = @except_str TypeWithIntParam{Any} TypeError
@test err_str == "TypeError: in TypeWithIntParam, in T, expected T<:Integer, got Type{Any}"
err_str = @except_str Type{Vararg} TypeError
@test err_str == "TypeError: in Type, in parameter, expected Type, got Vararg"
err_str = @except_str mod(1,0) DivideError
@test err_str == "DivideError: integer division error"
err_str = @except_str Vector{Any}(undef, 1)[1] UndefRefError
@test err_str == "UndefRefError: access to undefined reference"
err_str = @except_str undefvar UndefVarError
@test err_str == "UndefVarError: undefvar not defined"
err_str = @except_str read(IOBuffer(), UInt8) EOFError
@test err_str == "EOFError: read end of file"
err_str = @except_str Dict()[:doesnotexist] KeyError
@test err_str == "KeyError: key :doesnotexist not found"
err_str = @except_str throw(InterruptException()) InterruptException
@test err_str == "InterruptException:"
err_str = @except_str throw(ArgumentError("not an error")) ArgumentError
@test err_str == "ArgumentError: not an error"
err_str = @except_str @assert(false) AssertionError
@test err_str == "AssertionError: false"
end
# issue 11845
let buf = IOBuffer()
showerror(buf, MethodError(convert, (3, 1.0)))
showerror(buf, MethodError(convert, (Int, 1.0)))
showerror(buf, MethodError(convert, Tuple{Type, Float64}))
showerror(buf, MethodError(convert, Tuple{DataType, Float64}))
end
# Issue #14884
primitive type EightBitType 8 end
primitive type EightBitTypeT{T} 8 end
struct FunctionLike <: Function; end
let err_str,
i = reinterpret(EightBitType, 0x54),
j = reinterpret(EightBitTypeT{Int32}, 0x54)
err_str = @except_str Bool() MethodError
@test occursin("MethodError: no method matching Bool()", err_str)
err_str = @except_str :a() MethodError
@test occursin("MethodError: objects of type Symbol are not callable", err_str)
err_str = @except_str EightBitType() MethodError
@test occursin("MethodError: no method matching $(curmod_prefix)EightBitType()", err_str)
err_str = @except_str i() MethodError
@test occursin("MethodError: objects of type $(curmod_prefix)EightBitType are not callable", err_str)
err_str = @except_str EightBitTypeT() MethodError
@test occursin("MethodError: no method matching $(curmod_prefix)EightBitTypeT()", err_str)
err_str = @except_str EightBitTypeT{Int32}() MethodError
@test occursin("MethodError: no method matching $(curmod_prefix)EightBitTypeT{Int32}()", err_str)
err_str = @except_str j() MethodError
@test occursin("MethodError: objects of type $(curmod_prefix)EightBitTypeT{Int32} are not callable", err_str)
err_str = @except_str FunctionLike()() MethodError
@test occursin("MethodError: no method matching (::$(curmod_prefix)FunctionLike)()", err_str)
err_str = @except_str [1,2](1) MethodError
@test occursin("MethodError: objects of type Array{$Int,1} are not callable\nUse square brackets [] for indexing an Array.", err_str)
# Issue 14940
err_str = @except_str randn(1)() MethodError
@test occursin("MethodError: objects of type Array{Float64,1} are not callable", err_str)
end
@test repr("text/plain", FunctionLike()) == "(::$(curmod_prefix)FunctionLike) (generic function with 0 methods)"
@test occursin(r"^@doc \(macro with \d+ method[s]?\)$", repr("text/plain", getfield(Base, Symbol("@doc"))))
method_defs_lineno = @__LINE__() + 1
String() = throw(ErrorException("1"))
(::String)() = throw(ErrorException("2"))
EightBitType() = throw(ErrorException("3"))
(::EightBitType)() = throw(ErrorException("4"))
EightBitTypeT() = throw(ErrorException("5"))
EightBitTypeT{T}() where {T} = throw(ErrorException("6"))
(::EightBitTypeT)() = throw(ErrorException("7"))
(::FunctionLike)() = throw(ErrorException("8"))
let err_str,
i = reinterpret(EightBitType, 0x54),
j = reinterpret(EightBitTypeT{Int32}, 0x54),
sp = Base.source_path()
sn = basename(sp)
@test sprint(show, which(String, Tuple{})) ==
"String() in $curmod_str at $sp:$(method_defs_lineno + 0)"
@test sprint(show, which("a", Tuple{})) ==
"(::String)() in $curmod_str at $sp:$(method_defs_lineno + 1)"
@test sprint(show, which(EightBitType, Tuple{})) ==
"$(curmod_prefix)EightBitType() in $curmod_str at $sp:$(method_defs_lineno + 2)"
@test sprint(show, which(reinterpret(EightBitType, 0x54), Tuple{})) ==
"(::$(curmod_prefix)EightBitType)() in $curmod_str at $sp:$(method_defs_lineno + 3)"
@test sprint(show, which(EightBitTypeT, Tuple{})) ==
"(::Type{$(curmod_prefix)EightBitTypeT})() in $curmod_str at $sp:$(method_defs_lineno + 4)"
@test sprint(show, which(EightBitTypeT{Int32}, Tuple{})) ==
"(::Type{$(curmod_prefix)EightBitTypeT{T}})() where T in $curmod_str at $sp:$(method_defs_lineno + 5)"
@test sprint(show, which(reinterpret(EightBitTypeT{Int32}, 0x54), Tuple{})) ==
"(::$(curmod_prefix)EightBitTypeT)() in $curmod_str at $sp:$(method_defs_lineno + 6)"
@test startswith(sprint(show, which(getfield(Base, Symbol("@doc")), Tuple{LineNumberNode, Module, Vararg{Any}})),
"@doc(__source__::LineNumberNode, __module__::Module, x...) in Core at boot.jl:")
@test startswith(sprint(show, which(FunctionLike(), Tuple{})),
"(::$(curmod_prefix)FunctionLike)() in $curmod_str at $sp:$(method_defs_lineno + 7)")
@test repr("text/plain", FunctionLike()) == "(::$(curmod_prefix)FunctionLike) (generic function with 1 method)"
@test repr("text/plain", Core.arraysize) == "arraysize (built-in function)"
err_str = @except_stackframe String() ErrorException
@test err_str == "String() at $sn:$(method_defs_lineno + 0)"
err_str = @except_stackframe "a"() ErrorException
@test err_str == "(::String)() at $sn:$(method_defs_lineno + 1)"
err_str = @except_stackframe EightBitType() ErrorException
@test err_str == "$(curmod_prefix)EightBitType() at $sn:$(method_defs_lineno + 2)"
err_str = @except_stackframe i() ErrorException
@test err_str == "(::$(curmod_prefix)EightBitType)() at $sn:$(method_defs_lineno + 3)"
err_str = @except_stackframe EightBitTypeT() ErrorException
@test err_str == "$(curmod_prefix)EightBitTypeT() at $sn:$(method_defs_lineno + 4)"
err_str = @except_stackframe EightBitTypeT{Int32}() ErrorException
@test err_str == "$(curmod_prefix)EightBitTypeT{Int32}() at $sn:$(method_defs_lineno + 5)"
err_str = @except_stackframe j() ErrorException
@test err_str == "(::$(curmod_prefix)EightBitTypeT{Int32})() at $sn:$(method_defs_lineno + 6)"
err_str = @except_stackframe FunctionLike()() ErrorException
@test err_str == "(::$(curmod_prefix)FunctionLike)() at $sn:$(method_defs_lineno + 7)"
end
# Issue #20108
let err, buf = IOBuffer()
try Array() catch err end
Base.show_method_candidates(buf,err)
@test isa(err, MethodError)
@test occursin("Closest candidates are:", String(take!(buf)))
end
# @macroexpand tests
macro seven_dollar(ex)
# simonbyrne example 18240
isa(ex,Expr) && ex.head == :$ ? 7 : esc(ex)
end
let
@test (@macroexpand @macroexpand x) == macroexpand(@__MODULE__, :(@macroexpand x))
@test (@macroexpand :(1+$y) ) == macroexpand(@__MODULE__, :( :(1+ $y) ))
@test (@macroexpand @fastmath 1+2 ) == :(Base.FastMath.add_fast(1,2))
@test (@macroexpand @fastmath + ) == :(Base.FastMath.add_fast)
@test (@macroexpand @fastmath min(1) ) == :(Base.FastMath.min_fast(1))
let err = try; @macroexpand @doc "" f() = @x; catch ex; ex; end
file, line = @__FILE__, @__LINE__() - 1
err = err::LoadError
@test err.file == file && err.line == line
err = err.error::LoadError
@test err.file == file && err.line == line
err = err.error::UndefVarError
@test err == UndefVarError(Symbol("@x"))
end
@test (@macroexpand @seven_dollar $bar) == 7
x = 2
@test (@macroexpand @seven_dollar 1+$x) == :(1 + $(Expr(:$, :x)))
end
macro nest1(code)
code
end
macro nest2(code)
:(@nest1 $code)
end
macro nest2b(code)
:(@nest1($code); @nest1($code))
end
@testset "@macroexpand1" begin
M = @__MODULE__
_macroexpand1(ex) = macroexpand(M, ex, recursive=false)
ex = :(@nest1 42)
@test _macroexpand1(ex) == macroexpand(M,ex)
ex = :(@nest2 42)
@test _macroexpand1(ex) != macroexpand(M,ex)
@test _macroexpand1(_macroexpand1(ex)) == macroexpand(M,ex)
ex = :(@nest2b 42)
@test _macroexpand1(ex) != macroexpand(M,ex)
@test _macroexpand1(_macroexpand1(ex)) == macroexpand(M, ex)
@test (@macroexpand1 @nest2b 42) == _macroexpand1(ex)
end
foo_9965(x::Float64; w=false) = x
foo_9965(x::Int) = 2x
@testset "closest candidates kwarg #9965" begin
ex = try
foo_9965(1, w=true)
catch e
e
end
@test typeof(ex) == MethodError
io = IOBuffer()
Base.show_method_candidates(io, ex, pairs((w = true,)))
@test occursin("got unsupported keyword argument \"w\"", String(take!(io)))
end
# Issue #20556
import REPL
module EnclosingModule
abstract type AbstractTypeNoConstructors end
end
let
method_error = MethodError(EnclosingModule.AbstractTypeNoConstructors, ())
# Test that it shows a special message when no constructors have been defined by the user.
@test sprint(showerror, method_error) ==
"MethodError: no constructors have been defined for $(EnclosingModule.AbstractTypeNoConstructors)"
# Does it go back to previous behaviour when there *is* at least
# one constructor defined?
EnclosingModule.AbstractTypeNoConstructors(x, y) = x + y
@test startswith(sprint(showerror, method_error),
"MethodError: no method matching $(EnclosingModule.AbstractTypeNoConstructors)()")
# Test that the 'default' sysimg.jl method is not displayed.
@test !occursin("where T at sysimg.jl", sprint(showerror, method_error))
# Test that tab-completion will not show the 'default' sysimg.jl method.
for method_string in REPL.REPLCompletions.complete_methods(:(EnclosingModule.AbstractTypeNoConstructors()))
@test !startswith(method_string, "(::Type{T})(arg) where T in Base at sysimg.jl")
end
end
@testset "show for manually thrown MethodError" begin
global f21006
f21006() = nothing
# Normal method error should not warn about world age.
ex1 = try
f21006(())
catch e
e
end::MethodError
str = sprint(Base.showerror, ex1)
@test startswith(str, "MethodError: no method matching f21006(::Tuple{})")
@test !occursin("The applicable method may be too new", str)
# If newer applicable methods are available, world age should be mentioned.
f21006(x) = x
@test f21006(()) === ()
str = sprint(Base.showerror, ex1)
@test startswith(str, "MethodError: no method matching f21006(::Tuple{})")
@test occursin("The applicable method may be too new: running in world age $(ex1.world)", str)
# This method error should be thrown in a world new enough for `f21006(())`.
# Also makes sure it's printed correctly.
ex2 = try
f21006((), ())
catch e
e
end::MethodError
str = sprint(Base.showerror, ex2)
@test startswith(str, "MethodError: no method matching f21006(::Tuple{}, ::Tuple{})")
@test !occursin("The applicable method may be too new", str)
# If the method is available in the exception world or if the exception world is invalid,
# don't warn about world age
for ex3 in (MethodError(ex1.f, ex1.args, ex2.world),
MethodError(ex1.f, ex1.args, typemax(UInt)))
str = sprint(Base.showerror, ex3)
@test startswith(str, "MethodError: no method matching f21006(::Tuple{})")
@test !occursin("The applicable method may be too new", str)
end
end
# issue #28442
@testset "Long stacktrace printing" begin
f28442(c) = g28442(c + 1)
g28442(c) = c > 10000 ? (return backtrace()) : f28442(c+1)
bt = f28442(1)
io = IOBuffer()
Base.show_backtrace(io, bt)
output = split(String(take!(io)), '\n')
@test output[3][1:4] == " [1]"
@test occursin("g28442", output[3])
@test output[4][1:4] == " [2]"
@test occursin("f28442", output[4])
# Issue #30233
# Note that we can't use @test_broken on FreeBSD here, because the tests actually do
# pass with some compilation options, e.g. with assertions enabled
if !Sys.isfreebsd()
@test occursin("the last 2 lines are repeated 5000 more times", output[5])
@test output[6][1:8] == " [10003]"
end
end
# issue #30633
@test_throws ArgumentError("invalid index: \"foo\" of type String") [1]["foo"]
@test_throws ArgumentError("invalid index: nothing of type Nothing") [1][nothing]
# test showing MethodError with type argument
struct NoMethodsDefinedHere; end
let buf = IOBuffer()
Base.show_method_candidates(buf, Base.MethodError(sin, Tuple{NoMethodsDefinedHere}))
@test length(take!(buf)) !== 0
end
# pr #32814
let t1 = @async(error(1)),
t2 = @async(wait(t1))
local e
try
wait(t2)
catch e_
e = e_
end
buf = IOBuffer()
showerror(buf, e)
s = String(take!(buf))
@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