Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CU-86drpndnn - Throw a compilation error when variable type declaration doesn't match the assigned value type #1242

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
meevee98 marked this conversation as resolved.
Show resolved Hide resolved
annotation_type = self.get_type(ann_assign.value)
if not var_type.is_type_of(annotation_type):
meevee98 marked this conversation as resolved.
Show resolved Hide resolved
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')
meevee98 marked this conversation as resolved.
Show resolved Hide resolved

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')
meevee98 marked this conversation as resolved.
Show resolved Hide resolved

def test_assign_binary_operation_mismatched_type(self):
expected_output = (
Expand Down
Loading