Skip to content

Commit adf1bea

Browse files
authored
Merge pull request #6744 from dwijnand/use-more-union-types
Use more union types in compiler
2 parents d871d1f + 1805c22 commit adf1bea

File tree

4 files changed

+48
-47
lines changed

4 files changed

+48
-47
lines changed

compiler/src/dotty/tools/dotc/interactive/SourceTree.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import util._, util.Spans._
1414
*
1515
* `tree` can be either an `Import` or a `NameTree`.
1616
*/
17-
case class SourceTree(tree: tpd.Tree /** really: tpd.Import | tpd.NameTree */, source: SourceFile) {
17+
case class SourceTree(tree: tpd.Import | tpd.NameTree, source: SourceFile) {
1818

1919
/** The position of `tree` */
2020
final def pos(implicit ctx: Context): SourcePosition = source.atSpan(tree.span)

compiler/src/dotty/tools/dotc/printing/Formatting.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ object Formatting {
103103
else nonSensicalStartTag + str + nonSensicalEndTag
104104
}
105105

106-
private type Recorded = AnyRef /*Symbol | ParamRef | SkolemType */
106+
private type Recorded = Symbol | ParamRef | SkolemType
107107

108108
private case class SeenKey(str: String, isType: Boolean)
109109
private class Seen extends mutable.HashMap[SeenKey, List[Recorded]] {
@@ -206,23 +206,23 @@ object Formatting {
206206
private def explanations(seen: Seen)(implicit ctx: Context): String = {
207207
def needsExplanation(entry: Recorded) = entry match {
208208
case param: TypeParamRef => ctx.typerState.constraint.contains(param)
209-
case param: TermParamRef => false
209+
case param: ParamRef => false
210210
case skolem: SkolemType => true
211211
case sym: Symbol =>
212212
ctx.gadt.contains(sym) && ctx.gadt.fullBounds(sym) != TypeBounds.empty
213-
case _ =>
214-
assert(false, "unreachable")
215-
false
216213
}
217214

218-
val toExplain: List[(String, Recorded)] = seen.toList.flatMap {
219-
case (key, entry :: Nil) =>
220-
if (needsExplanation(entry)) (key.str, entry) :: Nil else Nil
221-
case (key, entries) =>
222-
for (alt <- entries) yield {
223-
val tickedString = seen.record(key.str, key.isType, alt)
224-
(tickedString, alt)
225-
}
215+
val toExplain: List[(String, Recorded)] = seen.toList.flatMap { kvs =>
216+
val res: List[(String, Recorded)] = kvs match {
217+
case (key, entry :: Nil) =>
218+
if (needsExplanation(entry)) (key.str, entry) :: Nil else Nil
219+
case (key, entries) =>
220+
for (alt <- entries) yield {
221+
val tickedString = seen.record(key.str, key.isType, alt)
222+
(tickedString, alt)
223+
}
224+
}
225+
res // help the inferrencer out
226226
}.sortBy(_._1)
227227

228228
def columnar(parts: List[(String, String)]): List[String] = {

compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
171171
}
172172

173173
def ClassDef_constructor(self: ClassDef)(implicit ctx: Context): DefDef = ClassDef_rhs(self).constr
174-
def ClassDef_parents(self: ClassDef)(implicit ctx: Context): List[Tree /* Term | TypeTree */] = ClassDef_rhs(self).parents
174+
def ClassDef_parents(self: ClassDef)(implicit ctx: Context): List[Term | TypeTree] = ClassDef_rhs(self).parents
175175
def ClassDef_derived(self: ClassDef)(implicit ctx: Context): List[TypeTree] = ClassDef_rhs(self).derived.asInstanceOf[List[TypeTree]]
176176
def ClassDef_self(self: ClassDef)(implicit ctx: Context): Option[ValDef] = optional(ClassDef_rhs(self).self)
177177
def ClassDef_body(self: ClassDef)(implicit ctx: Context): List[Statement] = ClassDef_rhs(self).body
178178
def ClassDef_symbol(self: ClassDef)(implicit ctx: Context): ClassDefSymbol = self.symbol.asClass
179179
private def ClassDef_rhs(self: ClassDef) = self.rhs.asInstanceOf[tpd.Template]
180180

181-
def ClassDef_copy(original: ClassDef)(name: String, constr: DefDef, parents: List[Tree /* Term | TypeTree */], derived: List[TypeTree], selfOpt: Option[ValDef], body: List[Statement])(implicit ctx: Context): ClassDef = {
181+
def ClassDef_copy(original: ClassDef)(name: String, constr: DefDef, parents: List[Term | TypeTree], derived: List[TypeTree], selfOpt: Option[ValDef], body: List[Statement])(implicit ctx: Context): ClassDef = {
182182
val Trees.TypeDef(_, originalImpl: tpd.Template) = original
183183
tpd.cpy.TypeDef(original)(name.toTypeName, tpd.cpy.Template(originalImpl)(constr, parents, derived, selfOpt.getOrElse(tpd.EmptyValDef), body))
184184
}
@@ -190,11 +190,11 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
190190
case _ => None
191191
}
192192

193-
def TypeDef_rhs(self: TypeDef)(implicit ctx: Context): Tree /*TypeTree | TypeBoundsTree*/ = self.rhs
193+
def TypeDef_rhs(self: TypeDef)(implicit ctx: Context): TypeTree | TypeBoundsTree = self.rhs
194194
def TypeDef_symbol(self: TypeDef)(implicit ctx: Context): TypeDefSymbol = self.symbol.asType
195195

196196
def TypeDef_apply(symbol: TypeDefSymbol)(implicit ctx: Context): TypeDef = withDefaultPos(ctx => tpd.TypeDef(symbol)(ctx))
197-
def TypeDef_copy(original: TypeDef)(name: String, rhs: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): TypeDef =
197+
def TypeDef_copy(original: TypeDef)(name: String, rhs: TypeTree | TypeBoundsTree)(implicit ctx: Context): TypeDef =
198198
tpd.cpy.TypeDef(original)(name.toTypeName, rhs)
199199

200200
type DefDef = tpd.DefDef
@@ -493,14 +493,14 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
493493
case _ => None
494494
}
495495

496-
def Inlined_call(self: Inlined)(implicit ctx: Context): Option[Tree /* Term | TypeTree */] = optional(self.call)
496+
def Inlined_call(self: Inlined)(implicit ctx: Context): Option[Term | TypeTree] = optional(self.call)
497497
def Inlined_bindings(self: Inlined)(implicit ctx: Context): List[Definition] = self.bindings
498498
def Inlined_body(self: Inlined)(implicit ctx: Context): Term = self.expansion
499499

500-
def Inlined_apply(call: Option[Tree /* Term | TypeTree */], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined =
500+
def Inlined_apply(call: Option[Term | TypeTree], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined =
501501
withDefaultPos(ctx => tpd.Inlined(call.getOrElse(tpd.EmptyTree), bindings.map { case b: tpd.MemberDef => b }, expansion)(ctx))
502502

503-
def Inlined_copy(original: Tree)(call: Option[Tree /* Term | TypeTree */], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined =
503+
def Inlined_copy(original: Tree)(call: Option[Term | TypeTree], bindings: List[Definition], expansion: Term)(implicit ctx: Context): Inlined =
504504
tpd.cpy.Inlined(original)(call.getOrElse(tpd.EmptyTree), bindings.asInstanceOf[List[tpd.MemberDef]], expansion)
505505

506506
type Lambda = tpd.Closure
@@ -657,7 +657,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
657657

658658
type TypeTree = tpd.Tree
659659

660-
def matchTypeTree(x: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[TypeTree] = x match {
660+
def matchTypeTree(x: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[TypeTree] = x match {
661661
case x: tpd.TypeBoundsTree => None
662662
case _ => if (x.isType) Some(x) else None
663663
}
@@ -668,7 +668,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
668668

669669
type Inferred = tpd.TypeTree
670670

671-
def matchInferred(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[Inferred] = tpt match {
671+
def matchInferred(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[Inferred] = tpt match {
672672
case tpt: tpd.TypeTree if !tpt.tpe.isInstanceOf[Types.TypeBounds] => Some(tpt)
673673
case _ => None
674674
}
@@ -677,7 +677,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
677677

678678
type TypeIdent = tpd.Ident
679679

680-
def matchTypeIdent(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[TypeIdent] = tpt match {
680+
def matchTypeIdent(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[TypeIdent] = tpt match {
681681
case tpt: tpd.Ident if tpt.isType => Some(tpt)
682682
case _ => None
683683
}
@@ -689,7 +689,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
689689

690690
type TypeSelect = tpd.Select
691691

692-
def matchTypeSelect(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[TypeSelect] = tpt match {
692+
def matchTypeSelect(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[TypeSelect] = tpt match {
693693
case tpt: tpd.Select if tpt.isType && tpt.qualifier.isTerm => Some(tpt)
694694
case _ => None
695695
}
@@ -706,7 +706,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
706706

707707
type Projection = tpd.Select
708708

709-
def matchProjection(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[Projection] = tpt match {
709+
def matchProjection(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[Projection] = tpt match {
710710
case tpt: tpd.Select if tpt.isType && tpt.qualifier.isType => Some(tpt)
711711
case _ => None
712712
}
@@ -719,7 +719,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
719719

720720
type Singleton = tpd.SingletonTypeTree
721721

722-
def matchSingleton(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[Singleton] = tpt match {
722+
def matchSingleton(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[Singleton] = tpt match {
723723
case tpt: tpd.SingletonTypeTree => Some(tpt)
724724
case _ => None
725725
}
@@ -734,7 +734,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
734734

735735
type Refined = tpd.RefinedTypeTree
736736

737-
def matchRefined(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[Refined] = tpt match {
737+
def matchRefined(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[Refined] = tpt match {
738738
case tpt: tpd.RefinedTypeTree => Some(tpt)
739739
case _ => None
740740
}
@@ -747,23 +747,23 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
747747

748748
type Applied = tpd.AppliedTypeTree
749749

750-
def matchApplied(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[Applied] = tpt match {
750+
def matchApplied(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[Applied] = tpt match {
751751
case tpt: tpd.AppliedTypeTree => Some(tpt)
752752
case _ => None
753753
}
754754

755755
def Applied_tpt(self: Applied)(implicit ctx: Context): TypeTree = self.tpt
756-
def Applied_args(self: Applied)(implicit ctx: Context): List[Tree /*TypeTree | TypeBoundsTree*/] = self.args
756+
def Applied_args(self: Applied)(implicit ctx: Context): List[TypeTree | TypeBoundsTree] = self.args
757757

758-
def Applied_apply(tpt: TypeTree, args: List[Tree /*TypeTree | TypeBoundsTree*/])(implicit ctx: Context): Applied =
758+
def Applied_apply(tpt: TypeTree, args: List[TypeTree | TypeBoundsTree])(implicit ctx: Context): Applied =
759759
withDefaultPos(ctx => tpd.AppliedTypeTree(tpt, args)(ctx))
760760

761-
def Applied_copy(original: Applied)(tpt: TypeTree, args: List[Tree /*TypeTree | TypeBoundsTree*/])(implicit ctx: Context): Applied =
761+
def Applied_copy(original: Applied)(tpt: TypeTree, args: List[TypeTree | TypeBoundsTree])(implicit ctx: Context): Applied =
762762
tpd.cpy.AppliedTypeTree(original)(tpt, args)
763763

764764
type Annotated = tpd.Annotated
765765

766-
def matchAnnotated(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[Annotated] = tpt match {
766+
def matchAnnotated(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[Annotated] = tpt match {
767767
case tpt: tpd.Annotated => Some(tpt)
768768
case _ => None
769769
}
@@ -779,7 +779,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
779779

780780
type MatchTypeTree = tpd.MatchTypeTree
781781

782-
def matchMatchTypeTree(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[MatchTypeTree] = tpt match {
782+
def matchMatchTypeTree(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[MatchTypeTree] = tpt match {
783783
case tpt: tpd.MatchTypeTree => Some(tpt)
784784
case _ => None
785785
}
@@ -796,7 +796,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
796796

797797
type ByName = tpd.ByNameTypeTree
798798

799-
def matchByName(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[ByName] = tpt match {
799+
def matchByName(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[ByName] = tpt match {
800800
case tpt: tpd.ByNameTypeTree => Some(tpt)
801801
case _ => None
802802
}
@@ -811,36 +811,36 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
811811

812812
type LambdaTypeTree = tpd.LambdaTypeTree
813813

814-
def matchLambdaTypeTree(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[LambdaTypeTree] = tpt match {
814+
def matchLambdaTypeTree(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[LambdaTypeTree] = tpt match {
815815
case tpt: tpd.LambdaTypeTree => Some(tpt)
816816
case _ => None
817817
}
818818

819819
def Lambdatparams(self: LambdaTypeTree)(implicit ctx: Context): List[TypeDef] = self.tparams
820-
def Lambdabody(self: LambdaTypeTree)(implicit ctx: Context): Tree /*TypeTree | TypeBoundsTree*/ = self.body
820+
def Lambdabody(self: LambdaTypeTree)(implicit ctx: Context): TypeTree | TypeBoundsTree = self.body
821821

822-
def Lambdaapply(tparams: List[TypeDef], body: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): LambdaTypeTree =
822+
def Lambdaapply(tparams: List[TypeDef], body: TypeTree | TypeBoundsTree)(implicit ctx: Context): LambdaTypeTree =
823823
withDefaultPos(ctx => tpd.LambdaTypeTree(tparams, body)(ctx))
824824

825-
def Lambdacopy(original: LambdaTypeTree)(tparams: List[TypeDef], body: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): LambdaTypeTree =
825+
def Lambdacopy(original: LambdaTypeTree)(tparams: List[TypeDef], body: TypeTree | TypeBoundsTree)(implicit ctx: Context): LambdaTypeTree =
826826
tpd.cpy.LambdaTypeTree(original)(tparams, body)
827827

828828
type TypeBind = tpd.Bind
829829

830-
def matchTypeBind(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[TypeBind] = tpt match {
830+
def matchTypeBind(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[TypeBind] = tpt match {
831831
case tpt: tpd.Bind if tpt.name.isTypeName => Some(tpt)
832832
case _ => None
833833
}
834834

835835
def TypeBind_name(self: TypeBind)(implicit ctx: Context): String = self.name.toString
836-
def TypeBind_body(self: TypeBind)(implicit ctx: Context): Tree /*TypeTree | TypeBoundsTree*/ = self.body
836+
def TypeBind_body(self: TypeBind)(implicit ctx: Context): TypeTree | TypeBoundsTree = self.body
837837

838-
def TypeBind_copy(original: TypeBind)(name: String, tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): TypeBind =
838+
def TypeBind_copy(original: TypeBind)(name: String, tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): TypeBind =
839839
tpd.cpy.Bind(original)(name.toTypeName, tpt)
840840

841841
type TypeBlock = tpd.Block
842842

843-
def matchTypeBlock(tpt: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[TypeBlock] = tpt match {
843+
def matchTypeBlock(tpt: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[TypeBlock] = tpt match {
844844
case tpt: tpd.Block => Some(tpt)
845845
case _ => None
846846
}
@@ -856,7 +856,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
856856

857857
type TypeBoundsTree = tpd.TypeBoundsTree
858858

859-
def matchTypeBoundsTree(x: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[TypeBoundsTree] = x match {
859+
def matchTypeBoundsTree(x: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[TypeBoundsTree] = x match {
860860
case x: tpd.TypeBoundsTree => Some(x)
861861
case x @ Trees.TypeTree() =>
862862
// TODO only enums generate this kind of type bounds. Is this possible without enums? If not generate tpd.TypeBoundsTree for enums instead
@@ -874,7 +874,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
874874

875875
type WildcardTypeTree = tpd.Ident
876876

877-
def matchWildcardTypeTree(x: Tree /*TypeTree | TypeBoundsTree*/)(implicit ctx: Context): Option[WildcardTypeTree] = x match {
877+
def matchWildcardTypeTree(x: TypeTree | TypeBoundsTree)(implicit ctx: Context): Option[WildcardTypeTree] = x match {
878878
case x @ Trees.Ident(nme.WILDCARD) => Some(x)
879879
case _ => None
880880
}
@@ -1119,7 +1119,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
11191119
def SymRef_qualifier(self: SymRef)(implicit ctx: Context): TypeOrBounds = self.prefix
11201120

11211121
// TODO remove this method. May require splitting SymRef into TypeSymRef and TermSymRef
1122-
def matchSymRef_unapply(tpe: TypeOrBounds)(implicit ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)] = tpe match {
1122+
def matchSymRef_unapply(tpe: TypeOrBounds)(implicit ctx: Context): Option[(Symbol, Type | NoPrefix)] = tpe match {
11231123
case tpe: Types.NamedType =>
11241124
tpe.designator match {
11251125
case sym: Symbol => Some((sym, tpe.prefix))

language-server/src/dotty/tools/languageserver/worksheet/Worksheet.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.languageserver.worksheet
22

3-
import dotty.tools.dotc.ast.tpd.{DefTree, Template, Tree, TypeDef}
3+
import dotty.tools.dotc.ast.tpd.{Import, DefTree, NameTree, Template, Tree, TypeDef}
44
import dotty.tools.dotc.core.Contexts.Context
55
import dotty.tools.dotc.interactive.SourceTree
66
import dotty.tools.dotc.util.Spans.Span
@@ -45,6 +45,7 @@ object Worksheet {
4545
case _ =>
4646
None
4747
}
48+
case _: Import | _: NameTree => Nil
4849
}
4950
}
5051
queries.foreach { (line, code) =>

0 commit comments

Comments
 (0)