Skip to content

Commit a0833b3

Browse files
committed
Extract applied constructors logic to a separate function
1 parent 896c844 commit a0833b3

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
@@ -1335,7 +1335,7 @@ object Parsers {
13351335
* | SimpleLiteral
13361336
* | Singleton ‘.’ id
13371337
* | Singleton ‘(’ Singletons ‘)’
1338-
* | Singleton ‘[’ Types ‘]’
1338+
* -- not yet | Singleton ‘[’ Types ‘]’
13391339
*/
13401340
def singleton(): Tree =
13411341
val res =
@@ -1346,8 +1346,8 @@ object Parsers {
13461346
def singletonArgs(t: Tree): Tree =
13471347
if in.token == LPAREN && in.featureEnabled(Feature.modularity) then
13481348
singletonArgs(AppliedTypeTree(t, inParensWithCommas(commaSeparated(singleton))))
1349-
else if in.token == LBRACKET && in.featureEnabled(Feature.modularity) then
1350-
singletonArgs(AppliedTypeTree(t, inBrackets(commaSeparated(() => typ()))))
1349+
// else if in.token == LBRACKET && in.featureEnabled(Feature.modularity) then
1350+
// 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?
13511351
else t
13521352

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

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

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

2559+
/** Type applied dependent class constructors in type positions */
2560+
private def typedTermAppliedTypeTree(tree: untpd.AppliedTypeTree, tpt1: Tree)(using Context): Tree = {
2561+
if Feature.enabled(Feature.modularity) then
2562+
val constr =
2563+
if tpt1.tpe.typeSymbol.primaryConstructor.exists then
2564+
tpt1.tpe.typeSymbol.primaryConstructor
2565+
else
2566+
tpt1.tpe.typeSymbol.companionClass.primaryConstructor
2567+
// TODO(kπ) vvvvvvv Might want to take the first term list? or all term lists? depends on the rest of the logic.
2568+
constr.paramSymss.flatten.foreach { p =>
2569+
if p.isTerm && !p.flags.is(Tracked) then
2570+
report.error(
2571+
em"""The constructor parameter `${p.name}` of `${tpt1.tpe}` is not tracked.
2572+
|Only tracked parameters are allowed in dependent constructor applications.""",
2573+
tree.srcPos
2574+
)
2575+
}
2576+
constr.typeRef.underlying match
2577+
case mt: MethodOrPoly =>
2578+
TypeTree(mt.instantiate(tree.args.map((typedExpr(_).tpe))))
2579+
else
2580+
errorTree(tree, dependentMsg)
2581+
}
2582+
25592583
def typedAppliedTypeTree(tree: untpd.AppliedTypeTree)(using Context): Tree = {
25602584
val tpt1 = withoutMode(Mode.Pattern):
25612585
typed(tree.tpt, AnyTypeConstructorProto)
25622586

25632587
tree.args match
25642588
case arg :: _ if arg.isTerm =>
2565-
if Feature.enabled(Feature.modularity) then
2566-
val constr =
2567-
if tpt1.tpe.typeSymbol.primaryConstructor.exists then
2568-
tpt1.tpe.typeSymbol.primaryConstructor
2569-
else
2570-
tpt1.tpe.typeSymbol.companionClass.primaryConstructor
2571-
constr.typeRef.underlying match
2572-
case mt: MethodOrPoly =>
2573-
return TypeTree(mt.instantiate(tree.args.map((typedExpr(_).tpe))))
2574-
else
2575-
return errorTree(tree, dependentMsg)
2589+
return typedTermAppliedTypeTree(tree, tpt1)
25762590
case _ =>
25772591

25782592
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)