@@ -116,12 +116,15 @@ class TailRec extends MiniPhase {
116116 override def transformDefDef (tree : DefDef )(implicit ctx : Context ): Tree = {
117117 val method = tree.symbol
118118 val mandatory = method.hasAnnotation(defn.TailrecAnnot )
119- def noTailTransform = {
119+ def noTailTransform ( failureReported : Boolean ) = {
120120 // FIXME: want to report this error on `tree.namePos`, but
121121 // because of extension method getting a weird pos, it is
122- // better to report on method symbol so there's no overlap
123- if (mandatory)
122+ // better to report on method symbol so there's no overlap.
123+ // We don't report a new error if failures were reported
124+ // during the transformation.
125+ if (mandatory && ! failureReported)
124126 ctx.error(TailrecNotApplicable (method), method.pos)
127+
125128 tree
126129 }
127130
@@ -182,14 +185,15 @@ class TailRec extends MiniPhase {
182185 )
183186 )
184187 }
185- else noTailTransform
188+ else noTailTransform(failureReported = transformer.failureReported)
186189 }
187- else noTailTransform
190+ else noTailTransform(failureReported = false )
188191 }
189192
190193 class TailRecElimination (method : Symbol , enclosingClass : ClassSymbol , paramSyms : List [Symbol ], isMandatory : Boolean ) extends TreeMap {
191194
192195 var rewrote : Boolean = false
196+ var failureReported : Boolean = false
193197
194198 /** The `tailLabelN` label symbol, used to encode a `continue` from the infinite `while` loop. */
195199 private [this ] var myContinueLabel : Symbol = _
@@ -276,8 +280,12 @@ class TailRec extends MiniPhase {
276280 cpy.Apply (tree)(noTailTransform(tree.fun), arguments)
277281
278282 def fail (reason : String ) = {
279- if (isMandatory) ctx.error(s " Cannot rewrite recursive call: $reason" , tree.pos)
280- else tailrec.println(" Cannot rewrite recursive call at: " + tree.pos + " because: " + reason)
283+ if (isMandatory) {
284+ failureReported = true
285+ ctx.error(s " Cannot rewrite recursive call: $reason" , tree.pos)
286+ }
287+ else
288+ tailrec.println(" Cannot rewrite recursive call at: " + tree.pos + " because: " + reason)
281289 continue
282290 }
283291
@@ -339,7 +347,7 @@ class TailRec extends MiniPhase {
339347 else fail(" it is not in tail position" )
340348 }
341349 else if (isRecursiveSuperCall)
342- fail(" it contains a recursive call targeting a supertype" )
350+ fail(" it targets a supertype" )
343351 else
344352 continue
345353 }
0 commit comments