Skip to content

Commit

Permalink
Fix NamedTuple selection on an unstable prefix (#20455)
Browse files Browse the repository at this point in the history
Without a stable prefix, asSeenFrom could end up widening `Fields` to
`>: Nothing <: Any`.
  • Loading branch information
odersky committed May 23, 2024
2 parents f99f268 + f8798d8 commit 6233495
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/pos/named-tuple-unstable.scala
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 6233495

Please sign in to comment.