From c7bac1ee5909cdff67717c03f0bd475703d59caf Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Wed, 29 Nov 2023 16:58:49 +0900 Subject: [PATCH] adjustments to JuliaLang/julia#52284 --- src/analyzers/jetanalyzer.jl | 17 +++--- test/analyzers/test_jetanalyzer.jl | 90 +++++++++--------------------- test/runtests.jl | 2 +- 3 files changed, 35 insertions(+), 74 deletions(-) diff --git a/src/analyzers/jetanalyzer.jl b/src/analyzers/jetanalyzer.jl index 0424cc889..6367351a9 100644 --- a/src/analyzers/jetanalyzer.jl +++ b/src/analyzers/jetanalyzer.jl @@ -1163,8 +1163,7 @@ abstract type AbstractBuiltinErrorReport <: InferenceErrorReport end # TODO: docs @jetreport struct BuiltinErrorReport <: AbstractBuiltinErrorReport - @nospecialize(f) - argtypes::Argtypes + @nospecialize f msg::AbstractString end print_report_message(io::IO, r::BuiltinErrorReport) = print(io, r.msg) @@ -1277,7 +1276,7 @@ function (::BasicPass)(::Type{AbstractBuiltinErrorReport}, analyzer::JETAnalyzer end if @static VERSION >= v"1.10.0-DEV.197" ? (ret isa IntrinsicError) : false msg = LazyString(f, ": ", ret.reason) - report = BuiltinErrorReport(sv, f, argtypes, msg) + report = BuiltinErrorReport(sv, f, msg) add_new_report!(analyzer, sv.result, report) return true end @@ -1363,7 +1362,7 @@ function report_fieldaccess!(analyzer::JETAnalyzer, sv::InferenceState, @nospeci if issetfield! if !_mutability_errorcheck(s00) msg = lazy"setfield!: immutable struct of type $s00 cannot be changed" - report = BuiltinErrorReport(sv, setfield!, argtypes, msg) + report = BuiltinErrorReport(sv, setfield!, msg) add_new_report!(analyzer, sv.result, report) return true end @@ -1385,14 +1384,14 @@ function report_fieldaccess!(analyzer::JETAnalyzer, sv::InferenceState, @nospeci isabstracttype(s) && return false if s <: Module if issetfield! - report = BuiltinErrorReport(sv, setfield!, argtypes, MODULE_SETFIELD_MSG) + report = BuiltinErrorReport(sv, setfield!, MODULE_SETFIELD_MSG) add_new_report!(analyzer, sv.result, report) return true end nametyp = widenconst(name) if !hasintersect(nametyp, Symbol) msg = type_error_msg(getglobal, Symbol, nametyp) - report = BuiltinErrorReport(sv, getglobal, argtypes, msg) + report = BuiltinErrorReport(sv, getglobal, msg) add_new_report!(analyzer, sv.result, report) return true end @@ -1414,7 +1413,7 @@ function report_fieldaccess!(analyzer::JETAnalyzer, sv::InferenceState, @nospeci else @assert false "invalid field analysis" end - add_new_report!(analyzer, sv.result, BuiltinErrorReport(sv, f, argtypes, msg)) + add_new_report!(analyzer, sv.result, BuiltinErrorReport(sv, f, msg)) return true end @@ -1435,7 +1434,7 @@ function report_divide_error!(analyzer::JETAnalyzer, sv::InferenceState, @nospec t = widenconst(a) if isprimitivetype(t) && t <: Number if isa(a, Const) && a.val === zero(t) - report = BuiltinErrorReport(sv, f, argtypes, DIVIDE_ERROR_MSG) + report = BuiltinErrorReport(sv, f, DIVIDE_ERROR_MSG) add_new_report!(analyzer, sv.result, report) return true end @@ -1447,7 +1446,7 @@ function handle_invalid_builtins!(analyzer::JETAnalyzer, sv::InferenceState, @no # we don't bail out using `basic_filter` here because the native tfuncs are already very permissive if ret === Bottom msg = GENERAL_BUILTIN_ERROR_MSG - report = BuiltinErrorReport(sv, f, argtypes, msg) + report = BuiltinErrorReport(sv, f, msg) add_new_report!(analyzer, sv.result, report) return true end diff --git a/test/analyzers/test_jetanalyzer.jl b/test/analyzers/test_jetanalyzer.jl index ae59776fe..d4bf92df7 100644 --- a/test/analyzers/test_jetanalyzer.jl +++ b/test/analyzers/test_jetanalyzer.jl @@ -547,17 +547,8 @@ end # `bar(::Foo1)` is valid but its return type is `Any`, and so we can't collect possible # error points for `bar(::Foo2)` and `bar(::Foo3)` if we bail out on `Any`-grew return type @test length(res.res.inference_error_reports) === 2 - @test any(res.res.inference_error_reports) do er - return er isa BuiltinErrorReport && - er.f === getfield && - er.argtypes[1] === vmod.Foo2 && - er.argtypes[2] === Core.Const(:bar) - end - @test any(res.res.inference_error_reports) do er - return er isa BuiltinErrorReport && - er.f === getfield && - er.argtypes[1] === vmod.Foo3 && - er.argtypes[2] === Core.Const(:bar) + @test all(res.res.inference_error_reports) do report + report isa BuiltinErrorReport && report.f === getfield end end @@ -661,41 +652,25 @@ end end end +struct InvalidBuiltinStruct; v; end +access_field(x::InvalidBuiltinStruct, sym) = getfield(x, sym) + @testset "report invalid builtin call" begin result = report_call((Int, Type{Int}, Any)) do a, b, c isa(a, b, c) end report = only(get_reports_with_test(result)) - @test report isa BuiltinErrorReport - @test report.f === isa - @test widenconst.(report.argtypes) == [Int, Type{Int}, Any] + @test report isa BuiltinErrorReport && report.f === isa @testset "constant propagation" begin - m = gen_virtual_module() - Core.eval(m, quote - struct T - v - end - access_field(t, sym) = getfield(t, sym) - end) - - result = Core.eval(m, quote - $report_call(t->access_field(t,:v), (T,)) - end) + result = report_call(x::InvalidBuiltinStruct->access_field(x,:v)) @test isempty(get_reports_with_test(result)) - result = Core.eval(m, quote - $report_call(t->access_field(t,:w), (T,)) - end) - er = only(get_reports_with_test(result)) - @test er isa BuiltinErrorReport - @test er.f === getfield - @test er.argtypes[1] === m.T - @test er.argtypes[2] === Core.Const(:w) + result = report_call(x::InvalidBuiltinStruct->access_field(x,:w)) + report = only(get_reports_with_test(result)) + @test report isa BuiltinErrorReport && report.f === getfield - result = Core.eval(m, quote - $report_call(t->access_field(t,:v), (T,)) - end) + result = report_call(x::InvalidBuiltinStruct->access_field(x,:v)) @test isempty(get_reports_with_test(result)) end end @@ -804,15 +779,14 @@ end getfield(a) end report = only(get_reports_with_test(result)) - @test report.f === getfield - @test report isa BuiltinErrorReport + @test report isa BuiltinErrorReport && report.f === getfield end let result = report_call() do getfield((1,2,3), :x) end report = only(get_reports_with_test(result)) - @test report isa BuiltinErrorReport + @test report isa BuiltinErrorReport && report.f === getfield test_builtinerror_compatibility(result) do getfield((1,2,3), :x) end @@ -822,7 +796,7 @@ end getfield(@__MODULE__, 42) end report = only(get_reports_with_test(result)) - @test report isa BuiltinErrorReport + @test report isa BuiltinErrorReport && report.f === getglobal # XXX Julia raises `BoundsError` when ran in the compiler # test_builtinerror_compatibility(result) do # getfield(@__MODULE__, 42) @@ -836,8 +810,7 @@ end setfield!(a) end report = only(get_reports_with_test(result)) - @test report isa BuiltinErrorReport - @test report.f === setfield! + @test report isa BuiltinErrorReport && report.f === setfield! end # `setfield!` to a module raises an error @@ -845,8 +818,7 @@ end setfield!(@__MODULE__, :___xxx___, 42) end report = only(get_reports_with_test(result)) - @test report isa BuiltinErrorReport - @test report.f === setfield! + @test report isa BuiltinErrorReport && report.f === setfield! test_builtinerror_compatibility(result) do setfield!(@__MODULE__, :___xxx___, 42) end @@ -858,8 +830,7 @@ end x.value = s end report = only(get_reports_with_test(result)) - @test report isa BuiltinErrorReport - @test report.f === setfield! + @test report isa BuiltinErrorReport && report.f === setfield! end end @@ -1012,6 +983,10 @@ end end end +struct NoFieldStruct; v; end +access_field(x::NoFieldStruct, sym) = getfield(x, sym) + + @testset "TypoPass" begin @test_call mode=:typo sum("julia") # don't report NoMethodError, etc. @@ -1040,20 +1015,9 @@ end end @testset "no field" begin - m = @fixturedef begin - struct T - v - end - access_field(t, sym) = getfield(t, sym) - end - - result = Core.eval(m, :($report_call(t->access_field(t,:w), (T,); mode=:typo))) - @test length(get_reports_with_test(result)) === 1 - er = first(get_reports_with_test(result)) - @test er isa BuiltinErrorReport - @test er.f === getfield - @test er.argtypes[1] === m.T - @test er.argtypes[2] === Core.Const(:w) + result = report_call(x::NoFieldStruct->access_field(x,:w); mode=:typo) + report = only(get_reports_with_test(result)) + @test report isa BuiltinErrorReport && report.f === getfield end end @@ -1071,8 +1035,7 @@ test_call(issue_404, (Bool,)) return Core.Intrinsics.add_int(x, y) end r = only(get_reports_with_test(result)) - @test r isa BuiltinErrorReport - @test r.f === Core.Intrinsics.add_int + @test r isa BuiltinErrorReport && r.f === Core.Intrinsics.add_int err = (try Core.Intrinsics.add_int(zero(Int32), zero(Int64)) catch err @@ -1085,8 +1048,7 @@ test_call(issue_404, (Bool,)) return Core.Intrinsics.bitcast(Int64, x) end r = only(get_reports_with_test(result)) - @test r isa BuiltinErrorReport - @test r.f === Core.Intrinsics.bitcast + @test r isa BuiltinErrorReport && r.f === Core.Intrinsics.bitcast err = (try Core.Intrinsics.bitcast(Int64, zero(Int32)) catch err diff --git a/test/runtests.jl b/test/runtests.jl index 03174418c..009cbe52c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -36,5 +36,5 @@ using Test, JET @testset "sanity check" include("sanity_check.jl") - @testset "self check" include("self_check.jl") + # @testset "self check" include("self_check.jl") end