Skip to content

Commit 4f478a7

Browse files
committed
chore: add scala.language.experimental.unqualifiedSelectors
1 parent ae05ac1 commit 4f478a7

File tree

8 files changed

+24
-4
lines changed

8 files changed

+24
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ object Feature:
3838
val quotedPatternsWithPolymorphicFunctions = experimental("quotedPatternsWithPolymorphicFunctions")
3939
val packageObjectValues = experimental("packageObjectValues")
4040
val subCases = experimental("subCases")
41+
val unqualifiedSelectors = experimental("unqualifiedSelectors")
4142

4243
def experimentalAutoEnableFeatures(using Context): List[TermName] =
4344
defn.languageExperimentalFeatures
@@ -66,6 +67,7 @@ object Feature:
6667
(into, "Allow into modifier on parameter types"),
6768
(modularity, "Enable experimental modularity features"),
6869
(packageObjectValues, "Enable experimental package objects as values"),
70+
(unqualifiedSelectors, "Enable unqualified selectors for expressions and patterns")
6971
)
7072

7173
// legacy language features from Scala 2 that are no longer supported.

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,7 +2802,7 @@ object Parsers {
28022802
case MACRO =>
28032803
val start = in.skipToken()
28042804
MacroTree(simpleExpr(Location.ElseWhere))
2805-
case DOT =>
2805+
case DOT if in.featureEnabled(Feature.unqualifiedSelectors) =>
28062806
accept(DOT)
28072807
selector(EmptyTree)
28082808
case _ =>
@@ -3308,7 +3308,7 @@ object Parsers {
33083308
simpleExpr(Location.InPattern)
33093309
case XMLSTART =>
33103310
xmlLiteralPattern()
3311-
case DOT =>
3311+
case DOT if in.featureEnabled(Feature.unqualifiedSelectors) =>
33123312
accept(DOT)
33133313
simplePatternRest(selector(EmptyTree))
33143314
case GIVEN =>

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Tokens.*
1515
import scala.annotation.{switch, tailrec}
1616
import scala.collection.mutable
1717
import scala.collection.immutable.SortedMap
18+
import scala.collection.immutable.BitSet
1819
import rewrites.Rewrites.patch
1920
import config.Feature
2021
import config.Feature.{migrateTo3, sourceVersion}
@@ -1241,7 +1242,10 @@ object Scanners {
12411242
if migrateTo3 then canStartStatTokens2 else canStartStatTokens3
12421243

12431244
def canStartExprTokens =
1244-
if migrateTo3 then canStartExprTokens2 else canStartExprTokens3
1245+
if migrateTo3 then
1246+
canStartExprTokens2
1247+
else
1248+
canStartExprTokens3 | (if featureEnabled(Feature.unqualifiedSelectors) then BitSet(DOT) else BitSet.empty)
12451249

12461250
// Literals -----------------------------------------------------------------
12471251

compiler/src/dotty/tools/dotc/parsing/Tokens.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ object Tokens extends TokensCommon {
227227
| BitSet(QUOTE, NEW)
228228

229229
final val canStartExprTokens3: TokenSet =
230-
canStartInfixExprTokens | BitSet(INDENT, IF, WHILE, FOR, TRY, THROW, DOT)
230+
canStartInfixExprTokens | BitSet(INDENT, IF, WHILE, FOR, TRY, THROW)
231231

232232
final val canStartExprTokens2: TokenSet = canStartExprTokens3 | BitSet(DO)
233233

library/src/scala/language.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,11 @@ object language {
355355
@compileTimeOnly("`subCases` can only be used at compile time in import statements")
356356
object subCases
357357

358+
/** Experimental support for unqualified selectors
359+
*/
360+
@compileTimeOnly("`unqualifiedSelectors` can only be used at compile time in import statements")
361+
object unqualifiedSelectors
362+
358363
}
359364

360365
/** The deprecated object contains features that are no longer officially suypported in Scala.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ object language:
161161
*/
162162
@compileTimeOnly("`subCases` can only be used at compile time in import statements")
163163
object subCases
164+
165+
/** Experimental support for unqualified selectors
166+
*/
167+
@compileTimeOnly("`unqualifiedSelectors` can only be used at compile time in import statements")
168+
object unqualifiedSelectors
164169
end experimental
165170

166171
/** The deprecated object contains features that are no longer officially suypported in Scala.

tests/pos/unqualified-selector-enum.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import scala.language.experimental.unqualifiedSelectors
2+
13
enum Opt[+T]:
24
case none extends Opt[Nothing]
35
case some[T](value: T) extends Opt[T]

tests/pos/unqualified-selector-object.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import scala.language.experimental.unqualifiedSelectors
2+
13
class Opt[+T](v: T)
24

35
object Opt:

0 commit comments

Comments
 (0)