Skip to content

Fix cycle detection for type aliases with wildcard arguments #15508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@ object SymDenotations {
containsOpaques ||
is(Module, butNot = Package) && owner.seesOpaques

def isProvisional(using Context): Boolean =
flagsUNSAFE.is(Provisional) // do not force the info to check the flag

/** Is this the denotation of a self symbol of some class?
* This is the case if one of two conditions holds:
* 1. It is the symbol referred to in the selfInfo part of the ClassInfo
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SymDenotations.LazyType
import Decorators._
import util.Stats._
import Names._
import Flags.Module
import Flags.{Module, Provisional}
import dotty.tools.dotc.config.Config

object TypeApplications {
Expand Down Expand Up @@ -284,7 +284,8 @@ class TypeApplications(val self: Type) extends AnyVal {

/** Dealias type if it can be done without forcing the TypeRef's info */
def safeDealias(using Context): Type = self match {
case self: TypeRef if self.denot.exists && self.symbol.isAliasType =>
case self: TypeRef
if self.denot.exists && self.symbol.isAliasType && !self.symbol.isProvisional =>
self.superType.stripTypeVar.safeDealias
case _ =>
self
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ object Types {
case t: TypeRef =>
!t.currentSymbol.isStatic && {
(t: Type).mightBeProvisional = false // break cycles
t.symbol.flagsUNSAFE.is(Provisional)
t.symbol.isProvisional
|| test(t.prefix, theAcc)
|| t.denot.infoOrCompleter.match
case info: LazyType => true
Expand Down
15 changes: 11 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2027,12 +2027,19 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
var checkedArgs = preCheckKinds(args1, paramBounds)
// check that arguments conform to bounds is done in phase PostTyper
val tycon = tpt1.symbol
if (tycon == defn.andType)
checkedArgs = checkedArgs.mapconserve(arg =>
checkSimpleKinded(checkNoWildcard(arg)))
else if (tycon == defn.orType)
if tycon == defn.andType || tycon == defn.orType then
checkedArgs = checkedArgs.mapconserve(arg =>
checkSimpleKinded(checkNoWildcard(arg)))
else if tycon.isProvisional then
// A type with Provisional flag is either an alias or abstract type.
// If it is an alias type, it would mean the type is cyclic
// If it is an abstract type, it would mean the type is an irreducible
// application of a higher-kinded type to a wildcard argument.
// Either way, the wildcard argument is illegal. The early test of
// `checkNoWildcard` here is needed, so that we do not accidentally reduce
// an application of a Provisional type away, which would mean that the type constructor
// is no longer present on the right hand side. See neg/i15507.scala.
checkedArgs = checkedArgs.mapconserve(checkNoWildcard)
else if tycon == defn.throwsAlias
&& checkedArgs.length == 2
&& checkedArgs(1).tpe.derivesFrom(defn.RuntimeExceptionClass)
Expand Down
15 changes: 15 additions & 0 deletions tests/neg/i15507.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
object TestNested:
type _NestedSet1[X] = Set[_NestedSet1[?]] // error // error
type _NestedSet2[X] <: Set[_NestedSet2[?]] // error
type _NestedSet3[X] <: Set[_NestedSet3[X]] // ok
type _NestedSet4[X] >: Set[_NestedSet4[X]] // error
type _NestedSet5[X] = Set[_NestedSet5[X]] // error
type _NestedSet6[X] = Set[_NestedSet6[Int]] // error

type _NestedList1[X] = List[_NestedList1[?]] // error // error
type _NestedList2[X] <: List[_NestedList2[?]] // error
type _NestedList3[X] <: List[_NestedList3[X]] // ok
type _NestedList4[X] >: List[_NestedList4[X]] // error
type _NestedList5[X] = List[_NestedList5[X]] // error
type _NestedList6[X] = List[_NestedList6[Int]] // error