From 604f409cda5d35a93cb0aa0c08f4cedda7349522 Mon Sep 17 00:00:00 2001 From: noti0na1 Date: Wed, 12 Nov 2025 14:18:32 +0100 Subject: [PATCH 1/2] Do not search contextual function type in union types in general --- compiler/src/dotty/tools/dotc/core/Definitions.scala | 4 +++- .../pos/union-contains-contextual-function.scala | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/explicit-nulls/pos/union-contains-contextual-function.scala 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..13e343e8c618 --- /dev/null +++ b/tests/explicit-nulls/pos/union-contains-contextual-function.scala @@ -0,0 +1,7 @@ +class Test: + val x: ((a: String) ?=> Int) | Null = null + + val y: ((a: String) ?=> Int) | String = "hello" + + def f(g: ((a: String) ?=> Int) | Null = null) = ??? + \ No newline at end of file From 504068c2d7b0a70ba08e1893b50e33b6509fe4e5 Mon Sep 17 00:00:00 2001 From: noti0na1 Date: Thu, 13 Nov 2025 17:18:59 +0100 Subject: [PATCH 2/2] Update tests/explicit-nulls/pos/union-contains-contextual-function.scala MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ondřej Lhoták --- .../explicit-nulls/pos/union-contains-contextual-function.scala | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/explicit-nulls/pos/union-contains-contextual-function.scala b/tests/explicit-nulls/pos/union-contains-contextual-function.scala index 13e343e8c618..ed395dc74430 100644 --- a/tests/explicit-nulls/pos/union-contains-contextual-function.scala +++ b/tests/explicit-nulls/pos/union-contains-contextual-function.scala @@ -1,6 +1,8 @@ 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) = ???