From 0894cd3c69d1c22819027d0788954981d8c22429 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Fri, 7 Feb 2020 10:52:41 -0800 Subject: [PATCH] Restore warn-any improvement --- .../scala/tools/nsc/typechecker/Infer.scala | 19 ++++++++++++++----- test/files/neg/warn-inferred-any.check | 5 +---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/compiler/scala/tools/nsc/typechecker/Infer.scala b/src/compiler/scala/tools/nsc/typechecker/Infer.scala index f86eb2f67bec..1688c0eb4aec 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Infer.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Infer.scala @@ -13,11 +13,10 @@ package scala.tools.nsc package typechecker -import scala.collection.mutable.ListBuffer -import scala.collection.{immutable, mutable} +import scala.collection.{immutable, mutable}, mutable.ListBuffer +import scala.reflect.internal.Depth import scala.util.control.ControlThrowable import symtab.Flags._ -import scala.reflect.internal.Depth /** This trait contains methods related to type parameter inference. * @@ -589,8 +588,18 @@ trait Infer extends Checkable { // ...or lower bound of a type param, since they're asking for it. var checked, warning = false def checkForAny(): Unit = { - val collector = new ContainsAnyCollector(topTypes) - @`inline` def containsAny(t: Type) = t.dealiasWidenChain.exists(collector.collect) + val collector = new ContainsAnyCollector(topTypes) { + val seen = mutable.Set.empty[Type] + override def apply(t: Type): Unit = { + def saw(dw: Type): Unit = + if (!result && !seen(dw)) { + seen += dw + if (!dw.typeSymbol.isRefinementClass) super.apply(dw) + } + if (!result && !seen(t)) t.dealiasWidenChain.foreach(saw) + } + } + @`inline` def containsAny(t: Type) = collector.collect(t) val hasAny = containsAny(pt) || containsAny(restpe) || formals.exists(containsAny) || argtpes.exists(containsAny) || diff --git a/test/files/neg/warn-inferred-any.check b/test/files/neg/warn-inferred-any.check index 862a74d664f8..e9809a04c723 100644 --- a/test/files/neg/warn-inferred-any.check +++ b/test/files/neg/warn-inferred-any.check @@ -13,9 +13,6 @@ warn-inferred-any.scala:27: warning: a type was inferred to be `Any`; this may i warn-inferred-any.scala:37: warning: a type was inferred to be `Object`; this may indicate a programming error. cs.contains(new C2) // warns ^ -warn-inferred-any.scala:52: warning: a type was inferred to be `Any`; this may indicate a programming error. - stream.mapM(f) // should not warn - ^ error: No warnings can be incurred under -Werror. -6 warnings +5 warnings 1 error