Skip to content

Commit 6ca653e

Browse files
committed
Support nested constructor applications as types
1 parent 40a10bd commit 6ca653e

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,15 +1322,20 @@ object Parsers {
13221322
* | SimpleLiteral
13231323
* | Singleton ‘.’ id
13241324
* | Singleton ‘(’ Singletons ‘)’
1325-
* -- not yet | Singleton ‘[’ Types ‘]’
1325+
* | Singleton ‘[’ Types ‘]’
13261326
*/
13271327
def singleton(): Tree =
13281328
val res =
13291329
if isSimpleLiteral then simpleLiteral()
13301330
else dotSelectors(simpleRef())
1331-
if in.token == LPAREN then
1332-
AppliedTypeTree(res, inParens(commaSeparated(() => singleton())))
1333-
else res
1331+
singletonArgs(res)
1332+
1333+
def singletonArgs(t: Tree): Tree =
1334+
if in.token == LPAREN && in.featureEnabled(Feature.modularity) then
1335+
singletonArgs(AppliedTypeTree(t, inParensWithCommas(commaSeparated(singleton))))
1336+
else if in.token == LBRACKET && in.featureEnabled(Feature.modularity) then
1337+
singletonArgs(AppliedTypeTree(t, inBrackets(commaSeparated(() => typ()))))
1338+
else t
13341339

13351340
/** SimpleLiteral ::= [‘-’] integerLiteral
13361341
* | [‘-’] floatingPointLiteral
@@ -2052,10 +2057,6 @@ object Parsers {
20522057
val start = in.skipToken()
20532058
typeBounds().withSpan(Span(start, in.lastOffset, start))
20542059
else
2055-
def singletonArgs(t: Tree): Tree =
2056-
if in.token == LPAREN && in.featureEnabled(Feature.modularity)
2057-
then singletonArgs(AppliedTypeTree(t, inParensWithCommas(commaSeparated(singleton))))
2058-
else t
20592060
singletonArgs(simpleType1())
20602061

20612062
/** SimpleType1 ::= id

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,8 +2521,13 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
25212521
tree.args match
25222522
case arg :: _ if arg.isTerm =>
25232523
if Feature.enabled(Feature.modularity) then
2524-
tpt1.tpe.typeSymbol.primaryConstructor.typeRef.underlying match
2525-
case mt: MethodType =>
2524+
val constr =
2525+
if tpt1.tpe.typeSymbol.primaryConstructor.exists then
2526+
tpt1.tpe.typeSymbol.primaryConstructor
2527+
else
2528+
tpt1.tpe.typeSymbol.companionClass.primaryConstructor
2529+
constr.typeRef.underlying match
2530+
case mt: MethodOrPoly =>
25262531
return TypeTree(mt.instantiate(tree.args.map((typedExpr(_).tpe))))
25272532
else
25282533
return errorTree(tree, dependentMsg)
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import scala.language.experimental.modularity
2+
import scala.language.future
23

34
class C(tracked val x: Int)
45
class D(tracked val c: C)
6+
class E(tracked val c: D)
7+
class F[A](tracked val a: A)
58

69
object Test extends App {
710
val c: C(42) = C(42)
8-
// val d: D(C(42)) = D(C(42))
11+
val d: D(C(42)) = D(C(42))
12+
val e: E(D(C(42))) = E(D(C(42)))
13+
// val f: F[Int](42) = F(42)
914
}

0 commit comments

Comments
 (0)