Skip to content

Commit ad9d163

Browse files
committed
Extract applied constructors logic to a separate function
1 parent 4ef6ac2 commit ad9d163

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ object Parsers {
13241324
* | SimpleLiteral
13251325
* | Singleton ‘.’ id
13261326
* | Singleton ‘(’ Singletons ‘)’
1327-
* | Singleton ‘[’ Types ‘]’
1327+
* -- not yet | Singleton ‘[’ Types ‘]’
13281328
*/
13291329
def singleton(): Tree =
13301330
val res =
@@ -1335,8 +1335,8 @@ object Parsers {
13351335
def singletonArgs(t: Tree): Tree =
13361336
if in.token == LPAREN && in.featureEnabled(Feature.modularity) then
13371337
singletonArgs(AppliedTypeTree(t, inParensWithCommas(commaSeparated(singleton))))
1338-
else if in.token == LBRACKET && in.featureEnabled(Feature.modularity) then
1339-
singletonArgs(AppliedTypeTree(t, inBrackets(commaSeparated(() => typ()))))
1338+
// else if in.token == LBRACKET && in.featureEnabled(Feature.modularity) then
1339+
// singletonArgs(AppliedTypeTree(t, inBrackets(commaSeparated(() => typ())))) // should this be marked in a different way, so that we know that it is a type application, and not a term application?
13401340
else t
13411341

13421342
/** SimpleLiteral ::= [‘-’] integerLiteral

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,23 +2514,37 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
25142514
assignType(cpy.RefinedTypeTree(tree)(tpt1, refinements1), tpt1, refinements1, refineCls)
25152515
}
25162516

2517+
/** Type applied dependent class constructors in type positions */
2518+
private def typedTermAppliedTypeTree(tree: untpd.AppliedTypeTree, tpt1: Tree)(using Context): Tree = {
2519+
if Feature.enabled(Feature.modularity) then
2520+
val constr =
2521+
if tpt1.tpe.typeSymbol.primaryConstructor.exists then
2522+
tpt1.tpe.typeSymbol.primaryConstructor
2523+
else
2524+
tpt1.tpe.typeSymbol.companionClass.primaryConstructor
2525+
// TODO(kπ) vvvvvvv Might want to take the first term list? or all term lists? depends on the rest of the logic.
2526+
constr.paramSymss.flatten.foreach { p =>
2527+
if p.isTerm && !p.flags.is(Tracked) then
2528+
report.error(
2529+
em"""The constructor parameter `${p.name}` of `${tpt1.tpe}` is not tracked.
2530+
|Only tracked parameters are allowed in dependent constructor applications.""",
2531+
tree.srcPos
2532+
)
2533+
}
2534+
constr.typeRef.underlying match
2535+
case mt: MethodOrPoly =>
2536+
TypeTree(mt.instantiate(tree.args.map((typedExpr(_).tpe))))
2537+
else
2538+
errorTree(tree, dependentMsg)
2539+
}
2540+
25172541
def typedAppliedTypeTree(tree: untpd.AppliedTypeTree)(using Context): Tree = {
25182542
val tpt1 = withoutMode(Mode.Pattern):
25192543
typed(tree.tpt, AnyTypeConstructorProto)
25202544

25212545
tree.args match
25222546
case arg :: _ if arg.isTerm =>
2523-
if Feature.enabled(Feature.modularity) then
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 =>
2531-
return TypeTree(mt.instantiate(tree.args.map((typedExpr(_).tpe))))
2532-
else
2533-
return errorTree(tree, dependentMsg)
2547+
return typedTermAppliedTypeTree(tree, tpt1)
25342548
case _ =>
25352549

25362550
val tparams = tpt1.tpe.typeParams

tests/pos/applied_constructors.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import scala.language.future
44
class C(tracked val x: Int)
55
class D(tracked val c: C)
66
class E(tracked val c: D)
7-
class F[A](tracked val a: A)
7+
class F[A](tracked val a: Int)
8+
class G[A](tracked val a: A)
89

910
object Test extends App {
1011
val c: C(42) = C(42)
1112
val d: D(C(42)) = D(C(42))
1213
val e: E(D(C(42))) = E(D(C(42)))
13-
// val f: F[Int](42) = F(42)
14+
// val f: F[Int](42) = F[Int](42)
15+
// val g: G(42) = G(42)
1416
}

0 commit comments

Comments
 (0)