Skip to content

Commit 4e91211

Browse files
authored
Do not search contextual function type in union types in general (#24393)
Being not able to assign `null` to `((a: String) ?=> Int) | Null` seems weird to me. The example is from https://users.scala-lang.org/t/type-a-parameter-as-either-a-contextual-function-null
2 parents 473246e + 504068c commit 4e91211

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,11 +2002,13 @@ class Definitions {
20022002
* - the upper bound of a TypeParamRef in the current constraint
20032003
*/
20042004
def asContextFunctionType(tp: Type)(using Context): Type =
2005-
tp.stripNull().stripTypeVar.dealias match
2005+
tp.stripTypeVar.dealias match
20062006
case tp1: TypeParamRef if ctx.typerState.constraint.contains(tp1) =>
20072007
asContextFunctionType(TypeComparer.bounds(tp1).hiBound)
20082008
case tp1 @ PolyFunctionOf(mt: MethodType) if mt.isContextualMethod =>
20092009
tp1
2010+
case tp: FlexibleType =>
2011+
asContextFunctionType(tp.hi)
20102012
case tp1 =>
20112013
if tp1.typeSymbol.name.isContextFunction && isFunctionNType(tp1) then tp1
20122014
else NoType
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Test:
2+
val x: ((a: String) ?=> Int) | Null = null
3+
4+
val x2: ((a: String) ?=> Int) | Null = (s: String) ?=> 42
5+
6+
val y: ((a: String) ?=> Int) | String = "hello"
7+
8+
def f(g: ((a: String) ?=> Int) | Null = null) = ???
9+

0 commit comments

Comments
 (0)