Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit return causes scalac -optimize to fail #7792

Closed
scabug opened this issue Aug 28, 2013 · 7 comments

Comments

@scabug
Copy link

commented Aug 28, 2013

Given this code:

object Return {
  def test(oi:Option[Int]):Option[String] =
    oi.map(i => return Some(i.toString))
}

When the return keyword is explicitly used and you try to compile it with -optimize, it causes the compiler to fail with the following message:

could not find init in: Return.test

The full trace is at https://gist.github.com/dkristian/6348965 (also attached).

It compiles fine without the -optimize flag, and works with the flag if the method definition is changed to

  oi.map(i => i.toString)

Discussion started at https://groups.google.com/forum/#!topic/scala-language/WTzjZ5dpPdo

@scabug

This comment has been minimized.

Copy link
Author

commented Aug 28, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7792?orig=1
Reporter: Kristian Domagala (kristian.domagala)
Affected Versions: 2.10.2
Attachments:

  • log.txt (created on Aug 28, 2013 2:31:21 AM UTC, 6699 bytes)
@scabug

This comment has been minimized.

Copy link
Author

commented Aug 28, 2013

@retronym said:
Selectively running different parts of the optimizer shows two failure modes. Both seem to involve DeadCodeElimination.

2.10.x ~/code/scala2 qbin/scalac -Yinline -Ydead-code -Yclosure-elim test/t7792.scala
2.10.x ~/code/scala2 qbin/scalac -Yinline -Ydead-code -Yinline-handlers test/t7792.scala
error: could not find init in: Return.test
error: uncaught exception during compilation: scala.reflect.internal.FatalError
error: scala.reflect.internal.FatalError:
     while compiling: test/t7792.scala
        during phase: dce
     library version: version 2.10.3-20130823-144852-b7e31d807e
    compiler version: version 2.10.3-20130826-123459-525ee49fe6
  reconstructed args: -Ydead-code -Yinline -Yinline-handlers


could not find init in: Return.test
	at scala.reflect.internal.SymbolTable.abort(SymbolTable.scala:49)
	at scala.tools.nsc.Global.abort(Global.scala:254)
	at scala.tools.nsc.backend.opt.DeadCodeElimination$DeadCode.scala$tools$nsc$backend$opt$DeadCodeElimination$DeadCode$$findInstruction(DeadCodeElimination.scala:428)
	at scala.tools.nsc.backend.opt.DeadCodeElimination$DeadCode.mark(DeadCodeElimination.scala:250)
	at scala.tools.nsc.backend.opt.DeadCodeElimination$DeadCode.dieCodeDie(DeadCodeElimination.scala:94)
	at scala.tools.nsc.backend.opt.DeadCodeElimination$DeadCode$$anonfun$analyzeClass$2.apply(DeadCodeElimination.scala:53)
	at scala.tools.nsc.backend.opt.DeadCodeElimination$DeadCode$$anonfun$analyzeClass$2.apply(DeadCodeElimination.scala:51)
	at scala.collection.immutable.List.foreach(List.scala:318)
	at scala.tools.nsc.backend.opt.DeadCodeElimination$DeadCode.analyzeClass(DeadCodeElimination.scala:51)
	at scala.tools.nsc.backend.opt.DeadCodeElimination$DeadCodeEliminationPhase.apply(DeadCodeElimination.scala:38)
	

2.10.x ~/code/scala2 qbin/scalac -Yinline -Yinline-handlers test/t7792.scala
2.10.x ~/code/scala2 qbin/scalac -Ydead-code -Yinline-handlers test/t7792.scala
2.10.x ~/code/scala2 qbin/scalac -Yinline -Ydead-code test/t7792.scala
in: 10 -> IState(, )
3 -> IState(, )
7 -> IState((value nonLocalReturnKey1,1,3), )
14 -> IState(, )
4 -> IState(, )
11 -> IState(, )
1 -> IState(, )
8 -> IState(, )
5 -> IState(, )
12 -> IState(, )
2 -> IState(, )
9 -> IState(, )
16 -> IState(, )
out: 10 -> IState(, )
3 -> IState(, )
7 -> IState(, )
14 -> IState(, )
4 -> IState(, )
11 -> IState(, )
1 -> IState((value nonLocalReturnKey1,1,3), )
8 -> IState(, )
5 -> IState(, )
12 -> IState(, )
2 -> IState(, )
9 -> IState(, )
16 -> IState(, )
java.util.NoSuchElementException: key not found: 13
	at scala.collection.MapLike$class.default(MapLike.scala:228)
	at scala.collection.AbstractMap.default(Map.scala:58)
	at scala.collection.mutable.HashMap.apply(HashMap.scala:64)
	at scala.tools.nsc.backend.icode.analysis.DataFlowAnalysis$$anonfun$forwardAnalysis$1$$anonfun$1.apply(DataFlowAnalysis.scala:69)
	at scala.tools.nsc.backend.icode.analysis.DataFlowAnalysis$$anonfun$forwardAnalysis$1$$anonfun$1.apply(DataFlowAnalysis.scala:69)
	at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
	at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244)
	at scala.collection.immutable.List.foreach(List.scala:318)
	at scala.collection.TraversableLike$class.map(TraversableLike.scala:244)
	at scala.collection.AbstractTraversable.map(Traversable.scala:105)
	at scala.tools.nsc.backend.icode.analysis.DataFlowAnalysis$$anonfun$forwardAnalysis$1.apply(DataFlowAnalysis.scala:69)
	at scala.tools.nsc.backend.icode.analysis.DataFlowAnalysis$$anonfun$forwardAnalysis$1.apply(DataFlowAnalysis.scala:68)
	at scala.collection.immutable.List.foreach(List.scala:318)
	at scala.tools.nsc.backend.icode.analysis.DataFlowAnalysis$class.forwardAnalysis(DataFlowAnalysis.scala:68)
...

uncaught exception during compilation: java.lang.RuntimeException
error: java.lang.RuntimeException: Could not find element key not found: 13
	at scala.sys.package$.error(package.scala:27)
	at scala.tools.nsc.backend.icode.analysis.DataFlowAnalysis$class.forwardAnalysis(DataFlowAnalysis.scala:82)
	at scala.tools.nsc.backend.icode.analysis.ReachingDefinitions$ReachingDefinitionsAnalysis.forwardAnalysis(ReachingDefinitions.scala:62)
	at scala.tools.nsc.backend.icode.analysis.ReachingDefinitions$ReachingDefinitionsAnalysis.run(ReachingDefinitions.scala:143)
	at scala.tools.nsc.backend.opt.DeadCodeElimination$DeadCode.collectRDef(DeadCodeElimination.scala:110)
	
2.10.x ~/code/scala2 qbin/scalac -Ydead-code -Yinline-handlers test/t7792.scala
2.10.x ~/code/scala2
@scabug

This comment has been minimized.

Copy link
Author

commented Aug 28, 2013

@retronym said:
The second crash (under just -Yinline -Ydead-code) regressed in scala/scala@0bcc8f8 ("Specialize NonLocalReturnControl") in 2.10.0-M3.

@scabug

This comment has been minimized.

Copy link
Author

commented Aug 28, 2013

@retronym said:
Specifically, the change that restricted rewriting of try bodies in CleanupTransformer to the MSIL backend exposes the "key not found: 13" crash.

scala/scala@0bcc8f8#L5R90

@scabug

This comment has been minimized.

Copy link
Author

commented Aug 28, 2013

@magarciaEPFL said:
The new optimizer ( http://magarciaepfl.github.io/scala/ ) doesn't exhibit this bug.

@scabug

This comment has been minimized.

Copy link
Author

commented Aug 29, 2013

@retronym said (edited on Aug 29, 2013 10:22:58 AM UTC):
A more focussed test case:

object Return {
  class Box[A](a: A) {
    // @noinline avoids crash.
    final def map[B](f: A => B): Box[B] = new Box(f(a))
  }

  def test(oi: Box[Int]) {
    var res: Any = null;
    try {
      res = {
        val f = (a: Any) => throw new RuntimeException()
        oi.map(f)
        ()
      }
    } finally ()
    ()
  }
}
@hrhino

This comment has been minimized.

Copy link
Member

commented Mar 1, 2018

Closing since it appears fixed in the new optimizer.

@hrhino hrhino closed this Mar 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.