typecheck: name methods in field diagnostics

This commit is contained in:
Hermes Agent
2026-09-13 10:58:08 +00:00
parent 8e9ab3c711
commit 915bc7f1bc
9 changed files with 42 additions and 8 deletions
+1 -1
View File
@@ -345,7 +345,7 @@ 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. 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. The checker also names typed method-body field assignments. Inside `Player.rename`, `SET name 123` reports `Type mismatch for field name in Player.rename: expected TEXT, but this value looks like NUMBER.` 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 the field belongs to the current class method, not an unrelated variable.
## Project and package workflow ## Project and package workflow
+1 -1
View File
@@ -825,7 +825,7 @@ Output:
Function greet needs 1 argument, but this call gives 0. Add the missing argument. 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. The same method-aware diagnostic is used when a method directly assigns the wrong value to a class field. For example, `SET name 123` inside `Player.rename` reports `Type mismatch for field name in Player.rename: expected TEXT, but this value looks like NUMBER.` The operator-specific form also remains available: `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.` These focused checks cover typed method bodies; broader control-flow and object-flow analysis remains planned.
## Status ## Status
+1 -1
View File
@@ -59,7 +59,7 @@ Ready now:
Still needed: Still needed:
- typed method-body assignments now 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`; matching numeric assignment coverage is also validated - typed method-body assignments name the class method when a simple class field receives a known mismatched value or arithmetic expression, such as `SET name 123` inside `Player.rename` or `SET score score + name` inside `Player.add RETURNS NUMBER`; matching text and numeric assignment coverage is also validated
- 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. - 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. - 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. - 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 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. 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.
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, with matching numeric assignment coverage; broader control-flow and object-flow analysis remains planned. 7. Keep method-body field diagnostics learner-facing: typed methods now name the class method when a simple class field receives a known mismatched value or a known TEXT operand in a NUMBER expression, with matching text and numeric assignment coverage; broader control-flow and object-flow analysis remains planned.
### 1. Strong static types ### 1. Strong static types
+4 -4
View File
File diff suppressed because one or more lines are too long
@@ -170,6 +170,18 @@ class ValidateTypecheckDiagnosticsTests(unittest.TestCase):
MODULE.EXPECTED_OK, MODULE.EXPECTED_OK,
) )
def test_includes_method_text_field_assignment_diagnostic_fixture(self):
self.assertIn(
"tests/typecheck_method_text_field_assignment_bad.claro",
MODULE.EXPECTED,
)
def test_includes_method_text_field_assignment_success_fixture(self):
self.assertIn(
"tests/typecheck_method_text_field_assignment_good.claro",
MODULE.EXPECTED_OK,
)
def test_release_validation_runs_method_call_fixture(self): def test_release_validation_runs_method_call_fixture(self):
result = subprocess.run( result = subprocess.run(
[str(ROOT / "claro"), "validate"], [str(ROOT / "claro"), "validate"],
@@ -0,0 +1,9 @@
CLASS Player
HAS name TEXT
TEACH rename
SET name 123
END
END
NEW Player player
DO player.rename
@@ -0,0 +1,9 @@
CLASS Player
HAS name TEXT
TEACH rename
SET name "Ada"
END
END
NEW Player player
DO player.rename
+4
View File
@@ -275,6 +275,9 @@ EXPECTED = {
"tests/typecheck_method_field_assignment_bad.claro": [ "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.", "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.",
], ],
"tests/typecheck_method_text_field_assignment_bad.claro": [
"tests/typecheck_method_text_field_assignment_bad.claro:4: Type mismatch for field name in Player.rename: expected TEXT, but this value looks like NUMBER.",
],
} }
EXPECTED_OK = [ EXPECTED_OK = [
@@ -305,6 +308,7 @@ EXPECTED_OK = [
"tests/typecheck_method_return_addition_good.claro", "tests/typecheck_method_return_addition_good.claro",
"tests/typecheck_method_unknown_return_expression_good.claro", "tests/typecheck_method_unknown_return_expression_good.claro",
"tests/typecheck_method_field_assignment_good.claro", "tests/typecheck_method_field_assignment_good.claro",
"tests/typecheck_method_text_field_assignment_good.claro",
"tests/typecheck_method_nested_branch_complete_good.claro", "tests/typecheck_method_nested_branch_complete_good.claro",
"tests/typecheck_function_multi_good.claro", "tests/typecheck_function_multi_good.claro",
"tests/typecheck_method_good.claro", "tests/typecheck_method_good.claro",