Skip to content

Commit

Permalink
Merge pull request #8 from CliMA/ck/strictness
Browse files Browse the repository at this point in the history
Make things more strict
  • Loading branch information
charleskawczynski authored Mar 11, 2024
2 parents 1b2a3de + 8600024 commit 8aa560a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/macro_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,31 @@ function _fused_pairs(expr::Expr)
# error("???")
# return ""
# end
if _expr isa QuoteNode
error("Dangling symbols are not allowed inside fused blocks")
end
_expr isa LineNumberNode && continue
if _expr.head == :for
error("Loops are not allowed inside fused blocks")
end
if _expr.head == :if
elseif _expr.head == :if
error("If-statements are not allowed inside fused blocks")
end
if _expr.head == :call
elseif _expr.head == :call
error("Function calls are not allowed inside fused blocks")
elseif _expr.head == :(=)
error(
"Non-broadcast assignments are not allowed inside fused blocks",
)
elseif _expr.head == :let
error("Let-blocks are not allowed inside fused blocks")
elseif _expr.head == :quote
error("Quotes are not allowed inside fused blocks")
end
if _expr.head == :macrocall && _expr.args[1] == Symbol("@__dot__")
se = code_lowered_single_expression(_expr)
margs = materialize_args(se)
push!(exprs_out, :(Pair($(margs[1]), $(margs[2]))))
else
@show _expr
@show dump(_expr)
error("Uncaught edge case")
end
end
Expand Down
51 changes: 51 additions & 0 deletions test/expr_errors_and_edge_cases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,57 @@ bar() = nothing
) MBF.fused_pairs(expr_in)
end

@testset "Non-broadcast variable assignments" begin
# Non-broadcast variable assignments is not allowed, because
# this could lead to subtle bugs (order of compute).
expr_in = quote
@. y1 = x1 + x2 + x3 + x4
x1 = x2
@. y1 = x1 + x2 + x3 + x4
end
@test_throws ErrorException(
"Non-broadcast assignments are not allowed inside fused blocks",
) MBF.fused_pairs(expr_in)
end

@testset "No let-blocks" begin
# Let-blocks could hide other non-allowed things
expr_in = quote
@. y1 = x1 + x2 + x3 + x4
let z = 1
end
@. y1 = x1 + x2 + x3 + x4
end
@test_throws ErrorException(
"Let-blocks are not allowed inside fused blocks",
) MBF.fused_pairs(expr_in)
end

@testset "Dangling symbols" begin
# While inaucuous, we prohibit dangling symbols
expr_in = quote
@. y1 = x1 + x2 + x3 + x4
:a
@. y1 = x1 + x2 + x3 + x4
end
@test_throws ErrorException(
"Dangling symbols are not allowed inside fused blocks",
) MBF.fused_pairs(expr_in)
end

@testset "quote" begin
# Not sure why this would be needed, so
# we don't allow quotes inside fused blocks.
expr_in = quote
@. y1 = x1 + x2 + x3 + x4
quote end
@. y1 = x1 + x2 + x3 + x4
end
@test_throws ErrorException("Quotes are not allowed inside fused blocks") MBF.fused_pairs(
expr_in,
)
end

@testset "Comments" begin
expr_in = quote
@. y1 = x1 + x2 + x3 + x4
Expand Down

2 comments on commit 8aa560a

@charleskawczynski
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/102559

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 8aa560ad98e4b8f3c115b0af6b4a5f1f44326e5c
git push origin v0.1.0

Please sign in to comment.