Skip to content

Commit

Permalink
Fix type checking of messages
Browse files Browse the repository at this point in the history
Ref. #522
  • Loading branch information
treiher committed Dec 8, 2020
1 parent 14d8bc8 commit 9eb650b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
17 changes: 9 additions & 8 deletions rflx/model/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ def __verify_expression_types(self) -> None:
types: Dict[ID, mty.Type] = {}

def typed_variable(expression: expr.Expr) -> expr.Expr:
expression = copy(expression)
if isinstance(expression, expr.Variable):
if expression.name.lower() == "message":
expression.type_ = rty.OPAQUE
Expand Down Expand Up @@ -683,15 +684,15 @@ def typed_variable(expression: expr.Expr) -> expr.Expr:
if l.source in self.types:
types[l.source.identifier] = self.types[l.source]

l.condition = l.condition.substituted(typed_variable)
l.size = l.size.substituted(typed_variable)
l.first = l.first.substituted(typed_variable)

for t in [l.condition, l.size, l.first]:
if t == expr.UNDEFINED:
for expression in [
l.condition.substituted(typed_variable),
l.size.substituted(typed_variable),
l.first.substituted(typed_variable),
]:
if expression == expr.UNDEFINED:
continue

error = t.check_type(rty.Any())
error = expression.check_type(rty.Any())

self.error.extend(error)

Expand All @@ -700,7 +701,7 @@ def typed_variable(expression: expr.Expr) -> expr.Expr:
"on path " + " -> ".join(f.name for f in path),
Subsystem.MODEL,
Severity.INFO,
t.location,
expression.location,
)

def __verify_expressions(self) -> None:
Expand Down
33 changes: 32 additions & 1 deletion tests/unit/model/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ def test_subsequent_variable() -> None:
)


def test_reference_to_optional_field() -> None:
def test_reference_to_optional_field_1() -> None:
structure = [
Link(INITIAL, Field("F1")),
Link(Field("F1"), Field("F2"), Equal(Variable("F1"), TRUE)),
Expand All @@ -704,6 +704,37 @@ def test_reference_to_optional_field() -> None:
)


def test_reference_to_optional_field_2() -> None:
structure = [
Link(INITIAL, Field("Flag")),
Link(Field("Flag"), Field("Opt"), Equal(Variable("Flag"), Number(1))),
Link(Field("Flag"), Field("Any"), NotEqual(Variable("Flag"), Number(1))),
Link(Field("Opt"), Field("Any")),
Link(
Field("Any"),
Field("Data"),
size=Mul(
Variable("Opt", location=Location((10, 30))), Number(8), location=Location((10, 20))
),
),
Link(Field("Data"), FINAL),
]
types = {
Field("Flag"): MODULAR_INTEGER,
Field("Opt"): MODULAR_INTEGER,
Field("Any"): MODULAR_INTEGER,
Field("Data"): OPAQUE,
}
assert_message_model_error(
structure,
types,
r"^"
r'<stdin>:10:30: model: error: undefined variable "Opt"\n'
r"<stdin>:10:20: model: info: on path Flag -> Any -> Data"
r"$",
)


def test_invalid_use_of_size_attribute() -> None:
structure = [
Link(INITIAL, Field("F1")),
Expand Down

0 comments on commit 9eb650b

Please sign in to comment.