Fix a confusing type mismatch

This commit is contained in:
Richard Feldman 2025-10-31 21:20:06 -04:00
parent 17adbc270b
commit 52287f1874
No known key found for this signature in database
7 changed files with 25 additions and 138 deletions

View file

@ -1068,13 +1068,17 @@ pub fn canonicalizeFile(
// Now, check the next non-malformed stmt to see if it matches this anno
// We need to skip malformed statements that might appear between the annotation and declaration
// If we encounter malformed statements, it means the annotation itself had parse errors,
// so we should not attach it to the declaration to avoid confusing type errors
var next_i = i + 1;
var skipped_malformed = false;
while (next_i < ast_stmt_idxs.len) {
const next_stmt_id = ast_stmt_idxs[next_i];
const next_stmt = self.parse_ir.store.getStatement(next_stmt_id);
// Skip malformed statements
if (next_stmt == .malformed) {
skipped_malformed = true;
next_i += 1;
continue;
}
@ -1093,9 +1097,10 @@ pub fn canonicalizeFile(
} else false;
if (names_match) {
// If so process it and increment i to skip past this decl
i = next_i;
_ = try self.canonicalizeStmtDecl(decl, TypeAnnoIdent{
// If we skipped malformed statements, the annotation had parse errors;
// don't attach it (to avoid confusing type mismatch errors).
_ = try self.canonicalizeStmtDecl(decl, if (skipped_malformed) null else TypeAnnoIdent{
.name = name_ident,
.anno_idx = type_anno_idx,
.where = where_clauses,

View file

@ -39,7 +39,6 @@ transform = |_, b| b
PARSE ERROR - underscore_in_regular_annotations.md:28:22:28:24
PARSE ERROR - underscore_in_regular_annotations.md:28:25:28:27
UNUSED VARIABLE - underscore_in_regular_annotations.md:9:12:9:16
TYPE MISMATCH - underscore_in_regular_annotations.md:29:13:29:21
# PROBLEMS
**PARSE ERROR**
Function types with multiple arrows need parentheses.
@ -78,20 +77,6 @@ process = |list| "processed"
^^^^
**TYPE MISMATCH**
This expression is used in an unexpected way:
**underscore_in_regular_annotations.md:29:13:29:21:**
```roc
transform = |_, b| b
```
^^^^^^^^
It has the type:
__arg, _b -> _b_
But the type annotation says it should have the type:
__a -> _b_
# TOKENS
~~~zig
LowerIdent,OpColon,Underscore,OpArrow,Underscore,
@ -369,11 +354,7 @@ transform = |_, b| b
(p-underscore)
(p-assign (ident "b")))
(e-lookup-local
(p-assign (ident "b"))))
(annotation
(ty-fn (effectful false)
(ty-rigid-var (name "_a"))
(ty-rigid-var (name "_b"))))))
(p-assign (ident "b"))))))
~~~
# TYPES
~~~clojure
@ -385,7 +366,7 @@ transform = |_, b| b
(patt (type "{ field: _field2, other: Num(Int(Unsigned32)) } -> Num(Int(Unsigned32))"))
(patt (type "Try(_c, Str) -> Str"))
(patt (type "a -> b, List(a) -> List(b)"))
(patt (type "Error")))
(patt (type "_arg, c -> c")))
(expressions
(expr (type "_arg -> _ret"))
(expr (type "a -> a"))
@ -393,5 +374,5 @@ transform = |_, b| b
(expr (type "{ field: _field2, other: Num(Int(Unsigned32)) } -> Num(Int(Unsigned32))"))
(expr (type "Try(_c, Str) -> Str"))
(expr (type "a -> b, List(a) -> List(b)"))
(expr (type "Error"))))
(expr (type "_arg, c -> c"))))
~~~

View file

@ -15,7 +15,6 @@ main! = |_| {}
# EXPECTED
PARSE ERROR - type_function_basic.md:3:26:3:28
PARSE ERROR - type_function_basic.md:3:29:3:31
TYPE MISMATCH - type_function_basic.md:4:9:4:22
# PROBLEMS
**PARSE ERROR**
Function types with multiple arrows need parentheses.
@ -42,20 +41,6 @@ apply : (_a -> _b) -> _a -> _b
^^
**TYPE MISMATCH**
This expression is used in an unexpected way:
**type_function_basic.md:4:9:4:22:**
```roc
apply = |fn, x| fn(x)
```
^^^^^^^^^^^^^
It has the type:
_a -> _a, a -> _a_
But the type annotation says it should have the type:
__a -> _b -> _a_
# TOKENS
~~~zig
KwApp,OpenSquare,LowerIdent,CloseSquare,OpenCurly,LowerIdent,OpColon,KwPlatform,StringStart,StringPart,StringEnd,CloseCurly,
@ -126,14 +111,7 @@ main! = |_| {}
(e-lookup-local
(p-assign (ident "fn")))
(e-lookup-local
(p-assign (ident "x")))))
(annotation
(ty-fn (effectful false)
(ty-parens
(ty-fn (effectful false)
(ty-rigid-var (name "_a"))
(ty-rigid-var (name "_b"))))
(ty-rigid-var-lookup (ty-rigid-var (name "_a"))))))
(p-assign (ident "x"))))))
(d-let
(p-assign (ident "main!"))
(e-lambda
@ -145,9 +123,9 @@ main! = |_| {}
~~~clojure
(inferred-types
(defs
(patt (type "Error"))
(patt (type "a -> b, a -> b"))
(patt (type "_arg -> {}")))
(expressions
(expr (type "Error"))
(expr (type "a -> b, a -> b"))
(expr (type "_arg -> {}"))))
~~~

View file

@ -15,7 +15,6 @@ main! = |_| {}
# EXPECTED
PARSE ERROR - type_function_effectful.md:3:31:3:33
PARSE ERROR - type_function_effectful.md:3:34:3:36
TYPE MISMATCH - type_function_effectful.md:4:14:4:29
# PROBLEMS
**PARSE ERROR**
Function types with multiple arrows need parentheses.
@ -42,20 +41,6 @@ runEffect! : (_a => _b) -> _a => _b
^^
**TYPE MISMATCH**
This expression is used in an unexpected way:
**type_function_effectful.md:4:14:4:29:**
```roc
runEffect! = |fn!, x| fn!(x)
```
^^^^^^^^^^^^^^^
It has the type:
_a -> _a, a -> _a_
But the type annotation says it should have the type:
__a => _b -> _a_
# TOKENS
~~~zig
KwApp,OpenSquare,LowerIdent,CloseSquare,OpenCurly,LowerIdent,OpColon,KwPlatform,StringStart,StringPart,StringEnd,CloseCurly,
@ -126,14 +111,7 @@ main! = |_| {}
(e-lookup-local
(p-assign (ident "fn!")))
(e-lookup-local
(p-assign (ident "x")))))
(annotation
(ty-fn (effectful false)
(ty-parens
(ty-fn (effectful true)
(ty-rigid-var (name "_a"))
(ty-rigid-var (name "_b"))))
(ty-rigid-var-lookup (ty-rigid-var (name "_a"))))))
(p-assign (ident "x"))))))
(d-let
(p-assign (ident "main!"))
(e-lambda
@ -145,9 +123,9 @@ main! = |_| {}
~~~clojure
(inferred-types
(defs
(patt (type "Error"))
(patt (type "a -> b, a -> b"))
(patt (type "_arg -> {}")))
(expressions
(expr (type "Error"))
(expr (type "a -> b, a -> b"))
(expr (type "_arg -> {}"))))
~~~

View file

@ -152,15 +152,7 @@ main! = |_| {}
(e-lookup-local
(p-assign (ident "x")))
(e-lookup-local
(p-assign (ident "y")))))))))
(annotation
(ty-fn (effectful false)
(ty-parens
(ty-fn (effectful false)
(ty-rigid-var (name "_a"))
(ty-rigid-var (name "_b"))
(ty-rigid-var (name "_c"))))
(ty-malformed))))
(p-assign (ident "y"))))))))))
(d-let
(p-assign (ident "main!"))
(e-lambda
@ -172,9 +164,9 @@ main! = |_| {}
~~~clojure
(inferred-types
(defs
(patt (type "_a, _b -> _c -> Error"))
(patt (type "a, b -> c -> a -> b -> c"))
(patt (type "_arg -> {}")))
(expressions
(expr (type "_a, _b -> _c -> Error"))
(expr (type "a, b -> c -> a -> b -> c"))
(expr (type "_arg -> {}"))))
~~~

View file

@ -15,7 +15,6 @@ main! = |_| {}
# EXPECTED
PARSE ERROR - type_function_simple.md:3:26:3:28
PARSE ERROR - type_function_simple.md:3:29:3:31
TYPE MISMATCH - type_function_simple.md:4:9:4:22
# PROBLEMS
**PARSE ERROR**
Function types with multiple arrows need parentheses.
@ -42,20 +41,6 @@ apply : (_a -> _b) -> _a -> _b
^^
**TYPE MISMATCH**
This expression is used in an unexpected way:
**type_function_simple.md:4:9:4:22:**
```roc
apply = |fn, x| fn(x)
```
^^^^^^^^^^^^^
It has the type:
_a -> _a, a -> _a_
But the type annotation says it should have the type:
__a -> _b -> _a_
# TOKENS
~~~zig
KwApp,OpenSquare,LowerIdent,CloseSquare,OpenCurly,LowerIdent,OpColon,KwPlatform,StringStart,StringPart,StringEnd,CloseCurly,
@ -126,14 +111,7 @@ main! = |_| {}
(e-lookup-local
(p-assign (ident "fn")))
(e-lookup-local
(p-assign (ident "x")))))
(annotation
(ty-fn (effectful false)
(ty-parens
(ty-fn (effectful false)
(ty-rigid-var (name "_a"))
(ty-rigid-var (name "_b"))))
(ty-rigid-var-lookup (ty-rigid-var (name "_a"))))))
(p-assign (ident "x"))))))
(d-let
(p-assign (ident "main!"))
(e-lambda
@ -145,9 +123,9 @@ main! = |_| {}
~~~clojure
(inferred-types
(defs
(patt (type "Error"))
(patt (type "a -> b, a -> b"))
(patt (type "_arg -> {}")))
(expressions
(expr (type "Error"))
(expr (type "a -> b, a -> b"))
(expr (type "_arg -> {}"))))
~~~

View file

@ -19,7 +19,6 @@ PARSE ERROR - type_higher_order_multiple_vars.md:3:40:3:42
PARSE ERROR - type_higher_order_multiple_vars.md:3:43:3:45
PARSE ERROR - type_higher_order_multiple_vars.md:3:46:3:48
PARSE ERROR - type_higher_order_multiple_vars.md:3:48:3:49
TYPE MISMATCH - type_higher_order_multiple_vars.md:4:11:4:29
# PROBLEMS
**PARSE ERROR**
Function types with multiple arrows need parentheses.
@ -93,20 +92,6 @@ compose : (_b -> _c) -> (_a -> _b) -> (_a -> _c)
^
**TYPE MISMATCH**
This expression is used in an unexpected way:
**type_higher_order_multiple_vars.md:4:11:4:29:**
```roc
compose = |f, g| |x| f(g(x))
```
^^^^^^^^^^^^^^^^^^
It has the type:
_a -> _b, _a -> a -> _a -> _b_
But the type annotation says it should have the type:
__b -> _c -> _a -> _b_
# TOKENS
~~~zig
KwApp,OpenSquare,LowerIdent,CloseSquare,OpenCurly,LowerIdent,OpColon,KwPlatform,StringStart,StringPart,StringEnd,CloseCurly,
@ -198,17 +183,7 @@ main! = |_| {}
(e-lookup-local
(p-assign (ident "g")))
(e-lookup-local
(p-assign (ident "x"))))))))
(annotation
(ty-fn (effectful false)
(ty-parens
(ty-fn (effectful false)
(ty-rigid-var (name "_b"))
(ty-rigid-var (name "_c"))))
(ty-parens
(ty-fn (effectful false)
(ty-rigid-var (name "_a"))
(ty-rigid-var-lookup (ty-rigid-var (name "_b"))))))))
(p-assign (ident "x")))))))))
(d-let
(p-assign (ident "main!"))
(e-lambda
@ -220,9 +195,9 @@ main! = |_| {}
~~~clojure
(inferred-types
(defs
(patt (type "Error"))
(patt (type "a -> b, c -> a -> c -> b"))
(patt (type "_arg -> {}")))
(expressions
(expr (type "Error"))
(expr (type "a -> b, c -> a -> c -> b"))
(expr (type "_arg -> {}"))))
~~~