Skip to content

Commit 173c3ff

Browse files
authored
Merge pull request #14885 from dotty-staging/fix-12462
Don't cast to a value class as self type
2 parents 7fbbeef + 4421d4c commit 173c3ff

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

compiler/src/dotty/tools/dotc/transform/ExplicitSelf.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package transform
33

44
import core._
55
import Contexts._, Types._, MegaPhase._, ast.Trees._, Symbols._, Decorators._, Flags._
6+
import SymUtils.*
67

78
/** Transform references of the form
89
*
@@ -29,7 +30,10 @@ class ExplicitSelf extends MiniPhase {
2930
!cls.is(Package) && cls.givenSelfType.exists && !cls.derivesFrom(tree.symbol.owner)
3031

3132
private def castQualifier(tree: RefTree, cls: ClassSymbol, thiz: Tree)(using Context) =
32-
cpy.Select(tree)(thiz.cast(AndType(cls.classInfo.selfType, thiz.tpe)), tree.name)
33+
val selfType = cls.classInfo.selfType
34+
if selfType.classSymbols.exists(_.isValueClass) && !cls.isUniversalTrait then
35+
report.error(em"self type $selfType of $cls may not be a value class", thiz.srcPos)
36+
cpy.Select(tree)(thiz.cast(AndType(selfType, thiz.tpe)), tree.name)
3337

3438
override def transformIdent(tree: Ident)(using Context): Tree = tree.tpe match {
3539
case tp: ThisType =>

compiler/src/dotty/tools/dotc/transform/SymUtils.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ object SymUtils:
6666

6767
def isNoValue(using Context): Boolean = self.is(Package) || self.isAllOf(JavaModule)
6868

69+
def isUniversalTrait(using Context): Boolean =
70+
self.is(Trait) && self.asClass.parentSyms.head == defn.AnyClass
71+
6972
/** Is this a type or term parameter or a term parameter accessor? */
7073
def isParamOrAccessor(using Context): Boolean =
7174
self.is(Param) || self.is(ParamAccessor)

tests/neg/i12462.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class B(x: Int) {
2+
self: Int =>
3+
def this(a: String) = this() // error
4+
}

0 commit comments

Comments
 (0)