Skip to content
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

Allow inner classes of universal traits #18796

Merged
merged 1 commit into from
Oct 30, 2023
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
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2785,11 +2785,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
checkDerivedValueClass(cls, body1)

val effectiveOwner = cls.owner.skipWeakOwner
if !cls.isRefinementClass
&& !cls.isAllOf(PrivateLocal)
if cls.is(ModuleClass)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not have any neg tests for this case. The following seems to cover what we are missing.

trait Outer extends Any {
  trait Inner1
  trait Inner2 extends Any
  class Inner3
  class Inner4(a: Int) extends AnyVal // error
  case class Inner5(a: Int) // error
  object Inner6 // error
}

&& effectiveOwner.is(Trait)
&& !effectiveOwner.derivesFrom(defn.ObjectClass)
&& !ctx.settings.YcompileScala2Library.value // FIXME?: class PermutationsItr cannot be defined in universal trait SeqOps
then
report.error(em"$cls cannot be defined in universal $effectiveOwner", cdef.srcPos)

Expand Down
8 changes: 8 additions & 0 deletions tests/neg/inner-classes-of-universal-traits.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
trait Outer extends Any {
trait Inner1
trait Inner2 extends Any
class Inner3
class Inner4(a: Int) extends AnyVal // error
case class Inner5(a: Int) // error
object Inner6 // error
}
2 changes: 1 addition & 1 deletion tests/pos-special/stdlib/collection/IndexedSeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait IndexedSeq[+A] extends Seq[A]
object IndexedSeq extends SeqFactory.Delegate[IndexedSeq](immutable.IndexedSeq)

/** Base trait for indexed Seq operations */
trait IndexedSeqOps[+A, +CC[_], +C] extends AnyRef with SeqOps[A, CC, C] { self =>
trait IndexedSeqOps[+A, +CC[_], +C] extends Any with SeqOps[A, CC, C] { self =>

def iterator: Iterator[A] = view.iterator

Expand Down
8 changes: 1 addition & 7 deletions tests/pos-special/stdlib/collection/Seq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,7 @@ object Seq extends SeqFactory.Delegate[Seq](immutable.Seq)
* @define coll sequence
* @define Coll `Seq`
*/
trait SeqOps[+A, +CC[_], +C] extends AnyRef
// CC TODO: Our treechecker disallows classes in universal traits, but the typer accepts
// them. Since SeqOps contains nested classes, I changed it to be no longer a universal trait.
// Alternatively we could
// - Change TreeChecker to accept this
// - Move nested classes out of the trait
with IterableOps[A, CC, C] { self =>
trait SeqOps[+A, +CC[_], +C] extends Any with IterableOps[A, CC, C] { self =>

override def view: SeqView[A] = new SeqView.Id[A](this)

Expand Down
Loading