Skip to content

Commit f59f2ba

Browse files
authored
Merge pull request #6097 from dotty-staging/fix-new-alias
Fix typing of new
2 parents ce0e3f2 + 5b87533 commit f59f2ba

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
331331
if (tree.hasType && tree.symbol == defn.QuotedExpr_splice) keywordStr("${") ~ toTextLocal(qual) ~ keywordStr("}")
332332
else if (tree.hasType && tree.symbol == defn.QuotedType_splice) typeText("${") ~ toTextLocal(qual) ~ typeText("}")
333333
else if (qual.isType) toTextLocal(qual) ~ "#" ~ typeText(toText(name))
334-
else toTextLocal(qual) ~ ("." ~ nameIdText(tree) provided name != nme.CONSTRUCTOR)
334+
else toTextLocal(qual) ~ ("." ~ nameIdText(tree) provided (name != nme.CONSTRUCTOR || ctx.settings.YprintDebug.value))
335335
case tree: This =>
336336
optDotPrefix(tree) ~ keywordStr("this") ~ idText(tree)
337337
case Super(qual: This, mix) =>
@@ -356,11 +356,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
356356
keywordStr("new ") ~ {
357357
tpt match {
358358
case tpt: Template => toTextTemplate(tpt, ofNew = true)
359-
case _ =>
360-
if (tpt.hasType)
361-
toTextLocal(tpt.typeOpt.underlyingClassRef(refinementOK = false))
362-
else
363-
toTextLocal(tpt)
359+
case _ => toTextLocal(tpt)
364360
}
365361
}
366362
case Typed(expr, tpt) =>

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,16 @@ trait TypeAssigner {
301301
ConstFold(tree.withType(tp))
302302
}
303303

304+
/** Normalize type T appearing in a new T by following eta expansions to
305+
* avoid higher-kinded types.
306+
*/
307+
def typeOfNew(tpt: Tree)(implicit ctx: Context): Type = tpt.tpe.dealias match {
308+
case TypeApplications.EtaExpansion(tycon) => tycon
309+
case t => tpt.tpe
310+
}
311+
304312
def assignType(tree: untpd.New, tpt: Tree)(implicit ctx: Context): New =
305-
tree.withType(tpt.tpe)
313+
tree.withType(typeOfNew(tpt))
306314

307315
def assignType(tree: untpd.Literal)(implicit ctx: Context): Literal =
308316
tree.withType {

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,8 @@ class Typer extends Namer
534534
case _ =>
535535
var tpt1 = typedType(tree.tpt)
536536
tpt1 = tpt1.withType(ensureAccessible(tpt1.tpe, superAccess = false, tpt1.sourcePos))
537-
tpt1.tpe.dealias match {
538-
case TypeApplications.EtaExpansion(tycon) => tpt1 = tpt1.withType(tycon)
539-
case _ =>
540-
}
541-
if (checkClassType(tpt1.tpe, tpt1.sourcePos, traitReq = false, stablePrefixReq = true) eq defn.ObjectType)
537+
538+
if (checkClassType(typeOfNew(tpt1), tpt1.sourcePos, traitReq = false, stablePrefixReq = true) eq defn.ObjectType)
542539
tpt1 = TypeTree(defn.ObjectType).withSpan(tpt1.span)
543540

544541
tpt1 match {
@@ -549,7 +546,6 @@ class Typer extends Namer
549546
}
550547

551548
assignType(cpy.New(tree)(tpt1), tpt1)
552-
// todo in a later phase: checkInstantiatable(cls, tpt1.pos)
553549
}
554550
}
555551

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,8 @@ trait Printers
593593
printParent(fun)
594594
if (!args.isEmpty || needEmptyParens)
595595
inParens(printTrees(args, ", "))
596-
case IsTerm(Term.Select(Term.New(tpt), _)) =>
597-
printTypeTree(tpt)
596+
case IsTerm(Term.Select(Term.IsNew(newTree), _)) =>
597+
printType(newTree.tpe)
598598
case IsTerm(parent) =>
599599
throw new MatchError(parent.show)
600600
}
@@ -772,9 +772,9 @@ trait Printers
772772
}
773773
this += "this"
774774

775-
case Term.New(tpt) =>
775+
case Term.IsNew(tree) =>
776776
this += "new "
777-
printTypeTree(tpt)
777+
printType(tree.tpe)
778778

779779
case Term.NamedArg(name, arg) =>
780780
this += name += " = "

tests/pos/alias-new.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object O {
2+
class B[T]
3+
}
4+
object O2 {
5+
type B[T] = O.B[T]
6+
}
7+
object Test {
8+
val x: O2.B[String] = new O2.B()
9+
}

tests/run-with-compiler/i3847-b.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
new scala.Array[scala.List[scala.Int]](1)
1+
new scala.Array[scala.collection.immutable.List[scala.Int]](1)

0 commit comments

Comments
 (0)