From d81428a28723bade19dd6ebc977281dbb4be3f6d Mon Sep 17 00:00:00 2001 From: Burak Emir Date: Fri, 8 Sep 2006 09:47:16 +0000 Subject: throw exception in caseelements --- .../scala/tools/nsc/symtab/Definitions.scala | 2 ++ .../tools/nsc/typechecker/SyntheticMethods.scala | 22 ++++++++++++---------- test/files/run/caseclasses.scala | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/compiler/scala/tools/nsc/symtab/Definitions.scala b/src/compiler/scala/tools/nsc/symtab/Definitions.scala index 246ea1152a..db040d975a 100644 --- a/src/compiler/scala/tools/nsc/symtab/Definitions.scala +++ b/src/compiler/scala/tools/nsc/symtab/Definitions.scala @@ -90,6 +90,7 @@ trait Definitions requires SymbolTable { var MatchErrorModule: Symbol = _ def MatchError_fail = getMember(MatchErrorModule, nme.fail) def MatchError_report = getMember(MatchErrorModule, nme.report) + var IndexOutOfBoundsExceptionClass: Symbol = _ //var RemoteExecutionModule: Symbol = _ // def RemoteExecution_detach = getMember(RemoteExecutionModule, "detach") var ScalaRunTimeModule: Symbol = _ @@ -523,6 +524,7 @@ trait Definitions requires SymbolTable { ConsoleModule = getModule("scala.Console") MatchErrorClass = getClass("scala.MatchError") MatchErrorModule = getModule("scala.MatchError") + IndexOutOfBoundsExceptionClass = getClass("java.lang.IndexOutOfBoundsException") //RemoteExecutionModule = getModule("scala.distributed.RemoteExecution") ScalaRunTimeModule = getModule("scala.runtime.ScalaRunTime") RepeatedParamClass = newCovariantPolyClass( diff --git a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala index 7c71ddbc84..1787be64ac 100644 --- a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala +++ b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala @@ -47,18 +47,20 @@ trait SyntheticMethods requires Analyzer { nme.caseElement, FINAL, MethodType(List(IntClass.tpe), AnyClass.tpe)) val caseFields = clazz.caseFieldAccessors map gen.mkAttributedRef typed( - DefDef(method, vparamss => - if (caseFields.isEmpty) Literal(Constant(null)) - else { - var i = caseFields.length - var cases = List(CaseDef(Ident(nme.WILDCARD), EmptyTree, Literal(Constant(null)))) - for (val field <- caseFields.reverse) { - i = i - 1; cases = CaseDef(Literal(Constant(i)), EmptyTree, field) :: cases + DefDef(method, {vparamss => + val doThrow:Tree = Throw(New(TypeTree(definitions.IndexOutOfBoundsExceptionClass.tpe), + List(List(Select(Ident(vparamss.head.head), nme.toString_))))) + if (caseFields.isEmpty) doThrow + else { + var i = caseFields.length + var cases = List(CaseDef(Ident(nme.WILDCARD), EmptyTree, doThrow)) + for (val field <- caseFields.reverse) { + i = i - 1; cases = CaseDef(Literal(Constant(i)), EmptyTree, field) :: cases + } + Match(Ident(vparamss.head.head), cases) } - Match(Ident(vparamss.head.head), cases) - })) + })) } - def caseArityMethod: Tree = { val method = syntheticMethod(nme.caseArity, FINAL, PolyType(List(), IntClass.tpe)) typed(DefDef(method, vparamss => Literal(Constant(clazz.caseFieldAccessors.length)))) diff --git a/test/files/run/caseclasses.scala b/test/files/run/caseclasses.scala index 18455768ce..147b1cdd4e 100644 --- a/test/files/run/caseclasses.scala +++ b/test/files/run/caseclasses.scala @@ -9,5 +9,20 @@ object Test extends Application { case Foo(1) => System.out.println("OK") case Bar() => System.out.println("NO") } + + try { + Bar() caseElement 2 + throw new NullPointerException("duh") + } catch { + case x:IndexOutOfBoundsException => + } + + try { + f(2) caseElement 2 + throw new NullPointerException("duh") + } catch { + case x:IndexOutOfBoundsException => + } + } -- cgit v1.2.3