Skip to content

Commit 8c52866

Browse files
committed
Introduce untpd.syntheticUnitLiteral to allow detection of explicit unit provided by user from synthetic unit introduced by compiler
1 parent 4afb8c7 commit 8c52866

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ object desugar {
188188
if isSetterNeeded(vdef) then
189189
val setterParam = makeSyntheticParameter(tpt = SetterParamTree().watching(vdef))
190190
// The rhs gets filled in later, when field is generated and getter has parameters (see Memoize miniphase)
191-
val setterRhs = if (vdef.rhs.isEmpty) EmptyTree else unitLiteral
191+
val setterRhs = if (vdef.rhs.isEmpty) EmptyTree else syntheticUnitLiteral
192192
val setter = cpy.DefDef(vdef)(
193193
name = valName.setterName,
194194
paramss = (setterParam :: Nil) :: Nil,
@@ -1489,7 +1489,7 @@ object desugar {
14891489
def block(tree: Block)(using Context): Block = tree.expr match {
14901490
case EmptyTree =>
14911491
cpy.Block(tree)(tree.stats,
1492-
unitLiteral.withSpan(if (tree.stats.isEmpty) tree.span else tree.span.endPos))
1492+
syntheticUnitLiteral.withSpan(if (tree.stats.isEmpty) tree.span else tree.span.endPos))
14931493
case _ =>
14941494
tree
14951495
}
@@ -2013,7 +2013,7 @@ object desugar {
20132013
case ts: Thicket => ts.trees.tail
20142014
case t => Nil
20152015
} map {
2016-
case Block(Nil, EmptyTree) => unitLiteral // for s"... ${} ..."
2016+
case Block(Nil, EmptyTree) => syntheticUnitLiteral // for s"... ${} ..."
20172017
case Block(Nil, expr) => expr // important for interpolated string as patterns, see i1773.scala
20182018
case t => t
20192019
}
@@ -2046,7 +2046,7 @@ object desugar {
20462046
val pats1 = if (tpt.isEmpty) pats else pats map (Typed(_, tpt))
20472047
flatTree(pats1 map (makePatDef(tree, mods, _, rhs)))
20482048
case ext: ExtMethods =>
2049-
Block(List(ext), unitLiteral.withSpan(ext.span))
2049+
Block(List(ext), syntheticUnitLiteral.withSpan(ext.span))
20502050
case f: FunctionWithMods if f.hasErasedParams => makeFunctionWithValDefs(f, pt)
20512051
}
20522052
desugared.withSpan(tree.span)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,11 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
495495
def InferredTypeTree(tpe: Type)(using Context): TypedSplice =
496496
TypedSplice(new InferredTypeTree().withTypeUnchecked(tpe))
497497

498-
def unitLiteral(implicit src: SourceFile): Literal = Literal(Constant(())).withAttachment(SyntheticUnit, ())
498+
def unitLiteral(implicit src: SourceFile): Literal =
499+
Literal(Constant(()))
500+
501+
def syntheticUnitLiteral(implicit src: SourceFile): Literal =
502+
unitLiteral.withAttachment(SyntheticUnit, ())
499503

500504
def ref(tp: NamedType)(using Context): Tree =
501505
TypedSplice(tpd.ref(tp))

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ object Parsers {
17251725
case arg =>
17261726
arg
17271727
val args1 = args.mapConserve(sanitize)
1728-
1728+
17291729
if in.isArrow || isPureArrow || erasedArgs.contains(true) then
17301730
functionRest(args)
17311731
else
@@ -2424,7 +2424,7 @@ object Parsers {
24242424
in.nextToken();
24252425
val expr = subExpr()
24262426
if expr.span.exists then expr
2427-
else unitLiteral // finally without an expression
2427+
else syntheticUnitLiteral // finally without an expression
24282428
}
24292429
else {
24302430
if handler.isEmpty then
@@ -3921,10 +3921,10 @@ object Parsers {
39213921
val stats = selfInvocation() :: (
39223922
if (isStatSep) { in.nextToken(); blockStatSeq() }
39233923
else Nil)
3924-
Block(stats, unitLiteral)
3924+
Block(stats, syntheticUnitLiteral)
39253925
}
39263926
}
3927-
else Block(selfInvocation() :: Nil, unitLiteral)
3927+
else Block(selfInvocation() :: Nil, syntheticUnitLiteral)
39283928

39293929
/** SelfInvocation ::= this ArgumentExprs {ArgumentExprs}
39303930
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
22212221
// because we do not know the internal type params and method params.
22222222
// Hence no adaptation is possible, and we assume WildcardType as prototype.
22232223
(from, proto)
2224-
val expr1 = typedExpr(tree.expr orElse untpd.unitLiteral.withSpan(tree.span), proto)
2224+
val expr1 = typedExpr(tree.expr orElse untpd.syntheticUnitLiteral.withSpan(tree.span), proto)
22252225
assignType(cpy.Return(tree)(expr1, from))
22262226
end typedReturn
22272227

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class ReplCompiler extends Compiler:
159159
def wrap(trees: List[untpd.Tree]): untpd.PackageDef = {
160160
import untpd.*
161161

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

0 commit comments

Comments
 (0)