test: cover typed method return expressions
This commit is contained in:
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
### Check typed parameter expressions in returns
|
### Check typed parameter expressions in returns
|
||||||
|
|
||||||
- `claro typecheck` now carries a function or method parameter's `CHECK TYPE` metadata into later return-expression inference.
|
- `claro typecheck` now carries a function or method parameter's `CHECK TYPE` metadata into later return-expression inference.
|
||||||
- Added positive and negative arithmetic return fixtures so a declared return type cannot silently accept an expression whose parameter type is already known.
|
- Added positive and negative arithmetic return fixtures for functions and object methods so a declared return type cannot silently accept an expression whose parameter type is already known.
|
||||||
|
|
||||||
### Check simple function return types
|
### Check simple function return types
|
||||||
|
|
||||||
|
|||||||
@@ -325,7 +325,7 @@ Returning text from this method produces a learner-facing diagnostic that names
|
|||||||
Type mismatch for return from Player.score: expected NUMBER, but this value looks like TEXT.
|
Type mismatch for return from Player.score: expected NUMBER, but this value looks like TEXT.
|
||||||
```
|
```
|
||||||
|
|
||||||
This is a focused check for simple return expressions. When a parameter has a `CHECK TYPE` declaration, that known type also informs arithmetic return expressions, so `RETURN amount + 1` is checked against the declared return type. Branch-sensitive return analysis and richer method signatures remain planned.
|
This is a focused check for simple return expressions. When a parameter has a `CHECK TYPE` declaration, that known type also informs arithmetic return expressions in functions and methods, so `RETURN amount + 1` is checked against the declared return type. The focused validation matrix covers both method success and mismatch fixtures. Branch-sensitive return analysis and richer method signatures remain planned.
|
||||||
|
|
||||||
## Object method parameter checks
|
## Object method parameter checks
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ Ready now:
|
|||||||
- `TYPE OF` and `CHECK TYPE`
|
- `TYPE OF` and `CHECK TYPE`
|
||||||
- `CHECK TYPE` rejects unknown expected type names with a beginner-facing list of supported types
|
- `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
|
- typed list/map checks through `claro typecheck`, including a nested `LIST OF MAP` insertion example
|
||||||
- simple function and object-method return checks with `TEACH name ... RETURNS TYPE`, including learner-facing diagnostics for mismatched `RETURN` expressions and declarations that contain no `RETURN`, with focused validation for both functions and methods; `CHECK TYPE` parameter metadata also informs arithmetic return-expression checking
|
- simple function and object-method return checks with `TEACH name ... RETURNS TYPE`, including learner-facing diagnostics for mismatched `RETURN` expressions and declarations that contain no `RETURN`, with focused validation for both functions and methods; `CHECK TYPE` parameter metadata also informs arithmetic return-expression checking, including method 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 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
|
- 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
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -37,7 +37,7 @@ See `CURRENT_STATUS.md` for the detailed feature matrix.
|
|||||||
4. Add small examples for each foundation feature before adding bigger syntax. The object-field foundation now includes positive and negative validation for field expressions such as `SET player.score player.name`, `SET player.score player.score + 1`, `SET player.name player.score + 1`, explicitly typed field assignments, explicit typed assignments to undeclared fields, simple and chained aliases such as `SET alias player`, `SET backup alias`, followed by `SET backup.score ...` or `CHECK TYPE backup.score IS ...`, chained aliases in object-method calls through both modern `DO` and compatibility `CALL ... WITH` forms (including a dedicated positive modern `DO` fixture), and arithmetic/text expression mismatches; typed containers also accept a map with nested type metadata when it is added to a `LIST OF MAP`; broader alias/control-flow checking remains planned.
|
4. Add small examples for each foundation feature before adding bigger syntax. The object-field foundation now includes positive and negative validation for field expressions such as `SET player.score player.name`, `SET player.score player.score + 1`, `SET player.name player.score + 1`, explicitly typed field assignments, explicit typed assignments to undeclared fields, simple and chained aliases such as `SET alias player`, `SET backup alias`, followed by `SET backup.score ...` or `CHECK TYPE backup.score IS ...`, chained aliases in object-method calls through both modern `DO` and compatibility `CALL ... WITH` forms (including a dedicated positive modern `DO` fixture), and arithmetic/text expression mismatches; typed containers also accept a map with nested type metadata when it is added to a `LIST OF MAP`; broader alias/control-flow checking remains planned.
|
||||||
5. Keep the focused typecheck validator complete: every `typecheck_*.claro` fixture, including positive fixtures, must be exercised by release validation.
|
5. Keep the focused typecheck validator complete: every `typecheck_*.claro` fixture, including positive fixtures, must be exercised by release validation.
|
||||||
6. Continue narrowing expression diagnostics: known TEXT operands now remain visible through arithmetic, with focused numeric-subtraction, numeric-division, and numeric-multiplication positives plus text-operand addition, subtraction, multiplication, and division coverage. Text concatenation into TEXT fields is covered in both operand orders, including field-to-field concatenation. Each numeric operator gives specific guidance naming the text operand. `CHECK TYPE` also rejects misspelled expected type names before comparing values.
|
6. Continue narrowing expression diagnostics: known TEXT operands now remain visible through arithmetic, with focused numeric-subtraction, numeric-division, and numeric-multiplication positives plus text-operand addition, subtraction, multiplication, and division coverage. Text concatenation into TEXT fields is covered in both operand orders, including field-to-field concatenation. Each numeric operator gives specific guidance naming the text operand. `CHECK TYPE` also rejects misspelled expected type names before comparing values.
|
||||||
7. Keep declared return types honest: `claro typecheck` now reports friendly diagnostics for mismatched return expressions and missing `RETURN` statements when a simple function or object method declares `RETURNS TYPE`; `CHECK TYPE` parameter metadata also informs arithmetic return-expression checking. Focused fixtures cover both forms. Branch-sensitive return analysis remains planned.
|
7. Keep declared return types honest: `claro typecheck` now reports friendly diagnostics for mismatched return expressions and missing `RETURN` statements when a simple function or object method declares `RETURNS TYPE`; `CHECK TYPE` parameter metadata also informs arithmetic return-expression checking, including object methods. Focused fixtures cover both forms. Branch-sensitive return analysis remains planned.
|
||||||
|
|
||||||
## Complete-platform milestones
|
## Complete-platform milestones
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,10 @@ class ValidateTypecheckDiagnosticsTests(unittest.TestCase):
|
|||||||
def test_includes_missing_method_return_fixture(self):
|
def test_includes_missing_method_return_fixture(self):
|
||||||
self.assertIn("tests/typecheck_method_missing_return_bad.claro", MODULE.EXPECTED)
|
self.assertIn("tests/typecheck_method_missing_return_bad.claro", MODULE.EXPECTED)
|
||||||
|
|
||||||
|
def test_includes_typed_method_return_expression_fixtures(self):
|
||||||
|
self.assertIn("tests/typecheck_method_return_expression_bad.claro", MODULE.EXPECTED)
|
||||||
|
self.assertIn("tests/typecheck_method_return_expression_good.claro", MODULE.EXPECTED_OK)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
CLASS Player
|
||||||
|
TEACH label amount RETURNS TEXT
|
||||||
|
CHECK TYPE amount IS NUMBER
|
||||||
|
RETURN amount + 1
|
||||||
|
END
|
||||||
|
END
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
CLASS Player
|
||||||
|
TEACH label amount RETURNS NUMBER
|
||||||
|
CHECK TYPE amount IS NUMBER
|
||||||
|
RETURN amount + 1
|
||||||
|
END
|
||||||
|
END
|
||||||
@@ -31,6 +31,9 @@ EXPECTED = {
|
|||||||
"tests/typecheck_method_return_bad.claro": [
|
"tests/typecheck_method_return_bad.claro": [
|
||||||
"tests/typecheck_method_return_bad.claro:3: Type mismatch for return from Player.score: expected NUMBER, but this value looks like TEXT.",
|
"tests/typecheck_method_return_bad.claro:3: Type mismatch for return from Player.score: expected NUMBER, but this value looks like TEXT.",
|
||||||
],
|
],
|
||||||
|
"tests/typecheck_method_return_expression_bad.claro": [
|
||||||
|
"tests/typecheck_method_return_expression_bad.claro:4: Type mismatch for return from Player.label: expected TEXT, but this value looks like NUMBER.",
|
||||||
|
],
|
||||||
"tests/typecheck_method_missing_return_bad.claro": [
|
"tests/typecheck_method_missing_return_bad.claro": [
|
||||||
"tests/typecheck_method_missing_return_bad.claro:4: Method Player.score declares RETURNS NUMBER but has no RETURN statement. Add RETURN with a NUMBER value.",
|
"tests/typecheck_method_missing_return_bad.claro:4: Method Player.score declares RETURNS NUMBER but has no RETURN statement. Add RETURN with a NUMBER value.",
|
||||||
],
|
],
|
||||||
@@ -195,6 +198,7 @@ EXPECTED_OK = [
|
|||||||
"tests/typecheck_function_return_good.claro",
|
"tests/typecheck_function_return_good.claro",
|
||||||
"tests/typecheck_function_return_expression_good.claro",
|
"tests/typecheck_function_return_expression_good.claro",
|
||||||
"tests/typecheck_method_return_good.claro",
|
"tests/typecheck_method_return_good.claro",
|
||||||
|
"tests/typecheck_method_return_expression_good.claro",
|
||||||
"tests/typecheck_function_multi_good.claro",
|
"tests/typecheck_function_multi_good.claro",
|
||||||
"tests/typecheck_method_good.claro",
|
"tests/typecheck_method_good.claro",
|
||||||
"tests/typecheck_method_call_good.claro",
|
"tests/typecheck_method_call_good.claro",
|
||||||
|
|||||||
Reference in New Issue
Block a user