Skip to content

Commit 6233495

Browse files
authored
Fix NamedTuple selection on an unstable prefix (#20455)
Without a stable prefix, asSeenFrom could end up widening `Fields` to `>: Nothing <: Any`.
2 parents f99f268 + f8798d8 commit 6233495

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
826826
if qual.tpe.derivesFrom(defn.SelectableClass) && !isDynamicExpansion(tree)
827827
&& !pt.isInstanceOf[FunOrPolyProto] && pt != LhsProto
828828
then
829-
val fieldsType = qual.tpe.select(tpnme.Fields).dealias.simplified
829+
val pre = if !TypeOps.isLegalPrefix(qual.tpe) then SkolemType(qual.tpe) else qual.tpe
830+
val fieldsType = pre.select(tpnme.Fields).dealias.simplified
830831
val fields = fieldsType.namedTupleElementTypes
831832
typr.println(i"try dyn select $qual, $selName, $fields")
832833
fields.find(_._1 == selName) match

tests/pos/named-tuple-unstable.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.language.experimental.namedTuples
2+
import NamedTuple.{AnyNamedTuple, NamedTuple}
3+
4+
trait Foo extends Selectable:
5+
val f: Any
6+
type Fields = (myfield: f.type)
7+
def selectDynamic(name: String): Any
8+
9+
object Test:
10+
val elem1: Foo { val f: Int } = ???
11+
def elem2: Foo { val f: Int } = ???
12+
13+
def test: Unit =
14+
val a: Int = elem1.myfield // OK
15+
val b: Int = elem2.myfield // error: value myfield is not a member of Foo { val f: Int }

0 commit comments

Comments
 (0)