Skip to content

Commit 24f5055

Browse files
committed
Don't cast to a value class as self type
Fixes #12462
1 parent 8d3083b commit 24f5055

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ class ExplicitSelf extends MiniPhase {
2929
!cls.is(Package) && cls.givenSelfType.exists && !cls.derivesFrom(tree.symbol.owner)
3030

3131
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)
32+
val selfType = cls.classInfo.selfType
33+
if selfType.classSymbols.exists(_.isValueClass) then
34+
report.error(em"self type $selfType of $cls may not be a value class", thiz.srcPos)
35+
cpy.Select(tree)(thiz.cast(AndType(selfType, thiz.tpe)), tree.name)
3336

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

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)