Skip to content

Make trace typed, to avoid needless casting #13464

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 1 commit into from
Sep 6, 2021
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: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/reporting/trace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait TraceSyntax:
apply(question, if cond then Printers.default else Printers.noPrinter, show)(op)
else op

inline def apply[T](inline question: String, inline printer: Printers.Printer, inline showOp: Any => String)(inline op: T)(using Context): T =
inline def apply[T, U >: T](inline question: String, inline printer: Printers.Printer, inline showOp: U => String)(inline op: T)(using Context): T =
inline if isEnabled then
doTrace[T](question, printer, showOp)(op)
else op
Expand All @@ -60,15 +60,15 @@ trait TraceSyntax:

private def doTrace[T](question: => String,
printer: Printers.Printer = Printers.default,
showOp: Any => String = alwaysToString)
showOp: T => String = alwaysToString)
(op: => T)(using Context): T =
if ctx.mode.is(Mode.Printing) || !isForced && (printer eq Printers.noPrinter) then op
else
// Avoid evaluating question multiple time, since each evaluation
// may cause some extra logging output.
val q = question
val leading = s"==> $q?"
val trailing = (res: Any) => s"<== $q = ${showOp(res)}"
val trailing = (res: T) => s"<== $q = ${showOp(res)}"
var finalized = false
var logctx = ctx
while logctx.reporter.isInstanceOf[StoreReporter] do logctx = logctx.outer
Expand Down
16 changes: 8 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/init/Semantic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class Semantic {
def widenArgs: List[Value] = values.map(_.widenArg).toList

extension (value: Value)
def select(field: Symbol, source: Tree, needResolve: Boolean = true): Contextual[Result] = log("select " + field.show, printer, res => res.asInstanceOf[Result].show) {
def select(field: Symbol, source: Tree, needResolve: Boolean = true): Contextual[Result] = log("select " + field.show, printer, (_: Result).show) {
if promoted.isCurrentObjectPromoted then Result(Hot, Nil)
else value match {
case Hot =>
Expand Down Expand Up @@ -371,7 +371,7 @@ class Semantic {
}
}

def call(meth: Symbol, args: List[ArgInfo], superType: Type, source: Tree, needResolve: Boolean = true): Contextual[Result] = log("call " + meth.show + ", args = " + args, printer, res => res.asInstanceOf[Result].show) {
def call(meth: Symbol, args: List[ArgInfo], superType: Type, source: Tree, needResolve: Boolean = true): Contextual[Result] = log("call " + meth.show + ", args = " + args, printer, (_: Result).show) {
def checkArgs = args.flatMap(_.promote)

// fast track if the current object is already initialized
Expand Down Expand Up @@ -445,7 +445,7 @@ class Semantic {
}

/** Handle a new expression `new p.C` where `p` is abstracted by `value` */
def instantiate(klass: ClassSymbol, ctor: Symbol, args: List[ArgInfo], source: Tree): Contextual[Result] = log("instantiating " + klass.show + ", value = " + value + ", args = " + args, printer, res => res.asInstanceOf[Result].show) {
def instantiate(klass: ClassSymbol, ctor: Symbol, args: List[ArgInfo], source: Tree): Contextual[Result] = log("instantiating " + klass.show + ", value = " + value + ", args = " + args, printer, (_: Result).show) {
val trace1 = trace.add(source)
if promoted.isCurrentObjectPromoted then Result(Hot, Nil)
else value match {
Expand Down Expand Up @@ -702,7 +702,7 @@ class Semantic {
*
* This method only handles cache logic and delegates the work to `cases`.
*/
def eval(expr: Tree, thisV: Addr, klass: ClassSymbol, cacheResult: Boolean = false): Contextual[Result] = log("evaluating " + expr.show + ", this = " + thisV.show, printer, res => res.asInstanceOf[Result].show) {
def eval(expr: Tree, thisV: Addr, klass: ClassSymbol, cacheResult: Boolean = false): Contextual[Result] = log("evaluating " + expr.show + ", this = " + thisV.show, printer, (_: Result).show) {
val innerMap = cache.getOrElseUpdate(thisV, new EqHashMap[Tree, Value])
if (innerMap.contains(expr)) Result(innerMap(expr), Errors.empty)
else {
Expand Down Expand Up @@ -911,7 +911,7 @@ class Semantic {
}

/** Handle semantics of leaf nodes */
def cases(tp: Type, thisV: Addr, klass: ClassSymbol, source: Tree): Contextual[Result] = log("evaluating " + tp.show, printer, res => res.asInstanceOf[Result].show) {
def cases(tp: Type, thisV: Addr, klass: ClassSymbol, source: Tree): Contextual[Result] = log("evaluating " + tp.show, printer, (_: Result).show) {
tp match {
case _: ConstantType =>
Result(Hot, Errors.empty)
Expand Down Expand Up @@ -941,7 +941,7 @@ class Semantic {
}

/** Resolve C.this that appear in `klass` */
def resolveThis(target: ClassSymbol, thisV: Value, klass: ClassSymbol, source: Tree): Contextual[Value] = log("resolving " + target.show + ", this = " + thisV.show + " in " + klass.show, printer, res => res.asInstanceOf[Value].show) {
def resolveThis(target: ClassSymbol, thisV: Value, klass: ClassSymbol, source: Tree): Contextual[Value] = log("resolving " + target.show + ", this = " + thisV.show + " in " + klass.show, printer, (_: Value).show) {
if target == klass then thisV
else if target.is(Flags.Package) then Hot
else
Expand Down Expand Up @@ -969,7 +969,7 @@ class Semantic {
*
* See `tpd.outerSelect` and `ElimOuterSelect`.
*/
def resolveOuterSelect(target: ClassSymbol, thisV: Value, hops: Int, source: Tree): Contextual[Value] = log("resolving outer " + target.show + ", this = " + thisV.show + ", hops = " + hops, printer, res => res.asInstanceOf[Value].show) {
def resolveOuterSelect(target: ClassSymbol, thisV: Value, hops: Int, source: Tree): Contextual[Value] = log("resolving outer " + target.show + ", this = " + thisV.show + ", hops = " + hops, printer, (_: Value).show) {
// Is `target` reachable from `cls` with the given `hops`?
def reachable(cls: ClassSymbol, hops: Int): Boolean =
if hops == 0 then cls == target
Expand Down Expand Up @@ -1011,7 +1011,7 @@ class Semantic {
else cases(tref.prefix, thisV, klass, source)

/** Initialize part of an abstract object in `klass` of the inheritance chain */
def init(tpl: Template, thisV: Addr, klass: ClassSymbol): Contextual[Result] = log("init " + klass.show, printer, res => res.asInstanceOf[Result].show) {
def init(tpl: Template, thisV: Addr, klass: ClassSymbol): Contextual[Result] = log("init " + klass.show, printer, (_: Result).show) {
val errorBuffer = new mutable.ArrayBuffer[Error]

val paramsMap = tpl.constr.termParamss.flatten.map { vdef =>
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ trait SpaceLogic {
def show(sp: Space): String

/** Simplify space such that a space equal to `Empty` becomes `Empty` */
def simplify(space: Space)(using Context): Space = trace(s"simplify ${show(space)} --> ", debug, x => show(x.asInstanceOf[Space]))(space match {
def simplify(space: Space)(using Context): Space = trace(s"simplify ${show(space)} --> ", debug, show)(space match {
case Prod(tp, fun, spaces) =>
val sps = spaces.map(simplify(_))
if (sps.contains(Empty)) Empty
Expand Down Expand Up @@ -194,7 +194,7 @@ trait SpaceLogic {
}

/** Intersection of two spaces */
def intersect(a: Space, b: Space)(using Context): Space = trace(s"${show(a)} & ${show(b)}", debug, x => show(x.asInstanceOf[Space])) {
def intersect(a: Space, b: Space)(using Context): Space = trace(s"${show(a)} & ${show(b)}", debug, show) {
def tryDecompose1(tp: Type) = intersect(Or(decompose(tp)), b)
def tryDecompose2(tp: Type) = intersect(a, Or(decompose(tp)))

Expand Down Expand Up @@ -226,7 +226,7 @@ trait SpaceLogic {
}

/** The space of a not covered by b */
def minus(a: Space, b: Space)(using Context): Space = trace(s"${show(a)} - ${show(b)}", debug, x => show(x.asInstanceOf[Space])) {
def minus(a: Space, b: Space)(using Context): Space = trace(s"${show(a)} - ${show(b)}", debug, show) {
def tryDecompose1(tp: Type) = minus(Or(decompose(tp)), b)
def tryDecompose2(tp: Type) = minus(a, Or(decompose(tp)))

Expand Down