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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user