Skip to content

Commit

Permalink
Fix ICE when '__init__' returns any other type than '()'
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Mar 22, 2021
1 parent 1e5ce10 commit 1cd44c6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions analyzer/src/traversal/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ pub fn func_def(
.transpose()?
.unwrap_or_else(|| Tuple::empty().into());

// `__init__` must not return any type other than `()`.
if name == "__init__" && !return_type.is_empty_tuple() {
return Err(SemanticError::type_error());
}

let attributes: FunctionAttributes = contract_scope
.borrow_mut()
.add_function(
Expand Down
1 change: 1 addition & 0 deletions compiler/tests/compile_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ use std::fs;
case("return_addition_with_mixed_types.fe", "TypeError"),
case("return_call_to_fn_with_param_type_mismatch.fe", "TypeError"),
case("return_call_to_fn_without_return.fe", "TypeError"),
case("return_from_init.fe", "TypeError"),
case("return_lt_mixed_types.fe", "TypeError"),
case("strict_boolean_if_else.fe", "TypeError"),
case("string_capacity_mismatch.fe", "StringCapacityMismatch"),
Expand Down
3 changes: 3 additions & 0 deletions compiler/tests/fixtures/compile_errors/return_from_init.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract C:
pub def __init__() -> i32:
return 0
9 changes: 9 additions & 0 deletions newsfragments/323.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Ensure analyzer rejects code that uses return values for `__init__` functions.

An example that now produces a compile time error:

```
contract C:
pub def __init__() -> i32:
return 0
```

0 comments on commit 1cd44c6

Please sign in to comment.