Skip to content

Commit 668b160

Browse files
authored
On invalid prefix, throw a TypeError (#18061)
Previously, the type was set to UnspecifiedErrorType. But that violates the rule that every ErrorType has to be reported. If that does not happen, crashes in pickler will result. Fixes #18058
2 parents 3b5c8c0 + 7a68165 commit 668b160

File tree

7 files changed

+23
-4
lines changed

7 files changed

+23
-4
lines changed

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,8 @@ object Types {
27622762
NamedType(prefix, name, d)
27632763
}
27642764
if (prefix eq this.prefix) this
2765-
else if !NamedType.validPrefix(prefix) then UnspecifiedErrorType
2765+
else if !NamedType.validPrefix(prefix) then
2766+
throw TypeError(em"invalid new prefix $prefix cannot replace ${this.prefix} in type $this")
27662767
else if (lastDenotation == null) NamedType(prefix, designator)
27672768
else designator match {
27682769
case sym: Symbol =>
@@ -5411,6 +5412,9 @@ object Types {
54115412
def explanation(using Context): String = msg.message
54125413
}
54135414

5415+
/** Note: Make sure an errors is reported before construtcing this
5416+
* as the type of a tree.
5417+
*/
54145418
object ErrorType:
54155419
def apply(m: Message)(using Context): ErrorType =
54165420
val et = new PreviousErrorType

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,9 @@ class TreePickler(pickler: TastyPickler) {
699699
case ex: AssertionError =>
700700
println(i"error when pickling tree $tree")
701701
throw ex
702+
case ex: MatchError =>
703+
println(i"error when pickling tree $tree")
704+
throw ex
702705
}
703706
}
704707

tests/neg/i12448.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Main {
22
def mkArray[T <: A]: T#AType // error // error
3-
mkArray[Array] // was: "assertion failed: invalid prefix HKTypeLambda..."
4-
val x = mkArray[Array]
3+
mkArray[Array] // was: "assertion failed: invalid prefix HKTypeLambda..." // error
4+
val x = mkArray[Array] // error
55
}

tests/neg/i16842.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/i16842.scala:24:7 ----------------------------------------------------------------------------------
2+
24 | Liter(SemanticArray[SemanticInt.type], x) // error
3+
| ^
4+
| invalid new prefix (dim: Int): SemanticArray[SemanticInt.type] cannot replace ty.type in type ty.T

tests/neg/i16842.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ def typecheckArrayLiter(
2121
a: ArrayLiter
2222
): Liter[SemanticArray[SemanticType]] = {
2323
val x: List[Expr2[SemanticInt.type]] = List()
24-
Liter(SemanticArray[SemanticInt.type], x) // error // error
24+
Liter(SemanticArray[SemanticInt.type], x) // error
2525
}

tests/neg/i18058.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/i18058.scala:4:21 ----------------------------------------------------------------------------------
2+
4 |type G = (f: _ <: F) => f.A // error
3+
| ^
4+
| invalid new prefix <: F cannot replace f.type in type f.A

tests/neg/i18058.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
trait F:
2+
type A
3+
4+
type G = (f: _ <: F) => f.A // error

0 commit comments

Comments
 (0)