Skip to content

Commit 5863b61

Browse files
authored
Merge pull request #9639 from dotty-staging/delay-msgs
Avoid using show or toText before an error is reported
2 parents bc13c67 + 9ddca02 commit 5863b61

File tree

9 files changed

+24
-17
lines changed

9 files changed

+24
-17
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,10 @@ object Trees {
393393
def isBackquoted: Boolean = hasAttachment(Backquoted)
394394
}
395395

396-
class SearchFailureIdent[-T >: Untyped] private[ast] (name: Name)(implicit @constructorOnly src: SourceFile)
396+
class SearchFailureIdent[-T >: Untyped] private[ast] (name: Name, expl: => String)(implicit @constructorOnly src: SourceFile)
397397
extends Ident[T](name) {
398-
override def toString: String = s"SearchFailureIdent($name)"
398+
def explanation = expl
399+
override def toString: String = s"SearchFailureIdent($explanation)"
399400
}
400401

401402
/** qualifier.name, or qualifier#name, if qualifier is a type */

compiler/src/dotty/tools/dotc/ast/untpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
359359
// ------ Creation methods for untyped only -----------------
360360

361361
def Ident(name: Name)(implicit src: SourceFile): Ident = new Ident(name)
362-
def SearchFailureIdent(name: Name)(implicit src: SourceFile): SearchFailureIdent = new SearchFailureIdent(name)
362+
def SearchFailureIdent(name: Name, explanation: => String)(implicit src: SourceFile): SearchFailureIdent = new SearchFailureIdent(name, explanation)
363363
def Select(qualifier: Tree, name: Name)(implicit src: SourceFile): Select = new Select(qualifier, name)
364364
def SelectWithSig(qualifier: Tree, name: Name, sig: Signature)(implicit src: SourceFile): Select = new SelectWithSig(qualifier, name, sig)
365365
def This(qual: Ident)(implicit src: SourceFile): This = new This(qual)

compiler/src/dotty/tools/dotc/core/Denotations.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ object Denotations {
317317

318318
def requiredMethod(pname: PreName, argTypes: List[Type])(using Context): TermSymbol = {
319319
val name = pname.toTermName
320-
info.member(name).requiredSymbol(i"method", name, this, argTypes) { x =>
320+
info.member(name).requiredSymbol("method", name, this, argTypes) { x =>
321321
x.is(Method) && {
322322
x.info.paramInfoss match {
323323
case paramInfos :: Nil => paramInfos.corresponds(argTypes)(_ =:= _)

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ object StdNames {
143143
val QUOTE: N = "'"
144144
val TYPE_QUOTE: N = "type_'"
145145
val TRAIT_SETTER_SEPARATOR: N = str.TRAIT_SETTER_SEPARATOR
146+
val AMBIGUOUS: N = "/* ambiguous */"
147+
val MISSING: N = "/* missing */"
146148

147149
// value types (and AnyRef) are all used as terms as well
148150
// as (at least) arguments to the @specialize annotation.

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,10 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
352352
case id: Trees.SearchFailureIdent[?] =>
353353
tree.typeOpt match {
354354
case reason: Implicits.SearchFailureType =>
355-
toText(id.name)
355+
Str(id.explanation)
356356
~ ("summon[" ~ toText(reason.clarify(reason.expectedType)) ~ "]").close
357357
case _ =>
358-
toText(id.name)
358+
Str(id.explanation)
359359
}
360360
case id @ Ident(name) =>
361361
val txt = tree.typeOpt match {

compiler/src/dotty/tools/dotc/report.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ object report:
4343
def featureWarning(msg: Message, pos: SrcPos = NoSourcePosition)(using Context): Unit =
4444
issueWarning(new FeatureWarning(msg, pos.sourcePos))
4545

46-
def featureWarning(feature: String, featureDescription: String,
46+
def featureWarning(feature: String, featureDescription: => String,
4747
featureUseSite: Symbol, required: Boolean, pos: SrcPos)(using Context): Unit = {
4848
val req = if (required) "needs to" else "should"
4949
val fqname = s"scala.language.$feature"
@@ -56,7 +56,7 @@ object report:
5656
|See the Scala docs for value $fqname for a discussion
5757
|why the feature $req be explicitly enabled.""".stripMargin
5858

59-
val msg = s"""$featureDescription $req be enabled
59+
def msg = s"""$featureDescription $req be enabled
6060
|by adding the import clause 'import $fqname'
6161
|or by setting the compiler option -language:$feature.$explain""".stripMargin
6262
if (required) error(msg, pos)

compiler/src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import util.NoSourcePosition
1717

1818
import java.io.{ BufferedReader, PrintWriter }
1919

20-
2120
object Reporter {
2221
/** Convert a SimpleReporter into a real Reporter */
2322
def fromSimpleReporter(simple: interfaces.SimpleReporter): Reporter =
@@ -142,9 +141,14 @@ abstract class Reporter extends interfaces.ReporterResult {
142141
var unreportedWarnings: Map[String, Int] = Map.empty
143142

144143
def report(dia: Diagnostic)(using Context): Unit =
145-
if (!isHidden(dia)) {
144+
val isSummarized = dia match
145+
case dia: ConditionalWarning => !dia.enablingOption.value
146+
case _ => false
147+
if isSummarized // avoid isHidden test for summarized warnings so that message is not forced
148+
|| !isHidden(dia)
149+
then
146150
withMode(Mode.Printing)(doReport(dia))
147-
dia match {
151+
dia match
148152
case dia: ConditionalWarning if !dia.enablingOption.value =>
149153
val key = dia.enablingOption.name
150154
unreportedWarnings =
@@ -155,8 +159,6 @@ abstract class Reporter extends interfaces.ReporterResult {
155159
_errorCount += 1
156160
case dia: Info => // nothing to do here
157161
// match error if d is something else
158-
}
159-
}
160162

161163
def incomplete(dia: Diagnostic)(using Context): Unit =
162164
incompleteHandler(dia, ctx)

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ trait Applications extends Compatibility {
617617
case nil =>
618618
args match {
619619
case arg :: args1 =>
620-
val msg = arg match
620+
def msg = arg match
621621
case untpd.Tuple(Nil)
622622
if applyKind == ApplyKind.InfixTuple && funType.widen.isNullaryMethod =>
623623
i"can't supply unit value with infix notation because nullary $methString takes no arguments; use dotted invocation instead: (...).${methRef.name}()"

compiler/src/dotty/tools/dotc/typer/Implicits.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,11 @@ object Implicits:
396396
object SearchFailure {
397397
def apply(tpe: SearchFailureType)(using Context): SearchFailure = {
398398
val id = tpe match
399-
case tpe: AmbiguousImplicits => i"/* ambiguous: ${tpe.explanation} */"
400-
case _ => "/* missing */"
401-
SearchFailure(untpd.SearchFailureIdent(id.toTermName).withTypeUnchecked(tpe))
399+
case tpe: AmbiguousImplicits =>
400+
untpd.SearchFailureIdent(nme.AMBIGUOUS, s"/* ambiguous: ${tpe.explanation} */")
401+
case _ =>
402+
untpd.SearchFailureIdent(nme.MISSING, "/* missing */")
403+
SearchFailure(id.withTypeUnchecked(tpe))
402404
}
403405
}
404406

0 commit comments

Comments
 (0)