typecheck: validate simple function returns
This commit is contained in:
@@ -2,6 +2,12 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
### Check simple function return types
|
||||
|
||||
- Added `TEACH name ... RETURNS TYPE` syntax for a focused static return check.
|
||||
- `claro typecheck` now reports the expected and inferred types when a `RETURN` expression does not match.
|
||||
- Added positive and negative fixtures and wired both into `claro validate` and the complete typecheck diagnostics validator.
|
||||
|
||||
### Diagnose explicitly typed unknown object fields
|
||||
|
||||
- Added focused typecheck coverage for `SET player.level NUMBER 3` when `Player` declares no `level` field.
|
||||
|
||||
@@ -162,6 +162,17 @@ Function square only accepts 1 argument, but this call gives 2. Remove the extra
|
||||
Function squre is not known yet. Check the function name or add TEACH squre before calling it.
|
||||
```
|
||||
|
||||
Simple functions can also declare a return type. `claro typecheck` checks each `RETURN` expression against it:
|
||||
|
||||
```claro
|
||||
TEACH square amount RETURNS NUMBER
|
||||
CHECK TYPE amount IS NUMBER
|
||||
RETURN amount
|
||||
END
|
||||
```
|
||||
|
||||
Returning text from this function reports: `Type mismatch for return from square: expected NUMBER, but this value looks like TEXT.`
|
||||
|
||||
The unknown-function diagnostic is validated for both modern `DO squre 4` and compatibility `CALL squre WITH 4` calls. Compatibility calls that leave `WITH` empty, such as `CALL greet WITH`, now count as zero arguments, so learners get the same missing-argument guidance as `DO greet` instead of the checker treating the blank as an argument.
|
||||
|
||||
For functions with more than one checked parameter, Claro reports each mismatched argument with the parameter name:
|
||||
|
||||
@@ -53,13 +53,14 @@ Ready now:
|
||||
- `TYPE OF` and `CHECK TYPE`
|
||||
- `CHECK TYPE` rejects unknown expected type names with a beginner-facing list of supported types
|
||||
- typed list/map checks through `claro typecheck`, including a nested `LIST OF MAP` insertion example
|
||||
- simple function return checks with `TEACH name ... RETURNS TYPE` and learner-facing diagnostics for mismatched `RETURN` expressions
|
||||
- a narrow function/method argument check: `CHECK TYPE parameter IS TYPE` inside a function or simple object method lets `claro typecheck` accept correct checked calls, catch mismatched `DO`, `CALL ... WITH`, `DO object.method ...`, and compatibility `CALL object.method WITH ...` arguments, including checked methods that appear after another method in the same class, report missing checked function and method arguments, report missing unchecked arguments for simple functions and simple object methods, treat empty compatibility calls such as `CALL greet WITH` as missing-argument mistakes, catch extra arguments to simple functions and checked methods even when the checked method appears after another method in the same class, catch modern `DO` and compatibility `CALL ... WITH` calls to undeclared simple functions, explain when `DO object.method ...` or compatibility `CALL object.method WITH ...` happens before the object is created with `NEW` even if the method name is also wrong, and catch modern plus compatibility calls to undeclared object methods with a class-specific `TEACH` hint
|
||||
- a narrow object-field assignment/check-type check for simple `NEW Class object` plus direct `SET object.field value` and `CHECK TYPE object.field IS TYPE` cases when the class declares `HAS field TYPE`; validation now covers correct NUMBER, TEXT, and YESNO direct assignments, explicitly typed field assignments, dedicated positive NUMBER/TEXT/YESNO `CHECK TYPE` metadata fixtures, simple and chained object aliases for both field assignments and `CHECK TYPE` metadata (including a chained TEXT-field check), aliased object-method calls including chained aliases in modern `DO` and compatibility `CALL ... WITH` forms (with a dedicated positive modern `DO` chained-alias fixture), field-to-field, compound arithmetic field expressions, and arithmetic/text expression result types, negative NUMBER/TEXT/YESNO field `CHECK TYPE` metadata mismatches, NUMBER/TEXT/YESNO-expectation unknown-field `CHECK TYPE` diagnostics, missing-object field assignment and `CHECK TYPE` diagnostics, NUMBER/TEXT/YESNO wrong-type diagnostics, field collection when a `HAS` field appears after a simple method, NUMBER/TEXT/YESNO-valued unknown-field diagnostics for direct assignments to undeclared fields including explicitly typed assignments, numeric and text results from simple arithmetic/text expressions in object-field assignments, plus a plain beginner-facing unknown-field diagnostic when the assigned expression type is not inferable yet
|
||||
|
||||
Still needed:
|
||||
- Keep the focused typecheck validator's fixture list complete as new positive and negative examples are added.
|
||||
- The expression checker now carries a known TEXT operand through all arithmetic operators so object-field diagnostics do not hide addition, subtraction, multiplication, or division mistakes; focused numeric-subtraction, numeric-division, and numeric-multiplication positives plus text-operand addition, subtraction, multiplication, and division negatives protect this behavior. Text concatenation into a TEXT field has focused positive fixtures for both operand orders, including concatenation of two TEXT object fields. Each numeric operator names the text operand and explains that it needs NUMBER values.
|
||||
- richer typed function signatures and return values
|
||||
- richer typed function signatures and return values beyond the focused simple `RETURNS TYPE` check
|
||||
- type checking through branches and loops
|
||||
- richer object field type checking beyond simple direct assignments
|
||||
- typed imports/modules
|
||||
|
||||
+2
-2
@@ -45,8 +45,8 @@ See `CURRENT_STATUS.md` for the detailed feature matrix.
|
||||
Goal: make larger beginner programs safer without making first scripts harder.
|
||||
|
||||
Needed next:
|
||||
- richer typed function signatures
|
||||
- typed function returns
|
||||
- richer typed function signatures beyond the focused simple `RETURNS TYPE` check
|
||||
- typed function returns beyond the focused simple `RETURNS TYPE` check
|
||||
- type checking across branches and loops
|
||||
- typed imports/modules
|
||||
- broader object field checking and method return typing
|
||||
|
||||
+10
-3
File diff suppressed because one or more lines are too long
@@ -40,6 +40,10 @@ class ValidateTypecheckDiagnosticsTests(unittest.TestCase):
|
||||
MODULE.EXPECTED_OK,
|
||||
)
|
||||
|
||||
def test_includes_typed_function_return_fixtures(self):
|
||||
self.assertIn("tests/typecheck_function_return_bad.claro", MODULE.EXPECTED)
|
||||
self.assertIn("tests/typecheck_function_return_good.claro", MODULE.EXPECTED_OK)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
TEACH square amount RETURNS NUMBER
|
||||
CHECK TYPE amount IS NUMBER
|
||||
RETURN "oops"
|
||||
END
|
||||
|
||||
DO square 4
|
||||
@@ -0,0 +1,6 @@
|
||||
TEACH square amount RETURNS NUMBER
|
||||
CHECK TYPE amount IS NUMBER
|
||||
RETURN amount
|
||||
END
|
||||
|
||||
DO square 4
|
||||
@@ -19,6 +19,9 @@ EXPECTED = {
|
||||
"tests/typecheck_function_bad.claro": [
|
||||
"tests/typecheck_function_bad.claro:5: Type mismatch for function square: parameter amount needs NUMBER, but this argument looks like TEXT.",
|
||||
],
|
||||
"tests/typecheck_function_return_bad.claro": [
|
||||
"tests/typecheck_function_return_bad.claro:3: Type mismatch for return from square: expected NUMBER, but this value looks like TEXT.",
|
||||
],
|
||||
"tests/typecheck_function_multi_bad.claro": [
|
||||
"tests/typecheck_function_multi_bad.claro:8: Type mismatch for function label: parameter name needs TEXT, but this argument looks like NUMBER.",
|
||||
"tests/typecheck_function_multi_bad.claro:8: Type mismatch for function label: parameter age needs NUMBER, but this argument looks like TEXT.",
|
||||
@@ -177,6 +180,7 @@ EXPECTED = {
|
||||
|
||||
EXPECTED_OK = [
|
||||
"tests/typecheck_function_good.claro",
|
||||
"tests/typecheck_function_return_good.claro",
|
||||
"tests/typecheck_function_multi_good.claro",
|
||||
"tests/typecheck_method_good.claro",
|
||||
"tests/typecheck_method_call_good.claro",
|
||||
|
||||
Reference in New Issue
Block a user