Skip to content

Refactor string interpolators and error message handling #16384

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 14 commits into from
Nov 24, 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
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import dotty.tools.dotc.transform.SymUtils._
import dotty.tools.dotc.util.Spans._
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases._
import dotty.tools.dotc.core.Decorators.em
import dotty.tools.dotc.report

/*
Expand Down Expand Up @@ -700,7 +701,7 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
var elemKind = arr.elementType
val argsSize = args.length
if (argsSize > dims) {
report.error(s"too many arguments for array constructor: found ${args.length} but array has only $dims dimension(s)", ctx.source.atSpan(app.span))
report.error(em"too many arguments for array constructor: found ${args.length} but array has only $dims dimension(s)", ctx.source.atSpan(app.span))
}
if (argsSize < dims) {
/* In one step:
Expand Down
13 changes: 7 additions & 6 deletions compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ trait BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
outputDirectory
} catch {
case ex: Throwable =>
report.error(s"Couldn't create file for class $cName\n${ex.getMessage}", ctx.source.atSpan(csym.span))
report.error(em"Couldn't create file for class $cName\n${ex.getMessage}", ctx.source.atSpan(csym.span))
null
}
}
Expand Down Expand Up @@ -422,7 +422,7 @@ trait BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
emitAssocs(nestedVisitor, assocs, bcodeStore)(innerClasesStore)

case t =>
report.error(ex"Annotation argument is not a constant", t.sourcePos)
report.error(em"Annotation argument is not a constant", t.sourcePos)
}
}

Expand Down Expand Up @@ -871,10 +871,11 @@ trait BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
try body
catch {
case ex: Throwable =>
report.error(i"""|compiler bug: created invalid generic signature for $sym in ${sym.denot.owner.showFullName}
|signature: $sig
|if this is reproducible, please report bug at https://github.com/lampepfl/dotty/issues
""".trim, sym.sourcePos)
report.error(
em"""|compiler bug: created invalid generic signature for $sym in ${sym.denot.owner.showFullName}
|signature: $sig
|if this is reproducible, please report bug at https://github.com/lampepfl/dotty/issues
""", sym.sourcePos)
throw ex
}
}
Expand Down
9 changes: 5 additions & 4 deletions compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ trait BCodeSkelBuilder extends BCodeHelpers {

if (params.size > MaximumJvmParameters) {
// SI-7324
report.error(s"Platform restriction: a parameter list's length cannot exceed $MaximumJvmParameters.", ctx.source.atSpan(methSymbol.span))
report.error(em"Platform restriction: a parameter list's length cannot exceed $MaximumJvmParameters.", ctx.source.atSpan(methSymbol.span))
return
}

Expand Down Expand Up @@ -800,9 +800,10 @@ trait BCodeSkelBuilder extends BCodeHelpers {
val veryFirstProgramPoint = currProgramPoint()

if trimmedRhs == tpd.EmptyTree then
report.error("Concrete method has no definition: " + dd + (
if (ctx.settings.Ydebug.value) "(found: " + methSymbol.owner.info.decls.toList.mkString(", ") + ")"
else ""),
report.error(
em"Concrete method has no definition: $dd${
if (ctx.settings.Ydebug.value) "(found: " + methSymbol.owner.info.decls.toList.mkString(", ") + ")"
else ""}",
ctx.source.atSpan(NoSpan)
)
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Contexts._
import Types._
import Symbols._
import Phases._
import Decorators.em

import dotty.tools.dotc.util.ReadOnlyMap
import dotty.tools.dotc.report
Expand Down Expand Up @@ -71,7 +72,7 @@ class DottyBackendInterface(val outputDirectory: AbstractFile, val superCallsMap
def _1: Type = field.tpe match {
case JavaArrayType(elem) => elem
case _ =>
report.error(s"JavaSeqArray with type ${field.tpe} reached backend: $field", ctx.source.atSpan(field.span))
report.error(em"JavaSeqArray with type ${field.tpe} reached backend: $field", ctx.source.atSpan(field.span))
UnspecifiedErrorType
}
def _2: List[Tree] = field.elems
Expand Down
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/backend/jvm/GenBCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import dotty.tools.dotc.sbt.ExtractDependencies
import Contexts._
import Phases._
import Symbols._
import Decorators.em

import java.io.DataOutputStream
import java.nio.channels.ClosedByInterruptException
Expand Down Expand Up @@ -308,7 +309,7 @@ class GenBCodePipeline(val int: DottyBackendInterface, val primitives: DottyPrim
getFileForClassfile(outF, cls.name, ".class")
} catch {
case e: FileConflictException =>
report.error(s"error writing ${cls.name}: ${e.getMessage}")
report.error(em"error writing ${cls.name}: ${e.getMessage}")
null
}
} else null
Expand Down Expand Up @@ -608,11 +609,11 @@ class GenBCodePipeline(val int: DottyBackendInterface, val primitives: DottyPrim
val method =
s"${e.getClassName.replaceAll("/", ".")}.${e.getMethodName}"
val msg =
s"Generated bytecode for method '$method' is too large. Size: ${e.getCodeSize} bytes. Limit is 64KB"
em"Generated bytecode for method '$method' is too large. Size: ${e.getCodeSize} bytes. Limit is 64KB"
report.error(msg)
case e: ClassTooLargeException =>
val msg =
s"Class '${e.getClassName.replaceAll("/", ".")}' is too large. Constant pool size: ${e.getConstantPoolCount}. Limit is 64K entries"
em"Class '${e.getClassName.replaceAll("/", ".")}' is too large. Constant pool size: ${e.getConstantPoolCount}. Limit is 64K entries"
report.error(msg)

}
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/backend/jvm/scalaPrimitives.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Contexts._
import Names.TermName, StdNames._
import Types.{JavaArrayType, UnspecifiedErrorType, Type}
import Symbols.{Symbol, NoSymbol}
import Decorators.em
import dotc.report
import dotc.util.ReadOnlyMap

Expand Down Expand Up @@ -66,7 +67,7 @@ class DottyPrimitives(ictx: Context) {
case defn.ArrayOf(el) => el
case JavaArrayType(el) => el
case _ =>
report.error(s"expected Array $tpe")
report.error(em"expected Array $tpe")
UnspecifiedErrorType
}

Expand Down Expand Up @@ -133,7 +134,7 @@ class DottyPrimitives(ictx: Context) {
def addPrimitives(cls: Symbol, method: TermName, code: Int)(using Context): Unit = {
val alts = cls.info.member(method).alternatives.map(_.symbol)
if (alts.isEmpty)
report.error(s"Unknown primitive method $cls.$method")
report.error(em"Unknown primitive method $cls.$method")
else alts foreach (s =>
addPrimitive(s,
s.info.paramInfoss match {
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/backend/sjs/JSCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2926,7 +2926,7 @@ class JSCodeGen()(using genCtx: Context) {
case defn.ArrayOf(el) => el
case JavaArrayType(el) => el
case tpe =>
val msg = ex"expected Array $tpe"
val msg = em"expected Array $tpe"
report.error(msg)
ErrorType(msg)
}
Expand Down Expand Up @@ -3652,7 +3652,7 @@ class JSCodeGen()(using genCtx: Context) {
} else if (sym.isJSType) {
if (sym.is(Trait)) {
report.error(
s"isInstanceOf[${sym.fullName}] not supported because it is a JS trait",
em"isInstanceOf[${sym.fullName}] not supported because it is a JS trait",
pos)
js.BooleanLiteral(true)
} else {
Expand Down
11 changes: 5 additions & 6 deletions compiler/src/dotty/tools/backend/sjs/JSExportsGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {

for ((info, _) <- tups.tail) {
report.error(
em"export overload conflicts with export of $firstSym: " +
"a field may not share its exported name with another export",
em"export overload conflicts with export of $firstSym: a field may not share its exported name with another export",
info.pos)
}

Expand Down Expand Up @@ -264,8 +263,8 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
.alternatives

assert(!alts.isEmpty,
em"Ended up with no alternatives for ${classSym.fullName}::$name. " +
em"Original set was ${alts} with types ${alts.map(_.info)}")
em"""Ended up with no alternatives for ${classSym.fullName}::$name.
|Original set was ${alts} with types ${alts.map(_.info)}""")

val (jsName, isProp) = exportNameInfo(name)

Expand Down Expand Up @@ -309,7 +308,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
if (isProp && methodSyms.nonEmpty) {
val firstAlt = alts.head
report.error(
i"Conflicting properties and methods for ${classSym.fullName}::$name.",
em"Conflicting properties and methods for ${classSym.fullName}::$name.",
firstAlt.srcPos)
implicit val pos = firstAlt.span
js.JSPropertyDef(js.MemberFlags.empty, genExpr(name)(firstAlt.sourcePos), None, None)
Expand Down Expand Up @@ -613,7 +612,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
val altsTypesInfo = alts.map(_.info.show).sorted.mkString("\n ")

report.error(
s"Cannot disambiguate overloads for $fullKind $displayName with types\n $altsTypesInfo",
em"Cannot disambiguate overloads for $fullKind $displayName with types\n $altsTypesInfo",
pos)
}

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/sjs/JSPositions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.net.{URI, URISyntaxException}

import dotty.tools.dotc.core._
import Contexts._
import Decorators.em

import dotty.tools.dotc.report

Expand All @@ -31,7 +32,7 @@ class JSPositions()(using Context) {
URIMap(from, to) :: Nil
} catch {
case e: URISyntaxException =>
report.error(s"${e.getInput} is not a valid URI")
report.error(em"${e.getInput} is not a valid URI")
Nil
}
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/backend/sjs/JSPrimitives.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Names.TermName
import Types._
import Contexts._
import Symbols._
import Decorators.em

import dotty.tools.dotc.ast.tpd._
import dotty.tools.backend.jvm.DottyPrimitives
Expand Down Expand Up @@ -90,7 +91,7 @@ class JSPrimitives(ictx: Context) extends DottyPrimitives(ictx) {
def addPrimitives(cls: Symbol, method: TermName, code: Int)(using Context): Unit = {
val alts = cls.info.member(method).alternatives.map(_.symbol)
if (alts.isEmpty) {
report.error(s"Unknown primitive method $cls.$method")
report.error(em"Unknown primitive method $cls.$method")
} else {
for (s <- alts)
addPrimitive(s, code)
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/CompilationUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ object CompilationUnit {
if (!mustExist)
source
else if (source.file.isDirectory) {
report.error(s"expected file, received directory '${source.file.path}'")
report.error(em"expected file, received directory '${source.file.path}'")
NoSource
}
else if (!source.file.exists) {
report.error(s"source file not found: ${source.file.path}")
report.error(em"source file not found: ${source.file.path}")
NoSource
}
else source
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/Driver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ class Driver {
val newEntries: List[String] = files
.flatMap { file =>
if !file.exists then
report.error(s"File does not exist: ${file.path}")
report.error(em"File does not exist: ${file.path}")
None
else file.extension match
case "jar" => Some(file.path)
case "tasty" =>
TastyFileUtil.getClassPath(file) match
case Some(classpath) => Some(classpath)
case _ =>
report.error(s"Could not load classname from: ${file.path}")
report.error(em"Could not load classname from: ${file.path}")
None
case _ =>
report.error(s"File extension is not `tasty` or `jar`: ${file.path}")
report.error(em"File extension is not `tasty` or `jar`: ${file.path}")
None
}
.distinct
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ object desugar {
if (local.exists) (defctx.owner.thisType select local).dealiasKeepAnnots
else {
def msg =
s"no matching symbol for ${tp.symbol.showLocated} in ${defctx.owner} / ${defctx.effectiveScope.toList}"
em"no matching symbol for ${tp.symbol.showLocated} in ${defctx.owner} / ${defctx.effectiveScope.toList}"
ErrorType(msg).assertingErrorsReported(msg)
}
case _ =>
Expand Down Expand Up @@ -911,7 +911,7 @@ object desugar {
case params :: paramss1 => // `params` must have a single parameter and without `given` flag

def badRightAssoc(problem: String) =
report.error(i"right-associative extension method $problem", mdef.srcPos)
report.error(em"right-associative extension method $problem", mdef.srcPos)
extParamss ++ mdef.paramss

params match
Expand Down Expand Up @@ -1137,7 +1137,7 @@ object desugar {
def errorOnGivenBinding(bind: Bind)(using Context): Boolean =
report.error(
em"""${hl("given")} patterns are not allowed in a ${hl("val")} definition,
|please bind to an identifier and use an alias given.""".stripMargin, bind)
|please bind to an identifier and use an alias given.""", bind)
false

def isTuplePattern(arity: Int): Boolean = pat match {
Expand Down Expand Up @@ -1237,7 +1237,7 @@ object desugar {
def checkOpaqueAlias(tree: MemberDef)(using Context): MemberDef =
def check(rhs: Tree): MemberDef = rhs match
case bounds: TypeBoundsTree if bounds.alias.isEmpty =>
report.error(i"opaque type must have a right-hand side", tree.srcPos)
report.error(em"opaque type must have a right-hand side", tree.srcPos)
tree.withMods(tree.mods.withoutFlags(Opaque))
case LambdaTypeTree(_, body) => check(body)
case _ => tree
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ object DesugarEnums {
case Ident(name) =>
val matches = tparamNames.contains(name)
if (matches && (caseTypeParams.nonEmpty || vparamss.isEmpty))
report.error(i"illegal reference to type parameter $name from enum case", tree.srcPos)
report.error(em"illegal reference to type parameter $name from enum case", tree.srcPos)
matches
case LambdaTypeTree(lambdaParams, body) =>
underBinders(lambdaParams, foldOver(x, tree))
Expand Down
22 changes: 11 additions & 11 deletions compiler/src/dotty/tools/dotc/ast/MainProxies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object MainProxies {

def addArgs(call: untpd.Tree, mt: MethodType, idx: Int): untpd.Tree =
if (mt.isImplicitMethod) {
report.error(s"@main method cannot have implicit parameters", pos)
report.error(em"@main method cannot have implicit parameters", pos)
call
}
else {
Expand All @@ -74,7 +74,7 @@ object MainProxies {
mt.resType match {
case restpe: MethodType =>
if (mt.paramInfos.lastOption.getOrElse(NoType).isRepeatedParam)
report.error(s"varargs parameter of @main method must come last", pos)
report.error(em"varargs parameter of @main method must come last", pos)
addArgs(call1, restpe, idx + args.length)
case _ =>
call1
Expand All @@ -83,17 +83,17 @@ object MainProxies {

var result: List[TypeDef] = Nil
if (!mainFun.owner.isStaticOwner)
report.error(s"@main method is not statically accessible", pos)
report.error(em"@main method is not statically accessible", pos)
else {
var call = ref(mainFun.termRef)
mainFun.info match {
case _: ExprType =>
case mt: MethodType =>
call = addArgs(call, mt, 0)
case _: PolyType =>
report.error(s"@main method cannot have type parameters", pos)
report.error(em"@main method cannot have type parameters", pos)
case _ =>
report.error(s"@main can only annotate a method", pos)
report.error(em"@main can only annotate a method", pos)
}
val errVar = Ident(nme.error)
val handler = CaseDef(
Expand Down Expand Up @@ -203,7 +203,7 @@ object MainProxies {
))
(sym, paramAnnotations.toVector, defaultValueSymbols(scope, sym), stat.rawComment) :: Nil
case mainAnnot :: others =>
report.error(s"method cannot have multiple main annotations", mainAnnot.tree)
report.error(em"method cannot have multiple main annotations", mainAnnot.tree)
Nil
}
case stat @ TypeDef(_, impl: Template) if stat.symbol.is(Module) =>
Expand Down Expand Up @@ -379,26 +379,26 @@ object MainProxies {
end generateMainClass

if (!mainFun.owner.isStaticOwner)
report.error(s"main method is not statically accessible", pos)
report.error(em"main method is not statically accessible", pos)
None
else mainFun.info match {
case _: ExprType =>
Some(generateMainClass(unitToValue(ref(mainFun.termRef)), Nil, Nil))
case mt: MethodType =>
if (mt.isImplicitMethod)
report.error(s"main method cannot have implicit parameters", pos)
report.error(em"main method cannot have implicit parameters", pos)
None
else mt.resType match
case restpe: MethodType =>
report.error(s"main method cannot be curried", pos)
report.error(em"main method cannot be curried", pos)
None
case _ =>
Some(generateMainClass(unitToValue(Apply(ref(mainFun.termRef), argRefs(mt))), argValDefs(mt), parameterInfos(mt)))
case _: PolyType =>
report.error(s"main method cannot have type parameters", pos)
report.error(em"main method cannot have type parameters", pos)
None
case _ =>
report.error(s"main can only annotate a method", pos)
report.error(em"main can only annotate a method", pos)
None
}
}
Expand Down
Loading