Skip to content

Commit

Permalink
Improve format of error when type is not compatible
Browse files Browse the repository at this point in the history
Ref. eng/recordflux/RecordFlux#1720
  • Loading branch information
Volham22 committed Jul 10, 2024
1 parent e48f782 commit b3517c7
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 54 deletions.
19 changes: 11 additions & 8 deletions rflx/typing_.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,16 @@ def check_type(
f"expected {desc}",
Severity.ERROR,
location,
annotations=[
Annotation(
f"found {actual}",
Severity.NOTE,
location,
),
],
annotations=(
[
Annotation(
f"found {actual}",
Severity.ERROR,
location,
),
]
),
generate_default_annotation=False,
),
)

Expand Down Expand Up @@ -435,7 +438,7 @@ def check_type_instance(
f"expected {desc}",
Severity.ERROR,
location,
annotations=[Annotation(f"found {actual}", Severity.NOTE, location)],
annotations=[Annotation(f"found {actual}", Severity.ERROR, location)],
generate_default_annotation=False,
),
)
Expand Down
92 changes: 90 additions & 2 deletions tests/integration/specification_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1975,8 +1975,96 @@ def test_incompatible_type_link_condition(
5 | with Size => 8
6 | then F2
7 | if F1 = 32;
| ^^
| -- note: found type universal integer (32)
| ^^ found type universal integer (32)
|
""",
),
capfd,
)


def test_aggregate_type_in_condition(
tmp_path: Path,
capfd: pytest.CaptureFixture[str],
) -> None:
file_path = tmp_path / "test.rflx"
file_path.write_text(
textwrap.dedent(
"""\
package Test is
type I is range 0 .. 255 with Size => 8;
type M (A : I) is
message
X : Boolean
then Z
if [1, 2, 3] = A;
Z : Opaque
with Size => 8;
end message;
end Test;
""",
),
)
assert_error_full_message(
file_path,
textwrap.dedent(
f"""\
info: Parsing {file_path}
info: Processing Test
info: Verifying __BUILTINS__::Boolean
info: Verifying __INTERNAL__::Opaque
info: Verifying Test::I
info: Verifying Test::M
error: expected aggregate with element type universal integer (1 .. 3)
--> {file_path}:7:31
|
5 | X : Boolean
| - note: on path "X"
6 | then Z
7 | if [1, 2, 3] = A;
| ^ found integer type "Test::I" (0 .. 255)
|
""",
),
capfd,
)


def test_aggregate_type_in_size(
tmp_path: Path,
capfd: pytest.CaptureFixture[str],
) -> None:
file_path = tmp_path / "test.rflx"
file_path.write_text(
textwrap.dedent(
"""\
package Test is
type I is range 0 .. 255 with Size => 8;
type M is
message
Z : Opaque
with Size => 1 + [1, 2, 3];
end message;
end Test;
""",
),
)
assert_error_full_message(
file_path,
textwrap.dedent(
f"""\
info: Parsing {file_path}
info: Processing Test
info: Verifying __BUILTINS__::Boolean
info: Verifying __INTERNAL__::Opaque
info: Verifying Test::I
info: Verifying Test::M
error: expected integer type
--> {file_path}:6:30
|
6 | with Size => 1 + [1, 2, 3];
| ^^^^^^^^^ found aggregate with element type universal \
integer (1 .. 3)
|
""",
),
Expand Down
36 changes: 18 additions & 18 deletions tests/unit/expr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_not_type_error() -> None:
assert_type_error(
Not(Variable("X", type_=rty.BaseInteger(), location=Location((10, 20)))),
r'^<stdin>:10:20: error: expected enumeration type "__BUILTINS__::Boolean"\n'
r"<stdin>:10:20: note: found integer type$",
r"<stdin>:10:20: error: found integer type$",
)


Expand Down Expand Up @@ -402,9 +402,9 @@ def test_bool_expr_type_error(operation: Callable[[Expr, Expr], Expr]) -> None:
Number(1, location=Location((10, 30))),
),
r'^<stdin>:10:20: error: expected enumeration type "__BUILTINS__::Boolean"\n'
r'<stdin>:10:20: note: found integer type "A" \(0 .. 100\)\n'
r'<stdin>:10:20: error: found integer type "A" \(0 .. 100\)\n'
r'<stdin>:10:30: error: expected enumeration type "__BUILTINS__::Boolean"\n'
r"<stdin>:10:30: note: found type universal integer \(1\)$",
r"<stdin>:10:30: error: found type universal integer \(1\)$",
)


Expand Down Expand Up @@ -633,9 +633,9 @@ def test_math_expr_type_error(operation: Callable[[Expr, Expr], Expr]) -> None:
Variable("True", type_=rty.BOOLEAN, location=Location((10, 30))),
),
r"^<stdin>:10:20: error: expected integer type\n"
r'<stdin>:10:20: note: found enumeration type "__BUILTINS__::Boolean"\n'
r'<stdin>:10:20: error: found enumeration type "__BUILTINS__::Boolean"\n'
r"<stdin>:10:30: error: expected integer type\n"
r'<stdin>:10:30: note: found enumeration type "__BUILTINS__::Boolean"$',
r'<stdin>:10:30: error: found enumeration type "__BUILTINS__::Boolean"$',
)


Expand All @@ -655,7 +655,7 @@ def test_neg_type_error() -> None:
assert_type_error(
Neg(Variable("X", type_=rty.BOOLEAN, location=Location((10, 20)))),
r"^<stdin>:10:20: error: expected integer type\n"
r'<stdin>:10:20: note: found enumeration type "__BUILTINS__::Boolean"$',
r'<stdin>:10:20: error: found enumeration type "__BUILTINS__::Boolean"$',
)


Expand Down Expand Up @@ -1173,7 +1173,7 @@ def test_relation_integer_type_error(relation: Callable[[Expr, Expr], Expr]) ->
Variable("True", type_=rty.BOOLEAN, location=Location((10, 30))),
),
r"^<stdin>:10:30: error: expected integer type\n"
r'<stdin>:10:30: note: found enumeration type "__BUILTINS__::Boolean"$',
r'<stdin>:10:30: error: found enumeration type "__BUILTINS__::Boolean"$',
)


Expand All @@ -1197,7 +1197,7 @@ def test_relation_composite_type_error(relation: Callable[[Expr, Expr], Expr]) -
),
r"^<stdin>:10:30: error: expected aggregate"
r" with element integer type\n"
r'<stdin>:10:30: note: found enumeration type "__BUILTINS__::Boolean"$',
r'<stdin>:10:30: error: found enumeration type "__BUILTINS__::Boolean"$',
)
assert_type_error(
relation(
Expand All @@ -1206,7 +1206,7 @@ def test_relation_composite_type_error(relation: Callable[[Expr, Expr], Expr]) -
),
r"^<stdin>:10:30: error: expected aggregate"
r" with element integer type\n"
r'<stdin>:10:30: note: found sequence type "A"'
r'<stdin>:10:30: error: found sequence type "A"'
r' with element enumeration type "__BUILTINS__::Boolean"$',
)

Expand Down Expand Up @@ -1474,9 +1474,9 @@ def test_value_range_type_error() -> None:
location=Location((10, 20)),
),
r"^<stdin>:10:30: error: expected integer type\n"
r'<stdin>:10:30: note: found enumeration type "__BUILTINS__::Boolean"\n'
r'<stdin>:10:30: error: found enumeration type "__BUILTINS__::Boolean"\n'
r"<stdin>:10:40: error: expected integer type\n"
r'<stdin>:10:40: note: found sequence type "A" with element integer type$',
r'<stdin>:10:40: error: found sequence type "A" with element integer type$',
)


Expand Down Expand Up @@ -1527,21 +1527,21 @@ def test_quantified_expression_type(expr: Callable[[str, Expr, Expr], Expr]) ->
Variable("Y", type_=rty.BOOLEAN, location=Location((10, 30))),
Variable("Z", type_=rty.Sequence("A", rty.BaseInteger()), location=Location((10, 40))),
r"^<stdin>:10:30: error: expected composite type\n"
r'<stdin>:10:30: note: found enumeration type "__BUILTINS__::Boolean"\n'
r'<stdin>:10:30: error: found enumeration type "__BUILTINS__::Boolean"\n'
r'<stdin>:10:40: error: expected enumeration type "__BUILTINS__::Boolean"\n'
r'<stdin>:10:40: note: found sequence type "A" with element integer type$',
r'<stdin>:10:40: error: found sequence type "A" with element integer type$',
),
(
Variable("Y", type_=rty.BOOLEAN, location=Location((10, 30))),
Equal(Variable("X"), Number(1)),
r"^<stdin>:10:30: error: expected composite type\n"
r'<stdin>:10:30: note: found enumeration type "__BUILTINS__::Boolean"$',
r'<stdin>:10:30: error: found enumeration type "__BUILTINS__::Boolean"$',
),
(
Variable("Y", type_=rty.Sequence("A", rty.BOOLEAN)),
Equal(Variable("X"), Number(1, location=Location((10, 30)))),
r'^<stdin>:10:30: error: expected enumeration type "__BUILTINS__::Boolean"\n'
r"<stdin>:10:30: note: found type universal integer \(1\)$",
r"<stdin>:10:30: error: found type universal integer \(1\)$",
),
],
)
Expand Down Expand Up @@ -1842,7 +1842,7 @@ def test_selected_type() -> None:
(
Selected(Variable("X", type_=rty.BOOLEAN, location=Location((10, 20))), "Y"),
r"^<stdin>:10:20: error: expected message type\n"
r'<stdin>:10:20: note: found enumeration type "__BUILTINS__::Boolean"$',
r'<stdin>:10:20: error: found enumeration type "__BUILTINS__::Boolean"$',
),
(
Selected(
Expand Down Expand Up @@ -1949,9 +1949,9 @@ def test_call_type_error() -> None:
],
),
r'^<stdin>:10:30: error: expected enumeration type "__BUILTINS__::Boolean"\n'
r"<stdin>:10:30: note: found integer type\n"
r"<stdin>:10:30: error: found integer type\n"
r"<stdin>:10:40: error: expected integer type\n"
r'<stdin>:10:40: note: found enumeration type "__BUILTINS__::Boolean"$',
r'<stdin>:10:40: error: found enumeration type "__BUILTINS__::Boolean"$',
)


Expand Down
20 changes: 10 additions & 10 deletions tests/unit/model/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ def test_invalid_relation_to_opaque() -> None:
types,
r'^<stdin>:10:20: error: expected sequence type "__INTERNAL__::Opaque"'
r' with element integer type "Byte" \(0 .. 255\)\n'
r"<stdin>:10:20: note: found type universal integer \(42\)\n"
r"<stdin>:10:20: error: found type universal integer \(42\)\n"
r'<stdin>:1:1: note: on path "Length"\n'
r'<stdin>:2:3: note: on path "Data"$',
)
Expand All @@ -1158,11 +1158,11 @@ def test_invalid_relation_to_aggregate() -> None:
structure,
types,
r"^<stdin>:10:20: error: expected integer type\n"
r'<stdin>:10:20: note: found sequence type "__INTERNAL__::Opaque"'
r'<stdin>:10:20: error: found sequence type "__INTERNAL__::Opaque"'
r' with element integer type "Byte" \(0 .. 255\)\n'
r'<stdin>:1:1: note: on path "F1"\n'
r"<stdin>:10:30: error: expected integer type\n"
r"<stdin>:10:30: note: found aggregate"
r"<stdin>:10:30: error: found aggregate"
r" with element type universal integer \(1 .. 2\)\n"
r'<stdin>:1:1: note: on path "F1"$',
)
Expand All @@ -1186,7 +1186,7 @@ def test_invalid_element_in_relation_to_aggregate(lower: Number) -> None:
structure,
types,
rf'^<stdin>:10:20: error: expected integer type "P::Integer" \({lower} .. 255\)\n'
r"<stdin>:10:20: note: found aggregate with element type universal integer"
r"<stdin>:10:20: error: found aggregate with element type universal integer"
r" \(1 .. 2\)\n"
r'<stdin>:1:2: note: on path "F1"$',
)
Expand Down Expand Up @@ -1214,7 +1214,7 @@ def test_opaque_aggregate_out_of_range() -> None:
types,
r'^<stdin>:10:20: error: expected sequence type "__INTERNAL__::Opaque"'
r' with element integer type "Byte" \(0 .. 255\)\n'
r"<stdin>:10:20: note: found aggregate"
r"<stdin>:10:20: error: found aggregate"
r" with element type universal integer \(1 .. 256\)\n"
r'<stdin>:1:2: note: on path "F"$',
)
Expand Down Expand Up @@ -1247,7 +1247,7 @@ def test_sequence_aggregate_out_of_range() -> None:
types,
r'^<stdin>:10:20: error: expected sequence type "P::Sequence"'
r' with element integer type "P::Element" \(0 .. 63\)\n'
r"<stdin>:10:20: note: found aggregate"
r"<stdin>:10:20: error: found aggregate"
r" with element type universal integer \(1 .. 64\)\n"
r'<stdin>:1:2: note: on path "F"$',
)
Expand Down Expand Up @@ -1285,7 +1285,7 @@ def test_sequence_aggregate_invalid_element_type() -> None:
types,
r'^<stdin>:10:20: error: expected sequence type "P::Sequence"'
r' with element message type "P::I"\n'
r"<stdin>:10:20: note: found aggregate with element type universal integer"
r"<stdin>:10:20: error: found aggregate with element type universal integer"
r" \(1 .. 64\)\n"
r'<stdin>:1:2: note: on path "F"$',
)
Expand Down Expand Up @@ -2062,7 +2062,7 @@ def test_invalid_type_condition_enum() -> None:
structure,
types,
r'^<stdin>:10:20: error: expected enumeration type "P::E1"\n'
r'<stdin>:10:20: note: found enumeration type "P::E2"\n'
r'<stdin>:10:20: error: found enumeration type "P::E2"\n'
r'<stdin>:1:1: note: on path "F1"$',
)

Expand Down Expand Up @@ -5872,7 +5872,7 @@ def test_merge_message_with_illegal_condition_on_message_type_field() -> None:
match=(
"^"
'<stdin>:1:2: error: expected enumeration type "__BUILTINS__::Boolean"\n'
r"<stdin>:1:2: note: found type universal integer \(1\)"
r"<stdin>:1:2: error: found type universal integer \(1\)"
"$"
),
):
Expand Down Expand Up @@ -6296,7 +6296,7 @@ def test_refinement_type_error_in_condition() -> None:
match=(
r"^"
r'<stdin>:10:20: error: expected integer type "P::T" \(0 \.\. 255\)\n'
r'<stdin>:10:20: note: found enumeration type "__BUILTINS__::Boolean"'
r'<stdin>:10:20: error: found enumeration type "__BUILTINS__::Boolean"'
r"$"
),
):
Expand Down
Loading

0 comments on commit b3517c7

Please sign in to comment.