Skip to content

Commit b1bf69c

Browse files
committed
Allow reflectiveCalls language import for structural types
The reflectiveCalls member of `scala.language` is now a forwarder to `scala.reflect.Selectable.reflectiveSelectable`.
1 parent aaa5402 commit b1bf69c

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,10 +792,11 @@ 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
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
799800
if (!conversionOK)
800801
checkFeature(nme.implicitConversions,
801802
i"Use of implicit conversion ${conv.showLocated}", NoSymbol, posd.sourcePos)

library/src/scalaShadowing/language.scala

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

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.
78+
/** A forwarder to `scala.reflect.Selectable.reflectiveSelectable`.
79+
* Enables cross compilation between Scala 2 and Scala 3.
9180
*
9281
* @group production
9382
*/
94-
implicit lazy val reflectiveCalls: reflectiveCalls = languageFeature.reflectiveCalls
83+
implicit def reflectiveCalls(receiver: Any): reflect.Selectable =
84+
reflect.Selectable.reflectiveSelectable(receiver)
9585

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

tests/run/i4496a.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import scala.reflect.Selectable.reflectiveSelectable
1+
//import scala.reflect.Selectable.reflectiveSelectable
2+
import scala.language.reflectiveCalls
23
class Foo1 { val a: Int = 10 }
34
class Foo2 { def a: Int = 10 }
45
class Foo3 { var a: Int = 10 }

tests/run/i4496b.scala

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

34
trait Foo1 { val a: Int }
45
trait Foo2 { def a: Int }

0 commit comments

Comments
 (0)