Skip to content

Commit

Permalink
CU-86drpndnn - Throw a compilation error when variable type declarati…
Browse files Browse the repository at this point in the history
…on doesn't match the assigned value type
  • Loading branch information
luc10921 committed Apr 23, 2024
1 parent 10b77de commit acb3f52
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
11 changes: 10 additions & 1 deletion boa3/internal/analyser/moduleanalyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,16 @@ def visit_AnnAssign(self, ann_assign: ast.AnnAssign):

self._check_annotation_type(ann_assign.annotation, ann_assign)

# TODO: check if the annotated type and the value type are the same #86a1ctmwy
if isinstance(ann_assign.value, ast.Constant):
annotation_type = self.get_type(ann_assign.value)
if not var_type.is_type_of(annotation_type):
self._log_error(
CompilerError.MismatchedTypes(line=ann_assign.value.lineno,
col=ann_assign.value.col_offset,
expected_type_id=var_type.identifier,
actual_type_id=annotation_type.identifier)
)

return self.assign_value(var_id, var_type,
source_node=ann_assign,
assignment=ann_assign.value is not None,
Expand Down
15 changes: 1 addition & 14 deletions boa3_test/tests/compiler_tests/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,20 +492,7 @@ async def test_byte_array_set_value_negative_index_run(self):
self.assertEqual(b'\x01', result)

def test_byte_array_literal_value(self):
data = b'\x01\x02\x03'
expected_output = (
Opcode.INITSLOT # function signature
+ b'\x01'
+ b'\x00'
+ Opcode.PUSHDATA1 # a = bytearray(b'\x01\x02\x03')
+ Integer(len(data)).to_byte_array(min_length=1)
+ data
+ Opcode.STLOC0
+ Opcode.RET # return
)

output, _ = self.assertCompilerLogs(CompilerWarning.TypeCasting, 'BytearrayLiteral.py')
self.assertEqual(expected_output, output)
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'BytearrayLiteral.py')

def test_byte_array_default_compile(self):
expected_output = (
Expand Down
17 changes: 1 addition & 16 deletions boa3_test/tests/compiler_tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,22 +303,7 @@ def test_return_undeclared_variable(self):
self.assertCompilerLogs(CompilerError.UnresolvedReference, 'ReturnUndeclaredVariable.py')

def test_assign_value_mismatched_type(self):
string_value = '1'
byte_input = String(string_value).to_bytes()

expected_output = (
Opcode.INITSLOT
+ b'\x01'
+ b'\x00'
+ Opcode.PUSHDATA1 # a = '1'
+ Integer(len(byte_input)).to_byte_array()
+ byte_input
+ Opcode.STLOC0
+ Opcode.RET
)

output, _ = self.assertCompilerLogs(CompilerWarning.TypeCasting, 'MismatchedTypeAssignValue.py')
self.assertEqual(expected_output, output)
self.assertCompilerLogs(CompilerError.MismatchedTypes, 'MismatchedTypeAssignValue.py')

def test_assign_binary_operation_mismatched_type(self):
expected_output = (
Expand Down

0 comments on commit acb3f52

Please sign in to comment.