From f8798d87668f039701ed528caa743c6bda2ed8a1 Mon Sep 17 00:00:00 2001 From: Guillaume Martres Date: Wed, 22 May 2024 18:21:09 +0200 Subject: [PATCH] Fix NamedTuple selection on an unstable prefix Without a stable prefix, asSeenFrom could end up widening `Fields` to `>: Nothing <: Any`. --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 3 ++- tests/pos/named-tuple-unstable.scala | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/pos/named-tuple-unstable.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index ae50d626cb1f..4bcf4f6632c8 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -826,7 +826,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer if qual.tpe.derivesFrom(defn.SelectableClass) && !isDynamicExpansion(tree) && !pt.isInstanceOf[FunOrPolyProto] && pt != LhsProto then - val fieldsType = qual.tpe.select(tpnme.Fields).dealias.simplified + val pre = if !TypeOps.isLegalPrefix(qual.tpe) then SkolemType(qual.tpe) else qual.tpe + val fieldsType = pre.select(tpnme.Fields).dealias.simplified val fields = fieldsType.namedTupleElementTypes typr.println(i"try dyn select $qual, $selName, $fields") fields.find(_._1 == selName) match diff --git a/tests/pos/named-tuple-unstable.scala b/tests/pos/named-tuple-unstable.scala new file mode 100644 index 000000000000..6a6a36732a14 --- /dev/null +++ b/tests/pos/named-tuple-unstable.scala @@ -0,0 +1,15 @@ +import scala.language.experimental.namedTuples +import NamedTuple.{AnyNamedTuple, NamedTuple} + +trait Foo extends Selectable: + val f: Any + type Fields = (myfield: f.type) + def selectDynamic(name: String): Any + +object Test: + val elem1: Foo { val f: Int } = ??? + def elem2: Foo { val f: Int } = ??? + + def test: Unit = + val a: Int = elem1.myfield // OK + val b: Int = elem2.myfield // error: value myfield is not a member of Foo { val f: Int }