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
Inconsistent behavior of -Ywarn-value-discard flag #11379
Comments
A bit richer example will be with bos-pchernikov-mac:personal pchernikov$ cat UnitOfTrust.scala
object UnitOfTrust {
import scalaz._
import scalaz.Scalaz._
private def unitRight[A]: A \/ Unit = ().right[A]
// fails with: discarded non-Unit value
// def test1: \/[String, Unit] = \/-(\/-(()))
// def test2: \/[String, Unit] = ().right[String].map(_ => unitRight[String])
// def test3: \/[String, Unit] = ().right[String].map { case _ => unitRight[String] }
// fails with: type mismatch;
// def test4: \/[String, Unit] = ().right[String].right[String]
// fails with: polymorphic expression cannot be instantiated to expected type;
// def test5: \/[String, Unit] = ().right[String].map(().right)
// compiles just fine
def test6: \/[String, Unit] = ().right[String].map { case _ => unitRight }
def test7: \/[String, Unit] = ().right[String].map { _ => unitRight}
def test8: \/[String, Unit] = ().right[String].map(_ => unitRight)
}bos-pchernikov-mac:personal pchernikov$ ~/Downloads/scala-2.12.8/bin/scalac -classpath /Users/pchernikov/.ivy2/cache/org.scalaz/scalaz-core_2.12/bundles/scalaz-core_2.12-7.2.27.jar -Ywarn-value-discad -Xfatal-warnings UnitOfTrust.scala
bos-pchernikov-mac:personal pchernikov$ |
I've created an example that I think demonstrates that this issue is very likely related to having multiple type parameters: Example specifically pertaining to this claim: class OneTypeParam[B](value: B) {
def map[B1](fn: B => B1): OneTypeParam[B1] = new OneTypeParam(fn(value))
def unitValue: OneTypeParam[Unit] = new OneTypeParam(())
def checkCompiler: OneTypeParam[Unit] = unitValue.map(_ => unitValue)
}
class TwoTypeParam[A, B](value: B) {
def map[B1](fn: B => B1): TwoTypeParam[A, B1] = new TwoTypeParam(fn(value))
def unitValue[C]: TwoTypeParam[C, Unit] = new TwoTypeParam(())
def checkCompiler: TwoTypeParam[String, Unit] = unitValue.map(_ => unitValue)
} Compiling this with both
|
Upon further investigation, this also effects a method marked as having a type that needs solving, even if it isn't used: class OneTypeParam[B](value: B) {
def map[B1](fn: B => B1): OneTypeParam[B1] = new OneTypeParam(fn(value))
def unitValue: OneTypeParam[Unit] = new OneTypeParam(())
def typedValue[C]: OneTypeParam[Unit] = new OneTypeParam(())
def checkCompilerUnTyped: OneTypeParam[Unit] = unitValue.map(_ => unitValue)
def checkCompilerTypedInner: OneTypeParam[Unit] = unitValue.map(_ => typedValue)
def checkCompilerTypedOuter: OneTypeParam[Unit] = typedValue.map(_ => unitValue)
def checkCompilerTypedBoth: OneTypeParam[Unit] = typedValue.map(_ => typedValue)
} This yields warnings on the original footprint (
Looking at the typer output yields this tree for
Compared to
|
As another data point, the minimized example progresses after M5. Maybe that's due to improvements in inference for functions.
|
@som-snytt is this, by any chance, essentially a duplicate of #11998? |
I pushed the tests for 2.13, but unlikely to backport a fix to 2.12 (which is probably a non-trivial change to type inference). |
It looks like
-Ywarn-value-discard
flag doesn't always warn on value discarding:scalac
output:This may also have a regression from
scala
2.11
to2.12
:The text was updated successfully, but these errors were encountered: