typecheck: align inline method field annotations
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
### Align inline method-field annotations with class fields
|
||||||
|
|
||||||
|
- `claro typecheck` now compares an inline method assignment annotation with the class field declared by `HAS`.
|
||||||
|
- A mismatch such as `SET score TEXT 10` inside a method for `HAS score NUMBER` explains both types and suggests the class-declared type.
|
||||||
|
- Added positive and negative fixtures to the complete typecheck validation matrix.
|
||||||
|
|
||||||
- Added focused modern-method coverage for the learner-facing diagnostic produced when an undeclared field is assigned from an expression whose type is not inferable yet.
|
- Added focused modern-method coverage for the learner-facing diagnostic produced when an undeclared field is assigned from an expression whose type is not inferable yet.
|
||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|||||||
@@ -109,6 +109,8 @@ OBJECT CLASS player AS kind
|
|||||||
OBJECT FIELDS player AS fields
|
OBJECT FIELDS player AS fields
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Inside a method, an inline field type must agree with the class declaration. For example, `HAS score NUMBER` must not be assigned with `SET score TEXT 10`; `claro typecheck` explains the conflict and suggests `NUMBER`.
|
||||||
|
|
||||||
## Static type safety
|
## Static type safety
|
||||||
|
|
||||||
Beginners can still write the simplest form:
|
Beginners can still write the simplest form:
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ Still needed:
|
|||||||
- 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
|
||||||
- typed imports/modules
|
- typed imports/modules
|
||||||
|
- inline method-field annotations must agree with the class `HAS` declaration; conflicting annotations now get a repair-oriented diagnostic even when the value's inferred type is otherwise correct.
|
||||||
|
|
||||||
Good starting docs:
|
Good starting docs:
|
||||||
- `ADVANCED_STATIC_TYPING.md`
|
- `ADVANCED_STATIC_TYPING.md`
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ Keep declared return types honest: `claro typecheck` now reports friendly diagno
|
|||||||
8a. Keep method-body text expressions balanced: modern TEXT field concatenation is covered when the field is on either side of `+`, so both common beginner word-order patterns remain accepted.
|
8a. Keep method-body text expressions balanced: modern TEXT field concatenation is covered when the field is on either side of `+`, so both common beginner word-order patterns remain accepted.
|
||||||
8b. Keep compatibility method-body text expressions aligned: `TAKES` / `LEARNED` methods now have positive TEXT concatenation coverage in both operand orders beside the modern `TEACH` / `END` examples.
|
8b. Keep compatibility method-body text expressions aligned: `TAKES` / `LEARNED` methods now have positive TEXT concatenation coverage in both operand orders beside the modern `TEACH` / `END` examples.
|
||||||
8c. Keep explicit method-field annotations aligned with class declarations: `SET score TEXT "oops"` inside a method with `HAS score NUMBER` now reports the declared field mismatch instead of accepting the inline annotation; broader annotation consistency remains planned.
|
8c. Keep explicit method-field annotations aligned with class declarations: `SET score TEXT "oops"` inside a method with `HAS score NUMBER` now reports the declared field mismatch instead of accepting the inline annotation; broader annotation consistency remains planned.
|
||||||
|
8d. Keep inline method-field annotations consistent with `HAS` declarations: `claro typecheck` now rejects a conflicting annotation even when the assigned value itself has the class-declared type, and explains which type to use.
|
||||||
|
|
||||||
### 1. Strong static types
|
### 1. Strong static types
|
||||||
|
|
||||||
|
|||||||
+1
-1
File diff suppressed because one or more lines are too long
@@ -0,0 +1,10 @@
|
|||||||
|
CLASS Player
|
||||||
|
HAS score NUMBER
|
||||||
|
|
||||||
|
TEACH set_score
|
||||||
|
SET score TEXT 10
|
||||||
|
END
|
||||||
|
END
|
||||||
|
|
||||||
|
NEW Player player
|
||||||
|
DO player.set_score
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
CLASS Player
|
||||||
|
HAS score NUMBER
|
||||||
|
|
||||||
|
TEACH set_score
|
||||||
|
SET score NUMBER 10
|
||||||
|
END
|
||||||
|
END
|
||||||
|
|
||||||
|
NEW Player player
|
||||||
|
DO player.set_score
|
||||||
@@ -294,7 +294,10 @@ EXPECTED = {
|
|||||||
"tests/typecheck_method_compat_unknown_field_typed_bad.claro:4: Object Player has no field level. Check the field name or add HAS level NUMBER to the class.",
|
"tests/typecheck_method_compat_unknown_field_typed_bad.claro:4: Object Player has no field level. Check the field name or add HAS level NUMBER to the class.",
|
||||||
],
|
],
|
||||||
"tests/typecheck_method_typed_field_assignment_bad.claro": [
|
"tests/typecheck_method_typed_field_assignment_bad.claro": [
|
||||||
"tests/typecheck_method_typed_field_assignment_bad.claro:5: Type mismatch for field score in Player.set_score: expected NUMBER, but this value looks like TEXT.",
|
"tests/typecheck_method_typed_field_assignment_bad.claro:5: Type mismatch for field score in Player.set_score: class declares NUMBER, but this assignment says TEXT. Use NUMBER for score.",
|
||||||
|
],
|
||||||
|
"tests/typecheck_method_inline_field_annotation_bad.claro": [
|
||||||
|
"tests/typecheck_method_inline_field_annotation_bad.claro:5: Type mismatch for field score in Player.set_score: class declares NUMBER, but this assignment says TEXT. Use NUMBER for score.",
|
||||||
],
|
],
|
||||||
"tests/typecheck_method_unknown_field_expression_bad.claro": [
|
"tests/typecheck_method_unknown_field_expression_bad.claro": [
|
||||||
"tests/typecheck_method_unknown_field_expression_bad.claro:5: Object Player has no field level. Check the field name or add the field to the class with the right type.",
|
"tests/typecheck_method_unknown_field_expression_bad.claro:5: Object Player has no field level. Check the field name or add the field to the class with the right type.",
|
||||||
@@ -368,6 +371,7 @@ EXPECTED_OK = [
|
|||||||
"tests/typecheck_method_compat_field_check_type_good.claro",
|
"tests/typecheck_method_compat_field_check_type_good.claro",
|
||||||
"tests/typecheck_method_compat_yesno_field_check_type_good.claro",
|
"tests/typecheck_method_compat_yesno_field_check_type_good.claro",
|
||||||
"tests/typecheck_method_yesno_field_check_type_good.claro",
|
"tests/typecheck_method_yesno_field_check_type_good.claro",
|
||||||
|
"tests/typecheck_method_inline_field_annotation_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",
|
||||||
|
|||||||
Reference in New Issue
Block a user