Skip to content

Commit 6070cce

Browse files
authored
Merge pull request #1617 from dotty-staging/change-shadow-scala
Introduce scalaShadowing package
2 parents 3e76f3c + 69c9300 commit 6070cce

File tree

12 files changed

+233
-32
lines changed

12 files changed

+233
-32
lines changed

src/dotty/Pair.scala

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/dotty/Singleton.scala

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ class Definitions {
140140
lazy val Sys_errorR = SysPackage.moduleClass.requiredMethodRef(nme.error)
141141
def Sys_error(implicit ctx: Context) = Sys_errorR.symbol
142142

143+
/** The `scalaShadowing` package is used to safely modify classes and
144+
* objects in scala so that they can be used from dotty. They will
145+
* be visible as members of the `scala` package, replacing any objects
146+
* or classes with the same name. But their binary artifacts are
147+
* in `scalaShadowing` so they don't clash with the same-named `scala`
148+
* members at runtime.
149+
*/
150+
lazy val ScalaShadowingPackageVal = ctx.requiredPackage("scalaShadowing")
151+
lazy val ScalaShadowingPackageClass = ScalaShadowingPackageVal.moduleClass.asClass
152+
143153
/** Note: We cannot have same named methods defined in Object and Any (and AnyVal, for that matter)
144154
* because after erasure the Any and AnyVal references get remapped to the Object methods
145155
* which would result in a double binding assertion failure.
@@ -410,8 +420,6 @@ class Definitions {
410420
lazy val StringAdd_plusR = StringAddClass.requiredMethodRef(nme.raw.PLUS)
411421
def StringAdd_+(implicit ctx: Context) = StringAdd_plusR.symbol
412422

413-
lazy val PairType: TypeRef = ctx.requiredClassRef("dotty.Pair")
414-
def PairClass(implicit ctx: Context) = PairType.symbol.asClass
415423
lazy val PartialFunctionType: TypeRef = ctx.requiredClassRef("scala.PartialFunction")
416424
def PartialFunctionClass(implicit ctx: Context) = PartialFunctionType.symbol.asClass
417425
lazy val AbstractPartialFunctionType: TypeRef = ctx.requiredClassRef("scala.runtime.AbstractPartialFunction")
@@ -450,37 +458,37 @@ class Definitions {
450458
def StaticAnnotationClass(implicit ctx: Context) = StaticAnnotationType.symbol.asClass
451459

452460
// Annotation classes
453-
lazy val AliasAnnotType = ctx.requiredClassRef("dotty.annotation.internal.Alias")
461+
lazy val AliasAnnotType = ctx.requiredClassRef("scala.annotation.internal.Alias")
454462
def AliasAnnot(implicit ctx: Context) = AliasAnnotType.symbol.asClass
455-
lazy val AnnotationDefaultAnnotType = ctx.requiredClassRef("dotty.annotation.internal.AnnotationDefault")
463+
lazy val AnnotationDefaultAnnotType = ctx.requiredClassRef("scala.annotation.internal.AnnotationDefault")
456464
def AnnotationDefaultAnnot(implicit ctx: Context) = AnnotationDefaultAnnotType.symbol.asClass
457-
lazy val BodyAnnotType = ctx.requiredClassRef("dotty.annotation.internal.Body")
465+
lazy val BodyAnnotType = ctx.requiredClassRef("scala.annotation.internal.Body")
458466
def BodyAnnot(implicit ctx: Context) = BodyAnnotType.symbol.asClass
459-
lazy val ChildAnnotType = ctx.requiredClassRef("dotty.annotation.internal.Child")
467+
lazy val ChildAnnotType = ctx.requiredClassRef("scala.annotation.internal.Child")
460468
def ChildAnnot(implicit ctx: Context) = ChildAnnotType.symbol.asClass
461-
lazy val CovariantBetweenAnnotType = ctx.requiredClassRef("dotty.annotation.internal.CovariantBetween")
469+
lazy val CovariantBetweenAnnotType = ctx.requiredClassRef("scala.annotation.internal.CovariantBetween")
462470
def CovariantBetweenAnnot(implicit ctx: Context) = CovariantBetweenAnnotType.symbol.asClass
463-
lazy val ContravariantBetweenAnnotType = ctx.requiredClassRef("dotty.annotation.internal.ContravariantBetween")
471+
lazy val ContravariantBetweenAnnotType = ctx.requiredClassRef("scala.annotation.internal.ContravariantBetween")
464472
def ContravariantBetweenAnnot(implicit ctx: Context) = ContravariantBetweenAnnotType.symbol.asClass
465473
lazy val DeprecatedAnnotType = ctx.requiredClassRef("scala.deprecated")
466474
def DeprecatedAnnot(implicit ctx: Context) = DeprecatedAnnotType.symbol.asClass
467475
lazy val ImplicitNotFoundAnnotType = ctx.requiredClassRef("scala.annotation.implicitNotFound")
468476
def ImplicitNotFoundAnnot(implicit ctx: Context) = ImplicitNotFoundAnnotType.symbol.asClass
469477
lazy val InlineAnnotType = ctx.requiredClassRef("scala.inline")
470478
def InlineAnnot(implicit ctx: Context) = InlineAnnotType.symbol.asClass
471-
lazy val InlineParamAnnotType = ctx.requiredClassRef("dotty.annotation.internal.InlineParam")
479+
lazy val InlineParamAnnotType = ctx.requiredClassRef("scala.annotation.internal.InlineParam")
472480
def InlineParamAnnot(implicit ctx: Context) = InlineParamAnnotType.symbol.asClass
473-
lazy val InvariantBetweenAnnotType = ctx.requiredClassRef("dotty.annotation.internal.InvariantBetween")
481+
lazy val InvariantBetweenAnnotType = ctx.requiredClassRef("scala.annotation.internal.InvariantBetween")
474482
def InvariantBetweenAnnot(implicit ctx: Context) = InvariantBetweenAnnotType.symbol.asClass
475483
lazy val MigrationAnnotType = ctx.requiredClassRef("scala.annotation.migration")
476484
def MigrationAnnot(implicit ctx: Context) = MigrationAnnotType.symbol.asClass
477485
lazy val NativeAnnotType = ctx.requiredClassRef("scala.native")
478486
def NativeAnnot(implicit ctx: Context) = NativeAnnotType.symbol.asClass
479487
lazy val RemoteAnnotType = ctx.requiredClassRef("scala.remote")
480488
def RemoteAnnot(implicit ctx: Context) = RemoteAnnotType.symbol.asClass
481-
lazy val RepeatedAnnotType = ctx.requiredClassRef("dotty.annotation.internal.Repeated")
489+
lazy val RepeatedAnnotType = ctx.requiredClassRef("scala.annotation.internal.Repeated")
482490
def RepeatedAnnot(implicit ctx: Context) = RepeatedAnnotType.symbol.asClass
483-
lazy val SourceFileAnnotType = ctx.requiredClassRef("dotty.annotation.internal.SourceFile")
491+
lazy val SourceFileAnnotType = ctx.requiredClassRef("scala.annotation.internal.SourceFile")
484492
def SourceFileAnnot(implicit ctx: Context) = SourceFileAnnotType.symbol.asClass
485493
lazy val ScalaSignatureAnnotType = ctx.requiredClassRef("scala.reflect.ScalaSignature")
486494
def ScalaSignatureAnnot(implicit ctx: Context) = ScalaSignatureAnnotType.symbol.asClass
@@ -510,7 +518,7 @@ class Definitions {
510518
def UncheckedStableAnnot(implicit ctx: Context) = UncheckedStableAnnotType.symbol.asClass
511519
lazy val UncheckedVarianceAnnotType = ctx.requiredClassRef("scala.annotation.unchecked.uncheckedVariance")
512520
def UncheckedVarianceAnnot(implicit ctx: Context) = UncheckedVarianceAnnotType.symbol.asClass
513-
lazy val UnsafeNonvariantAnnotType = ctx.requiredClassRef("dotty.annotation.internal.UnsafeNonvariant")
521+
lazy val UnsafeNonvariantAnnotType = ctx.requiredClassRef("scala.annotation.internal.UnsafeNonvariant")
514522
def UnsafeNonvariantAnnot(implicit ctx: Context) = UnsafeNonvariantAnnotType.symbol.asClass
515523
lazy val VolatileAnnotType = ctx.requiredClassRef("scala.volatile")
516524
def VolatileAnnot(implicit ctx: Context) = VolatileAnnotType.symbol.asClass
@@ -781,6 +789,11 @@ class Definitions {
781789
if (!_isInitialized) {
782790
// force initialization of every symbol that is synthesized or hijacked by the compiler
783791
val forced = syntheticCoreClasses ++ syntheticCoreMethods ++ ScalaValueClasses()
792+
793+
// Enter all symbols from the scalaShadowing package in the scala package
794+
for (m <- ScalaShadowingPackageClass.info.decls)
795+
ScalaPackageClass.enter(m)
796+
784797
_isInitialized = true
785798
}
786799
}

src/dotty/annotation/internal/Alias.scala renamed to src/scala/annotation/internal/Alias.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.annotation.internal
1+
package scala.annotation.internal
22

33
import scala.annotation.Annotation
44

src/dotty/annotation/internal/AnnotationDefault.scala renamed to src/scala/annotation/internal/AnnotationDefault.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.annotation.internal
1+
package scala.annotation.internal
22

33
import scala.annotation.Annotation
44

src/dotty/annotation/internal/Body.scala renamed to src/scala/annotation/internal/Body.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.annotation.internal
1+
package scala.annotation.internal
22

33
import scala.annotation.Annotation
44

src/dotty/annotation/internal/Child.scala renamed to src/scala/annotation/internal/Child.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.annotation.internal
1+
package scala.annotation.internal
22

33
import scala.annotation.Annotation
44

@@ -9,7 +9,7 @@ import scala.annotation.Annotation
99
* case class B() extends A
1010
* case class C() extends A
1111
*
12-
* Then the class symbol `A` would carry the annotations
12+
* Then the class symbol `A` would carry the annotations
1313
* `@Child[Bref] @Child[Cref]` where `Bref`, `Cref` are TypeRefs
1414
* referring to the class symbols of `B` and `C`
1515
*/

src/dotty/annotation/internal/InlineParam.scala renamed to src/scala/annotation/internal/InlineParam.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.annotation.internal
1+
package scala.annotation.internal
22

33
import scala.annotation.Annotation
44

src/dotty/annotation/internal/Repeated.scala renamed to src/scala/annotation/internal/Repeated.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.annotation.internal
1+
package scala.annotation.internal
22

33
import scala.annotation.Annotation
44

src/dotty/annotation/internal/SourceFile.scala renamed to src/scala/annotation/internal/SourceFile.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.annotation.internal
1+
package scala.annotation.internal
22

33
import scala.annotation.Annotation
44

src/dotty/annotation/internal/UnsafeNonvariant.scala renamed to src/scala/annotation/internal/UnsafeNonvariant.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dotty.annotation.internal
1+
package scala.annotation.internal
22

33
import scala.annotation.Annotation
44

src/scalaShadowing/language.scala

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/* __ *\
2+
** ________ ___ / / ___ Scala API **
3+
** / __/ __// _ | / / / _ | (c) 2003-2015, LAMP/EPFL **
4+
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
5+
** /____/\___/_/ |_/____/_/ | | **
6+
** |/ **
7+
\* */
8+
package scalaShadowing
9+
10+
/**
11+
* The `scala.language` object controls the language features available to the programmer, as proposed in the
12+
* [[https://docs.google.com/document/d/1nlkvpoIRkx7at1qJEZafJwthZ3GeIklTFhqmXMvTX9Q/edit '''SIP-18 document''']].
13+
*
14+
* Each of these features has to be explicitly imported into the current scope to become available:
15+
* {{{
16+
* import language.postfixOps // or language._
17+
* List(1, 2, 3) reverse
18+
* }}}
19+
*
20+
* The language features are:
21+
* - [[dynamics `dynamics`]] enables defining calls rewriting using the [[scala.Dynamic `Dynamic`]] trait
22+
* - [[postfixOps `postfixOps`]] enables postfix operators
23+
* - [[reflectiveCalls `reflectiveCalls`]] enables using structural types
24+
* - [[implicitConversions `implicitConversions`]] enables defining implicit methods and members
25+
* - [[higherKinds `higherKinds`]] enables writing higher-kinded types
26+
* - [[existentials `existentials`]] enables writing existential types
27+
* - [[experimental `experimental`]] contains newer features that have not yet been tested in production
28+
*
29+
* and, for dotty:
30+
*
31+
* - [[Scala2 `Scala2`] backwards compatibility mode for Scala2
32+
* - [[noAtoTupling `noAutoTupling`]] disable auto-tupling
33+
*
34+
* @groupname production Language Features
35+
* @groupname experimental Experimental Language Features
36+
* @groupprio experimental 10
37+
*
38+
* Dotty-specific features come at the end.
39+
*
40+
* Note: Due to the more restricted language import mechanism in dotty (only
41+
* imports count, implicits are disregarded) we don't need the constructions
42+
* of the inherited language features. A simple object for each feature is
43+
* sufficient.
44+
*/
45+
object language {
46+
47+
import languageFeature._
48+
49+
/** Where enabled, direct or indirect subclasses of trait scala.Dynamic can
50+
* be defined. Unless dynamics is enabled, a definition of a class, trait,
51+
* or object that has Dynamic as a base trait is rejected. Dynamic member
52+
* selection of existing subclasses of trait Dynamic are unaffected;
53+
* they can be used anywhere.
54+
*
55+
* '''Why introduce the feature?''' To enable flexible DSLs and convenient interfacing
56+
* with dynamic languages.
57+
*
58+
* '''Why control it?''' Dynamic member selection can undermine static checkability
59+
* of programs. Furthermore, dynamic member selection often relies on reflection,
60+
* which is not available on all platforms.
61+
*
62+
* @group production
63+
*/
64+
@volatile implicit lazy val dynamics: dynamics = languageFeature.dynamics
65+
66+
/** Only where enabled, postfix operator notation `(expr op)` will be allowed.
67+
*
68+
* '''Why keep the feature?''' Several DSLs written in Scala need the notation.
69+
*
70+
* '''Why control it?''' Postfix operators interact poorly with semicolon inference.
71+
* Most programmers avoid them for this reason.
72+
*
73+
* @group production
74+
*/
75+
@volatile implicit lazy val postfixOps: postfixOps = languageFeature.postfixOps
76+
77+
/** Only where enabled, accesses to members of structural types that need
78+
* reflection are supported. Reminder: A structural type is a type of the form
79+
* `Parents { Decls }` where `Decls` contains declarations of new members that do
80+
* not override any member in `Parents`. To access one of these members, a
81+
* reflective call is needed.
82+
*
83+
* '''Why keep the feature?''' Structural types provide great flexibility because
84+
* they avoid the need to define inheritance hierarchies a priori. Besides,
85+
* their definition falls out quite naturally from Scala’s concept of type refinement.
86+
*
87+
* '''Why control it?''' Reflection is not available on all platforms. Popular tools
88+
* such as ProGuard have problems dealing with it. Even where reflection is available,
89+
* reflective dispatch can lead to surprising performance degradations.
90+
*
91+
* @group production
92+
*/
93+
@volatile implicit lazy val reflectiveCalls: reflectiveCalls = languageFeature.reflectiveCalls
94+
95+
/** Only where enabled, definitions of implicit conversions are allowed. An
96+
* implicit conversion is an implicit value of unary function type `A => B`,
97+
* or an implicit method that has in its first parameter section a single,
98+
* non-implicit parameter. Examples:
99+
*
100+
* {{{
101+
* implicit def stringToInt(s: String): Int = s.length
102+
* implicit val conv = (s: String) => s.length
103+
* implicit def listToX(xs: List[T])(implicit f: T => X): X = ...
104+
* }}}
105+
*
106+
* implicit values of other types are not affected, and neither are implicit
107+
* classes.
108+
*
109+
* '''Why keep the feature?''' Implicit conversions are central to many aspects
110+
* of Scala’s core libraries.
111+
*
112+
* '''Why control it?''' Implicit conversions are known to cause many pitfalls
113+
* if over-used. And there is a tendency to over-use them because they look
114+
* very powerful and their effects seem to be easy to understand. Also, in
115+
* most situations using implicit parameters leads to a better design than
116+
* implicit conversions.
117+
*
118+
* @group production
119+
*/
120+
@volatile implicit lazy val implicitConversions: implicitConversions = languageFeature.implicitConversions
121+
122+
/** Only where this flag is enabled, higher-kinded types can be written.
123+
*
124+
* '''Why keep the feature?''' Higher-kinded types enable the definition of very general
125+
* abstractions such as functor, monad, or arrow. A significant set of advanced
126+
* libraries relies on them. Higher-kinded types are also at the core of the
127+
* scala-virtualized effort to produce high-performance parallel DSLs through staging.
128+
*
129+
* '''Why control it?''' Higher kinded types in Scala lead to a Turing-complete
130+
* type system, where compiler termination is no longer guaranteed. They tend
131+
* to be useful mostly for type-level computation and for highly generic design
132+
* patterns. The level of abstraction implied by these design patterns is often
133+
* a barrier to understanding for newcomers to a Scala codebase. Some syntactic
134+
* aspects of higher-kinded types are hard to understand for the uninitiated and
135+
* type inference is less effective for them than for normal types. Because we are
136+
* not completely happy with them yet, it is possible that some aspects of
137+
* higher-kinded types will change in future versions of Scala. So an explicit
138+
* enabling also serves as a warning that code involving higher-kinded types
139+
* might have to be slightly revised in the future.
140+
*
141+
* @group production
142+
*/
143+
@volatile implicit lazy val higherKinds: higherKinds = languageFeature.higherKinds
144+
145+
/** Only where enabled, existential types that cannot be expressed as wildcard
146+
* types can be written and are allowed in inferred types of values or return
147+
* types of methods. Existential types with wildcard type syntax such as `List[_]`,
148+
* or `Map[String, _]` are not affected.
149+
*
150+
* '''Why keep the feature?''' Existential types are needed to make sense of Java’s wildcard
151+
* types and raw types and the erased types of run-time values.
152+
*
153+
* '''Why control it?''' Having complex existential types in a code base usually makes
154+
* application code very brittle, with a tendency to produce type errors with
155+
* obscure error messages. Therefore, going overboard with existential types
156+
* is generally perceived not to be a good idea. Also, complicated existential types
157+
* might be no longer supported in a future simplification of the language.
158+
*
159+
* @group production
160+
*/
161+
@volatile implicit lazy val existentials: existentials = languageFeature.existentials
162+
163+
/** The experimental object contains features that have been recently added but have not
164+
* been thoroughly tested in production yet.
165+
*
166+
* Experimental features '''may undergo API changes''' in future releases, so production
167+
* code should not rely on them.
168+
*
169+
* Programmers are encouraged to try out experimental features and
170+
* [[http://issues.scala-lang.org report any bugs or API inconsistencies]]
171+
* they encounter so they can be improved in future releases.
172+
*
173+
* @group experimental
174+
*/
175+
object experimental {
176+
177+
import languageFeature.experimental._
178+
179+
/** Where enabled, macro definitions are allowed. Macro implementations and
180+
* macro applications are unaffected; they can be used anywhere.
181+
*
182+
* '''Why introduce the feature?''' Macros promise to make the language more regular,
183+
* replacing ad-hoc language constructs with a general powerful abstraction
184+
* capability that can express them. Macros are also a more disciplined and
185+
* powerful replacement for compiler plugins.
186+
*
187+
* '''Why control it?''' For their very power, macros can lead to code that is hard
188+
* to debug and understand.
189+
*/
190+
@volatile implicit lazy val macros: macros = languageFeature.experimental.macros
191+
}
192+
193+
/** Where imported, a backwards compatibility mode for Scala2 is enabled */
194+
object Scala2
195+
196+
/** Where imported, auto-tupling is disabled */
197+
object noAutoTupling
198+
}

0 commit comments

Comments
 (0)