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 1 commit
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
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.flagsUNSAFE.is(Provisional) 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 here is needed, so that we do not accidentally reduce
// an application of a Provisional type away so that the type constructor
// is no longer present on the roght 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