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

Return type in derived functions #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 1 addition & 1 deletion src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
end

function is_already_linted(l::LintResult, filename)
return filename in l.linted_files

Check failure on line 51 in src/interface.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `tin(item,collection)` instead of the Julia's `in` or `∈`. raicode/src/interface.jl
end

function has_values(l::LintResult, a, b, c)
Expand Down Expand Up @@ -196,7 +196,7 @@
this_annotation = annotation_for_this_line(line)
# current_codepoint + sizeof(line) is possibly pointing at the newline that isn't
# actually stored in `line`.
if offset in current_codepoint:(current_codepoint + sizeof(line))

Check failure on line 199 in src/interface.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `tin(item,collection)` instead of the Julia's `in` or `∈`. raicode/src/interface.jl
index_in_line = offset - current_codepoint + 1 # possibly off the end by 1.
if !isnothing(this_annotation)
annotation = this_annotation
Expand Down Expand Up @@ -276,7 +276,7 @@
# Filter along the file content
ss = split(hint_as_string)
has_filename = isfile(last(ss))
has_filename || error("Should have a filename")

Check failure on line 279 in src/interface.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use custom exception instead of the generic `error()`. raicode/src/interface.jl

filename = string(last(ss))

Expand Down Expand Up @@ -463,7 +463,7 @@
end

# If already linted, then we merely exit
rootpath in result.linted_files && return result

Check failure on line 466 in src/interface.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `tin(item,collection)` instead of the Julia's `in` or `∈`. raicode/src/interface.jl

# If we are running Lint on a directory
isdir(rootpath) && return _run_lint_on_dir(rootpath; result, server, io, io_violations, io_recommendations, filters, formatter)
Expand All @@ -484,7 +484,7 @@

# We are now reaching a bug HERE
if isnothing(r)
@error "BUG FOUND IN StaticLint.jl, message from hint $(m) cannot be extracted"
@error "BUG FOUND IN StaticLint.jl, message from hint: $(m) cannot be extracted"
return "ERROR"
end

Expand Down
7 changes: 7 additions & 0 deletions src/linting/extended_checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
!is_there_any_star_marker && return x == y

contains(x, "QQQ") && contains(y, "QQQ") &&
throw(BothCannotHaveStarException("Cannot both $x and $y have a star marker"))

Check failure on line 58 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use $(x) instead of $x ([explanation](https://github.com/RelationalAI/RAIStyle?tab=readme-ov-file#string-interpolation)). raicode/src/linting/extended_checks.jl
if contains(x, "QQQ")
reg_exp = Regex(replace(x, "QQQ" => ".*"))
return !isnothing(match(reg_exp, y))
Expand Down Expand Up @@ -212,6 +212,7 @@
struct NonFrontShapeAPIUsage_Extension <: ViolationExtendedRule end
struct InterpolationInSafeLog_Extension <: RecommendationExtendedRule end
struct UseOfStaticThreads <: ViolationExtendedRule end
struct ReturnTypeInDerived <: ViolationExtendedRule end


const all_extended_rule_types = Ref{Any}(
Expand Down Expand Up @@ -268,7 +269,7 @@

function get_recommendation(msg_prefix)
m = retrieve_full_msg_from_prefix(msg_prefix)
m in keys(is_recommendation) || return nothing

Check failure on line 272 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `tin(item,collection)` instead of the Julia's `in` or `∈`. raicode/src/linting/extended_checks.jl
return is_recommendation[m]
end

Expand Down Expand Up @@ -311,7 +312,7 @@
end

function check_for_recommendation(T::DataType, msg::String)
@assert supertype(T) in [RecommendationExtendedRule, ViolationExtendedRule]

Check failure on line 315 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `tin(item,collection)` instead of the Julia's `in` or `∈`. raicode/src/linting/extended_checks.jl
b = supertype(T) == RecommendationExtendedRule
get!(is_recommendation, msg, b)
return nothing
Expand Down Expand Up @@ -343,7 +344,7 @@

function check(t::NThreads_Extention, x::EXPR, markers::Dict{Symbol,String})
# Threads.nthreads() must not be used in a const field, but it is allowed elsewhere
haskey(markers, :const) || return

Check failure on line 347 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `thaskey(dict,key)` instead of the Julia's `haskey`. raicode/src/linting/extended_checks.jl
generic_check(t, x, "Threads.nthreads()", "`Threads.nthreads()` should not be used in a constant variable.")
end

Expand Down Expand Up @@ -397,10 +398,10 @@
check(t::Ptr_Extension, x::EXPR) = generic_check(t, x, "Ptr{hole_variable}(hole_variable)")

function check(t::ArrayWithNoType_Extension, x::EXPR, markers::Dict{Symbol,String})
haskey(markers, :filename) || return

Check failure on line 401 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `thaskey(dict,key)` instead of the Julia's `haskey`. raicode/src/linting/extended_checks.jl
contains(markers[:filename], "src/Compiler") || return

haskey(markers, :macrocall) && markers[:macrocall] == "@match" && return

Check failure on line 404 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `thaskey(dict,key)` instead of the Julia's `haskey`. raicode/src/linting/extended_checks.jl
haskey(markers, :macrocall) && markers[:macrocall] == "@matchrule" && return

generic_check(t, x, "[]", "Need a specific Array type to be provided.")
Expand Down Expand Up @@ -574,4 +575,10 @@
msg = "Use `Threads.@threads :dynamic` instead of `Threads.@threads :static`. Static threads must not be used as generated tasks will not be able to migrate across threads."
generic_check(t, x, "@threads :static hole_variable_star", msg)
generic_check(t, x, "Threads.@threads :static hole_variable_star", msg)
end

function check(t::ReturnTypeInDerived, x::EXPR, markers::Dict{Symbol,String})
msg = "Return type necessary in `@derived` functions."
generic_check(t, x, "@derived hole_variable function hole_variable(hole_variable_star) hole_variable_star end", msg)
generic_check(t, x, "@derived function hole_variable(hole_variable_star) hole_variable_star end", msg)
end
47 changes: 47 additions & 0 deletions test/rai_rules_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1988,3 +1988,50 @@ end
@test lint_test(source, "Line 2, column 5: Use `Threads.@threads :dynamic` instead of `Threads.@threads :static`.")
@test lint_test(source, "Line 6, column 5: Use `Threads.@threads :dynamic` instead of `Threads.@threads :static`.")
end

@testset "Return type in derived functions" begin
@testset "No error" begin
source = """
@derived v=10 function PhaseAPI.generated_defs_for_id(
rt::Runtime,
path::RelPath,
phase::PhaseNormalization,
)::PhaseResult

return nothing
end
"""
@test !lint_has_error_test(source)
# @test lint_test(source, "Line 1, column 1: Return type necessary in `@derived` functions.")
end
@testset "With error 01" begin
source = """
@derived v=10 function PhaseAPI.generated_defs_for_id(
rt::Runtime,
path::RelPath,
phase::PhaseNormalization,
)

return nothing
end
"""
@test lint_has_error_test(source)
@test lint_test(source, "Line 1, column 1: Return type necessary in `@derived` functions.")
end

@testset "With error 02" begin
source = """
@derived function PhaseAPI.generated_defs_for_id(
rt::Runtime,
path::RelPath,
phase::PhaseNormalization,
)

return nothing
end
"""
@test lint_has_error_test(source)
@test lint_test(source, "Line 1, column 1: Return type necessary in `@derived` functions.")
end
end

Loading