Skip to content

On invalid prefix, throw a TypeError #18061

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
Jun 26, 2023
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
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2751,7 +2751,8 @@ object Types {
NamedType(prefix, name, d)
}
if (prefix eq this.prefix) this
else if !NamedType.validPrefix(prefix) then UnspecifiedErrorType
else if !NamedType.validPrefix(prefix) then
throw TypeError(em"invalid new prefix $prefix cannot replace ${this.prefix} in type $this")
else if (lastDenotation == null) NamedType(prefix, designator)
else designator match {
case sym: Symbol =>
Expand Down Expand Up @@ -5400,6 +5401,9 @@ object Types {
def explanation(using Context): String = msg.message
}

/** Note: Make sure an errors is reported before construtcing this
* as the type of a tree.
*/
object ErrorType:
def apply(m: Message)(using Context): ErrorType =
val et = new PreviousErrorType
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,9 @@ class TreePickler(pickler: TastyPickler) {
case ex: AssertionError =>
println(i"error when pickling tree $tree")
throw ex
case ex: MatchError =>
println(i"error when pickling tree $tree")
throw ex
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/neg/i12448.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
object Main {
def mkArray[T <: A]: T#AType // error // error
mkArray[Array] // was: "assertion failed: invalid prefix HKTypeLambda..."
val x = mkArray[Array]
mkArray[Array] // was: "assertion failed: invalid prefix HKTypeLambda..." // error
val x = mkArray[Array] // error
}
4 changes: 4 additions & 0 deletions tests/neg/i16842.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/i16842.scala:24:7 ----------------------------------------------------------------------------------
24 | Liter(SemanticArray[SemanticInt.type], x) // error
| ^
| invalid new prefix (dim: Int): SemanticArray[SemanticInt.type] cannot replace ty.type in type ty.T
2 changes: 1 addition & 1 deletion tests/neg/i16842.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ def typecheckArrayLiter(
a: ArrayLiter
): Liter[SemanticArray[SemanticType]] = {
val x: List[Expr2[SemanticInt.type]] = List()
Liter(SemanticArray[SemanticInt.type], x) // error // error
Liter(SemanticArray[SemanticInt.type], x) // error
}
4 changes: 4 additions & 0 deletions tests/neg/i18058.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/i18058.scala:4:21 ----------------------------------------------------------------------------------
4 |type G = (f: _ <: F) => f.A // error
| ^
| invalid new prefix <: F cannot replace f.type in type f.A
4 changes: 4 additions & 0 deletions tests/neg/i18058.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trait F:
type A

type G = (f: _ <: F) => f.A // error