Skip to content

Commit

Permalink
fix(type-inference): forbid void variables in let-statements (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich authored Jun 19, 2024
1 parent 880d82b commit 9ed9193
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Trailing semicolons in struct and message declarations are optional now: PR [#395](https://github.com/tact-lang/tact/pull/395)
- Tests are refactored and renamed to convey the sense of what is being tested and to reduce the amount of merge conflicts during development: PR [#402](https://github.com/tact-lang/tact/pull/402)
- `let` statements can now be used without an explicit type declaration and determine the type automatically if it was not specified: PR [#198](https://github.com/tact-lang/tact/pull/198)
- `let` statements can now be used without an explicit type declaration and determine the type automatically if it was not specified: PR [#198](https://github.com/tact-lang/tact/pull/198) and PR [#438](https://github.com/tact-lang/tact/pull/438)
- The outdated TextMate-style grammar files for text editors have been removed (the most recent grammar files can be found in the [tact-sublime](https://github.com/tact-lang/tact-sublime) repo): PR [#404](https://github.com/tact-lang/tact/pull/404)
- The JSON schema for `tact.config.json` has been moved to the `json-schemas` project folder: PR [#404](https://github.com/tact-lang/tact/pull/404)
- Allow underscores as unused variable identifiers: PR [#338](https://github.com/tact-lang/tact/pull/338)
Expand All @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- External fallback receivers now work properly: PR [#408](https://github.com/tact-lang/tact/pull/408)
- `Int as coins` as a value type of a map in persistent storage does not throw compilation error anymore: PR [#413](https://github.com/tact-lang/tact/pull/413)
- The semantics of the Tact arithmetic operations in the constant evaluator to perform rounding towards negative infinity: PR [#432](https://github.com/tact-lang/tact/pull/432)
- Inferring `void` type in let statements is now forbidden: PR [#438](https://github.com/tact-lang/tact/pull/438)

## [1.3.1] - 2024-06-08

Expand Down
10 changes: 10 additions & 0 deletions src/types/__snapshots__/resolveStatements.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,16 @@ Line 8, col 5:
"
`;
exports[`resolveStatements should fail statements for stmt-let-void-inference 1`] = `
"<unknown>:12:9: The inferred type of variable "voidVar" is "void", which is not allowed
Line 12, col 9:
11 | get fun foo(): Int {
> 12 | let voidVar = foo();
^~~~~~~~~~~~~~~~~~~~
13 | return 42;
"
`;
exports[`resolveStatements should fail statements for stmt-let-wrong-rhs 1`] = `
"<unknown>:10:5: Type mismatch: "Int" is not assignable to "Bool"
Line 10, col 5:
Expand Down
6 changes: 6 additions & 0 deletions src/types/resolveStatements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ function processStatements(
s.ref,
);
}
if (expressionType.kind === "void") {
throwSyntaxError(
`The inferred type of variable "${s.name}" is "void", which is not allowed`,
s.ref,
);
}
sctx = addVariable(s.name, expressionType, sctx);
}
} else if (s.kind === "statement_assign") {
Expand Down
15 changes: 15 additions & 0 deletions src/types/stmts-failed/stmt-let-void-inference.tact
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
primitive Int;

trait BaseTrait {

}

fun foo() { return }

contract Foo {

get fun foo(): Int {
let voidVar = foo();
return 42;
}
}

0 comments on commit 9ed9193

Please sign in to comment.