Skip to content

Commit

Permalink
Test all lowering error messages!
Browse files Browse the repository at this point in the history
  • Loading branch information
c42f committed Sep 26, 2024
1 parent 0494237 commit 4228f0a
Show file tree
Hide file tree
Showing 19 changed files with 374 additions and 162 deletions.
2 changes: 1 addition & 1 deletion src/desugaring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ function expand_property_destruct(ctx, ex)
lhs = ex[1]
@assert kind(lhs) == K"tuple"
if numchildren(lhs) != 1
throw(LoweringError(ex, "Property destructuring must use a single `;` before the property names, eg `(; a, b) = rhs`"))
throw(LoweringError(lhs, "Property destructuring must use a single `;` before the property names, eg `(; a, b) = rhs`"))
end
params = lhs[1]
@assert kind(params) == K"parameters"
Expand Down
32 changes: 0 additions & 32 deletions test/assignments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,5 @@ let
end
""") === 10

#-------------------------------------------------------------------------------
# Invalid assignment left hand sides with specific error messages
@test_throws LoweringError JuliaLowering.include_string(test_mod, """
a.(b) = c
""")

@test_throws LoweringError JuliaLowering.include_string(test_mod, """
T[x y] = z
""")
@test_throws LoweringError JuliaLowering.include_string(test_mod, """
T[x; y] = z
""")
@test_throws LoweringError JuliaLowering.include_string(test_mod, """
T[x ;;; y] = z
""")

@test_throws LoweringError JuliaLowering.include_string(test_mod, """
[x, y] = z
""")
@test_throws LoweringError JuliaLowering.include_string(test_mod, """
[x y] = z
""")
@test_throws LoweringError JuliaLowering.include_string(test_mod, """
[x; y] = z
""")
@test_throws LoweringError JuliaLowering.include_string(test_mod, """
[x ;;; y] = z
""")

@test_throws LoweringError JuliaLowering.include_string(test_mod, """
1 = x
""")

end
72 changes: 72 additions & 0 deletions test/assignments_ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,75 @@ end
3 (call top.setindex! %%₁)
4 (return %₁)

########################################
# Error: Invalid lhs in `=`
a.(b) = rhs
#---------------------
LoweringError:
a.(b) = rhs
└───┘ ── invalid dot call syntax on left hand side of assignment

########################################
# Error: Invalid lhs in `=`
T[x y] = rhs
#---------------------
LoweringError:
T[x y] = rhs
└────┘ ── invalid spacing in left side of indexed assignment

########################################
# Error: Invalid lhs in `=`
T[x; y] = rhs
#---------------------
LoweringError:
T[x; y] = rhs
└─────┘ ── unexpected `;` in left side of indexed assignment

########################################
# Error: Invalid lhs in `=`
T[x ;;; y] = rhs
#---------------------
LoweringError:
T[x ;;; y] = rhs
└────────┘ ── unexpected `;` in left side of indexed assignment

########################################
# Error: Invalid lhs in `=`
[x, y] = rhs
#---------------------
LoweringError:
[x, y] = rhs
└────┘ ── use `(a, b) = ...` to assign multiple values

########################################
# Error: Invalid lhs in `=`
[x y] = rhs
#---------------------
LoweringError:
[x y] = rhs
└───┘ ── use `(a, b) = ...` to assign multiple values

########################################
# Error: Invalid lhs in `=`
[x; y] = rhs
#---------------------
LoweringError:
[x; y] = rhs
└────┘ ── use `(a, b) = ...` to assign multiple values

########################################
# Error: Invalid lhs in `=`
[x ;;; y] = rhs
#---------------------
LoweringError:
[x ;;; y] = rhs
└───────┘ ── use `(a, b) = ...` to assign multiple values

########################################
# Error: Invalid lhs in `=`
1 = rhs
#---------------------
LoweringError:
1 = rhs
╙ ── invalid assignment location

37 changes: 11 additions & 26 deletions test/branching.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,36 +305,21 @@ end
end

@testset "symbolic goto/label" begin
JuliaLowering.include_string(test_mod, """
let
a = []
i = 1
@label foo
push!(a, i)
i = i + 1
if i <= 2
@goto foo
end
a
end
""") == [1,2]

@test_throws LoweringError JuliaLowering.include_string(test_mod, """
begin
JuliaLowering.include_string(test_mod, """
let
a = []
i = 1
@label foo
push!(a, i)
i = i + 1
if i <= 2
@goto foo
end
""")

@test_throws LoweringError JuliaLowering.include_string(test_mod, """
begin
@label foo
@label foo
end
""")
a
end
""") == [1,2]

@test_throws LoweringError JuliaLowering.include_string(test_mod, """
x = @label foo
""")
end

end
34 changes: 34 additions & 0 deletions test/branching_ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,37 @@ end
23 (pop_exception %₁)
24 (return core.nothing)

########################################
# Error: no symbolic label
begin
@goto foo
end
#---------------------
LoweringError:
begin
@goto foo
# └─┘ ── label `foo` referenced but not defined
end

########################################
# Error: duplicate symbolic label
begin
@label foo
@label foo
end
#---------------------
LoweringError:
begin
@label foo
@label foo
# └─┘ ── Label `foo` defined multiple times
end

########################################
# Error: using value of symbolic label
x = @label foo
#---------------------
LoweringError:
x = @label foo
# └─┘ ── misplaced label in value position

24 changes: 0 additions & 24 deletions test/decls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,4 @@ end
@test Core.get_binding_type(test_mod, :a_typed_global_2) === Int
@test test_mod.a_typed_global_2 === 10

@test_throws LoweringError JuliaLowering.include_string(test_mod, """
begin
local x::T = 1
local x::S = 1
end
""")

# Const not supported on locals
@test_throws LoweringError JuliaLowering.include_string(test_mod, """
const local x = 1
""")
@test_throws LoweringError JuliaLowering.include_string(test_mod, """
let
const x = 1
end
""")

# global type decls only allowed at top level
@test_throws LoweringError JuliaLowering.include_string(test_mod, """
function f()
global x::Int = 1
end
""")

end
49 changes: 43 additions & 6 deletions test/decls_ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,48 @@ global xx::T = 10
14 (return 10)

########################################
# Type assert (TODO: move this?)
x::T
# Error: x declared twice
begin
local x::T = 1
local x::S = 1
end
#---------------------
1 TestMod.x
2 TestMod.T
3 (call core.typeassert %%₂)
4 (return %₃)
LoweringError:
begin
local x::T = 1
local x::S = 1
# └──┘ ── multiple type declarations found for `x`
end

########################################
# Error: Const not supported on locals
const local x = 1
#---------------------
LoweringError:
const local x = 1
# ╙ ── unsupported `const` declaration on local variable

########################################
# Error: Const not supported on locals
let
const x = 1
end
#---------------------
LoweringError:
let
const x = 1
# ╙ ── unsupported `const` declaration on local variable
end

########################################
# Error: global type decls only allowed at top level
function f()
global x::Int = 1
end
#---------------------
LoweringError:
function f()
global x::Int = 1
# └────┘ ── type declarations for global variables must be at top level, not inside a function
end

7 changes: 0 additions & 7 deletions test/destructuring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ let
end
""") == (1, [2,3], 4)

@test_throws LoweringError JuliaLowering.include_string(test_mod, """
(xs..., ys...) = x
""")

end


Expand Down Expand Up @@ -138,9 +134,6 @@ let
end
""") == (1, 2)

@test_throws LoweringError JuliaLowering.include_string(test_mod, "(x ; a, b) = rhs")
@test_throws LoweringError JuliaLowering.include_string(test_mod, "(; a=1, b) = rhs")

end

end
24 changes: 24 additions & 0 deletions test/destructuring_ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ end
12 TestMod.as
13 (return %₁₂)

########################################
# Error: Slurping multiple args
(xs..., ys...) = x
#---------------------
LoweringError:
(xs..., ys...) = x
# └────┘ ── multiple `...` in destructuring assignment are ambiguous

########################################
# Recursive destructuring
let
Expand Down Expand Up @@ -255,3 +263,19 @@ end
16 TestMod.rhs
17 (return %₁₆)

########################################
# Error: Property destructuring with frankentuple
(x ; a, b) = rhs
#---------------------
LoweringError:
(x ; a, b) = rhs
└────────┘ ── Property destructuring must use a single `;` before the property names, eg `(; a, b) = rhs`

########################################
# Error: Property destructuring with values for properties
(; a=1, b) = rhs
#---------------------
LoweringError:
(; a=1, b) = rhs
# └─┘ ── invalid assignment location

10 changes: 0 additions & 10 deletions test/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ begin
end
""") === (42, 255)

@test_throws LoweringError JuliaLowering.include_string(test_mod, """
function ccall()
end
""")

@test_throws LoweringError JuliaLowering.include_string(test_mod, """
function A.ccall()
end
""")

Base.include_string(test_mod,
"""
struct X end
Expand Down
20 changes: 20 additions & 0 deletions test/functions_ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,23 @@ end
1 (return core.nothing)
8 (return %₂)

########################################
# Error: Invalid function name
function ccall()
end
#---------------------
LoweringError:
function ccall()
# └───┘ ── Invalid function name
end

########################################
# Error: Invalid function name
function A.ccall()
end
#---------------------
LoweringError:
function A.ccall()
# └─────┘ ── Invalid function name
end

Loading

0 comments on commit 4228f0a

Please sign in to comment.