Scala Programming Language
  1. Scala Programming Language
  2. SI-963

Overly permissive overriding allowed in refinements: unsound

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: Scala 2.10.0
    • Component/s: Misc Compiler
    • Labels:
      None

      Description

      scala> import scala.util.Random
      import scala.util.Random
      
      scala> val r = new Random()
      r: Random = scala.Random@1b3d448
      
      scala> val y : { val x : java.lang.Integer  } = new { def x = new java.lang.Integer(r.nextInt) }
      y: AnyRef{def x: java.lang.Integer} = $$anon$$1@e8709d
      
      scala> val w1 : y.x.type = y.x
      w1: y.x.type = 16667032
      
      scala> val w2 : y.x.type = y.x
      w2: y.x.type = -1279252109
      
      scala> w1 == w2
      res6: Boolean = false
      

      Compare with

      scala> class Foo {
           |   def x = new java.lang.Integer(r.nextInt)
           | }
      defined class Foo
      
      scala> val f = new Foo
      f: Foo = Foo@db71e4
      
      scala> val u1 : f.x.type = f.x
      <console>:7: error: stable identifier required, but f.x found.
             val u1 : f.x.type = f.x
      

        Issue Links

          Activity

          Hide
          Iulian Dragos added a comment -

          I wonder why the `def x` is allowed to implement `val x` in the first line of your example..

          Show
          Iulian Dragos added a comment - I wonder why the `def x` is allowed to implement `val x` in the first line of your example..
          Hide
          Martin Odersky added a comment -

          Milestone next_bugfix deleted

          Show
          Martin Odersky added a comment - Milestone next_bugfix deleted
          Hide
          Paul Phillips added a comment -

          I see I didn't attach the source to the ticket way back when, but since it's still not fixed maybe I'd better. This is a soundness bug: here is CCE inducing source.

          // example mostly by DRMacIver, prettified a bit by paulp
          object o {
            trait X { type T; def t: T; def doStuff(t : T): Unit }
            var stuff: X = new X { 
              type T = String
              def t = "abc"
              def doStuff(t: String) = ()
            }
            
            // here is the hole: x will now be treated as a stable identifier
            // even though the actual implementation of it is a def, because
            // structural types don't perform the necessary override checks
            val evil: { val x: X } = new { def x = stuff }
            
            // t is a String
            val t = evil.x.t
            
            // replace "stable" id with method which thinks t is an Int
            stuff = new X {
               type T = Int
               def t = 21
               def doStuff(t : Int) = ()
            }
          
            // oh noes!
            def cce() = evil.x.doStuff(t)
          }
          
          Show
          Paul Phillips added a comment - I see I didn't attach the source to the ticket way back when, but since it's still not fixed maybe I'd better. This is a soundness bug: here is CCE inducing source. // example mostly by DRMacIver, prettified a bit by paulp object o { trait X { type T; def t: T; def doStuff(t : T): Unit } var stuff: X = new X { type T = String def t = "abc" def doStuff(t: String) = () } // here is the hole: x will now be treated as a stable identifier // even though the actual implementation of it is a def, because // structural types don't perform the necessary override checks val evil: { val x: X } = new { def x = stuff } // t is a String val t = evil.x.t // replace "stable" id with method which thinks t is an Int stuff = new X { type T = Int def t = 21 def doStuff(t : Int) = () } // oh noes! def cce() = evil.x.doStuff(t) }
          Hide
          Gilles Dubochet added a comment -

          Before closing, check that it also solves (presumed duplicate) SI-2079.

          Show
          Gilles Dubochet added a comment - Before closing, check that it also solves (presumed duplicate) SI-2079 .
          Hide
          Paul Phillips added a comment -

          Punting down the road just a littttle further.

          Show
          Paul Phillips added a comment - Punting down the road just a littttle further.
          Hide
          Simon Ochsenreither added a comment -

          This now crashes the compiler:

          import scala.util.Random
          val r = new Random()
          val y : { val x : java.lang.Integer  } = new { def x = new java.lang.Integer(r.nextInt) }
          val w1 : y.x.type = y.x //<---
          
          scala.MatchError: None (of class scala.None$$)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.scala$$tools$$nsc$$backend$$icode$$GenICode$$ICodePhase$$$$genStat(GenICode.scala:169)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$genStat$$1.apply(GenICode.scala:143)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$genStat$$1.apply(GenICode.scala:143)
          	at scala.collection.LinearSeqOptimized$$class.foldLeft(LinearSeqOptimized.scala:111)
          	at scala.collection.immutable.List.foldLeft(List.scala:45)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.genStat(GenICode.scala:143)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.scala$$tools$$nsc$$backend$$icode$$GenICode$$ICodePhase$$$$genLoad(GenICode.scala:1052)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:114)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$gen$$1.apply(GenICode.scala:69)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$gen$$1.apply(GenICode.scala:69)
          	at scala.collection.LinearSeqOptimized$$class.foreach(LinearSeqOptimized.scala:59)
          	at scala.collection.immutable.List.foreach(List.scala:45)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:69)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:136)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:88)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$gen$$1.apply(GenICode.scala:69)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$gen$$1.apply(GenICode.scala:69)
          	at scala.collection.LinearSeqOptimized$$class.foreach(LinearSeqOptimized.scala:59)
          	at scala.collection.immutable.List.foreach(List.scala:45)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:69)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:79)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:65)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.apply(GenICode.scala:61)
          	at scala.tools.nsc.Global$$GlobalPhase$$$$anonfun$$applyPhase$$1.apply(Global.scala:326)
          	at scala.tools.nsc.Global$$GlobalPhase$$$$anonfun$$applyPhase$$1.apply(Global.scala:326)
          	at scala.tools.nsc.reporters.Reporter.withSource(Reporter.scala:47)
          	at scala.tools.nsc.Global$$GlobalPhase.applyPhase(Global.scala:326)
          	at scala.tools.nsc.Global$$GlobalPhase$$$$anonfun$$run$$1.apply(Global.scala:294)
          	at scala.tools.nsc.Global$$GlobalPhase$$$$anonfun$$run$$1.apply(Global.scala:294)
          	at scala.collection.Iterator$$class.foreach(Iterator.scala:652)
          	at scala.collection.mutable.ListBuffer$$$$anon$$1.foreach(ListBuffer.scala:311)
          	at scala.tools.nsc.Global$$GlobalPhase.run(Global.scala:294)
          	at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.run(GenICode.scala:54)
          	at scala.tools.nsc.Global$$Run.compileSources(Global.scala:949)
          	at scala.tools.nsc.interpreter.IMain$$ReadEvalPrint.compileAndSaveRun(IMain.scala:636)
          	at scala.tools.nsc.interpreter.IMain$$ReadEvalPrint.compile(IMain.scala:604)
          	at scala.tools.nsc.interpreter.IMain$$Request.compile(IMain.scala:748)
          	at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:502)
          	at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:468)
          	at scala.tools.nsc.interpreter.ILoop.reallyInterpret$$1(ILoop.scala:724)
          	at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:775)
          	at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:677)
          	at scala.tools.nsc.interpreter.ILoop.processLine$$1(ILoop.scala:571)
          	at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:578)
          	at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:845)
          	at scala.tools.nsc.MainGenericRunner.runTarget$$1(MainGenericRunner.scala:67)
          	at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
          	at scala.tools.nsc.MainGenericRunner$$.main(MainGenericRunner.scala:89)
          	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
          

          The last code example too.

          Show
          Simon Ochsenreither added a comment - This now crashes the compiler: import scala.util.Random val r = new Random() val y : { val x : java.lang.Integer } = new { def x = new java.lang.Integer(r.nextInt) } val w1 : y.x.type = y.x //<--- scala.MatchError: None (of class scala.None$$) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.scala$$tools$$nsc$$backend$$icode$$GenICode$$ICodePhase$$$$genStat(GenICode.scala:169) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$genStat$$1.apply(GenICode.scala:143) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$genStat$$1.apply(GenICode.scala:143) at scala.collection.LinearSeqOptimized$$class.foldLeft(LinearSeqOptimized.scala:111) at scala.collection.immutable.List.foldLeft(List.scala:45) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.genStat(GenICode.scala:143) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.scala$$tools$$nsc$$backend$$icode$$GenICode$$ICodePhase$$$$genLoad(GenICode.scala:1052) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:114) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$gen$$1.apply(GenICode.scala:69) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$gen$$1.apply(GenICode.scala:69) at scala.collection.LinearSeqOptimized$$class.foreach(LinearSeqOptimized.scala:59) at scala.collection.immutable.List.foreach(List.scala:45) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:69) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:136) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:88) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$gen$$1.apply(GenICode.scala:69) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase$$$$anonfun$$gen$$1.apply(GenICode.scala:69) at scala.collection.LinearSeqOptimized$$class.foreach(LinearSeqOptimized.scala:59) at scala.collection.immutable.List.foreach(List.scala:45) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:69) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:79) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.gen(GenICode.scala:65) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.apply(GenICode.scala:61) at scala.tools.nsc.Global$$GlobalPhase$$$$anonfun$$applyPhase$$1.apply(Global.scala:326) at scala.tools.nsc.Global$$GlobalPhase$$$$anonfun$$applyPhase$$1.apply(Global.scala:326) at scala.tools.nsc.reporters.Reporter.withSource(Reporter.scala:47) at scala.tools.nsc.Global$$GlobalPhase.applyPhase(Global.scala:326) at scala.tools.nsc.Global$$GlobalPhase$$$$anonfun$$run$$1.apply(Global.scala:294) at scala.tools.nsc.Global$$GlobalPhase$$$$anonfun$$run$$1.apply(Global.scala:294) at scala.collection.Iterator$$class.foreach(Iterator.scala:652) at scala.collection.mutable.ListBuffer$$$$anon$$1.foreach(ListBuffer.scala:311) at scala.tools.nsc.Global$$GlobalPhase.run(Global.scala:294) at scala.tools.nsc.backend.icode.GenICode$$ICodePhase.run(GenICode.scala:54) at scala.tools.nsc.Global$$Run.compileSources(Global.scala:949) at scala.tools.nsc.interpreter.IMain$$ReadEvalPrint.compileAndSaveRun(IMain.scala:636) at scala.tools.nsc.interpreter.IMain$$ReadEvalPrint.compile(IMain.scala:604) at scala.tools.nsc.interpreter.IMain$$Request.compile(IMain.scala:748) at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:502) at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:468) at scala.tools.nsc.interpreter.ILoop.reallyInterpret$$1(ILoop.scala:724) at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:775) at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:677) at scala.tools.nsc.interpreter.ILoop.processLine$$1(ILoop.scala:571) at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:578) at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:845) at scala.tools.nsc.MainGenericRunner.runTarget$$1(MainGenericRunner.scala:67) at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80) at scala.tools.nsc.MainGenericRunner$$.main(MainGenericRunner.scala:89) at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) The last code example too.
          Hide
          Simon Ochsenreither added a comment -

          In 2.10.0.r25248-b20110707020221 things compile again ...

          Show
          Simon Ochsenreither added a comment - In 2.10.0.r25248-b20110707020221 things compile again ...
          Hide
          Paul Phillips added a comment -

          01f6ed8e22

          Show
          Paul Phillips added a comment - 01f6ed8e22

            People

            • Assignee:
              Paul Phillips
              Reporter:
              Geoffrey Alan Washburn
              TracCC:
              Paul Phillips, Simon Ochsenreither
            • Votes:
              2 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development