diff --git a/src/main/scala/leon/purescala/Trees.scala b/src/main/scala/leon/purescala/Trees.scala index 5c7b83f10b554ee9ec6a4d1c1d307268b623563f..9ae497e6c7585bb1b3527e25c659d82cf5f5d5dd 100644 --- a/src/main/scala/leon/purescala/Trees.scala +++ b/src/main/scala/leon/purescala/Trees.scala @@ -81,7 +81,7 @@ object Trees { funDef.args.zip(args).foreach{ case (a, c) => if (!isSubtypeOf(c.getType, a.tpe)) { - sys.error("Invalid invocation of "+funDef.id+": argument "+a.id+":"+a.tpe+" was passed "+c+":"+c.getType) + throw new TypeErrorException("Invalid invocation of "+funDef.id+": argument "+a.id+":"+a.tpe+" was passed "+c+":"+c.getType) } } } diff --git a/src/main/scala/leon/purescala/TypeTrees.scala b/src/main/scala/leon/purescala/TypeTrees.scala index 03e29dfcdaf39353d9458daed39a130e0b348c86..feb400baf7f4a71a5fc51ab050b17b5ce5662c39 100644 --- a/src/main/scala/leon/purescala/TypeTrees.scala +++ b/src/main/scala/leon/purescala/TypeTrees.scala @@ -23,6 +23,26 @@ object TypeTrees { } } + class TypeErrorException(msg: String) extends Exception(msg) + + object TypeErrorException { + def apply(obj: Expr, exp: List[TypeTree]): TypeErrorException = { + new TypeErrorException("Type error: "+obj+", expected: "+exp.mkString(" or ")+", found "+obj.getType) + } + + def apply(obj: Expr, exp: TypeTree): TypeErrorException = { + apply(obj, List(exp)) + } + } + + def typeCheck(obj: Expr, exps: TypeTree*) { + val res = exps.exists(e => isSubtypeOf(obj.getType, e)) + + if (!res) { + throw TypeErrorException(obj, exps.toList) + } + } + trait FixedType extends Typed { self => diff --git a/src/test/scala/leon/test/codegen/CodeGenEvaluation.scala b/src/test/scala/leon/test/codegen/CodeGenEvaluation.scala index 1c09fdfa1a8562fe83ca15f1dc968803ad8046b0..734f3936e3345d266a48cf647207b8a84c9e1acd 100644 --- a/src/test/scala/leon/test/codegen/CodeGenEvaluation.scala +++ b/src/test/scala/leon/test/codegen/CodeGenEvaluation.scala @@ -6,6 +6,7 @@ import leon.plugin.ExtractionPhase import leon.codegen.CodeGenPhase import leon.codegen.CompilationUnit import leon.purescala.Definitions._ +import leon.purescala.TypeTrees.TypeErrorException import org.scalatest.FunSuite @@ -105,7 +106,7 @@ class CodeGenEvaluation extends FunSuite { assert(javaEval(unit)(expr2) === IntLiteral(162)) //Type error - intercept[RuntimeException] { + intercept[TypeErrorException] { val expr3 = FunctionInvocation(fact, Seq(BooleanLiteral(false))) assert(javaEval(unit)(expr3) != IntLiteral(1), "This should be a type error") }