Skip to content

Commit db5d26d

Browse files
committed
Make everything available under experimental.errorHandling
1 parent 27a6595 commit db5d26d

111 files changed

Lines changed: 338 additions & 319 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/src/dotty/tools/dotc/CompilationUnit.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ class CompilationUnit protected (val source: SourceFile, val info: CompilationUn
8282
/** Will be set to true if unit was compiled with `magic` language import. */
8383
var magic: Boolean = false
8484

85+
/** Will be set to true if unit was compiled with `errorHandling` language import. */
86+
var newErrorHandling: Boolean = false
87+
8588
/** Will be set to true if the unit contains a pureFunctions language import */
8689
var knowsPureFuns: Boolean = false
87-
90+
8891
/** Will be set to true if the unit contains an inlineTrait language import */
8992
var knowsInlineTraits: Boolean = false
9093

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2425,7 +2425,7 @@ object desugar {
24252425
AppliedTypeTree(ref(defn.SeqType), t),
24262426
New(ref(defn.RepeatedAnnot.typeRef), Nil :: Nil))
24272427
else if isOp(tpnme.?) then
2428-
AppliedTypeTree(ref(defn.MagicMaybeClass.typeRef), t :: ref(defn.UnitClass.typeRef) :: Nil)
2428+
AppliedTypeTree(ref(defn.MaybeClass.typeRef), t :: ref(defn.UnitClass.typeRef) :: Nil)
24292429
else
24302430
assert(ctx.mode.isExpr || ctx.reporter.errorsReported || ctx.mode.is(Mode.Interactive), ctx.mode)
24312431
Select(t, op.name)

compiler/src/dotty/tools/dotc/cc/Setup.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
313313
CapturingType(OrType(tp1, parent2, tp.isSoft), refs2, tp2.isBoxed)
314314
case tp @ AppliedType(tycon, args) if tp.dealias eq tp =>
315315
val sym = tycon.typeSymbol
316-
if !defn.isFunctionClass(sym) && sym != defn.MagicMaybeClass
316+
if !defn.isFunctionClass(sym) && sym != defn.MaybeClass
317317
then tp.derivedAppliedType(tycon, args.mapConserve(_.boxDeeply))
318318
else tp
319319
case tp: RealTypeBounds =>

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object Feature:
4545
val magic = experimental("magic")
4646
val inlineTraits = experimental("inlineTraits")
4747
val specializedTraits = experimental("specializedTraits")
48-
val maybe = experimental("maybe")
48+
val errorHandling = experimental("errorHandling")
4949

5050
val nonViralExperimentalFeatures: Set[TermName] =
5151
Set(captureChecking, separationChecking, safe, magic)
@@ -87,7 +87,7 @@ object Feature:
8787
(magic, "Enable extensions for working with coding agents"),
8888
(inlineTraits, "Allow inline traits"),
8989
(specializedTraits, "Allow specialized traits"),
90-
(maybe, "Allow T? and T ? E types"),
90+
(errorHandling, "Allow error handling using T? and T ? E types"),
9191
)
9292

9393
/** Features that are now standard; the language import / -language choice is
@@ -195,8 +195,10 @@ object Feature:
195195
|| enabledBySetting(specializedTraits)
196196
|| ctx.compilationUnit.knowsInlineTraits
197197

198-
def maybeEnabled(using Context) =
199-
enabled(maybe) || magicEnabled
198+
def errorHandlingEnabled(using Context) =
199+
ctx.originalCompilationUnit.newErrorHandling
200+
|| enabledBySetting(errorHandling)
201+
|| enabledBySetting(magic)
200202

201203
/** Is pureFunctions enabled for this compilation unit? */
202204
def pureFunsEnabled(using Context) =
@@ -283,7 +285,7 @@ object Feature:
283285
|| sym.exists && defn.ccExperimental.contains(sym.owner))
284286

285287
private def magicException(sym: Symbol)(using Context): Boolean =
286-
Feature.magicEnabled && sym.isContainedIn(defn.MagicPackageClass)
288+
Feature.magicEnabled // magic enables all experimental definitions
287289

288290
def checkExperimentalDef(sym: Symbol, srcPos: SrcPos)(using Context) =
289291
val experimentalSym =
@@ -354,12 +356,16 @@ object Feature:
354356
case `magic` =>
355357
enableCC()
356358
ctx.compilationUnit.magic = true
359+
ctx.compilationUnit.newErrorHandling = true
357360
ctx.compilationUnit.sourceVersion = Some(SourceVersion.future)
358361
true
359362
case `inlineTraits` =>
360363
ctx.compilationUnit.knowsInlineTraits = true
361364
if ctx.run != null then ctx.run.nn.inlineTraitsImportEncountered = true
362365
true
366+
case `errorHandling` =>
367+
ctx.compilationUnit.newErrorHandling = true
368+
true
363369
case _ =>
364370
false
365371

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -484,26 +484,29 @@ class Definitions {
484484
// Magic stuff
485485
@tu lazy val MagicPackage: Symbol = requiredPackage("scala.magic")
486486
@tu lazy val MagicPackageClass: ClassSymbol = MagicPackage.moduleClass.asClass
487-
@tu lazy val MagicMaybeClass: ClassSymbol = requiredClass("scala.magic.compiletime.Maybe")
488-
@tu lazy val MagicValidClass: ClassSymbol = requiredClass("scala.magic.runtime.Valid")
489-
@tu lazy val MagicFailClass: ClassSymbol = requiredClass("scala.magic.runtime.Fail")
490-
@tu lazy val Magic_provided1: Symbol = MagicPackage.info.member(termName("provided")).suchThat(_.info.isInstanceOf[MethodType]).symbol
491-
@tu lazy val Magic_provided2: Symbol = MagicPackage.info.member(termName("provided")).suchThat(_.info.isInstanceOf[PolyType]).symbol
492-
@tu lazy val Magic_CanErr: Symbol = MagicPackageClass.requiredType("CanErr")
493-
494-
@tu lazy val MagicOkModule: Symbol = requiredModule("scala.magic.Ok")
495-
@tu lazy val Magic_OkApply: Symbol = MagicOkModule.requiredMethod(nme.apply)
496-
@tu lazy val Magic_OkUnapply: Symbol = MagicOkModule.requiredMethod(nme.unapply)
497-
498-
@tu lazy val MagicErrModule: Symbol = requiredModule("scala.magic.Err")
499-
@tu lazy val Magic_ErrUnapply: Symbol = MagicErrModule.requiredMethod(nme.unapply)
500487

501488
@tu lazy val MagicCompiletimePackage: Symbol = requiredPackage("scala.magic.compiletime")
502489
@tu lazy val Magic_spec: Symbol = MagicCompiletimePackage.requiredMethod("$spec")
503490
@tu lazy val Magic_wrappedType: Symbol = MagicCompiletimePackage.requiredMethod("$wrappedType")
504491

505492
@tu lazy val MagicRuntimePackageClass = requiredPackage("scala.magic.runtime").moduleClass.asClass
506493

494+
// Maybe-related
495+
@tu lazy val MaybeClass: ClassSymbol = requiredClass("scala.compiletime.Maybe")
496+
@tu lazy val ValidClass: ClassSymbol = requiredClass("scala.runtime.Valid")
497+
@tu lazy val FailClass: ClassSymbol = requiredClass("scala.runtime.Fail")
498+
@tu lazy val maybeModule: Symbol = requiredModule("scala.maybe")
499+
@tu lazy val maybe_provided1: Symbol = maybeModule.info.member(termName("provided")).suchThat(_.info.isInstanceOf[MethodType]).symbol
500+
@tu lazy val maybe_provided2: Symbol = maybeModule.info.member(termName("provided")).suchThat(_.info.isInstanceOf[PolyType]).symbol
501+
@tu lazy val maybe_CanErr: Symbol = maybeModule.requiredType("CanErr")
502+
503+
@tu lazy val OkModule: Symbol = requiredModule("scala.util.Ok")
504+
@tu lazy val Ok_apply: Symbol = OkModule.requiredMethod(nme.apply)
505+
@tu lazy val Ok_unapply: Symbol = OkModule.requiredMethod(nme.unapply)
506+
507+
@tu lazy val ErrModule: Symbol = requiredModule("scala.util.Err")
508+
@tu lazy val Err_unapply: Symbol = ErrModule.requiredMethod(nme.unapply)
509+
507510
// More synthetic symbols
508511
@tu lazy val andType: TypeSymbol = enterBinaryAlias(tpnme.AND, AndType(_, _))
509512
@tu lazy val orType: TypeSymbol = enterBinaryAlias(tpnme.OR, OrType(_, _, soft = false))
@@ -2240,7 +2243,7 @@ class Definitions {
22402243
m(TupleClass) = ProductClass
22412244
m(NonEmptyTupleClass) = ProductClass
22422245
m(PairClass) = ObjectClass
2243-
m(MagicMaybeClass) = ObjectClass
2246+
m(MaybeClass) = ObjectClass
22442247
m
22452248

22462249
// ----- Initialization ---------------------------------------------------

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
857857
tp2 match
858858
case OrNull(tp2a) =>
859859
tp1w match
860-
case MagicMaybeType(tp1a, errArg, _) =>
860+
case MaybeType(tp1a, errArg, _) =>
861861
if errArg.isRef(defn.UnitClass) && tp1a.isNotNullNorMaybe then
862862
return recur(tp1a, tp2a)
863863
case _ =>
@@ -1050,7 +1050,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
10501050
// Same as above; this.type is also a singleton type in spec language
10511051
!ctx.explicitNulls && isNullable(tp.underlying)
10521052
case tp: RefinedOrRecType => isNullable(tp.parent)
1053-
case AppliedType(tycon, _ :: errArg :: Nil) if tycon.isRef(defn.MagicMaybeClass) =>
1053+
case AppliedType(tycon, _ :: errArg :: Nil) if tycon.isRef(defn.MaybeClass) =>
10541054
isSubType(defn.UnitType, errArg)
10551055
case tp: AppliedType => isNullable(tp.tycon)
10561056
case AndType(tp1, tp2) => isNullable(tp1) && isNullable(tp2)
@@ -1518,7 +1518,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
15181518

15191519
/** T <: T? if T is not null */
15201520
def byMaybeWidening: Boolean = tp2 match
1521-
case MagicMaybeType(res2, err2, _) if tp1.isNotNullNorMaybe =>
1521+
case MaybeType(res2, err2, _) if tp1.isNotNullNorMaybe =>
15221522
recur(tp1, res2)
15231523
case _ => false
15241524

compiler/src/dotty/tools/dotc/core/TypeErasure.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ object TypeErasure:
8484
private def erasureDependsOnArgs(sym: Symbol)(using Context) =
8585
sym == defn.ArrayClass
8686
|| sym == defn.PairClass
87-
|| sym == defn.MagicMaybeClass
87+
|| sym == defn.MaybeClass
8888
|| sym.isDerivedValueClass
8989
|| sym.isSpecializedTrait
9090

@@ -809,7 +809,7 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
809809
else if (tycon.isRef(defn.PairClass)) erasePair(tp)
810810
else if (tp.isRepeatedParam) apply(tp.translateFromRepeated(toArray = sourceLanguage.isJava))
811811
else if (semiEraseVCs && tycon.classSymbol.isDerivedValueClass) eraseDerivedValueClass(tp)
812-
else if tycon.isRef(defn.MagicMaybeClass) then eraseMaybe(tp)
812+
else if tycon.isRef(defn.MaybeClass) then eraseMaybe(tp)
813813
else this(checkedSuperType(tp))
814814
case tp: TermRef =>
815815
this(underlyingOfTermRef(tp))

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ object Types extends TypeUtils {
385385
case tp: ConstantType => tp.value.value != null
386386
case tp: FlexibleType => false
387387
case tp: ClassInfo => !tp.cls.isNullableClass && !tp.isNothingType
388-
case MagicMaybeType(_, _, nullable) => !norMaybe && !nullable
388+
case MaybeType(_, _, nullable) => !norMaybe && !nullable
389389
case tp: TypeBounds => tp.hi.isNotNull(norMaybe)
390390
case tp: TypeProxy => tp.underlying.isNotNull(norMaybe)
391391
case AndType(tp1, tp2) => tp1.isNotNull(norMaybe) || tp2.isNotNull(norMaybe)
@@ -5795,14 +5795,14 @@ object Types extends TypeUtils {
57955795
def unapply(tp: MatchAlias): Option[Type] = Some(tp.alias)
57965796
}
57975797

5798-
object MagicMaybeType {
5798+
object MaybeType {
57995799
/** The maybe type `resTp ? errTp` */
58005800
def apply(resTp: Type, errTp: Type)(using Context) =
5801-
defn.MagicMaybeClass.typeRef.appliedTo(resTp, errTp)
5801+
defn.MaybeClass.typeRef.appliedTo(resTp, errTp)
58025802

58035803
/** Matches types T ? E, returns (T, E, E >: Unit) */
58045804
def unapply(tp: Type)(using Context): Option[(Type, Type, Boolean)] = tp.dealias match
5805-
case AppliedType(tycon, resArg :: errArg :: Nil) if tycon.isRef(defn.MagicMaybeClass) =>
5805+
case AppliedType(tycon, resArg :: errArg :: Nil) if tycon.isRef(defn.MaybeClass) =>
58065806
Some((resArg, errArg, defn.unitSuperClasses.contains(errArg.classSymbol)))
58075807
case _ =>
58085808
None

compiler/src/dotty/tools/dotc/inlines/Inlines.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ object Inlines:
731731
return unitLiteral.withSpan(call.span)
732732
else if inlinedMethod == defn.Compiletime_codeOf then
733733
return Intrinsics.codeOf(arg, call.srcPos)
734-
else if inlinedMethod == defn.Magic_OkApply && arg.tpe.isNotNullNorMaybe then
734+
else if inlinedMethod == defn.Ok_unapply && arg.tpe.isNotNullNorMaybe then
735735
return arg
736736
case _ =>
737737

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,7 +2116,7 @@ object Parsers {
21162116
if in.token == LBRACE
21172117
then makeRetaining(t, captureSet(), tpnme.retains)
21182118
else makeRetaining(t, Nil, tpnme.retainsCap)
2119-
else if in.isIdent(nme.?) && Feature.maybeEnabled && isPostfixQmark(isType = true) then
2119+
else if in.isIdent(nme.?) && Feature.errorHandlingEnabled && isPostfixQmark(isType = true) then
21202120
atSpan(t.span.start):
21212121
PostfixOp(t, typeIdent())
21222122
else
@@ -2678,7 +2678,7 @@ object Parsers {
26782678

26792679
def expr1(location: Location = Location.Elsewhere): Tree = in.token match
26802680
case IF =>
2681-
ifExpr(in.offset, If, canOmitThen = Feature.maybeEnabled && location == Location.InBlock)
2681+
ifExpr(in.offset, If, canOmitThen = Feature.errorHandlingEnabled && location == Location.InBlock)
26822682
case WHILE =>
26832683
atSpan(in.skipToken()) {
26842684
val cond = condExpr(DO)
@@ -3127,7 +3127,7 @@ object Parsers {
31273127
case USCORE =>
31283128
atSpan(startOffset(t), in.skipToken()) { PostfixOp(t, Ident(nme.WILDCARD)) }
31293129
case _ =>
3130-
if in.isIdent(nme.?) && Feature.maybeEnabled && isPostfixQmark(isType = false) then
3130+
if in.isIdent(nme.?) && Feature.errorHandlingEnabled && isPostfixQmark(isType = false) then
31313131
atSpan(t.span.start):
31323132
PostfixOp(t, termIdent())
31333133
else if in.isColon && location == Location.InParens && followingIsLambdaParams() then

0 commit comments

Comments
 (0)