Skip to content

Commit bcda25a

Browse files
committed
Revert "Allow reflectiveCalls language import for structural types"
This reverts commit b1bf69c.
1 parent 61afaa0 commit bcda25a

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -792,11 +792,10 @@ trait Checking {
792792
sym.owner
793793
}
794794
val conversionOK =
795-
conv.is(Synthetic)
796-
|| sym.info.finalResultType.classSymbols.exists(_.isLinkedWith(conv.owner))
797-
|| defn.isPredefClass(conv.owner)
798-
|| conv.name == nme.reflectiveSelectable && conv.maybeOwner.maybeOwner.maybeOwner == defn.ScalaPackageClass
799-
|| conv.maybeOwner == defn.LanguageModule.moduleClass
795+
conv.is(Synthetic) ||
796+
sym.info.finalResultType.classSymbols.exists(_.isLinkedWith(conv.owner)) ||
797+
defn.isPredefClass(conv.owner) ||
798+
conv.name == nme.reflectiveSelectable && conv.maybeOwner.maybeOwner.maybeOwner == defn.ScalaPackageClass
800799
if (!conversionOK)
801800
checkFeature(nme.implicitConversions,
802801
i"Use of implicit conversion ${conv.showLocated}", NoSymbol, posd.sourcePos)

docs/docs/reference/changed-features/structural-types.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ Structural calls like this tend to be much slower than normal method calls. The
114114
**Note:** In Scala 2, Java reflection is the only mechanism available for structural types and it is automatically enabled without needing the
115115
`reflectiveSelectable` conversion. However, to warn against inefficient
116116
dispatch, Scala 2 requires a language import `import scala.language.reflectiveCalls`.
117-
For a limited time, Scala 3 will honor the same language import (but not the
118-
associated command-line option) to import `reflectiveSelectable` instead.
119117

120118
Before resorting to structural calls with Java reflection one should consider alternatives. For instance, sometimes a more a modular _and_ efficient architecture can be obtained using typeclasses.
121119

library/src/scalaShadowing/language.scala

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,23 @@ object language {
7575
*/
7676
implicit lazy val postfixOps: postfixOps = languageFeature.postfixOps
7777

78-
/** A forwarder to `scala.reflect.Selectable.reflectiveSelectable`.
79-
* Enables cross compilation between Scala 2 and Scala 3.
78+
/** Only where enabled, accesses to members of structural types that need
79+
* reflection are supported. Reminder: A structural type is a type of the form
80+
* `Parents { Decls }` where `Decls` contains declarations of new members that do
81+
* not override any member in `Parents`. To access one of these members, a
82+
* reflective call is needed.
83+
*
84+
* '''Why keep the feature?''' Structural types provide great flexibility because
85+
* they avoid the need to define inheritance hierarchies a priori. Besides,
86+
* their definition falls out quite naturally from Scala’s concept of type refinement.
87+
*
88+
* '''Why control it?''' Reflection is not available on all platforms. Popular tools
89+
* such as ProGuard have problems dealing with it. Even where reflection is available,
90+
* reflective dispatch can lead to surprising performance degradations.
8091
*
8192
* @group production
8293
*/
83-
implicit def reflectiveCalls(receiver: Any): reflect.Selectable =
84-
reflect.Selectable.reflectiveSelectable(receiver)
94+
implicit lazy val reflectiveCalls: reflectiveCalls = languageFeature.reflectiveCalls
8595

8696
/** Only where enabled, definitions of legacy implicit conversions and certain uses
8797
* of implicit conversions are allowed.

tests/run/i4496a.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//import scala.reflect.Selectable.reflectiveSelectable
2-
import scala.language.reflectiveCalls
1+
import scala.reflect.Selectable.reflectiveSelectable
32
class Foo1 { val a: Int = 10 }
43
class Foo2 { def a: Int = 10 }
54
class Foo3 { var a: Int = 10 }

tests/run/i4496b.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
//import scala.reflect.Selectable.reflectiveSelectable
2-
import scala.language.reflectiveCalls
1+
import scala.reflect.Selectable.reflectiveSelectable
32

43
trait Foo1 { val a: Int }
54
trait Foo2 { def a: Int }

0 commit comments

Comments
 (0)