From 483920640f016c7ccdb31144df7ca602d99297fa Mon Sep 17 00:00:00 2001 From: odersky Date: Thu, 5 Dec 2024 16:45:53 +0100 Subject: [PATCH] Widen singleton types when computing fields from .Fields Fixes #22018 --- .../src/dotty/tools/dotc/typer/Typer.scala | 2 +- tests/pos/i22018.scala | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i22018.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 6bb5d1ee70ff..1a3f5c2d97c3 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -868,7 +868,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer && pt != LhsProto then val pre = if !TypeOps.isLegalPrefix(qual.tpe) then SkolemType(qual.tpe) else qual.tpe - val fieldsType = pre.select(tpnme.Fields).dealias.simplified + val fieldsType = pre.select(tpnme.Fields).widenDealias.simplified val fields = fieldsType.namedTupleElementTypes typr.println(i"try dyn select $qual, $selName, $fields") fields.find(_._1 == selName) match diff --git a/tests/pos/i22018.scala b/tests/pos/i22018.scala new file mode 100644 index 000000000000..14f4733732be --- /dev/null +++ b/tests/pos/i22018.scala @@ -0,0 +1,18 @@ +import scala.language.experimental.namedTuples + +class SelectableNT[A <: NamedTuple.AnyNamedTuple](val nt: A) extends Selectable: + type Fields = A + def selectDynamic(x: String) = ??? + +object Test: + + val a = (name = "foo", age = 1) + + val sa = SelectableNT(a) + sa.name // ok + + type B = a.type + val b: B = a + + val sb = SelectableNT(b) + sb.name // fails \ No newline at end of file