From 4291e80c0445bd717d60047be5ef001213730c13 Mon Sep 17 00:00:00 2001 From: Alexandre Bergel Date: Mon, 4 Mar 2024 13:42:48 +0100 Subject: [PATCH] Adding threads --- src/linting/extended_checks.jl | 9 +- test/rai_rules_tests.jl | 178 +++++++++++++++++---------------- 2 files changed, 101 insertions(+), 86 deletions(-) diff --git a/src/linting/extended_checks.jl b/src/linting/extended_checks.jl index fd9e79c..045265e 100644 --- a/src/linting/extended_checks.jl +++ b/src/linting/extended_checks.jl @@ -63,8 +63,7 @@ struct Inbounds_Extension <: ExtendedRule end struct Atomic_Extension <: ExtendedRule end struct Ptr_Extension <: ExtendedRule end struct ArrayWithNoType_Extension <: ExtendedRule end - - +struct Threads_Extension <: ExtendedRule end const all_extended_rule_types = Ref{Any}(InteractiveUtils.subtypes(ExtendedRule)) @@ -178,3 +177,9 @@ function check(::ArrayWithNoType_Extension, x::EXPR, markers::Dict{Symbol,String contains(markers[:filename], "src/Compiler") || return generic_check(x, "[]", "Need a specific Array type to be provided.") end + +function check(::Threads_Extension, x::EXPR) + msg = "`@threads` should be used with extreme caution." + generic_check(x, "Threads.@threads hole_variable", msg) + generic_check(x, "@threads hole_variable", msg) +end diff --git a/test/rai_rules_tests.jl b/test/rai_rules_tests.jl index 5c10038..8c82542 100644 --- a/test/rai_rules_tests.jl +++ b/test/rai_rules_tests.jl @@ -64,105 +64,30 @@ end "Line 2, column 5: Macro `@cfunction` should not be used.") end - @testset "@lock" begin + @testset "@lock ,@threads" begin source = """ function mark_transaction_as_database_creation!(kv::SpcsKV, transaction_id::String) @lock kv.latch begin push!(kv.create_database_transactions, transaction_id) end + + + Threads.@threads for (e, v) in slots + @test e[] == v + free_slot!(pool, Blob{Nothing}(e)) + end return nothing end """ @test lint_has_error_test(source) @test lint_test(source, "Line 2, column 5: `@lock` should be used with extreme caution") + @test lint_test(source, + "Line 7, column 5: `@threads` should be used with extreme caution.") end - @testset "Locally disabling lint" begin - @testset "lint-disable-lint" begin - @test !lint_has_error_test(""" - function f() - @async 1 + 2 # lint-disable-line - end - """) - @test !lint_has_error_test(""" - function f() - @async 1 + 2 #lint-disable-line - end - """) - - @test !lint_has_error_test(""" - function f() - @async 1 + 2 # lint-disable-line - end - """) - - @test lint_has_error_test(""" - function f() - @async 1 + 2 # lint-disable-line - @async 1 + 3 - end - """) - end - @testset "lint-disable-next-line" begin - @test !lint_has_error_test(""" - function f() - # lint-disable-next-line - @async 1 + 2 - end - """) - @test !lint_has_error_test(""" - function f() - # lint-disable-next-line - @async 1 + 2 - end - """) - - @test !lint_has_error_test(""" - function f() - # lint-disable-next-line - @async 1 + 2 - end - """) - - @test lint_has_error_test(""" - function f() - # lint-disable-next-line - @async 1 + 2 - - @async 1 + 3 - end - """) - @test lint_has_error_test(""" - function f() - @async 1 + 2 - # lint-disable-next-line - - @async 1 + 3 - end - """) - @test lint_has_error_test(""" - function f() - @async 1 + 2 - # lint-disable-next-line - @async 1 + 3 - end - """) - - source = """ - function f() - # lint-disable-next-line - @async 1 + 2 - @async 1 + 3 - end - """ - source_lines = split(source, "\n") - @test convert_offset_to_line_from_lines(46, source_lines) == (3, 4, Symbol("lint-disable-line")) - @test convert_offset_to_line_from_lines(64, source_lines) == (5, 4, nothing) - end - end end @testset "forbidden functions" begin @@ -875,4 +800,89 @@ end @test iszero(StaticLint.run_lint(dir)) end end +end + +@testset "Locally disabling lint" begin + @testset "lint-disable-lint" begin + @test !lint_has_error_test(""" + function f() + @async 1 + 2 # lint-disable-line + end + """) + @test !lint_has_error_test(""" + function f() + @async 1 + 2 #lint-disable-line + end + """) + + @test !lint_has_error_test(""" + function f() + @async 1 + 2 # lint-disable-line + end + """) + + @test lint_has_error_test(""" + function f() + @async 1 + 2 # lint-disable-line + @async 1 + 3 + end + """) + end + @testset "lint-disable-next-line" begin + @test !lint_has_error_test(""" + function f() + # lint-disable-next-line + @async 1 + 2 + end + """) + @test !lint_has_error_test(""" + function f() + # lint-disable-next-line + @async 1 + 2 + end + """) + + @test !lint_has_error_test(""" + function f() + # lint-disable-next-line + @async 1 + 2 + end + """) + + @test lint_has_error_test(""" + function f() + # lint-disable-next-line + @async 1 + 2 + + @async 1 + 3 + end + """) + @test lint_has_error_test(""" + function f() + @async 1 + 2 + # lint-disable-next-line + + @async 1 + 3 + end + """) + @test lint_has_error_test(""" + function f() + @async 1 + 2 + # lint-disable-next-line + @async 1 + 3 + end + """) + + source = """ + function f() + # lint-disable-next-line + @async 1 + 2 + + @async 1 + 3 + end + """ + source_lines = split(source, "\n") + @test convert_offset_to_line_from_lines(46, source_lines) == (3, 4, Symbol("lint-disable-line")) + @test convert_offset_to_line_from_lines(64, source_lines) == (5, 4, nothing) + end end \ No newline at end of file