Skip to content

Commit

Permalink
Improving method call error
Browse files Browse the repository at this point in the history
  • Loading branch information
bergel committed Mar 8, 2024
1 parent 59440d7 commit ddce961
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/linting/checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ LintOptions(options::Vararg{Union{Bool,Nothing},length(default_options)}) =
LintOptions(something.(options, default_options)...)


function fetch_value(x::SymbolServer.VarRef, tag::Symbol)
return x.name
end

function fetch_value(x::EXPR, tag::Symbol)
if headof(x) == tag
return x.val
Expand Down Expand Up @@ -341,7 +345,13 @@ function check_call(x, env::ExternalEnv)
tls = retrieve_toplevel_scope(x)
tls === nothing && return @warn "Couldn't get top-level scope." # General check, this means something has gone wrong.
func_ref === nothing && return
!sig_match_any(func_ref, x, call_counts, tls, env) && seterror!(x, IncorrectCallArgs)
if !sig_match_any(func_ref, x, call_counts, tls, env)
# isdefined(Main, :Infiltrator) && Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__)
# if func_ref.name isa SymbolServer.VarRef
# isdefined(Main, :Infiltrator) && Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__)
# end
seterror!(x, "Possible method call error. Call of: $(fetch_value(func_ref.name, :IDENTIFIER)).")
end
end
end

Expand Down
9 changes: 8 additions & 1 deletion test/rai_rules_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,6 @@ end
expected = r"""
- \*\*\[Line 1, column 11:\]\(https://github\.com/RelationalAI/raicode/blob/axb-example-with-lint-errors/\H+/src/Compiler/tmp_julia_file\.jl#L1\)\*\* `Threads.nthreads\(\)` should not be used in a constant variable\. at offset 10 of \H+
"""
println("DEBUG: $result")
@test !isnothing(match(expected, result))
end

Expand Down Expand Up @@ -1167,3 +1166,11 @@ end
end
""")
end

@testset "IncorrectCallArgs" begin
source = """
f(x) = 1
f(1, 2)
"""
@test lint_test(source, "Line 2, column 1: Possible method call error. Call of: f.")
end
16 changes: 11 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ f(arg) = arg
let cst = parse_and_pass("""
sin(1,2,3)
""")
@test errorof(cst.args[1]) === StaticLint.IncorrectCallArgs
@test startswith(errorof(cst.args[1]), "Possible method call error.")
end
let cst = parse_and_pass("""
for i in length(1) end
Expand Down Expand Up @@ -522,7 +522,9 @@ f(arg) = arg
sin(1,2)
""")
@test StaticLint.errorof(cst.args[1]) === nothing
@test StaticLint.errorof(cst.args[2]) == StaticLint.IncorrectCallArgs
# @test StaticLint.errorof(cst.args[2]) == StaticLint.IncorrectCallArgs
@test startswith(errorof(cst[2]), "Possible method call error.")

end

let cst = parse_and_pass("""
Expand All @@ -539,7 +541,8 @@ f(arg) = arg
f(x) = 1
f(1, 2)
""")
@test StaticLint.errorof(cst.args[2]) === StaticLint.IncorrectCallArgs
# @test StaticLint.errorof(cst.args[2]) === StaticLint.IncorrectCallArgs
@test startswith(errorof(cst[2]), "Possible method call error.")
end

let cst = parse_and_pass("""
Expand Down Expand Up @@ -1040,7 +1043,8 @@ f(arg) = arg
""")
@test StaticLint.scopehasbinding(scopeof(cst), "adf")
@test !StaticLint.scopehasbinding(scopeof(cst.args[1]), "adf")
@test errorof(cst.args[2]) === StaticLint.IncorrectCallArgs
# @test errorof(cst.args[2]) === StaticLint.IncorrectCallArgs
@test startswith(errorof(cst[2]), "Possible method call error.")
end
let cst = parse_and_pass("""
for name in (:sdf, :asdf)
Expand All @@ -1050,7 +1054,9 @@ f(arg) = arg
""")
@test StaticLint.scopehasbinding(scopeof(cst), "sdf")
@test !StaticLint.scopehasbinding(scopeof(cst.args[1]), "asdf")
@test errorof(cst[2]) === StaticLint.IncorrectCallArgs
# @test errorof(cst[2]) === StaticLint.IncorrectCallArgs
@test startswith(errorof(cst[2]), "Possible method call error.")

end
end
end
Expand Down

0 comments on commit ddce961

Please sign in to comment.