Skip to content

Commit 79ed159

Browse files
oderskyWojciechMazur
authored andcommitted
Treat more closure parameter types as inferred
This is necessary for types that contain possibly illegal @retains annotations since those annotations are only removed before pickling for InferredTypes. Fixes #21347
1 parent 78ce6ab commit 79ed159

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -1903,9 +1903,16 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19031903
if knownFormal then formal0
19041904
else errorType(AnonymousFunctionMissingParamType(param, tree, inferredType = formal, expectedType = pt), param.srcPos)
19051905
)
1906+
val untpdTpt = formal match
1907+
case _: WildcardType =>
1908+
// In this case we have a situation like f(_), where we expand in the end to
1909+
// (x: T) => f(x) and `T` is taken from `f`'s declared parameters. In this case
1910+
// we treat the type as declared instead of inferred. InferredType is used for
1911+
// types that are inferred from the context.
1912+
untpd.TypeTree()
1913+
case _ => InferredTypeTree()
19061914
val paramTpt = untpd.TypedSplice(
1907-
(if knownFormal then InferredTypeTree() else untpd.TypeTree())
1908-
.withType(paramType.translateFromRepeated(toArray = false))
1915+
untpdTpt.withType(paramType.translateFromRepeated(toArray = false))
19091916
.withSpan(param.span.endPos)
19101917
)
19111918
val param0 = cpy.ValDef(param)(tpt = paramTpt)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//> using scala 3.6.0-RC1-bin-SNAPSHOT
2+
3+
import language.experimental.captureChecking
4+
5+
class Box[Cap^] {}
6+
7+
def run[Cap^](f: Box[Cap]^{Cap^} => Unit): Box[Cap]^{Cap^} = ???
8+
9+
def main() =
10+
val b = run(_ => ())
11+
// val b = run[caps.CapSet](_ => ()) // this compiles

0 commit comments

Comments
 (0)