Skip to content

Fix typing of new #6097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
if (tree.hasType && tree.symbol == defn.QuotedExpr_splice) keywordStr("${") ~ toTextLocal(qual) ~ keywordStr("}")
else if (tree.hasType && tree.symbol == defn.QuotedType_splice) typeText("${") ~ toTextLocal(qual) ~ typeText("}")
else if (qual.isType) toTextLocal(qual) ~ "#" ~ typeText(toText(name))
else toTextLocal(qual) ~ ("." ~ nameIdText(tree) provided name != nme.CONSTRUCTOR)
else toTextLocal(qual) ~ ("." ~ nameIdText(tree) provided (name != nme.CONSTRUCTOR || ctx.settings.YprintDebug.value))
case tree: This =>
optDotPrefix(tree) ~ keywordStr("this") ~ idText(tree)
case Super(qual: This, mix) =>
Expand All @@ -356,11 +356,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
keywordStr("new ") ~ {
tpt match {
case tpt: Template => toTextTemplate(tpt, ofNew = true)
case _ =>
if (tpt.hasType)
toTextLocal(tpt.typeOpt.underlyingClassRef(refinementOK = false))
else
toTextLocal(tpt)
case _ => toTextLocal(tpt)
}
}
case Typed(expr, tpt) =>
Expand Down
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,16 @@ trait TypeAssigner {
ConstFold(tree.withType(tp))
}

/** Normalize type T appearing in a new T by following eta expansions to
* avoid higher-kinded types.
*/
def typeOfNew(tpt: Tree)(implicit ctx: Context): Type = tpt.tpe.dealias match {
case TypeApplications.EtaExpansion(tycon) => tycon
case t => tpt.tpe
}

def assignType(tree: untpd.New, tpt: Tree)(implicit ctx: Context): New =
tree.withType(tpt.tpe)
tree.withType(typeOfNew(tpt))

def assignType(tree: untpd.Literal)(implicit ctx: Context): Literal =
tree.withType {
Expand Down
8 changes: 2 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,8 @@ class Typer extends Namer
case _ =>
var tpt1 = typedType(tree.tpt)
tpt1 = tpt1.withType(ensureAccessible(tpt1.tpe, superAccess = false, tpt1.sourcePos))
tpt1.tpe.dealias match {
case TypeApplications.EtaExpansion(tycon) => tpt1 = tpt1.withType(tycon)
case _ =>
}
if (checkClassType(tpt1.tpe, tpt1.sourcePos, traitReq = false, stablePrefixReq = true) eq defn.ObjectType)

if (checkClassType(typeOfNew(tpt1), tpt1.sourcePos, traitReq = false, stablePrefixReq = true) eq defn.ObjectType)
tpt1 = TypeTree(defn.ObjectType).withSpan(tpt1.span)

tpt1 match {
Expand All @@ -548,7 +545,6 @@ class Typer extends Namer
}

assignType(cpy.New(tree)(tpt1), tpt1)
// todo in a later phase: checkInstantiatable(cls, tpt1.pos)
}
}

Expand Down
8 changes: 4 additions & 4 deletions library/src/scala/tasty/reflect/Printers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,8 @@ trait Printers
printParent(fun)
if (!args.isEmpty || needEmptyParens)
inParens(printTrees(args, ", "))
case IsTerm(Term.Select(Term.New(tpt), _)) =>
printTypeTree(tpt)
case IsTerm(Term.Select(Term.IsNew(newTree), _)) =>
printType(newTree.tpe)
case IsTerm(parent) =>
throw new MatchError(parent.show)
}
Expand Down Expand Up @@ -772,9 +772,9 @@ trait Printers
}
this += "this"

case Term.New(tpt) =>
case Term.IsNew(tree) =>
this += "new "
printTypeTree(tpt)
printType(tree.tpe)

case Term.NamedArg(name, arg) =>
this += name += " = "
Expand Down
9 changes: 9 additions & 0 deletions tests/pos/alias-new.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object O {
class B[T]
}
object O2 {
type B[T] = O.B[T]
}
object Test {
val x: O2.B[String] = new O2.B()
}
2 changes: 1 addition & 1 deletion tests/run-with-compiler/i3847-b.check
Original file line number Diff line number Diff line change
@@ -1 +1 @@
new scala.Array[scala.List[scala.Int]](1)
new scala.Array[scala.collection.immutable.List[scala.Int]](1)