Skip to content

Move classType checks for new expressions to PostTyper #11788

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 2 commits into from
Mar 17, 2021
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
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
case Select(nu: New, nme.CONSTRUCTOR) if isCheckable(nu) =>
// need to check instantiability here, because the type of the New itself
// might be a type constructor.
ctx.typer.checkClassType(tree.tpe, tree.srcPos, traitReq = false, stablePrefixReq = true)
Checking.checkInstantiable(tree.tpe, nu.srcPos)
withNoCheckNews(nu :: Nil)(app1)
case _ =>
Expand Down
4 changes: 0 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,6 @@ class Typer extends Namer
case _ =>
var tpt1 = typedType(tree.tpt)
tpt1 = tpt1.withType(ensureAccessible(tpt1.tpe, superAccess = false, tpt1.srcPos))

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

tpt1 match {
case AppliedTypeTree(_, targs) =>
for case targ: TypeBoundsTree <- targs do
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-custom-args/fatal-warnings/pureStatement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Test {

doSideEffects(1) // error: pure expression does nothing in statement position

val broken = new IDontExist("") // error // error
val broken = new IDontExist("") // error
broken.foo // no extra error, and no pure expression warning
broken.foo() // same
}
14 changes: 7 additions & 7 deletions tests/neg/alloc-abstract.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ class Test[T] {

type Foo[T] = Array[T]

new T // error: not a class type
new T() // error: not a class type
new U // error: not a class type
new U() // error: not a class type
new IArray[String] // error: not a class type
new IArray[String]() // error: not a class type
new IArray[String](10) // error: not a class type // error: too mamy arguments
new T // error: does not have a constructor
new T() // error: does not have a constructor
new U // error: does not have a constructor
new U() // error: does not have a constructor
new IArray[String] // error: does not have a constructor
new IArray[String]() // error: does not have a constructor
new IArray[String](10) // error: does not have a constructor

new Foo[String](10) // ok
}
24 changes: 3 additions & 21 deletions tests/neg/creator-applys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,8 @@ class Test {
def run = s"C $x $y"
}

val x1 = new Test().A() // error: object A does not take parameters
val x2 = new Test().B() // error: value B is not a member of Test
val x3 = new Test().B[Int]() // error: value B is not a member of Test
val x1 = new Test().A() // error: not stable
val x2 = new Test().B() // error: not stable
val x3 = new Test().B[Int]() // error: not stable
}


object Test2 {
class A(s: String = "A") {
def run = s
}
object A {
def apply() = A("X") // error: recursive method needs return type
}
}

object Test3 {
class A(s: String = "A") {
def run = s
}
object A {
def apply(): A = A("X") // error too many arguments
}
}
18 changes: 18 additions & 0 deletions tests/neg/creator-applys2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

object Test2 {
class A(s: String = "A") {
def run = s
}
object A {
def apply() = A("X") // error: recursive method needs return type
}
}

object Test3 {
class A(s: String = "A") {
def run = s
}
object A {
def apply(): A = A("X") // error too many arguments
}
}
4 changes: 4 additions & 0 deletions tests/neg/i11781.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

trait A:
type Foo
new Foo // error: does not have a constructor
14 changes: 4 additions & 10 deletions tests/neg/i8569.check
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
-- [E083] Type Error: tests/neg/i8569.scala:8:8 ------------------------------------------------------------------------
8 | outer.Inner(2) // error
| ^^^^^^^^^^^
| Outer is not a valid class prefix, since it is not an immutable path

longer explanation available when compiling with `-explain`
-- [E083] Type Error: tests/neg/i8569.scala:9:6 ------------------------------------------------------------------------
9 | new outer.Inner(2) // error
| ^^^^^
| (Test.outer : => Outer) is not a valid type prefix, since it is not an immutable path
-- [E083] Type Error: tests/neg/i8569.scala:13:21 ----------------------------------------------------------------------
13 | val x = outer.Inner(2) // error (at posttyper)
| ^^^^^^^^^^^^^^
| Outer is not a valid class prefix, since it is not an immutable path

longer explanation available when compiling with `-explain`
8 changes: 6 additions & 2 deletions tests/neg/i8569.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ class Outer(x: Int) {
class Inner(y: Int) {
}
}
class Outer2(x: Int) {
class Inner(y: Int) {
}
}
object Test {
def outer = Outer(1)
def outer2 = Outer2(1)

outer.Inner(2) // error
new outer.Inner(2) // error
val x = outer.Inner(2) // error (at posttyper)
}
6 changes: 6 additions & 0 deletions tests/neg/i8569a.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- [E083] Type Error: tests/neg/i8569a.scala:13:14 ---------------------------------------------------------------------
13 | val x = new outer2.Inner(2) // error (at typer)
| ^^^^^^
| (Test.outer2 : => Outer2) is not a valid type prefix, since it is not an immutable path

longer explanation available when compiling with `-explain`
14 changes: 14 additions & 0 deletions tests/neg/i8569a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Outer(x: Int) {
class Inner(y: Int) {
}
}
class Outer2(x: Int) {
class Inner(y: Int) {
}
}
object Test {
def outer = Outer(1)
def outer2 = Outer2(1)

val x = new outer2.Inner(2) // error (at typer)
}
2 changes: 1 addition & 1 deletion tests/neg/iarrays.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
object Test {

// Can't allocate an IArray
new IArray[String](10) // error: not a class type // error: too many arguments
new IArray[String](10) // error: does not have a constructor

val xs = IArray(1, 2, 3)

Expand Down
24 changes: 24 additions & 0 deletions tests/pos/i11781.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
object Test:
class C1[A](a: A)
type D1[A] = C1[A]
new D1(1)

class C2[B, A](a: A)
type D2[A] = C2[Int, A]
new D2(1)
new D2[Int](1)

class E(x: Int) extends D2[Int](x)
class F(x: Int) extends D2(x)

object Ah {
final case class Values[B, +A](a: A)

trait Object[B] {
final type Values[+A] = Ah.Values[B, A]
object Values {
def blah: Values[Unit] =
new Values(())
}
}
}