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

views inference on non-accessible selection

    Details

      Description

      Spec:

      Views are applied in three situations.
      [...]
      2. In a selection e.m with e of type T, if the selector m does not denote a member of T.
      In this case, a view v is searched [...]

      However, the implementation also searches for an implicit conversion when m denotes a
      non-accessible member of T:

        scala> class A { protected def f = 101 }
        scala> class B { def f = "duckflow" }
        scala> object T {
             |   implicit def a2b(a: A): B = new B
             | 
             |   def t = (new A).f
             | }
        scala> T.t
        res0: String = duckflow
      

      Even worse, the same happens for constructors! An expression "new T()" is represented
      as a tree Apply(Selelct(New(T), <init>), ())

      If the constructor is private, an conversion to "?

      {def <init>: ?}

      " is searched! This generates
      invalid trees and crashes later phases, e.g. NamesDefaults, SI-5231, but also GenICode,
      as in the following example:

        scala> class A private () { }
        scala> class B { }
        scala> object T {
             |   implicit def a2b(a: A): B = null
             |   def t = new A()
             | }
        
        Unexpected New(TypeTree(class $read$$iw$$iw$A)/$line7.$read$$iw$$iw$A) received in icode.
        [...]
          at scala.reflect.internal.SymbolTable.abort(SymbolTable.scala:43)
          at scala.tools.nsc.Global.abort(Global.scala:228)
          at scala.tools.nsc.interpreter.IMain$$anon$1.scala$tools$nsc$interpreter$ReplGlobal$$super$abort(IMain.scala:277)
          at scala.tools.nsc.interpreter.ReplGlobal$class.abort(ReplGlobal.scala:21)
          at scala.tools.nsc.interpreter.IMain$$anon$1.abort(IMain.scala:277)
          at scala.tools.nsc.backend.icode.GenICode$ICodePhase.scala$tools$nsc$backend$icode$GenICode$ICodePhase$$genLoad(GenICode.scala:708)
          at scala.tools.nsc.backend.icode.GenICode$ICodePhase$$anonfun$genLoadArguments$1.apply(GenICode.scala:1201)
          at scala.tools.nsc.backend.icode.GenICode$ICodePhase$$anonfun$genLoadArguments$1.apply(GenICode.scala:1199)
          at scala.collection.LinearSeqOptimized$class.foldLeft(LinearSeqOptimized.scala:111)
      

        Issue Links

          Activity

          Hide
          Lukas Rytz added a comment -

          two more examples that crash the compiler in different places, probably all due to this bug. the first one is actually SI-5231:

          object T {
            def m = new ConfigBuilder()
            implicit def build( cb: ConfigBuilder ) : Config = null
          }
          
          class Config(x: Int)
          
          class ConfigBuilder private ()  { }
          

          Second one, crash in Symbols.info

          
          object Config {
            def apply() : ConfigBuilder = new ConfigBuilder()
            implicit def build( cb: ConfigBuilder ) : Config = cb.build
          }
          
          final class Config private[Client]( val clientID: Int )
          
          final class ConfigBuilder {
            var clientID: Int = 0
            def build : Config = new Config( clientID )
          }
          
          Show
          Lukas Rytz added a comment - two more examples that crash the compiler in different places, probably all due to this bug. the first one is actually SI-5231 : object T { def m = new ConfigBuilder() implicit def build( cb: ConfigBuilder ) : Config = null } class Config(x: Int) class ConfigBuilder private () { } Second one, crash in Symbols.info object Config { def apply() : ConfigBuilder = new ConfigBuilder() implicit def build( cb: ConfigBuilder ) : Config = cb.build } final class Config private[Client]( val clientID: Int ) final class ConfigBuilder { var clientID: Int = 0 def build : Config = new Config( clientID ) }
          Hide
          Paul Phillips added a comment -

          Not the same bug, but reminiscent of SI-4645, SI-4685, SI-4707, SI-4987, SI-5058.

          Show
          Paul Phillips added a comment - Not the same bug, but reminiscent of SI-4645 , SI-4685 , SI-4707 , SI-4987 , SI-5058 .
          Hide
          Lukas Rytz added a comment -

          update spec. fix the constructor case (don't search a view there). `adaptToMember`.

          Show
          Lukas Rytz added a comment - update spec. fix the constructor case (don't search a view there). `adaptToMember`.
          Hide
          Lukas Rytz added a comment -

          `hasMethodMatching` should always be false for `<init>`

          Show
          Lukas Rytz added a comment - `hasMethodMatching` should always be false for `<init>`
          Hide
          Lukas Rytz added a comment -
          Show
          Lukas Rytz added a comment - fixed in https://github.com/scala/scala/pull/1051 spec clarification in https://github.com/scala/scala-dist/pull/50
          Hide
          Jason Zaugg added a comment -

          Spec update merged.

          Show
          Jason Zaugg added a comment - Spec update merged.

            People

            • Assignee:
              Lukas Rytz
              Reporter:
              Lukas Rytz
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development