From 43f0ddd25d26b0c5ffc1eafe620f773582fffed0 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sun, 20 Jan 2019 03:39:30 -0500 Subject: [PATCH] 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. --- lang/interpret_test/TestAstFunc1/fail2.graph | 1 + .../TestAstFunc1/fail2/main.mcl | 9 +++ lang/unification_test.go | 55 +++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 lang/interpret_test/TestAstFunc1/fail2.graph create mode 100644 lang/interpret_test/TestAstFunc1/fail2/main.mcl diff --git a/lang/interpret_test/TestAstFunc1/fail2.graph b/lang/interpret_test/TestAstFunc1/fail2.graph new file mode 100644 index 00000000..456bf1cb --- /dev/null +++ b/lang/interpret_test/TestAstFunc1/fail2.graph @@ -0,0 +1 @@ +# err: can't unify, invariant illogicality with equality: base kind does not match (2 != 3) diff --git a/lang/interpret_test/TestAstFunc1/fail2/main.mcl b/lang/interpret_test/TestAstFunc1/fail2/main.mcl new file mode 100644 index 00000000..0ca8a2de --- /dev/null +++ b/lang/interpret_test/TestAstFunc1/fail2/main.mcl @@ -0,0 +1,9 @@ +import "fmt" +$x str = if true { # should fail unification + 42 +} else { + 13 +} +test "t1" { + anotherstr => fmt.printf("hello %s", $x), +} diff --git a/lang/unification_test.go b/lang/unification_test.go index 664e1608..6cb55cd4 100644 --- a/lang/unification_test.go +++ b/lang/unification_test.go @@ -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