Skip to content

Commit ee55bde

Browse files
authored
Guard against invalid prefixes in argForParam (#23508)
Fixes #23504
2 parents 699296f + 4a959b1 commit ee55bde

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2304,7 +2304,7 @@ object Types extends TypeUtils {
23042304
def _1: Type
23052305
def _2: Designator
23062306

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

23092309
private var myName: Name | Null = null
23102310
private var lastDenotation: Denotation | Null = null
@@ -3069,6 +3069,8 @@ object Types extends TypeUtils {
30693069
apply(prefix, designatorFor(prefix, name, denot)).withDenot(denot)
30703070
}
30713071

3072+
class InvalidPrefix extends Exception
3073+
30723074
// --- Other SingletonTypes: ThisType/SuperType/ConstantType ---------------------------
30733075

30743076
/** The type cls.this

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33

44
import Types.*, Contexts.*, util.Stats.*, Hashable.*, Names.*
55
import config.Config
6+
import Symbols.Symbol
67
import Decorators.*
78
import util.{WeakHashSet, Stats}
89
import WeakHashSet.Entry
@@ -41,8 +42,10 @@ object Uniques:
4142
val h = doHash(null, designator, prefix)
4243
if monitored then recordCaching(h, classOf[NamedType])
4344
def newType =
44-
if (isTerm) new CachedTermRef(prefix, designator, h)
45-
else new CachedTypeRef(prefix, designator, h)
45+
try
46+
if isTerm then new CachedTermRef(prefix, designator, h)
47+
else new CachedTypeRef(prefix, designator, h)
48+
catch case ex: InvalidPrefix => badPrefix(prefix, designator)
4649
if h == NotCached then newType
4750
else
4851
// Inlined from WeakHashSet#put
@@ -61,6 +64,14 @@ object Uniques:
6164

6265
linkedListLoop(oldHead)
6366
end if
67+
end enterIfNew
68+
69+
private def badPrefix(prefix: Type, desig: Designator)(using Context): Nothing =
70+
def name = desig match
71+
case desig: Name => desig
72+
case desig: Symbol => desig.name
73+
throw TypeError(em"invalid prefix $prefix when trying to form $prefix . $name")
74+
6475
end NamedTypeUniques
6576

6677
final class AppliedUniques extends WeakHashSet[AppliedType](Config.initialUniquesCapacity * 2) with Hashable:

compiler/test/dotc/neg-best-effort-unpickling.excludelist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ context-function-syntax.scala
2424

2525
# Failure to disambiguate overloaded reference
2626
i23402b.scala
27+
28+
# Unhandled TypeError exception
29+
i23504.scala

tests/neg/i23504.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def test =
2+
Seq.empty[[T] =>> () => ?].head() // error
3+
Seq.empty[[T] =>> Int => Int].head(1) // error

0 commit comments

Comments
 (0)