diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala index 372c6994e655..d2e4348c96fc 100644 --- a/compiler/src/dotty/tools/dotc/core/Definitions.scala +++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala @@ -2002,11 +2002,13 @@ class Definitions { * - the upper bound of a TypeParamRef in the current constraint */ def asContextFunctionType(tp: Type)(using Context): Type = - tp.stripNull().stripTypeVar.dealias match + tp.stripTypeVar.dealias match case tp1: TypeParamRef if ctx.typerState.constraint.contains(tp1) => asContextFunctionType(TypeComparer.bounds(tp1).hiBound) case tp1 @ PolyFunctionOf(mt: MethodType) if mt.isContextualMethod => tp1 + case tp: FlexibleType => + asContextFunctionType(tp.hi) case tp1 => if tp1.typeSymbol.name.isContextFunction && isFunctionNType(tp1) then tp1 else NoType diff --git a/tests/explicit-nulls/pos/union-contains-contextual-function.scala b/tests/explicit-nulls/pos/union-contains-contextual-function.scala new file mode 100644 index 000000000000..ed395dc74430 --- /dev/null +++ b/tests/explicit-nulls/pos/union-contains-contextual-function.scala @@ -0,0 +1,9 @@ +class Test: + val x: ((a: String) ?=> Int) | Null = null + + val x2: ((a: String) ?=> Int) | Null = (s: String) ?=> 42 + + val y: ((a: String) ?=> Int) | String = "hello" + + def f(g: ((a: String) ?=> Int) | Null = null) = ??? + \ No newline at end of file