Skip to content

Commit

Permalink
Refactor SymUtils
Browse files Browse the repository at this point in the history
 - Move to core package. It was for historical reasons in transform because
   it was originally intended as a collection of type operations that were
   were useful in transform phases. But it's now used from everywhere.
 - Make a base class of Types, so that it does not need to be imported explicitly.

Also: move isDerivedValueClass to SymUtils
  • Loading branch information
odersky committed Nov 23, 2023
1 parent 6793291 commit 125321e
Show file tree
Hide file tree
Showing 90 changed files with 91 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import dotty.tools.dotc.core.Types.*
import dotty.tools.dotc.core.StdNames.{nme, str}
import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.transform.Erasure
import dotty.tools.dotc.transform.SymUtils.*
import dotty.tools.dotc.util.Spans.*
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Phases.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import dotty.tools.dotc.core.Types.*
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.util.Spans.*
import dotty.tools.dotc.report
import dotty.tools.dotc.transform.SymUtils.*


/*
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Phases.*
import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.transform.SymUtils.*

import dotty.tools.dotc.core.StdNames
import dotty.tools.dotc.core.Phases

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/backend/jvm/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import dotty.tools.dotc.core.Phases.Phase

import scala.collection.mutable
import scala.jdk.CollectionConverters.*
import dotty.tools.dotc.transform.SymUtils.*

import dotty.tools.dotc.interfaces
import dotty.tools.dotc.report

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.language.unsafeNulls

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.transform.SymUtils.*

import java.io.{File => _}

import scala.reflect.ClassTag
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import StdNames.*
import TypeErasure.ErasedValueType

import dotty.tools.dotc.transform.{Erasure, ValueClasses}
import dotty.tools.dotc.transform.SymUtils.*

import dotty.tools.dotc.util.SourcePosition
import dotty.tools.dotc.report

Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/CompilationUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import ast.{tpd, untpd}
import tpd.{Tree, TreeTraverser}
import ast.Trees.{Import, Ident}
import typer.Nullables
import transform.SymUtils.*
import core.Decorators.*
import config.{SourceVersion, Feature}
import StdNames.nme
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package ast
import core.*
import util.Spans.*, Types.*, Contexts.*, Constants.*, Names.*, NameOps.*, Flags.*
import Symbols.*, StdNames.*, Trees.*, ContextOps.*
import Decorators.*, transform.SymUtils.*
import Decorators.*
import Annotations.Annotation
import NameKinds.{UniqueName, ContextBoundParamName, ContextFunctionParamName, DefaultGetterName, WildcardParamName}
import typer.{Namer, Checking}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Flags.*, Trees.*, Types.*, Contexts.*
import Names.*, StdNames.*, NameOps.*, Symbols.*
import typer.ConstFold
import reporting.trace
import dotty.tools.dotc.transform.SymUtils.*

import Decorators.*
import Constants.Constant
import scala.collection.mutable
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import core.*
import Types.*, Contexts.*, Flags.*
import Symbols.*, Annotations.*, Trees.*, Symbols.*, Constants.Constant
import Decorators.*
import dotty.tools.dotc.transform.SymUtils.*


/** A map that applies three functions and a substitution together to a tree and
* makes sure they are coordinated so that the result is well-typed. The functions are
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package ast

import dotty.tools.dotc.transform.{ExplicitOuter, Erasure}
import typer.ProtoTypes
import transform.SymUtils.*
import core.*
import Scopes.newScope
import util.Spans.*, Types.*, Contexts.*, Constants.*, Names.*, Flags.*, NameOps.*
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import typer.Checking.{checkBounds, checkAppliedTypesIn}
import typer.ErrorReporting.{Addenda, err}
import typer.ProtoTypes.{AnySelectionProto, LhsProto}
import util.{SimpleIdentitySet, EqHashMap, EqHashSet, SrcPos, Property}
import transform.SymUtils.*
import transform.{Recheck, PreRecheck, CapturedVars}
import Recheck.*
import scala.collection.mutable
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/config/JavaPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classpath.AggregateClassPath
import core.*
import Symbols.*, Types.*, Contexts.*, StdNames.*
import Flags.*
import transform.ExplicitOuter, transform.SymUtils.*
import transform.ExplicitOuter

class JavaPlatform extends Platform {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package dotty.tools.dotc
package transform
package core

import core.*
import Types.*
Expand All @@ -11,18 +11,18 @@ import NameOps.*
import StdNames.*
import NameKinds.*
import Flags.*
import ValueClasses.isDerivedValueClass
import Decorators.*
import Constants.Constant
import Annotations.Annotation
import Phases.*
import ast.tpd.Literal
import transform.Mixin

import dotty.tools.dotc.transform.sjs.JSSymUtils.sjsNeedsField

import scala.annotation.tailrec

object SymUtils:
class SymUtils:

extension (self: Symbol)

Expand Down Expand Up @@ -79,6 +79,14 @@ object SymUtils:
self.is(Enum, butNot = Case) &&
self.info.parents.exists(p => p.typeSymbol == defn.JavaEnumClass)

def isDerivedValueClass(using Context): Boolean = self.isClass && {
val d = self.denot
!d.isRefinementClass &&
d.isValueClass &&
(d.initial.symbol ne defn.AnyValClass) && // Compare the initial symbol because AnyVal does not exist after erasure
!d.isPrimitiveValueClass
}

/** Is this a case class for which a product mirror is generated?
* Excluded are value classes, abstract classes and case classes with more than one
* parameter section.
Expand All @@ -100,7 +108,7 @@ object SymUtils:
if (!self.is(CaseClass)) "it is not a case class"
else if (self.is(Abstract)) "it is an abstract class"
else if (self.primaryConstructor.info.paramInfoss.length != 1) "it takes more than one parameter list"
else if (isDerivedValueClass(self)) "it is a value class"
else if self.isDerivedValueClass then "it is a value class"
else if (!(companionMirror || canAccessCtor)) s"the constructor of $self is inaccessible from the calling scope."
else ""
end whyNotGenericProduct
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import util.Spans.*
import DenotTransformers.*
import StdNames.*
import NameOps.*
import transform.SymUtils.*
import NameKinds.LazyImplicitName
import ast.tpd
import tpd.{Tree, TreeProvider, TreeOps}
Expand All @@ -35,7 +34,7 @@ import dotty.tools.dotc.classpath.FileUtils.isScalaBinary

import scala.compiletime.uninitialized

object Symbols {
object Symbols extends SymUtils {

implicit def eqSymbol: CanEqual[Symbol, Symbol] = CanEqual.derived

Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import TypeErasure.{erasedLub, erasedGlb}
import TypeApplications.*
import Variances.{Variance, variancesConform}
import Constants.Constant
import transform.SymUtils.*
import scala.util.control.NonFatal
import typer.ProtoTypes.constrained
import typer.Applications.productSelectorTypes
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/core/TypeErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ end SourceLanguage
object TypeErasure {

private def erasureDependsOnArgs(sym: Symbol)(using Context) =
sym == defn.ArrayClass || sym == defn.PairClass || isDerivedValueClass(sym)
sym == defn.ArrayClass || sym == defn.PairClass || sym.isDerivedValueClass

/** The arity of this tuple type, which can be made up of EmptyTuple, TupleX and `*:` pairs.
*
Expand Down Expand Up @@ -125,7 +125,7 @@ object TypeErasure {
case tp: TypeRef =>
val sym = tp.symbol
sym.isClass &&
(!erasureDependsOnArgs(sym) || isDerivedValueClass(sym)) &&
(!erasureDependsOnArgs(sym) || sym.isDerivedValueClass) &&
!defn.specialErasure.contains(sym) &&
!defn.isSyntheticFunctionClass(sym)
case _: TermRef =>
Expand Down Expand Up @@ -630,15 +630,15 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
case tp: TypeRef =>
val sym = tp.symbol
if !sym.isClass then this(checkedSuperType(tp))
else if semiEraseVCs && isDerivedValueClass(sym) then eraseDerivedValueClass(tp)
else if semiEraseVCs && sym.isDerivedValueClass then eraseDerivedValueClass(tp)
else if defn.isSyntheticFunctionClass(sym) then defn.functionTypeErasure(sym)
else eraseNormalClassRef(tp)
case tp: AppliedType =>
val tycon = tp.tycon
if (tycon.isRef(defn.ArrayClass)) eraseArray(tp)
else if (tycon.isRef(defn.PairClass)) erasePair(tp)
else if (tp.isRepeatedParam) apply(tp.translateFromRepeated(toArray = sourceLanguage.isJava))
else if (semiEraseVCs && isDerivedValueClass(tycon.classSymbol)) eraseDerivedValueClass(tp)
else if (semiEraseVCs && tycon.classSymbol.isDerivedValueClass) eraseDerivedValueClass(tp)
else this(checkedSuperType(tp))
case tp: TermRef =>
this(underlyingOfTermRef(tp))
Expand Down Expand Up @@ -898,7 +898,7 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
if (!info.exists) assert(false, i"undefined: $tp with symbol $sym")
return sigName(info)
}
if (semiEraseVCs && isDerivedValueClass(sym)) {
if (semiEraseVCs && sym.isDerivedValueClass) {
val erasedVCRef = eraseDerivedValueClass(tp)
if (erasedVCRef.exists) return sigName(erasedVCRef)
}
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import ast.tpd.*
import reporting.trace
import config.Printers.typr
import config.Feature
import transform.SymUtils.*
import typer.ProtoTypes.*
import typer.ForceDegree
import typer.Inferencing.*
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import CaptureSet.{CompareResult, IdempotentCaptRefMap, IdentityCaptRefMap}
import scala.annotation.internal.sharable
import scala.annotation.threadUnsafe

import dotty.tools.dotc.transform.SymUtils.*


object Types extends TypeUtils {

Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Contexts.*, Symbols.*, Types.*, Names.*, Constants.*, Decorators.*, Annot
import Comments.{Comment, CommentsContext}
import NameKinds.*
import StdNames.nme
import transform.SymUtils.*
import config.Config
import collection.mutable
import reporting.{Profile, NoProfile}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import util.{SourceFile, Property}
import ast.{Trees, tpd, untpd}
import Trees.*
import Decorators.*
import transform.SymUtils.*
import dotty.tools.dotc.quoted.QuotePatterns

import dotty.tools.tasty.{TastyBuffer, TastyReader}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import util.common.*
import util.NoSourcePosition
import typer.Checking.checkNonCyclic
import typer.Nullables.*
import transform.SymUtils.*
import PickleBuffer.*
import PickleFormat.*
import Decorators.*
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package inlines
import ast.*, core.*
import Flags.*, Symbols.*, Types.*, Decorators.*, Contexts.*
import StdNames.nme
import transform.SymUtils.*
import typer.*
import Names.TermName
import NameKinds.{InlineAccessorName, InlineBinderName, InlineScrutineeName}
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/inlines/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package inlines

import ast.*, core.*
import Flags.*, Symbols.*, Types.*, Decorators.*, Constants.*, Contexts.*
import transform.SymUtils.*
import StdNames.nme
import typer.*
import Names.Name
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/inlines/Inlines.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package inlines
import ast.*, core.*
import Flags.*, Symbols.*, Types.*, Decorators.*, Constants.*, Contexts.*
import StdNames.{tpnme, nme}
import transform.SymUtils.*
import typer.*
import NameKinds.BodyRetainerName
import SymDenotations.SymDenotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import NameOps.*
import Annotations.*
import transform.{AccessProxies, Splicer}
import staging.CrossStageSafety
import transform.SymUtils.*
import config.Printers.inlining
import util.Property
import staging.StagingLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import ast.{NavigateAST, Trees, tpd, untpd}
import core.*
import Decorators.*, ContextOps.*
import Contexts.*, Flags.*, Names.*, NameOps.*, Symbols.*, Trees.*, Types.*
import transform.SymUtils.*
import util.Spans.*, util.SourceFile, util.SourcePosition

/** High-level API to get information out of typed trees, designed to be used by IDEs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import Trees.*
import TypeApplications.*
import NameKinds.{WildcardParamName, DefaultGetterName}
import util.Chars.isOperatorPart
import transform.SymUtils.*
import config.{Config, Feature}

import dotty.tools.dotc.util.SourcePosition
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/quoted/QuotePatterns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.core.TypeOps.*
import dotty.tools.dotc.core.Types.*
import dotty.tools.dotc.reporting.IllegalVariableInPatternAlternative
import dotty.tools.dotc.transform.SymUtils.*


import scala.collection.mutable

Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/DidYouMean.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import core.*
import Contexts.*
import Decorators.*, Symbols.*, Names.*, Types.*, Flags.*
import typer.ProtoTypes.{FunProto, SelectionProto}
import transform.SymUtils.isNoValue

/** A utility object to support "did you mean" hinting */
object DidYouMean:
Expand Down
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import printing.Formatting.hl
import ast.Trees.*
import ast.untpd
import ast.tpd
import transform.SymUtils.*
import scala.util.matching.Regex
import java.util.regex.Matcher.quoteReplacement
import cc.CaptureSet.IdentityCaptRefMap
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import Names.*
import NameOps.*
import inlines.Inlines
import transform.ValueClasses
import transform.SymUtils.*
import dotty.tools.io.File
import java.io.PrintWriter

Expand Down Expand Up @@ -274,7 +273,7 @@ private class ExtractAPICollector(using Context) extends ThunkHolder {
report.error(ex, csym.sourcePos)
defn.ObjectType :: Nil
}
if (ValueClasses.isDerivedValueClass(csym)) {
if (csym.isDerivedValueClass) {
val underlying = ValueClasses.valueClassUnbox(csym).info.finalResultType
// The underlying type of a value class should be part of the name hash
// of the value class (see the test `value-class-underlying`), this is accomplished
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import dotty.tools.dotc.core.Phases.*
import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.core.Denotations.StaleSymbol
import dotty.tools.dotc.core.Types.*
import dotty.tools.dotc.transform.SymUtils.*

import dotty.tools.dotc.util.{SrcPos, NoSourcePosition}
import dotty.tools.io
import dotty.tools.io.{AbstractFile, PlainFile, ZipArchive, NoAbstractFile}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import NameOps.*
import Denotations.StaleSymbol
import util.Spans.Span
import util.SourceFile
import transform.SymUtils.*

import scala.collection.mutable
import scala.annotation.{ threadUnsafe => tu, tailrec }
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/staging/HealType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.core.Types.*
import dotty.tools.dotc.staging.StagingLevel.*
import dotty.tools.dotc.staging.QuoteTypeTags.*
import dotty.tools.dotc.transform.SymUtils.*

import dotty.tools.dotc.typer.Implicits.SearchFailureType
import dotty.tools.dotc.util.SrcPos

Expand Down
Loading

0 comments on commit 125321e

Please sign in to comment.