-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-use isConcrete checking in match types for NamedTuple.From (#20947)
- Move isConcrete to a new object `MatchTypes`. We should also move other MatchType-related stuff from Types and TypeComparer here. Type and TypeComparer are already unconfortably big, and MatchTypes are a coherent topic where everything should work together. - Streamline isConcrete a bit. - Re-use isConcrete for a similar test in CheckRealizable. - Re-use isConcrete for evaluating NamedTuple.From Fixes #20517
- Loading branch information
Showing
7 changed files
with
75 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package dotty.tools | ||
package dotc | ||
package core | ||
|
||
import Types.*, Contexts.*, Symbols.*, Flags.*, Decorators.* | ||
|
||
object MatchTypes: | ||
|
||
/* Concreteness checking | ||
* | ||
* When following a baseType and reaching a non-wildcard, in-variant-pos type capture, | ||
* we have to make sure that the scrutinee is concrete enough to uniquely determine | ||
* the values of the captures. This comes down to checking that we do not follow any | ||
* upper bound of an abstract type. | ||
* | ||
* See notably neg/wildcard-match.scala for examples of this. | ||
* | ||
* See neg/i13780.scala, neg/i13780-1.scala and neg/i19746.scala for | ||
* ClassCastException reproducers if we disable this check. | ||
*/ | ||
def isConcrete(tp: Type)(using Context): Boolean = | ||
val tp1 = tp.normalized | ||
|
||
tp1 match | ||
case tp1: TypeRef => | ||
if tp1.symbol.isClass then true | ||
else | ||
tp1.info match | ||
case info: AliasingBounds => isConcrete(info.alias) | ||
case _ => false | ||
case tp1: AppliedType => | ||
isConcrete(tp1.tycon) && isConcrete(tp1.superType) | ||
case tp1: HKTypeLambda => | ||
true | ||
case tp1: TermRef => | ||
!tp1.symbol.is(Param) && isConcrete(tp1.underlying) | ||
case _: (ParamRef | MatchType) => | ||
false | ||
case tp1: TypeProxy => | ||
isConcrete(tp1.underlying) | ||
case tp1: AndOrType => | ||
isConcrete(tp1.tp1) && isConcrete(tp1.tp2) | ||
case _ => | ||
false | ||
end isConcrete | ||
|
||
end MatchTypes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-- [E007] Type Mismatch Error: tests/neg/i20517.scala:10:43 ------------------------------------------------------------ | ||
10 | def dep(foo: Foo[Any]): From[foo.type] = (elem = "") // error | ||
| ^^^^^^^^^^^ | ||
| Found: (elem : String) | ||
| Required: NamedTuple.From[(foo : Foo[Any])] | ||
| | ||
| longer explanation available when compiling with `-explain` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import scala.language.experimental.namedTuples | ||
import NamedTuple.From | ||
|
||
case class Foo[+T](elem: T) | ||
|
||
trait Base[M[_]]: | ||
def dep(foo: Foo[Any]): M[foo.type] | ||
|
||
class SubAny extends Base[From]: | ||
def dep(foo: Foo[Any]): From[foo.type] = (elem = "") // error | ||
|
||
object Test: | ||
@main def run = | ||
val f: Foo[Int] = Foo(elem = 1) | ||
val b: Base[From] = SubAny() | ||
val nt: (elem: Int) = b.dep(f) | ||
val x: Int = nt.elem // was ClassCastException |