Skip to content

Commit abbd01c

Browse files
committed
Put changes under experimental language import
As long as this is not SIP approved, we need to put this under experimental.
1 parent 4e028bf commit abbd01c

File tree

9 files changed

+63
-16
lines changed

9 files changed

+63
-16
lines changed

compiler/src/dotty/tools/dotc/config/Feature.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ object Feature:
2929
val fewerBraces = experimental("fewerBraces")
3030
val saferExceptions = experimental("saferExceptions")
3131
val clauseInterleaving = experimental("clauseInterleaving")
32+
val relaxedExtensionImports = experimental("relaxedExtensionImports")
3233
val pureFunctions = experimental("pureFunctions")
3334
val captureChecking = experimental("captureChecking")
3435
val into = experimental("into")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
260260
then
261261
altImports.uncheckedNN += altImp
262262

263-
if altImports != null && ctx.isImportContext then
263+
if Feature.enabled(Feature.relaxedExtensionImports) && altImports != null && ctx.isImportContext then
264264
val curImport = ctx.importInfo.uncheckedNN
265265
namedImportRef(curImport) match
266266
case altImp: TermRef =>

docs/_docs/reference/contextual/extension-methods.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ The following two rewritings are tried in order:
252252
If only one import leads to an expansion that typechecks without errors, pick
253253
that expansion. If there are several such imports, but only one import which is
254254
not a wildcard import, pick the expansion from that import. Otherwise, report
255-
an ambiguous reference error.
255+
an ambiguous reference error.
256+
257+
**Note**: This relaxation is currently enabled only under the `experimental.relaxedExtensionImports` language import.
256258

257259
2. If the first rewriting does not typecheck with expected type `T`,
258260
and there is an extension method `m` in some eligible object `o`, the selection is rewritten to `o.m[Ts](e)`. An object `o` is _eligible_ if

library/src/scala/runtime/stdLibPatches/language.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ object language:
6969
@compileTimeOnly("`clauseInterleaving` can only be used at compile time in import statements")
7070
object clauseInterleaving
7171

72+
/** Adds support for relaxed imports of extension methods.
73+
* Extension methods with the same name can be imported from several places.
74+
*
75+
* @see [[http://dotty.epfl.ch/docs/reference/contextual/extension-methods]]
76+
*/
77+
@compileTimeOnly("`relaxedExtensionImports` can only be used at compile time in import statements")
78+
object relaxedExtensionImports
79+
7280
/** Experimental support for pure function type syntax
7381
*
7482
* @see [[https://dotty.epfl.ch/docs/reference/experimental/purefuns]]

tests/neg/i13558.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package testcode
2+
3+
class A
4+
5+
class B
6+
7+
object ExtensionA {
8+
extension (self: A) {
9+
def id = "A"
10+
}
11+
}
12+
object ExtensionB {
13+
extension (self: B) {
14+
def id = "B"
15+
}
16+
}
17+
18+
object Main {
19+
def main1(args: Array[String]): Unit = {
20+
import ExtensionB._
21+
import ExtensionA._
22+
val a = A()
23+
println(a.id) // error
24+
}
25+
def main2(args: Array[String]): Unit = {
26+
import ExtensionA._
27+
import ExtensionB._
28+
val a = A()
29+
println(a.id) // error
30+
}
31+
}

tests/neg/i16920.check

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
-- [E008] Not Found Error: tests/neg/i16920.scala:18:11 ----------------------------------------------------------------
2-
18 | "five".wow // error
1+
-- [E008] Not Found Error: tests/neg/i16920.scala:20:11 ----------------------------------------------------------------
2+
20 | "five".wow // error
33
| ^^^^^^^^^^
44
| value wow is not a member of String.
55
| An extension method was tried, but could not be fully constructed:
@@ -10,8 +10,8 @@
1010
|
1111
| Found: ("five" : String)
1212
| Required: Int
13-
-- [E008] Not Found Error: tests/neg/i16920.scala:26:6 -----------------------------------------------------------------
14-
26 | 5.wow // error
13+
-- [E008] Not Found Error: tests/neg/i16920.scala:28:6 -----------------------------------------------------------------
14+
28 | 5.wow // error
1515
| ^^^^^
1616
| value wow is not a member of Int.
1717
| An extension method was tried, but could not be fully constructed:
@@ -22,8 +22,8 @@
2222
|
2323
| Found: (5 : Int)
2424
| Required: Boolean
25-
-- [E008] Not Found Error: tests/neg/i16920.scala:27:11 ----------------------------------------------------------------
26-
27 | "five".wow // error
25+
-- [E008] Not Found Error: tests/neg/i16920.scala:29:11 ----------------------------------------------------------------
26+
29 | "five".wow // error
2727
| ^^^^^^^^^^
2828
| value wow is not a member of String.
2929
| An extension method was tried, but could not be fully constructed:
@@ -34,8 +34,8 @@
3434
|
3535
| Found: ("five" : String)
3636
| Required: Boolean
37-
-- [E008] Not Found Error: tests/neg/i16920.scala:34:6 -----------------------------------------------------------------
38-
34 | 5.wow // error
37+
-- [E008] Not Found Error: tests/neg/i16920.scala:36:6 -----------------------------------------------------------------
38+
36 | 5.wow // error
3939
| ^^^^^
4040
| value wow is not a member of Int.
4141
| An extension method was tried, but could not be fully constructed:
@@ -48,8 +48,8 @@
4848
| both Three.wow(5)
4949
| and Two.wow(5)
5050
| are possible expansions of 5.wow
51-
-- [E008] Not Found Error: tests/neg/i16920.scala:42:11 ----------------------------------------------------------------
52-
42 | "five".wow // error
51+
-- [E008] Not Found Error: tests/neg/i16920.scala:44:11 ----------------------------------------------------------------
52+
44 | "five".wow // error
5353
| ^^^^^^^^^^
5454
| value wow is not a member of String.
5555
| An extension method was tried, but could not be fully constructed:
@@ -60,8 +60,8 @@
6060
|
6161
| Found: ("five" : String)
6262
| Required: Int
63-
-- [E008] Not Found Error: tests/neg/i16920.scala:49:11 ----------------------------------------------------------------
64-
49 | "five".wow // error
63+
-- [E008] Not Found Error: tests/neg/i16920.scala:51:11 ----------------------------------------------------------------
64+
51 | "five".wow // error
6565
| ^^^^^^^^^^
6666
| value wow is not a member of String.
6767
| An extension method was tried, but could not be fully constructed:
@@ -72,8 +72,8 @@
7272
|
7373
| Found: ("five" : String)
7474
| Required: Int
75-
-- [E008] Not Found Error: tests/neg/i16920.scala:56:6 -----------------------------------------------------------------
76-
56 | 5.wow // error
75+
-- [E008] Not Found Error: tests/neg/i16920.scala:58:6 -----------------------------------------------------------------
76+
58 | 5.wow // error
7777
| ^^^^^
7878
| value wow is not a member of Int.
7979
| An extension method was tried, but could not be fully constructed:

tests/neg/i16920.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import language.experimental.relaxedExtensionImports
2+
13
object One:
24
extension (s: String)
35
def wow: Unit = println(s)

tests/pos/i13558.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package testcode
2+
import language.experimental.relaxedExtensionImports
23

34
class A
45

tests/pos/i16920.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import language.experimental.relaxedExtensionImports
2+
13
object One:
24
extension (s: String)
35
def wow: Unit = println(s)

0 commit comments

Comments
 (0)