-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathFeature.scala
More file actions
311 lines (265 loc) · 12.6 KB
/
Feature.scala
File metadata and controls
311 lines (265 loc) · 12.6 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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 Annotations.PreviewAnnotation
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 strictEqualityPatternMatching = experimental("strictEqualityPatternMatching")
val symbolLiterals = deprecated("symbolLiterals")
val saferExceptions = experimental("saferExceptions")
val pureFunctions = experimental("pureFunctions")
val captureChecking = experimental("captureChecking")
val separationChecking = experimental("separationChecking")
val into = experimental("into")
val modularity = experimental("modularity")
val quotedPatternsWithPolymorphicFunctions = experimental("quotedPatternsWithPolymorphicFunctions")
val packageObjectValues = experimental("packageObjectValues")
val multiSpreads = experimental("multiSpreads")
val subCases = experimental("subCases")
val relaxedLambdaSyntax = experimental("relaxedLambdaSyntax")
val safe = experimental("safe")
val nonViralExperimentalFeatures: Set[TermName] =
Set(captureChecking, separationChecking, safe)
/** Experimental language imports that imply that the importing unit
* is experimental.
*/
def experimentalAutoEnableFeatures(using Context): List[TermName] =
defn.languageExperimentalFeatures
.map(sym => experimental(sym.name))
.filterNot(nonViralExperimentalFeatures.contains(_))
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"),
(strictEqualityPatternMatching, "relaxed CanEqual checks for ADT pattern matching"),
(symbolLiterals, "Allow symbol literals"),
(saferExceptions, "Enable safer exceptions"),
(pureFunctions, "Enable pure functions for capture checking"),
(captureChecking, "Enable experimental capture checking"),
(separationChecking, "Enable experimental separation checking (requires captureChecking)"),
(into, "Allow into modifier on parameter types"),
(modularity, "Enable experimental modularity features"),
(packageObjectValues, "Enable experimental package objects as values"),
(multiSpreads, "Enable experimental varargs with multi-spreads"),
(subCases, "Enable experimental match expressions with sub-cases"),
(relaxedLambdaSyntax, "Enable experimental relaxed lambda syntax"),
(safe, "Require safe mode"),
)
// 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 genericNumberLiteralsEnabled(using Context) = enabled(genericNumberLiterals)
def scala2ExperimentalMacroEnabled(using Context) = enabled(scala2macros)
def quotedPatternsWithPolymorphicFunctionsEnabled(using Context) =
enabled(quotedPatternsWithPolymorphicFunctions)
/** Is pureFunctions enabled for this compilation unit? */
def pureFunsEnabled(using Context) =
enabledBySetting(pureFunctions)
|| ctx.compilationUnit.knowsPureFuns
|| ccEnabled
/** Is capture checking enabled by a command-line setting? */
def ccEnabledBySetting(using Context): Boolean =
enabledBySetting(captureChecking)
|| enabledBySetting(separationChecking)
|| enabledBySetting(safe)
/** Is capture checking enabled for this compilation unit? */
def ccEnabled(using Context) =
ccEnabledBySetting
|| ctx.originalCompilationUnit.needsCaptureChecking
/** Is separation checking enabled for this compilation unit? */
def sepChecksEnabled(using Context) =
enabledBySetting(separationChecking)
|| ctx.originalCompilationUnit.needsSeparationChecking
/** Is safe mode set for this compilation unit? */
def safeEnabled(using Context) =
enabledBySetting(safe)
|| ctx.originalCompilationUnit.safeMode
/** 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 ccEnabled
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
/* Should we behave as scala 2?*/
def shouldBehaveAsScala2(using Context): Boolean =
ctx.settings.YcompileScala2Library.value || sourceVersion.isScala2
def migrateTo3(using Context): Boolean =
sourceVersion == `3.0-migration`
/** 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 =
ccEnabledSomewhere && (defn.ccExperimental.contains(sym)
|| sym.exists && defn.ccExperimental.contains(sym.owner))
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)
/** Global language imports that affect parsing and must be handled specially.
* These need per-compilation-unit flags (set in `handleGlobalLanguageImport`)
* and also require propagation to rootCtx in the REPL and hoisting in the
* snippet compiler so they take effect across inputs (i16250).
*/
val globalLanguageImports: Set[TermName] =
Set(pureFunctions, captureChecking, separationChecking, safe)
/** Handle a global language import `import language.<prefix>.<imported>`.
* Sets the compilation unit's and current run's fields accordingly.
* @return true iff the import was handled
*/
def handleGlobalLanguageImport(prefix: TermName, imported: Name)(using Context): Boolean =
QualifiedName(prefix, imported.asTermName) match
case `pureFunctions` =>
ctx.compilationUnit.knowsPureFuns = true
if ctx.run != null then ctx.run.nn.pureFunsImportEncountered = true
true
case `captureChecking` =>
ctx.compilationUnit.needsCaptureChecking = true
if ctx.run != null then ctx.run.nn.ccEnabledSomewhere = true
true
case `separationChecking` =>
ctx.compilationUnit.needsCaptureChecking = true
ctx.compilationUnit.needsSeparationChecking = true
if ctx.run != null then ctx.run.nn.ccEnabledSomewhere = true
true
case `safe` =>
ctx.compilationUnit.needsCaptureChecking = true
ctx.compilationUnit.safeMode = true
if ctx.run != null then ctx.run.nn.ccEnabledSomewhere = true
true
case _ =>
false
def isPreviewEnabled(using Context): Boolean =
ctx.settings.preview.value
def checkPreviewFeature(which: String, srcPos: SrcPos, note: => String = "")(using Context) =
if !isPreviewEnabled then
report.error(previewUseSite(which) + note, srcPos)
def checkPreviewDef(sym: Symbol, srcPos: SrcPos)(using Context) = if !isPreviewEnabled then
val previewSym =
if sym.hasAnnotation(defn.PreviewAnnot) then sym
else if sym.owner.hasAnnotation(defn.PreviewAnnot) then sym.owner
else NoSymbol
val msg =
previewSym.getAnnotation(defn.PreviewAnnot).collectFirst {
case PreviewAnnotation(msg) if msg.nonEmpty => s": $msg"
}.getOrElse("")
val markedPreview =
if previewSym.exists
then i"$previewSym is marked @preview$msg"
else i"$sym inherits @preview$msg"
report.error(i"${markedPreview}\n\n${previewUseSite("definition")}", srcPos)
private def previewUseSite(which: String): String =
s"Preview $which may only be used when compiling with the `-preview` compiler flag"
end Feature