lang: unification: Catch unification error on typed if expr

I found a case where we had two missing unification rules. Now fixed in
the previous commits, and including this test to show I'm responsible.
I've added the same test in two locations for redundancy and as an
example.
This commit is contained in:
James Shubin
2019-01-20 03:39:30 -05:00
parent 7a28b00d75
commit 43f0ddd25d
3 changed files with 65 additions and 0 deletions

View File

@@ -0,0 +1 @@
# err: can't unify, invariant illogicality with equality: base kind does not match (2 != 3)

View File

@@ -0,0 +1,9 @@
import "fmt"
$x str = if true { # should fail unification
42
} else {
13
}
test "t1" {
anotherstr => fmt.printf("hello %s", $x),
}

View File

@@ -645,6 +645,61 @@ func TestUnification1(t *testing.T) {
},
})
}
{
//import "fmt"
//$x str = if true { # should fail unification
// 42
//} else {
// 13
//}
//test "t1" {
// stringptr => fmt.printf("hello %s", $x),
//}
cond := &ExprIf{
Condition: &ExprBool{V: true},
ThenBranch: &ExprInt{V: 42},
ElseBranch: &ExprInt{V: 13},
}
cond.SetType(types.TypeStr) // should fail unification
expr := &ExprCall{
Name: "fmt.printf",
Args: []interfaces.Expr{
&ExprStr{
V: "hello %s",
},
&ExprVar{
Name: "x", // the var
},
},
}
stmt := &StmtProg{
Prog: []interfaces.Stmt{
&StmtImport{
Name: "fmt",
},
&StmtBind{
Ident: "x", // the var
Value: cond,
},
&StmtRes{
Kind: "test",
Name: &ExprStr{V: "t1"},
Contents: []StmtResContents{
&StmtResField{
Field: "anotherstr",
Value: expr,
},
},
},
},
}
testCases = append(testCases, test{
name: "typed if expr",
ast: stmt,
fail: true,
experrstr: "can't unify, invariant illogicality with equality: base kind does not match (2 != 3)",
})
}
names := []string{}
for index, tc := range testCases { // run all the tests