Skip to content

Guard against invalid prefixes in argForParam #23508

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2302,7 +2302,7 @@ object Types extends TypeUtils {
def _1: Type
def _2: Designator

assert(NamedType.validPrefix(prefix), s"invalid prefix $prefix")
if !NamedType.validPrefix(prefix) then throw InvalidPrefix()

private var myName: Name | Null = null
private var lastDenotation: Denotation | Null = null
Expand Down Expand Up @@ -3067,6 +3067,8 @@ object Types extends TypeUtils {
apply(prefix, designatorFor(prefix, name, denot)).withDenot(denot)
}

class InvalidPrefix extends Exception

// --- Other SingletonTypes: ThisType/SuperType/ConstantType ---------------------------

/** The type cls.this
Expand Down
15 changes: 13 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Uniques.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core

import Types.*, Contexts.*, util.Stats.*, Hashable.*, Names.*
import config.Config
import Symbols.Symbol
import Decorators.*
import util.{WeakHashSet, Stats}
import WeakHashSet.Entry
Expand Down Expand Up @@ -41,8 +42,10 @@ object Uniques:
val h = doHash(null, designator, prefix)
if monitored then recordCaching(h, classOf[NamedType])
def newType =
if (isTerm) new CachedTermRef(prefix, designator, h)
else new CachedTypeRef(prefix, designator, h)
try
if isTerm then new CachedTermRef(prefix, designator, h)
else new CachedTypeRef(prefix, designator, h)
catch case ex: InvalidPrefix => badPrefix(prefix, designator)
if h == NotCached then newType
else
// Inlined from WeakHashSet#put
Expand All @@ -61,6 +64,14 @@ object Uniques:

linkedListLoop(oldHead)
end if
end enterIfNew

private def badPrefix(prefix: Type, desig: Designator)(using Context): Nothing =
def name = desig match
case desig: Name => desig
case desig: Symbol => desig.name
throw TypeError(em"invalid prefix $prefix when trying to form $prefix . $name")

end NamedTypeUniques

final class AppliedUniques extends WeakHashSet[AppliedType](Config.initialUniquesCapacity * 2) with Hashable:
Expand Down
3 changes: 3 additions & 0 deletions compiler/test/dotc/neg-best-effort-unpickling.excludelist
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ context-function-syntax.scala

# Failure to disambiguate overloaded reference
i23402b.scala

# Unhandled TypeError exception
i23504.scala
3 changes: 3 additions & 0 deletions tests/neg/i23504.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test =
Seq.empty[[T] =>> () => ?].head() // error
Seq.empty[[T] =>> Int => Int].head(1) // error
Loading