Skip to content

Don't cast to a value class as self type #14885

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 2 commits into from
May 16, 2022
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
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/ExplicitSelf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transform

import core._
import Contexts._, Types._, MegaPhase._, ast.Trees._, Symbols._, Decorators._, Flags._
import SymUtils.*

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

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

override def transformIdent(tree: Ident)(using Context): Tree = tree.tpe match {
case tp: ThisType =>
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/SymUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ object SymUtils:

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

def isUniversalTrait(using Context): Boolean =
self.is(Trait) && self.asClass.parentSyms.head == defn.AnyClass

/** Is this a type or term parameter or a term parameter accessor? */
def isParamOrAccessor(using Context): Boolean =
self.is(Param) || self.is(ParamAccessor)
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i12462.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class B(x: Int) {
self: Int =>
def this(a: String) = this() // error
}