Open smarter opened 5 months ago
Yes, widening singleton types of paths that can be substituted later creates established unsoundness issues. See https://github.com/scala/scala3/issues/19746
Thanks for the reference. A possibly viable workaround is to use a wrapper type:
import scala.language.experimental.namedTuples
class Wrapper[T](x: T)
object Wrapper:
type Extract[W <: Wrapper[?]] = W match
case Wrapper[t] => t
object Test:
val f: (name: String, age: Int) = ???
val f2 = Wrapper(f)
val x: NamedTupleDecomposition.Names[Wrapper.Extract[f2.type]] = ("name", "age")
Not sure if there's a better way or if that can help suggest some more systematic fix.
@smarter what if we explicitly create a Widen
type operation? So Names
will be implemented as Widen[X] match ...
That would reopen the soundness hole of https://github.com/scala/scala3/issues/19746 if Widen[this.type]
can reduce to different types depending on the context.
I'm not sure that will happen, because of the lazy nature of singleton ops operations.
Though note that the fix for #19746 (https://github.com/scala/scala3/commit/0a3497bf7f0ac032f98d36cf1713e8926b5d176c) specifically target singleton types coming from parameters (since those can be substituted), so maybe something like a widenSingletonOfNonParamRef
operation? It's not clear to me that this is enough if the underlying type of a non-param singleton is a GADT, in which case maybe the original hole isn't fixed.
in which case maybe the original hole isn't fixed.
Indeed: #20515.
Compiler version
7d559ad06bdfe448450341c7fdf12cc0513456f3
Minimized code
Output
Expectation
I'd expect this to reduce, otherwise all NamedTuple operations that use
NamedTupleDecomposition
likeNamedTuple.Map
will fail to reduce on singleton types. NamedTupleDecomposition is defined as:So NamedTuple is treated as an abstract type constructor and we end up in https://github.com/scala/scala3/blob/7d559ad06bdfe448450341c7fdf12cc0513456f3/compiler/src/dotty/tools/dotc/core/TypeComparer.scala#L3506-L3511
Which fails since the scrutinee isn't widened. @sjrd : do you anticipate a soundness issue if we added a
.widenSingleton
on the scrutinee here? Or is the whole design of NamedTupleDecomposition doomed?