Skip to content

Commit e2c9dc0

Browse files
authored
Allow inner classes of universal traits (#18796)
Just forbid inner objects. This aligns with Scala 2 and allows us to keep Seq's definition as in Scala 2 when porting or capture checking the standard library.
2 parents 6dbdc74 + 0758156 commit e2c9dc0

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+1-3
Original file line numberDiff line numberDiff line change
@@ -2785,11 +2785,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
27852785
checkDerivedValueClass(cls, body1)
27862786

27872787
val effectiveOwner = cls.owner.skipWeakOwner
2788-
if !cls.isRefinementClass
2789-
&& !cls.isAllOf(PrivateLocal)
2788+
if cls.is(ModuleClass)
27902789
&& effectiveOwner.is(Trait)
27912790
&& !effectiveOwner.derivesFrom(defn.ObjectClass)
2792-
&& !ctx.settings.YcompileScala2Library.value // FIXME?: class PermutationsItr cannot be defined in universal trait SeqOps
27932791
then
27942792
report.error(em"$cls cannot be defined in universal $effectiveOwner", cdef.srcPos)
27952793

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
trait Outer extends Any {
2+
trait Inner1
3+
trait Inner2 extends Any
4+
class Inner3
5+
class Inner4(a: Int) extends AnyVal // error
6+
case class Inner5(a: Int) // error
7+
object Inner6 // error
8+
}

tests/pos-special/stdlib/collection/IndexedSeq.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ trait IndexedSeq[+A] extends Seq[A]
3333
object IndexedSeq extends SeqFactory.Delegate[IndexedSeq](immutable.IndexedSeq)
3434

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

3838
def iterator: Iterator[A] = view.iterator
3939

tests/pos-special/stdlib/collection/Seq.scala

+1-7
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,7 @@ object Seq extends SeqFactory.Delegate[Seq](immutable.Seq)
7777
* @define coll sequence
7878
* @define Coll `Seq`
7979
*/
80-
trait SeqOps[+A, +CC[_], +C] extends AnyRef
81-
// CC TODO: Our treechecker disallows classes in universal traits, but the typer accepts
82-
// them. Since SeqOps contains nested classes, I changed it to be no longer a universal trait.
83-
// Alternatively we could
84-
// - Change TreeChecker to accept this
85-
// - Move nested classes out of the trait
86-
with IterableOps[A, CC, C] { self =>
80+
trait SeqOps[+A, +CC[_], +C] extends Any with IterableOps[A, CC, C] { self =>
8781

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

0 commit comments

Comments
 (0)