typecheck: clarify typed method field diagnostics

This commit is contained in:
Hermes Agent
2026-09-13 06:50:33 +00:00
parent a2a4d3d477
commit d7df4591a4
9 changed files with 29 additions and 3 deletions
+2
View File
@@ -2,6 +2,8 @@
## Unreleased
- Typecheck diagnostics for typed method-body field assignments now name the class method and arithmetic operation when a known TEXT operand is used in a NUMBER field update.
### Validate compatibility method multiplication diagnostics
- Added focused negative typecheck coverage proving that the older `TAKES` / `LEARNED` method syntax identifies a TEXT operand in a NUMBER multiplication return and explains the required operand type.
+2
View File
@@ -345,6 +345,8 @@ The same missing-object guidance is now validated for direct field assignment be
Object player is not known yet. Create it with NEW ClassName player before setting player.score.
```
The checker also names typed method-body field assignments. Inside `Player.add RETURNS NUMBER`, `SET score score + name` reports `Type mismatch for field score in Player.add: addition needs NUMBER values, but name looks like TEXT.` so the learner sees that `score` is a class field, not an unrelated variable.
## Project and package workflow
v1.18.26 hardens Claro's project/package workflow.
+2
View File
@@ -825,6 +825,8 @@ Output:
Function greet needs 1 argument, but this call gives 0. Add the missing argument.
```
The same operator-specific diagnostic is used inside a typed method when it updates a class field by its simple name. For example, `SET score score + name` inside `Player.add RETURNS NUMBER` reports `Type mismatch for field score in Player.add: addition needs NUMBER values, but name looks like TEXT.` instead of describing the field as an ordinary variable. This focused check covers typed method bodies; broader control-flow and object-flow analysis remains planned.
## Status
This is currently a static checker feature. It improves `claro typecheck` and validation confidence for `DO` and compatibility `CALL ... WITH` function calls, simple `DO object.method ...` calls where the object was created with `NEW Class name`, and direct assignments to known object fields. Runtime enforcement for every container mutation and richer function/object signatures can be added later after the syntax is classroom-tested.
+1
View File
@@ -59,6 +59,7 @@ Ready now:
Still needed:
- typed method-body assignments name the class method when a simple class field receives a known mismatched arithmetic expression, such as `SET score score + name` inside `Player.add RETURNS NUMBER`
- Keep the focused typecheck validator's fixture list complete as new positive and negative examples are added. `claro validate` now executes the complete focused fixture matrix as well, so release validation cannot silently omit a listed typecheck example.
- The expression checker now carries a known TEXT operand through all arithmetic operators so object-field and typed-function-return 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. Typed-function return diagnostics now have focused subtraction and division positives alongside multiplication coverage. 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.
- Method return validation now has focused positive subtraction and division fixtures alongside the existing arithmetic return coverage, including compatibility `TAKES` / `LEARNED` subtraction and division cases, so checked NUMBER method parameters and numeric subtraction or division remain accepted by the release validator in both method spellings.
+1 -1
View File
@@ -42,7 +42,7 @@ See `CURRENT_STATUS.md` for the detailed feature matrix.
- Keep compatibility-call coverage aligned with modern calls: empty `CALL object.method WITH` forms now have a focused missing-argument diagnostic fixture alongside the modern `DO object.method` case.
Keep declared return types honest: `claro typecheck` now reports friendly diagnostics for unknown declared return types, mismatched, not-yet-inferable, and empty return expressions, declarations with no `RETURN`, and incomplete conditional branches, when a simple function or object method declares `RETURNS TYPE`; the declaration keyword is case-insensitive like other Claro keywords, with positive coverage in modern and compatibility function forms, including lowercase compatibility declarations and lowercase compatibility method declarations, plus a lowercase compatibility-method mismatch fixture. Complete `IF`/`ELSE` return branches, including nested complete conditionals in functions and methods, are accepted, and modern and compatibility function syntax plus compatibility method syntax are covered by focused fixtures. `CHECK TYPE` parameter metadata also informs arithmetic return-expression checking, including object methods; positive numeric multiplication return coverage now sits beside the mismatch fixtures, including a dedicated compatibility-method multiplication success fixture; typed methods can also return class-declared fields by simple name, with positive and mismatch fixtures, and unknown method return expressions now have dedicated modern and compatibility diagnostic fixtures, and NUMBER return expressions with known TEXT arithmetic operands identify the operation and offending operand, including focused addition, subtraction, multiplication, and division coverage for functions and methods. Compatibility method subtraction diagnostics are also covered alongside the existing addition diagnostic. Full path-sensitive analysis across nested conditionals and loops remains planned.
## Complete-platform milestones
7. Keep method-body field diagnostics learner-facing: typed methods now name the class method when a simple class field assignment mixes a known TEXT operand into a NUMBER expression; broader control-flow and object-flow analysis remains planned.
### 1. Strong static types
+2 -2
View File
File diff suppressed because one or more lines are too long
@@ -158,6 +158,12 @@ class ValidateTypecheckDiagnosticsTests(unittest.TestCase):
MODULE.EXPECTED,
)
def test_includes_method_field_assignment_diagnostic_fixture(self):
self.assertIn(
"tests/typecheck_method_field_assignment_bad.claro",
MODULE.EXPECTED,
)
def test_release_validation_runs_method_call_fixture(self):
result = subprocess.run(
[str(ROOT / "claro"), "validate"],
@@ -0,0 +1,10 @@
CLASS Player
HAS score NUMBER
HAS name TEXT
TEACH add RETURNS NUMBER
SET score score + name
RETURN 1
END
END
NEW Player player
DO player.add
+3
View File
@@ -272,6 +272,9 @@ EXPECTED = {
"tests/typecheck_object_alias_method_call_extra_arg_bad.claro": [
"tests/typecheck_object_alias_method_call_extra_arg_bad.claro:12: Method Player.add only accepts 1 argument, but this call gives 2. Remove the extra argument.",
],
"tests/typecheck_method_field_assignment_bad.claro": [
"tests/typecheck_method_field_assignment_bad.claro:5: Type mismatch for field score in Player.add: addition needs NUMBER values, but name looks like TEXT.",
],
}
EXPECTED_OK = [