typecheck: explain text operands in addition

This commit is contained in:
Hermes Agent
2026-09-08 03:02:24 +00:00
parent 7ecaf3fc94
commit f51ce6b56a
8 changed files with 34 additions and 7 deletions
+5
View File
@@ -2,6 +2,11 @@
## Unreleased ## Unreleased
### Explain text operands in object-field addition
- Added focused typecheck coverage for `SET player.score player.name + 1`.
- `claro typecheck` now names the `TEXT` operand and explains that addition into a `NUMBER` field needs numeric values, matching the existing subtraction, multiplication, and division diagnostics.
### Complete numeric object-field arithmetic coverage ### Complete numeric object-field arithmetic coverage
- Added a focused positive typecheck fixture for numeric subtraction in an object field. - Added a focused positive typecheck fixture for numeric subtraction in an object field.
+3 -2
View File
@@ -229,12 +229,13 @@ SET alias.score "ten"
Type mismatch for field alias.score: expected NUMBER, but this value looks like TEXT. Type mismatch for field alias.score: expected NUMBER, but this value looks like TEXT.
``` ```
The same check follows simple arithmetic and text-concatenation expressions. For example, `SET player.score player.name + 1` reports that the value looks like `TEXT`, rather than silently accepting an expression whose result cannot fit the `NUMBER` field. The same check follows simple arithmetic and text-concatenation expressions. For example, `SET player.score player.name + 1` now names the text operand and explains that addition into a `NUMBER` field needs numeric values, rather than silently accepting an expression whose result cannot fit the field.
The checker also identifies text operands in other arithmetic expressions. For example, `SET player.score player.score - player.name` now explains the operator rule and names the operand, rather than silently treating an invalid numeric subtraction as unknown: The checker identifies text operands in arithmetic expressions and explains the operator rule instead of treating an invalid numeric field assignment as an unknown expression:
```text ```text
Type mismatch for field player.score: subtraction needs NUMBER values, but player.name looks like TEXT. Type mismatch for field player.score: subtraction needs NUMBER values, but player.name looks like TEXT.
Type mismatch for field player.score: addition needs NUMBER values, but player.name looks like TEXT.
Type mismatch for field player.score: multiplication needs NUMBER values, but player.name looks like TEXT. Type mismatch for field player.score: multiplication needs NUMBER values, but player.name looks like TEXT.
Type mismatch for field player.score: division needs NUMBER values, but player.name looks like TEXT. Type mismatch for field player.score: division needs NUMBER values, but player.name looks like TEXT.
``` ```
+11 -1
View File
@@ -89,7 +89,17 @@ Type mismatch for field backup.score: expected NUMBER, but this value looks like
This remains limited to direct assignments through simple alias chains; control-flow and complex object-flow analysis are still planned. This remains limited to direct assignments through simple alias chains; control-flow and complex object-flow analysis are still planned.
Text operands are also identified when they appear in subtraction, multiplication, or division expressions. The diagnostic names the operator and the text operand, which catches a common beginner mistake such as subtracting a text field from a number field: Text operands are also identified when they appear in arithmetic expressions assigned to NUMBER fields. The diagnostic names the operator and the text operand, which catches a common beginner mistake such as adding a text field to a number field:
```claro
SET player.score player.name + 1
```
```text
Type mismatch for field player.score: addition needs NUMBER values, but player.name looks like TEXT.
```
Subtraction, multiplication, and division use the same beginner-facing guidance:
```claro ```claro
SET player.score player.score - player.name SET player.score player.score - player.name
+1 -1
View File
@@ -57,7 +57,7 @@ Ready now:
Still needed: Still needed:
- Keep the focused typecheck validator's fixture list complete as new positive and negative examples are added. - 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 subtraction, multiplication, or division mistakes; focused numeric-subtraction, numeric-division, and numeric-multiplication positives plus text-operand subtraction, multiplication, and division negatives protect this behavior. Subtraction, multiplication, and division name the text operand and explain that the operator needs NUMBER values. - 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. Each 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
- type checking through branches and loops - type checking through branches and loops
- richer object field type checking beyond simple direct assignments - richer object field type checking beyond simple direct assignments
+1 -1
View File
@@ -36,7 +36,7 @@ See `CURRENT_STATUS.md` for the detailed feature matrix.
3b. Keep package security validation in Forgejo/Gitea CI so unsafe names, exact manifest-version/name/version/checksum mismatches, missing manifests, duplicate project manifest versions and packages, duplicate lock packages, duplicate package manifest names, versions, and checksums, and lockfile errors remain release blockers. 3b. Keep package security validation in Forgejo/Gitea CI so unsafe names, exact manifest-version/name/version/checksum mismatches, missing manifests, duplicate project manifest versions and packages, duplicate lock packages, duplicate package manifest names, versions, and checksums, and lockfile errors remain release blockers.
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, 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, 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 non-concatenation arithmetic, with focused numeric-subtraction, numeric-division, and numeric-multiplication positives plus text-operand subtraction, multiplication, and division coverage. Subtraction, multiplication, and division give operator-specific guidance naming the text operand. 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. Each operator gives specific guidance naming the text operand.
## Complete-platform milestones ## Complete-platform milestones
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -0,0 +1,8 @@
CLASS Player
HAS name TEXT
HAS score NUMBER
END
NEW Player player
SET player.name "Ada"
SET player.score player.name + 1
+4 -1
View File
@@ -108,7 +108,10 @@ EXPECTED = {
"tests/typecheck_object_field_expression_bad.claro:8: Type mismatch for field player.score: expected NUMBER, but this value looks like TEXT.", "tests/typecheck_object_field_expression_bad.claro:8: Type mismatch for field player.score: expected NUMBER, but this value looks like TEXT.",
], ],
"tests/typecheck_object_field_compound_bad.claro": [ "tests/typecheck_object_field_compound_bad.claro": [
"tests/typecheck_object_field_compound_bad.claro:8: Type mismatch for field player.score: expected NUMBER, but this value looks like TEXT.", "tests/typecheck_object_field_compound_bad.claro:8: Type mismatch for field player.score: addition needs NUMBER values, but player.name looks like TEXT.",
],
"tests/typecheck_object_field_addition_bad.claro": [
"tests/typecheck_object_field_addition_bad.claro:8: Type mismatch for field player.score: addition needs NUMBER values, but player.name looks like TEXT.",
], ],
"tests/typecheck_object_field_subtraction_bad.claro": [ "tests/typecheck_object_field_subtraction_bad.claro": [
"tests/typecheck_object_field_subtraction_bad.claro:8: Type mismatch for field player.score: subtraction needs NUMBER values, but player.name looks like TEXT.", "tests/typecheck_object_field_subtraction_bad.claro:8: Type mismatch for field player.score: subtraction needs NUMBER values, but player.name looks like TEXT.",