Skip to content

Commit f267f91

Browse files
authored
Merge pull request #11788 from dotty-staging/fix-11781
Move classType checks for new expressions to PostTyper
2 parents 2075e4c + eb7e94a commit f267f91

File tree

13 files changed

+89
-46
lines changed

13 files changed

+89
-46
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
295295
case Select(nu: New, nme.CONSTRUCTOR) if isCheckable(nu) =>
296296
// need to check instantiability here, because the type of the New itself
297297
// might be a type constructor.
298+
ctx.typer.checkClassType(tree.tpe, tree.srcPos, traitReq = false, stablePrefixReq = true)
298299
Checking.checkInstantiable(tree.tpe, nu.srcPos)
299300
withNoCheckNews(nu :: Nil)(app1)
300301
case _ =>

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,6 @@ class Typer extends Namer
772772
case _ =>
773773
var tpt1 = typedType(tree.tpt)
774774
tpt1 = tpt1.withType(ensureAccessible(tpt1.tpe, superAccess = false, tpt1.srcPos))
775-
776-
if (checkClassType(typeOfNew(tpt1), tpt1.srcPos, traitReq = false, stablePrefixReq = true) eq defn.ObjectType)
777-
tpt1 = TypeTree(defn.ObjectType).withSpan(tpt1.span)
778-
779775
tpt1 match {
780776
case AppliedTypeTree(_, targs) =>
781777
for case targ: TypeBoundsTree <- targs do

tests/neg-custom-args/fatal-warnings/pureStatement.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object Test {
2626

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

29-
val broken = new IDontExist("") // error // error
29+
val broken = new IDontExist("") // error
3030
broken.foo // no extra error, and no pure expression warning
3131
broken.foo() // same
3232
}

tests/neg/alloc-abstract.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ class Test[T] {
33

44
type Foo[T] = Array[T]
55

6-
new T // error: not a class type
7-
new T() // error: not a class type
8-
new U // error: not a class type
9-
new U() // error: not a class type
10-
new IArray[String] // error: not a class type
11-
new IArray[String]() // error: not a class type
12-
new IArray[String](10) // error: not a class type // error: too mamy arguments
6+
new T // error: does not have a constructor
7+
new T() // error: does not have a constructor
8+
new U // error: does not have a constructor
9+
new U() // error: does not have a constructor
10+
new IArray[String] // error: does not have a constructor
11+
new IArray[String]() // error: does not have a constructor
12+
new IArray[String](10) // error: does not have a constructor
1313

1414
new Foo[String](10) // ok
1515
}

tests/neg/creator-applys.scala

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,8 @@ class Test {
1010
def run = s"C $x $y"
1111
}
1212

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

18-
19-
object Test2 {
20-
class A(s: String = "A") {
21-
def run = s
22-
}
23-
object A {
24-
def apply() = A("X") // error: recursive method needs return type
25-
}
26-
}
27-
28-
object Test3 {
29-
class A(s: String = "A") {
30-
def run = s
31-
}
32-
object A {
33-
def apply(): A = A("X") // error too many arguments
34-
}
35-
}

tests/neg/creator-applys2.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
object Test2 {
3+
class A(s: String = "A") {
4+
def run = s
5+
}
6+
object A {
7+
def apply() = A("X") // error: recursive method needs return type
8+
}
9+
}
10+
11+
object Test3 {
12+
class A(s: String = "A") {
13+
def run = s
14+
}
15+
object A {
16+
def apply(): A = A("X") // error too many arguments
17+
}
18+
}

tests/neg/i11781.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
trait A:
3+
type Foo
4+
new Foo // error: does not have a constructor

tests/neg/i8569.check

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
-- [E083] Type Error: tests/neg/i8569.scala:8:8 ------------------------------------------------------------------------
2-
8 | outer.Inner(2) // error
3-
| ^^^^^^^^^^^
4-
| Outer is not a valid class prefix, since it is not an immutable path
5-
6-
longer explanation available when compiling with `-explain`
7-
-- [E083] Type Error: tests/neg/i8569.scala:9:6 ------------------------------------------------------------------------
8-
9 | new outer.Inner(2) // error
9-
| ^^^^^
10-
| (Test.outer : => Outer) is not a valid type prefix, since it is not an immutable path
1+
-- [E083] Type Error: tests/neg/i8569.scala:13:21 ----------------------------------------------------------------------
2+
13 | val x = outer.Inner(2) // error (at posttyper)
3+
| ^^^^^^^^^^^^^^
4+
| Outer is not a valid class prefix, since it is not an immutable path
115

126
longer explanation available when compiling with `-explain`

tests/neg/i8569.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ class Outer(x: Int) {
22
class Inner(y: Int) {
33
}
44
}
5+
class Outer2(x: Int) {
6+
class Inner(y: Int) {
7+
}
8+
}
59
object Test {
610
def outer = Outer(1)
11+
def outer2 = Outer2(1)
712

8-
outer.Inner(2) // error
9-
new outer.Inner(2) // error
13+
val x = outer.Inner(2) // error (at posttyper)
1014
}

tests/neg/i8569a.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E083] Type Error: tests/neg/i8569a.scala:13:14 ---------------------------------------------------------------------
2+
13 | val x = new outer2.Inner(2) // error (at typer)
3+
| ^^^^^^
4+
| (Test.outer2 : => Outer2) is not a valid type prefix, since it is not an immutable path
5+
6+
longer explanation available when compiling with `-explain`

tests/neg/i8569a.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Outer(x: Int) {
2+
class Inner(y: Int) {
3+
}
4+
}
5+
class Outer2(x: Int) {
6+
class Inner(y: Int) {
7+
}
8+
}
9+
object Test {
10+
def outer = Outer(1)
11+
def outer2 = Outer2(1)
12+
13+
val x = new outer2.Inner(2) // error (at typer)
14+
}

tests/neg/iarrays.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
object Test {
22

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

66
val xs = IArray(1, 2, 3)
77

tests/pos/i11781.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
object Test:
2+
class C1[A](a: A)
3+
type D1[A] = C1[A]
4+
new D1(1)
5+
6+
class C2[B, A](a: A)
7+
type D2[A] = C2[Int, A]
8+
new D2(1)
9+
new D2[Int](1)
10+
11+
class E(x: Int) extends D2[Int](x)
12+
class F(x: Int) extends D2(x)
13+
14+
object Ah {
15+
final case class Values[B, +A](a: A)
16+
17+
trait Object[B] {
18+
final type Values[+A] = Ah.Values[B, A]
19+
object Values {
20+
def blah: Values[Unit] =
21+
new Values(())
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)