Skip to content

Commit

Permalink
Give warning when assigning local to itself. (#875)
Browse files Browse the repository at this point in the history
Fixes #867.
  • Loading branch information
floitsch committed Jul 19, 2022
1 parent 93fd449 commit 7148063
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/compiler/resolver_method.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4031,7 +4031,30 @@ ir::Expression* MethodResolver::_assign_identifier(ast::Binary* node,
} else {
ir_value = resolve_expression(ast_right, "Can't use block value in assignment", true);
}
return create_set(ir_value);
auto result = create_set(ir_value);
if (result->is_AssignmentLocal()) {
bool reported_warning = false;
auto assig = result->as_AssignmentLocal();
auto local = assig->local();
auto right = assig->right();
if (right->is_ReferenceLocal() && right->as_ReferenceLocal()->target() == local) {
if (_method->is_constructor() || _method->is_instance()) {
auto fields = _method->holder()->fields();
for (int i = 0; i < fields.length(); i++) {
auto field_name = fields[i]->name();
if (field_name.is_valid() && field_name == local->name()) {
diagnostics()->report_warning(node, "Assigning local to itself has no effect. Did you forget 'this.'?");
reported_warning = true;
break;
}
}
}
if (!reported_warning) {
diagnostics()->report_warning(node, "Assigning local to itself");
}
}
}
return result;
}


Expand Down
16 changes: 16 additions & 0 deletions tests/negative/assign_self_test.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
main:
local := 3
local = local
unresolved

class A:
field := 499

constructor field:
field = field

method field:
field = field

static static_method field:
field = field
16 changes: 16 additions & 0 deletions tests/negative/gold/assign_self_test.gold
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests/negative/assign_self_test.toit:3:9: warning: Assigning local to itself
local = local
^
tests/negative/assign_self_test.toit:4:3: error: Unresolved identifier: 'unresolved'
unresolved
^~~~~~~~~~
tests/negative/assign_self_test.toit:10:11: warning: Assigning local to itself has no effect. Did you forget 'this.'?
field = field
^
tests/negative/assign_self_test.toit:16:11: warning: Assigning local to itself
field = field
^
tests/negative/assign_self_test.toit:13:11: warning: Assigning local to itself has no effect. Did you forget 'this.'?
field = field
^
Compilation failed.

0 comments on commit 7148063

Please sign in to comment.