-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathFeature.scala
238 lines (200 loc) · 9.44 KB
/
Feature.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package dotty.tools
package dotc
package config
import core.*
import Contexts.*, Symbols.*, Names.*
import StdNames.nme
import Decorators.*
import util.{SrcPos, NoSourcePosition}
import SourceVersion.*
import reporting.Message
import NameKinds.QualifiedName
import Annotations.ExperimentalAnnotation
import Settings.Setting.ChoiceWithHelp
object Feature:
def experimental(str: PreName): TermName =
QualifiedName(nme.experimental, str.toTermName)
private def deprecated(str: PreName): TermName =
QualifiedName(nme.deprecated, str.toTermName)
private val namedTypeArguments = experimental("namedTypeArguments")
private val genericNumberLiterals = experimental("genericNumberLiterals")
val scala2macros = experimental("macros")
val dependent = experimental("dependent")
val erasedDefinitions = experimental("erasedDefinitions")
val symbolLiterals = deprecated("symbolLiterals")
val fewerBraces = experimental("fewerBraces")
val saferExceptions = experimental("saferExceptions")
val clauseInterleaving = experimental("clauseInterleaving")
val pureFunctions = experimental("pureFunctions")
val captureChecking = experimental("captureChecking")
val into = experimental("into")
val namedTuples = experimental("namedTuples")
val modularity = experimental("modularity")
val betterMatchTypeExtractors = experimental("betterMatchTypeExtractors")
def experimentalAutoEnableFeatures(using Context): List[TermName] =
defn.languageExperimentalFeatures
.map(sym => experimental(sym.name))
.filterNot(_ == captureChecking) // TODO is this correct?
val values = List(
(nme.help, "Display all available features"),
(nme.noAutoTupling, "Disable automatic tupling"),
(nme.dynamics, "Allow direct or indirect subclasses of scala.Dynamic"),
(nme.unsafeNulls, "Enable unsafe nulls for explicit nulls"),
(nme.postfixOps, "Allow postfix operators (not recommended)"),
(nme.strictEquality, "Enable strict equality (disable canEqualAny)"),
(nme.implicitConversions, "Allow implicit conversions without warnings"),
(nme.adhocExtensions, "Allow ad-hoc extension methods"),
(namedTypeArguments, "Allow named type arguments"),
(genericNumberLiterals, "Allow generic number literals"),
(scala2macros, "Allow Scala 2 macros"),
(dependent, "Allow dependent method types"),
(erasedDefinitions, "Allow erased definitions"),
(symbolLiterals, "Allow symbol literals"),
(fewerBraces, "Enable support for using indentation for arguments"),
(saferExceptions, "Enable safer exceptions"),
(clauseInterleaving, "Enable clause interleaving"),
(pureFunctions, "Enable pure functions for capture checking"),
(captureChecking, "Enable experimental capture checking"),
(into, "Allow into modifier on parameter types"),
(namedTuples, "Allow named tuples"),
(modularity, "Enable experimental modularity features"),
(betterMatchTypeExtractors, "Enable better match type extractors")
)
// legacy language features from Scala 2 that are no longer supported.
val legacyFeatures = List(
"higherKinds",
"existentials",
"reflectiveCalls"
)
private def enabledLanguageFeaturesBySetting(using Context): List[String] =
ctx.settings.language.value.asInstanceOf
/** Is `feature` enabled by by a command-line setting? The enabling setting is
*
* -language:<prefix>feature
*
* where <prefix> is the fully qualified name of `owner`, followed by a ".",
* but subtracting the prefix `scala.language.` at the front.
*/
def enabledBySetting(feature: TermName)(using Context): Boolean =
enabledLanguageFeaturesBySetting.contains(feature.toString)
/** Is `feature` enabled by by an import? This is the case if the feature
* is imported by a named import
*
* import owner.feature
*
* and there is no visible nested import that excludes the feature, as in
*
* import owner.{ feature => _ }
*/
def enabledByImport(feature: TermName)(using Context): Boolean =
//atPhase(typerPhase) {
val info = ctx.importInfo
info != null && info.featureImported(feature)
//}
/** Is `feature` enabled by either a command line setting or an import?
* @param feature The name of the feature
* @param owner The prefix symbol (nested in `scala.language`) where the
* feature is defined.
*/
def enabled(feature: TermName)(using Context): Boolean =
enabledBySetting(feature) || enabledByImport(feature)
/** Is auto-tupling enabled? */
def autoTuplingEnabled(using Context): Boolean = !enabled(nme.noAutoTupling)
def dynamicsEnabled(using Context): Boolean = enabled(nme.dynamics)
def dependentEnabled(using Context) = enabled(dependent)
def namedTypeArgsEnabled(using Context) = enabled(namedTypeArguments)
def clauseInterleavingEnabled(using Context) = enabled(clauseInterleaving)
def genericNumberLiteralsEnabled(using Context) = enabled(genericNumberLiterals)
def scala2ExperimentalMacroEnabled(using Context) = enabled(scala2macros)
def betterMatchTypeExtractorsEnabled(using Context) = enabled(betterMatchTypeExtractors)
/** Is pureFunctions enabled for this compilation unit? */
def pureFunsEnabled(using Context) =
enabledBySetting(pureFunctions)
|| ctx.compilationUnit.knowsPureFuns
|| ccEnabled
/** Is captureChecking enabled for this compilation unit? */
def ccEnabled(using Context) =
enabledBySetting(captureChecking)
|| ctx.compilationUnit.needsCaptureChecking
/** Is pureFunctions enabled for any of the currently compiled compilation units? */
def pureFunsEnabledSomewhere(using Context) =
enabledBySetting(pureFunctions)
|| ctx.run != null && ctx.run.nn.pureFunsImportEncountered
|| ccEnabledSomewhere
/** Is captureChecking enabled for any of the currently compiled compilation units? */
def ccEnabledSomewhere(using Context) =
if ctx.run != null then ctx.run.nn.ccEnabledSomewhere
else enabledBySetting(captureChecking)
def sourceVersionSetting(using Context): SourceVersion =
SourceVersion.valueOf(ctx.settings.source.value)
def sourceVersion(using Context): SourceVersion =
ctx.compilationUnit.sourceVersion match
case Some(v) => v
case none => sourceVersionSetting
def migrateTo3(using Context): Boolean =
sourceVersion == `3.0-migration`
def fewerBracesEnabled(using Context) =
sourceVersion.isAtLeast(`3.3`) || enabled(fewerBraces)
/** If current source migrates to `version`, issue given warning message
* and return `true`, otherwise return `false`.
*/
def warnOnMigration(msg: Message, pos: SrcPos, version: SourceVersion)(using Context): Boolean =
if sourceVersion.isMigrating && sourceVersion.stable == version
|| (version == `3.0` || version == `3.1`) && migrateTo3
then
report.migrationWarning(msg, pos)
true
else
false
def checkExperimentalFeature(which: String, srcPos: SrcPos, note: => String = "")(using Context) =
if !isExperimentalEnabled then
report.error(experimentalUseSite(which) + note, srcPos)
private def ccException(sym: Symbol)(using Context): Boolean =
ccEnabled && defn.ccExperimental.contains(sym)
def checkExperimentalDef(sym: Symbol, srcPos: SrcPos)(using Context) =
val experimentalSym =
if sym.hasAnnotation(defn.ExperimentalAnnot) then sym
else if sym.owner.hasAnnotation(defn.ExperimentalAnnot) then sym.owner
else NoSymbol
if !isExperimentalEnabled && !ccException(experimentalSym) then
val msg =
experimentalSym.getAnnotation(defn.ExperimentalAnnot).map {
case ExperimentalAnnotation(msg) if msg.nonEmpty => s": $msg"
case _ => ""
}.getOrElse("")
val markedExperimental =
if experimentalSym.exists
then i"$experimentalSym is marked @experimental$msg"
else i"$sym inherits @experimental$msg"
report.error(markedExperimental + "\n\n" + experimentalUseSite("definition"), srcPos)
private def experimentalUseSite(which: String): String =
s"""Experimental $which may only be used under experimental mode:
| 1. in a definition marked as @experimental, or
| 2. an experimental feature is imported at the package level, or
| 3. compiling with the -experimental compiler flag.
|""".stripMargin
def isExperimentalEnabled(using Context): Boolean =
ctx.settings.experimental.value ||
experimentalAutoEnableFeatures.exists(enabled)
def experimentalEnabledByLanguageSetting(using Context): Option[TermName] =
experimentalAutoEnableFeatures.find(enabledBySetting)
def isExperimentalEnabledByImport(using Context): Boolean =
experimentalAutoEnableFeatures.exists(enabledByImport)
/** Handle language import `import language.<prefix>.<imported>` if it is one
* of the global imports `pureFunctions` or `captureChecking`. In this case
* make the compilation unit's and current run's fields accordingly.
* @return true iff import that was handled
*/
def handleGlobalLanguageImport(prefix: TermName, imported: Name)(using Context): Boolean =
val fullFeatureName = QualifiedName(prefix, imported.asTermName)
if fullFeatureName == pureFunctions then
ctx.compilationUnit.knowsPureFuns = true
if ctx.run != null then ctx.run.nn.pureFunsImportEncountered = true
true
else if fullFeatureName == captureChecking then
ctx.compilationUnit.needsCaptureChecking = true
if ctx.run != null then ctx.run.nn.ccEnabledSomewhere = true
true
else
false
end Feature