From 6bea14bf843f705ad252adfc3ef5f8bd945b1c4d Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 19 Jul 2023 14:48:25 +0100 Subject: [PATCH] Avoid over widening in SpaceEngine --- .../src/dotty/tools/dotc/transform/patmat/Space.scala | 8 +++++++- tests/patmat/i17184.scala | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/patmat/i17184.scala diff --git a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala index c45cb1379a56..45c1444879db 100644 --- a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala +++ b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala @@ -578,7 +578,13 @@ object SpaceEngine { if (arity > 0) productSelectorTypes(resTp, unappSym.srcPos) else { - val getTp = resTp.select(nme.get).finalResultType.widenTermRefExpr + val getTp = resTp.select(nme.get).finalResultType match + case tp: TermRef if !tp.isOverloaded => + // Like widenTermRefExpr, except not recursively. + // For example, in i17184 widen Option[foo.type]#get + // to Option[foo.type] instead of Option[Int]. + tp.underlying.widenExpr + case tp => tp if (argLen == 1) getTp :: Nil else productSelectorTypes(getTp, unappSym.srcPos) } diff --git a/tests/patmat/i17184.scala b/tests/patmat/i17184.scala new file mode 100644 index 000000000000..9c04ade63510 --- /dev/null +++ b/tests/patmat/i17184.scala @@ -0,0 +1,9 @@ +class Foo +trait Bar: + val foo : Int + val f : Option[foo.type] = Some(foo) + + def g : Boolean = + f match + case None => false + case Some(_) => true