Skip to content

Commit 8b28722

Browse files
committed
Introduce untpd.syntheticUnitLiteral to allow detection of explicit unit provided by user from synthetic unit introduced by compiler
[Cherry-picked 8c52866][modified]
1 parent 902da78 commit 8b28722

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ object desugar {
190190
if isSetterNeeded(vdef) then
191191
val setterParam = makeSyntheticParameter(tpt = SetterParamTree().watching(vdef))
192192
// The rhs gets filled in later, when field is generated and getter has parameters (see Memoize miniphase)
193-
val setterRhs = if (vdef.rhs.isEmpty) EmptyTree else unitLiteral
193+
val setterRhs = if (vdef.rhs.isEmpty) EmptyTree else syntheticUnitLiteral
194194
val setter = cpy.DefDef(vdef)(
195195
name = valName.setterName,
196196
paramss = (setterParam :: Nil) :: Nil,
@@ -1311,7 +1311,7 @@ object desugar {
13111311
def block(tree: Block)(using Context): Block = tree.expr match {
13121312
case EmptyTree =>
13131313
cpy.Block(tree)(tree.stats,
1314-
unitLiteral.withSpan(if (tree.stats.isEmpty) tree.span else tree.span.endPos))
1314+
syntheticUnitLiteral.withSpan(if (tree.stats.isEmpty) tree.span else tree.span.endPos))
13151315
case _ =>
13161316
tree
13171317
}
@@ -1811,7 +1811,7 @@ object desugar {
18111811
case ts: Thicket => ts.trees.tail
18121812
case t => Nil
18131813
} map {
1814-
case Block(Nil, EmptyTree) => unitLiteral // for s"... ${} ..."
1814+
case Block(Nil, EmptyTree) => syntheticUnitLiteral // for s"... ${} ..."
18151815
case Block(Nil, expr) => expr // important for interpolated string as patterns, see i1773.scala
18161816
case t => t
18171817
}
@@ -1839,7 +1839,7 @@ object desugar {
18391839
val pats1 = if (tpt.isEmpty) pats else pats map (Typed(_, tpt))
18401840
flatTree(pats1 map (makePatDef(tree, mods, _, rhs)))
18411841
case ext: ExtMethods =>
1842-
Block(List(ext), unitLiteral.withSpan(ext.span))
1842+
Block(List(ext), syntheticUnitLiteral.withSpan(ext.span))
18431843
case f: FunctionWithMods if f.hasErasedParams => makeFunctionWithValDefs(f, pt)
18441844
}
18451845
desugared.withSpan(tree.span)

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,11 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
481481
def InferredTypeTree(tpe: Type)(using Context): TypedSplice =
482482
TypedSplice(new InferredTypeTree().withTypeUnchecked(tpe))
483483

484-
def unitLiteral(implicit src: SourceFile): Literal = Literal(Constant(())).withAttachment(SyntheticUnit, ())
484+
def unitLiteral(implicit src: SourceFile): Literal =
485+
Literal(Constant(()))
486+
487+
def syntheticUnitLiteral(implicit src: SourceFile): Literal =
488+
unitLiteral.withAttachment(SyntheticUnit, ())
485489

486490
def ref(tp: NamedType)(using Context): Tree =
487491
TypedSplice(tpd.ref(tp))

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,7 @@ object Parsers {
22842284
in.nextToken();
22852285
val expr = subExpr()
22862286
if expr.span.exists then expr
2287-
else unitLiteral // finally without an expression
2287+
else syntheticUnitLiteral // finally without an expression
22882288
}
22892289
else {
22902290
if handler.isEmpty then
@@ -3768,10 +3768,10 @@ object Parsers {
37683768
val stats = selfInvocation() :: (
37693769
if (isStatSep) { in.nextToken(); blockStatSeq() }
37703770
else Nil)
3771-
Block(stats, unitLiteral)
3771+
Block(stats, syntheticUnitLiteral)
37723772
}
37733773
}
3774-
else Block(selfInvocation() :: Nil, unitLiteral)
3774+
else Block(selfInvocation() :: Nil, syntheticUnitLiteral)
37753775

37763776
/** SelfInvocation ::= this ArgumentExprs {ArgumentExprs}
37773777
*/

compiler/src/dotty/tools/dotc/typer/Typer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2009,7 +2009,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
20092009
// because we do not know the internal type params and method params.
20102010
// Hence no adaptation is possible, and we assume WildcardType as prototype.
20112011
(from, proto)
2012-
val expr1 = typedExpr(tree.expr orElse untpd.unitLiteral.withSpan(tree.span), proto)
2012+
val expr1 = typedExpr(tree.expr orElse untpd.syntheticUnitLiteral.withSpan(tree.span), proto)
20132013
assignType(cpy.Return(tree)(expr1, from))
20142014
end typedReturn
20152015

compiler/src/dotty/tools/repl/ReplCompiler.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class ReplCompiler extends Compiler:
158158
def wrap(trees: List[untpd.Tree]): untpd.PackageDef = {
159159
import untpd.*
160160

161-
val valdef = ValDef("expr".toTermName, TypeTree(), Block(trees, unitLiteral).withSpan(Span(0, expr.length)))
161+
val valdef = ValDef("expr".toTermName, TypeTree(), Block(trees, syntheticUnitLiteral).withSpan(Span(0, expr.length)))
162162
val tmpl = Template(emptyConstructor, Nil, Nil, EmptyValDef, List(valdef))
163163
val wrapper = TypeDef("$wrapper".toTypeName, tmpl)
164164
.withMods(Modifiers(Final))

0 commit comments

Comments
 (0)