Skip to content

Commit 4ef6ac2

Browse files
committed
Support nested constructor applications as types
1 parent 50a8347 commit 4ef6ac2

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
@@ -1324,15 +1324,20 @@ object Parsers {
13241324
* | SimpleLiteral
13251325
* | Singleton ‘.’ id
13261326
* | Singleton ‘(’ Singletons ‘)’
1327-
* -- not yet | Singleton ‘[’ Types ‘]’
1327+
* | Singleton ‘[’ Types ‘]’
13281328
*/
13291329
def singleton(): Tree =
13301330
val res =
13311331
if isSimpleLiteral then simpleLiteral()
13321332
else dotSelectors(simpleRef())
1333-
if in.token == LPAREN then
1334-
AppliedTypeTree(res, inParens(commaSeparated(() => singleton())))
1335-
else res
1333+
singletonArgs(res)
1334+
1335+
def singletonArgs(t: Tree): Tree =
1336+
if in.token == LPAREN && in.featureEnabled(Feature.modularity) then
1337+
singletonArgs(AppliedTypeTree(t, inParensWithCommas(commaSeparated(singleton))))
1338+
else if in.token == LBRACKET && in.featureEnabled(Feature.modularity) then
1339+
singletonArgs(AppliedTypeTree(t, inBrackets(commaSeparated(() => typ()))))
1340+
else t
13361341

13371342
/** SimpleLiteral ::= [‘-’] integerLiteral
13381343
* | [‘-’] floatingPointLiteral
@@ -2054,10 +2059,6 @@ object Parsers {
20542059
val start = in.skipToken()
20552060
typeBounds().withSpan(Span(start, in.lastOffset, start))
20562061
else
2057-
def singletonArgs(t: Tree): Tree =
2058-
if in.token == LPAREN && in.featureEnabled(Feature.modularity)
2059-
then singletonArgs(AppliedTypeTree(t, inParensWithCommas(commaSeparated(singleton))))
2060-
else t
20612062
singletonArgs(simpleType1())
20622063

20632064
/** 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)

tests/pos/applied_constructors.scala

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)