Skip to content

Commit fa8471d

Browse files
authored
Merge pull request #5883 from dotty-staging/fix-source-after-erasure
Fix the source of inlined trees after Erasure
2 parents d7090ab + ec20869 commit fa8471d

File tree

5 files changed

+126
-93
lines changed

5 files changed

+126
-93
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import dotty.tools.FatalError
66
import config.CompilerCommand
77
import core.Comments.{ContextDoc, ContextDocstrings}
88
import core.Contexts.{Context, ContextBase}
9-
import core.Mode
9+
import core.{Mode, TypeError}
1010
import reporting._
1111

1212
import scala.util.control.NonFatal
@@ -37,6 +37,9 @@ class Driver {
3737
case ex: FatalError =>
3838
ctx.error(ex.getMessage) // signals that we should fail compilation.
3939
ctx.reporter
40+
case ex: TypeError =>
41+
println(s"${ex.toMessage} while compiling ${fileNames.mkString(", ")}")
42+
throw ex
4043
case ex: Throwable =>
4144
println(s"$ex while compiling ${fileNames.mkString(", ")}")
4245
throw ex

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

Lines changed: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,9 @@ object Trees {
981981
protected def postProcess(tree: Tree, copied: untpd.Tree): copied.ThisTree[T]
982982
protected def postProcess(tree: Tree, copied: untpd.MemberDef): copied.ThisTree[T]
983983

984+
/** Soucre of the copied tree */
985+
protected def sourceFile(tree: Tree): SourceFile = tree.source
986+
984987
protected def finalize(tree: Tree, copied: untpd.Tree): copied.ThisTree[T] =
985988
postProcess(tree, copied.withSpan(tree.span).withAttachmentsFrom(tree))
986989

@@ -990,187 +993,187 @@ object Trees {
990993
def Ident(tree: Tree)(name: Name)(implicit ctx: Context): Ident = tree match {
991994
case tree: BackquotedIdent =>
992995
if (name == tree.name) tree
993-
else finalize(tree, new BackquotedIdent(name)(tree.source))
996+
else finalize(tree, new BackquotedIdent(name)(sourceFile(tree)))
994997
case tree: Ident if name == tree.name => tree
995-
case _ => finalize(tree, untpd.Ident(name)(tree.source))
998+
case _ => finalize(tree, untpd.Ident(name)(sourceFile(tree)))
996999
}
9971000
def Select(tree: Tree)(qualifier: Tree, name: Name)(implicit ctx: Context): Select = tree match {
9981001
case tree: SelectWithSig =>
9991002
if ((qualifier eq tree.qualifier) && (name == tree.name)) tree
1000-
else finalize(tree, new SelectWithSig(qualifier, name, tree.sig)(tree.source))
1003+
else finalize(tree, new SelectWithSig(qualifier, name, tree.sig)(sourceFile(tree)))
10011004
case tree: Select if (qualifier eq tree.qualifier) && (name == tree.name) => tree
1002-
case _ => finalize(tree, untpd.Select(qualifier, name)(tree.source))
1005+
case _ => finalize(tree, untpd.Select(qualifier, name)(sourceFile(tree)))
10031006
}
10041007
/** Copy Ident or Select trees */
10051008
def Ref(tree: RefTree)(name: Name)(implicit ctx: Context): RefTree = tree match {
10061009
case Ident(_) => Ident(tree)(name)
10071010
case Select(qual, _) => Select(tree)(qual, name)
10081011
}
10091012
def This(tree: Tree)(qual: untpd.Ident)(implicit ctx: Context): This = tree match {
1010-
case tree: This if qual eq tree.qual => tree
1011-
case _ => finalize(tree, untpd.This(qual)(tree.source))
1013+
case tree: This if (qual eq tree.qual) => tree
1014+
case _ => finalize(tree, untpd.This(qual)(sourceFile(tree)))
10121015
}
10131016
def Super(tree: Tree)(qual: Tree, mix: untpd.Ident)(implicit ctx: Context): Super = tree match {
10141017
case tree: Super if (qual eq tree.qual) && (mix eq tree.mix) => tree
1015-
case _ => finalize(tree, untpd.Super(qual, mix)(tree.source))
1018+
case _ => finalize(tree, untpd.Super(qual, mix)(sourceFile(tree)))
10161019
}
10171020
def Apply(tree: Tree)(fun: Tree, args: List[Tree])(implicit ctx: Context): Apply = tree match {
10181021
case tree: Apply if (fun eq tree.fun) && (args eq tree.args) => tree
1019-
case _ => finalize(tree, untpd.Apply(fun, args)(tree.source))
1022+
case _ => finalize(tree, untpd.Apply(fun, args)(sourceFile(tree)))
10201023
}
10211024
def TypeApply(tree: Tree)(fun: Tree, args: List[Tree])(implicit ctx: Context): TypeApply = tree match {
10221025
case tree: TypeApply if (fun eq tree.fun) && (args eq tree.args) => tree
1023-
case _ => finalize(tree, untpd.TypeApply(fun, args)(tree.source))
1026+
case _ => finalize(tree, untpd.TypeApply(fun, args)(sourceFile(tree)))
10241027
}
10251028
def Literal(tree: Tree)(const: Constant)(implicit ctx: Context): Literal = tree match {
10261029
case tree: Literal if const == tree.const => tree
1027-
case _ => finalize(tree, untpd.Literal(const)(tree.source))
1030+
case _ => finalize(tree, untpd.Literal(const)(sourceFile(tree)))
10281031
}
10291032
def New(tree: Tree)(tpt: Tree)(implicit ctx: Context): New = tree match {
1030-
case tree: New if tpt eq tree.tpt => tree
1031-
case _ => finalize(tree, untpd.New(tpt)(tree.source))
1033+
case tree: New if (tpt eq tree.tpt) => tree
1034+
case _ => finalize(tree, untpd.New(tpt)(sourceFile(tree)))
10321035
}
10331036
def Typed(tree: Tree)(expr: Tree, tpt: Tree)(implicit ctx: Context): Typed = tree match {
10341037
case tree: Typed if (expr eq tree.expr) && (tpt eq tree.tpt) => tree
1035-
case tree => finalize(tree, untpd.Typed(expr, tpt)(tree.source))
1038+
case tree => finalize(tree, untpd.Typed(expr, tpt)(sourceFile(tree)))
10361039
//.ensuring(res => res.uniqueId != 1471, s"source = $tree, ${tree.uniqueId}")
10371040
}
10381041
def NamedArg(tree: Tree)(name: Name, arg: Tree)(implicit ctx: Context): NamedArg = tree match {
10391042
case tree: NamedArg if (name == tree.name) && (arg eq tree.arg) => tree
1040-
case _ => finalize(tree, untpd.NamedArg(name, arg)(tree.source))
1043+
case _ => finalize(tree, untpd.NamedArg(name, arg)(sourceFile(tree)))
10411044
}
10421045
def Assign(tree: Tree)(lhs: Tree, rhs: Tree)(implicit ctx: Context): Assign = tree match {
10431046
case tree: Assign if (lhs eq tree.lhs) && (rhs eq tree.rhs) => tree
1044-
case _ => finalize(tree, untpd.Assign(lhs, rhs)(tree.source))
1047+
case _ => finalize(tree, untpd.Assign(lhs, rhs)(sourceFile(tree)))
10451048
}
10461049
def Block(tree: Tree)(stats: List[Tree], expr: Tree)(implicit ctx: Context): Block = tree match {
10471050
case tree: Block if (stats eq tree.stats) && (expr eq tree.expr) => tree
1048-
case _ => finalize(tree, untpd.Block(stats, expr)(tree.source))
1051+
case _ => finalize(tree, untpd.Block(stats, expr)(sourceFile(tree)))
10491052
}
10501053
def If(tree: Tree)(cond: Tree, thenp: Tree, elsep: Tree)(implicit ctx: Context): If = tree match {
10511054
case tree: If if (cond eq tree.cond) && (thenp eq tree.thenp) && (elsep eq tree.elsep) => tree
1052-
case tree: InlineIf => finalize(tree, untpd.InlineIf(cond, thenp, elsep)(tree.source))
1053-
case _ => finalize(tree, untpd.If(cond, thenp, elsep)(tree.source))
1055+
case tree: InlineIf => finalize(tree, untpd.InlineIf(cond, thenp, elsep)(sourceFile(tree)))
1056+
case _ => finalize(tree, untpd.If(cond, thenp, elsep)(sourceFile(tree)))
10541057
}
10551058
def Closure(tree: Tree)(env: List[Tree], meth: Tree, tpt: Tree)(implicit ctx: Context): Closure = tree match {
10561059
case tree: Closure if (env eq tree.env) && (meth eq tree.meth) && (tpt eq tree.tpt) => tree
1057-
case _ => finalize(tree, untpd.Closure(env, meth, tpt)(tree.source))
1060+
case _ => finalize(tree, untpd.Closure(env, meth, tpt)(sourceFile(tree)))
10581061
}
10591062
def Match(tree: Tree)(selector: Tree, cases: List[CaseDef])(implicit ctx: Context): Match = tree match {
10601063
case tree: Match if (selector eq tree.selector) && (cases eq tree.cases) => tree
1061-
case tree: InlineMatch => finalize(tree, untpd.InlineMatch(selector, cases)(tree.source))
1062-
case _ => finalize(tree, untpd.Match(selector, cases)(tree.source))
1064+
case tree: InlineMatch => finalize(tree, untpd.InlineMatch(selector, cases)(sourceFile(tree)))
1065+
case _ => finalize(tree, untpd.Match(selector, cases)(sourceFile(tree)))
10631066
}
10641067
def CaseDef(tree: Tree)(pat: Tree, guard: Tree, body: Tree)(implicit ctx: Context): CaseDef = tree match {
10651068
case tree: CaseDef if (pat eq tree.pat) && (guard eq tree.guard) && (body eq tree.body) => tree
1066-
case _ => finalize(tree, untpd.CaseDef(pat, guard, body)(tree.source))
1069+
case _ => finalize(tree, untpd.CaseDef(pat, guard, body)(sourceFile(tree)))
10671070
}
10681071
def Labeled(tree: Tree)(bind: Bind, expr: Tree)(implicit ctx: Context): Labeled = tree match {
10691072
case tree: Labeled if (bind eq tree.bind) && (expr eq tree.expr) => tree
1070-
case _ => finalize(tree, untpd.Labeled(bind, expr)(tree.source))
1073+
case _ => finalize(tree, untpd.Labeled(bind, expr)(sourceFile(tree)))
10711074
}
10721075
def Return(tree: Tree)(expr: Tree, from: Tree)(implicit ctx: Context): Return = tree match {
10731076
case tree: Return if (expr eq tree.expr) && (from eq tree.from) => tree
1074-
case _ => finalize(tree, untpd.Return(expr, from)(tree.source))
1077+
case _ => finalize(tree, untpd.Return(expr, from)(sourceFile(tree)))
10751078
}
10761079
def WhileDo(tree: Tree)(cond: Tree, body: Tree)(implicit ctx: Context): WhileDo = tree match {
10771080
case tree: WhileDo if (cond eq tree.cond) && (body eq tree.body) => tree
1078-
case _ => finalize(tree, untpd.WhileDo(cond, body)(tree.source))
1081+
case _ => finalize(tree, untpd.WhileDo(cond, body)(sourceFile(tree)))
10791082
}
10801083
def Try(tree: Tree)(expr: Tree, cases: List[CaseDef], finalizer: Tree)(implicit ctx: Context): Try = tree match {
10811084
case tree: Try if (expr eq tree.expr) && (cases eq tree.cases) && (finalizer eq tree.finalizer) => tree
1082-
case _ => finalize(tree, untpd.Try(expr, cases, finalizer)(tree.source))
1085+
case _ => finalize(tree, untpd.Try(expr, cases, finalizer)(sourceFile(tree)))
10831086
}
10841087
def SeqLiteral(tree: Tree)(elems: List[Tree], elemtpt: Tree)(implicit ctx: Context): SeqLiteral = tree match {
10851088
case tree: JavaSeqLiteral =>
10861089
if ((elems eq tree.elems) && (elemtpt eq tree.elemtpt)) tree
1087-
else finalize(tree, new JavaSeqLiteral(elems, elemtpt))
1090+
else finalize(tree, untpd.JavaSeqLiteral(elems, elemtpt))
10881091
case tree: SeqLiteral if (elems eq tree.elems) && (elemtpt eq tree.elemtpt) => tree
1089-
case _ => finalize(tree, untpd.SeqLiteral(elems, elemtpt)(tree.source))
1092+
case _ => finalize(tree, untpd.SeqLiteral(elems, elemtpt)(sourceFile(tree)))
10901093
}
10911094
def Inlined(tree: Tree)(call: tpd.Tree, bindings: List[MemberDef], expansion: Tree)(implicit ctx: Context): Inlined = tree match {
10921095
case tree: Inlined if (call eq tree.call) && (bindings eq tree.bindings) && (expansion eq tree.expansion) => tree
1093-
case _ => finalize(tree, untpd.Inlined(call, bindings, expansion)(tree.source))
1096+
case _ => finalize(tree, untpd.Inlined(call, bindings, expansion)(sourceFile(tree)))
10941097
}
10951098
def SingletonTypeTree(tree: Tree)(ref: Tree)(implicit ctx: Context): SingletonTypeTree = tree match {
1096-
case tree: SingletonTypeTree if ref eq tree.ref => tree
1097-
case _ => finalize(tree, untpd.SingletonTypeTree(ref)(tree.source))
1099+
case tree: SingletonTypeTree if (ref eq tree.ref) => tree
1100+
case _ => finalize(tree, untpd.SingletonTypeTree(ref)(sourceFile(tree)))
10981101
}
10991102
def AndTypeTree(tree: Tree)(left: Tree, right: Tree)(implicit ctx: Context): AndTypeTree = tree match {
11001103
case tree: AndTypeTree if (left eq tree.left) && (right eq tree.right) => tree
1101-
case _ => finalize(tree, untpd.AndTypeTree(left, right)(tree.source))
1104+
case _ => finalize(tree, untpd.AndTypeTree(left, right)(sourceFile(tree)))
11021105
}
11031106
def OrTypeTree(tree: Tree)(left: Tree, right: Tree)(implicit ctx: Context): OrTypeTree = tree match {
11041107
case tree: OrTypeTree if (left eq tree.left) && (right eq tree.right) => tree
1105-
case _ => finalize(tree, untpd.OrTypeTree(left, right)(tree.source))
1108+
case _ => finalize(tree, untpd.OrTypeTree(left, right)(sourceFile(tree)))
11061109
}
11071110
def RefinedTypeTree(tree: Tree)(tpt: Tree, refinements: List[Tree])(implicit ctx: Context): RefinedTypeTree = tree match {
11081111
case tree: RefinedTypeTree if (tpt eq tree.tpt) && (refinements eq tree.refinements) => tree
1109-
case _ => finalize(tree, untpd.RefinedTypeTree(tpt, refinements)(tree.source))
1112+
case _ => finalize(tree, untpd.RefinedTypeTree(tpt, refinements)(sourceFile(tree)))
11101113
}
11111114
def AppliedTypeTree(tree: Tree)(tpt: Tree, args: List[Tree])(implicit ctx: Context): AppliedTypeTree = tree match {
11121115
case tree: AppliedTypeTree if (tpt eq tree.tpt) && (args eq tree.args) => tree
1113-
case _ => finalize(tree, untpd.AppliedTypeTree(tpt, args)(tree.source))
1116+
case _ => finalize(tree, untpd.AppliedTypeTree(tpt, args)(sourceFile(tree)))
11141117
}
11151118
def LambdaTypeTree(tree: Tree)(tparams: List[TypeDef], body: Tree)(implicit ctx: Context): LambdaTypeTree = tree match {
11161119
case tree: LambdaTypeTree if (tparams eq tree.tparams) && (body eq tree.body) => tree
1117-
case _ => finalize(tree, untpd.LambdaTypeTree(tparams, body)(tree.source))
1120+
case _ => finalize(tree, untpd.LambdaTypeTree(tparams, body)(sourceFile(tree)))
11181121
}
11191122
def MatchTypeTree(tree: Tree)(bound: Tree, selector: Tree, cases: List[CaseDef])(implicit ctx: Context): MatchTypeTree = tree match {
11201123
case tree: MatchTypeTree if (bound eq tree.bound) && (selector eq tree.selector) && (cases eq tree.cases) => tree
1121-
case _ => finalize(tree, untpd.MatchTypeTree(bound, selector, cases)(tree.source))
1124+
case _ => finalize(tree, untpd.MatchTypeTree(bound, selector, cases)(sourceFile(tree)))
11221125
}
11231126
def ByNameTypeTree(tree: Tree)(result: Tree)(implicit ctx: Context): ByNameTypeTree = tree match {
1124-
case tree: ByNameTypeTree if result eq tree.result => tree
1125-
case _ => finalize(tree, untpd.ByNameTypeTree(result)(tree.source))
1127+
case tree: ByNameTypeTree if (result eq tree.result) => tree
1128+
case _ => finalize(tree, untpd.ByNameTypeTree(result)(sourceFile(tree)))
11261129
}
11271130
def TypeBoundsTree(tree: Tree)(lo: Tree, hi: Tree)(implicit ctx: Context): TypeBoundsTree = tree match {
11281131
case tree: TypeBoundsTree if (lo eq tree.lo) && (hi eq tree.hi) => tree
1129-
case _ => finalize(tree, untpd.TypeBoundsTree(lo, hi)(tree.source))
1132+
case _ => finalize(tree, untpd.TypeBoundsTree(lo, hi)(sourceFile(tree)))
11301133
}
11311134
def Bind(tree: Tree)(name: Name, body: Tree)(implicit ctx: Context): Bind = tree match {
11321135
case tree: Bind if (name eq tree.name) && (body eq tree.body) => tree
1133-
case _ => finalize(tree, untpd.Bind(name, body)(tree.source))
1136+
case _ => finalize(tree, untpd.Bind(name, body)(sourceFile(tree)))
11341137
}
11351138
def Alternative(tree: Tree)(trees: List[Tree])(implicit ctx: Context): Alternative = tree match {
1136-
case tree: Alternative if trees eq tree.trees => tree
1137-
case _ => finalize(tree, untpd.Alternative(trees)(tree.source))
1139+
case tree: Alternative if (trees eq tree.trees) => tree
1140+
case _ => finalize(tree, untpd.Alternative(trees)(sourceFile(tree)))
11381141
}
11391142
def UnApply(tree: Tree)(fun: Tree, implicits: List[Tree], patterns: List[Tree])(implicit ctx: Context): UnApply = tree match {
11401143
case tree: UnApply if (fun eq tree.fun) && (implicits eq tree.implicits) && (patterns eq tree.patterns) => tree
1141-
case _ => finalize(tree, untpd.UnApply(fun, implicits, patterns)(tree.source))
1144+
case _ => finalize(tree, untpd.UnApply(fun, implicits, patterns)(sourceFile(tree)))
11421145
}
11431146
def ValDef(tree: Tree)(name: TermName, tpt: Tree, rhs: LazyTree)(implicit ctx: Context): ValDef = tree match {
11441147
case tree: ValDef if (name == tree.name) && (tpt eq tree.tpt) && (rhs eq tree.unforcedRhs) => tree
1145-
case _ => finalize(tree, untpd.ValDef(name, tpt, rhs)(tree.source))
1148+
case _ => finalize(tree, untpd.ValDef(name, tpt, rhs)(sourceFile(tree)))
11461149
}
11471150
def DefDef(tree: Tree)(name: TermName, tparams: List[TypeDef], vparamss: List[List[ValDef]], tpt: Tree, rhs: LazyTree)(implicit ctx: Context): DefDef = tree match {
11481151
case tree: DefDef if (name == tree.name) && (tparams eq tree.tparams) && (vparamss eq tree.vparamss) && (tpt eq tree.tpt) && (rhs eq tree.unforcedRhs) => tree
1149-
case _ => finalize(tree, untpd.DefDef(name, tparams, vparamss, tpt, rhs)(tree.source))
1152+
case _ => finalize(tree, untpd.DefDef(name, tparams, vparamss, tpt, rhs)(sourceFile(tree)))
11501153
}
11511154
def TypeDef(tree: Tree)(name: TypeName, rhs: Tree)(implicit ctx: Context): TypeDef = tree match {
11521155
case tree: TypeDef if (name == tree.name) && (rhs eq tree.rhs) => tree
1153-
case _ => finalize(tree, untpd.TypeDef(name, rhs)(tree.source))
1156+
case _ => finalize(tree, untpd.TypeDef(name, rhs)(sourceFile(tree)))
11541157
}
11551158
def Template(tree: Tree)(constr: DefDef, parents: List[Tree], derived: List[untpd.Tree], self: ValDef, body: LazyTreeList)(implicit ctx: Context): Template = tree match {
11561159
case tree: Template if (constr eq tree.constr) && (parents eq tree.parents) && (derived eq tree.derived) && (self eq tree.self) && (body eq tree.unforcedBody) => tree
1157-
case tree => finalize(tree, untpd.Template(constr, parents, derived, self, body)(tree.source))
1160+
case tree => finalize(tree, untpd.Template(constr, parents, derived, self, body)(sourceFile(tree)))
11581161
}
11591162
def Import(tree: Tree)(impliedOnly: Boolean, expr: Tree, selectors: List[untpd.Tree])(implicit ctx: Context): Import = tree match {
11601163
case tree: Import if (impliedOnly == tree.impliedOnly) && (expr eq tree.expr) && (selectors eq tree.selectors) => tree
1161-
case _ => finalize(tree, untpd.Import(impliedOnly, expr, selectors)(tree.source))
1164+
case _ => finalize(tree, untpd.Import(impliedOnly, expr, selectors)(sourceFile(tree)))
11621165
}
11631166
def PackageDef(tree: Tree)(pid: RefTree, stats: List[Tree])(implicit ctx: Context): PackageDef = tree match {
11641167
case tree: PackageDef if (pid eq tree.pid) && (stats eq tree.stats) => tree
1165-
case _ => finalize(tree, untpd.PackageDef(pid, stats)(tree.source))
1168+
case _ => finalize(tree, untpd.PackageDef(pid, stats)(sourceFile(tree)))
11661169
}
11671170
def Annotated(tree: Tree)(arg: Tree, annot: Tree)(implicit ctx: Context): Annotated = tree match {
11681171
case tree: Annotated if (arg eq tree.arg) && (annot eq tree.annot) => tree
1169-
case _ => finalize(tree, untpd.Annotated(arg, annot)(tree.source))
1172+
case _ => finalize(tree, untpd.Annotated(arg, annot)(sourceFile(tree)))
11701173
}
11711174
def Thicket(tree: Tree)(trees: List[Tree])(implicit ctx: Context): Thicket = tree match {
1172-
case tree: Thicket if trees eq tree.trees => tree
1173-
case _ => finalize(tree, untpd.Thicket(trees)(tree.source))
1175+
case tree: Thicket if (trees eq tree.trees) => tree
1176+
case _ => finalize(tree, untpd.Thicket(trees)(sourceFile(tree)))
11741177
}
11751178

11761179
// Copier methods with default arguments; these demand that the original tree

0 commit comments

Comments
 (0)