Skip to content
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

Spurious unchecked warning #6275

Closed
scabug opened this issue Aug 23, 2012 · 9 comments
Closed

Spurious unchecked warning #6275

scabug opened this issue Aug 23, 2012 · 9 comments
Assignees
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Aug 23, 2012

def foo = {
  var a = ArrayBuffer[Int]()
  println (a.isInstanceOf[Seq[Int]])
}

gives the following warning under -checked:

warning: non variable type-argument Int in type scala.collection.mutable.Seq[Int] is unchecked since it is eliminated by erasure
  println (a.isInstanceOf[Seq[Int]])

Surely there should be no complaint about erasure in a purely local analysis. I'm telling it that it is a buffer of Int, and expect it to infer that it is a seq of Int.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Aug 23, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6275?orig=1
Reporter: Sriram Srinivasan (sriram.srinivasan)
Affected Versions: 2.10.0-M7

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Aug 24, 2012

@retronym said:
Use a type ascription, not a cast, to achieve what you describe.

println(a: Seq[Int])

In the future, could you please first check on the [scala-language] mailing list before lodging tickets for things that are not clear bugs.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Aug 24, 2012

Sriram Srinivasan (sriram.srinivasan) said (edited on Aug 24, 2012 6:11:11 AM UTC):
You could start by giving me a little credit. I know exactly how to get around the warning. The issue is not about the cast.

The bigger issue, which affects my code everywhere is this: The variable a is local, and any self-respecting intra-procedural analysis should reflect that a is a Seq[Int]. It should not need any further ascription, and requiring it should be considered a bug. This should NOT be an issue of erasure.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Aug 24, 2012

@retronym said:
That's all fine, but JIRA is just so overloaded that we can't use it for these sort of discussions. "Please use the mailing list" isn't intended to be dismissive, it is just to channel your questions / suggestions to a suitable forum with a wider audience.

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Aug 24, 2012

@som-snytt said:
You don't have to give me any credit, if I don't get it.

The example is isInstanceOf, not asInstanceOf.

For the case of asInstanceOf, there is no extra checkcast. Let's give intraprocedural analysis some respect.

For the lack of a warning, #1558. So on that basis, I'll never asInstanceOf, because I'll never presume to tell the type system I know what I'm doing. Except maybe null.asInstanceOf.

For the case of isInstanceOf, maybe the question is, should instanceof be optimized away.

If you issue the instanceof, should you still warn? And if you must warn, is there a way to selectively turn off warnings? (Hopefully eventually?)

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Aug 30, 2012

@SethTisue said:
So Jason mistook isInstanceOf for asInstanceOf, but it doesn't matter: he's right on the larger issue, which is that this should be a mailing list discussion, not a ticket. Eventually one or more tickets might result. (And telling you this is being helpful, not issuing a reprimand.)

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Aug 30, 2012

@SethTisue said:
there is now a thread, started by Mark Harrah, at https://groups.google.com/d/topic/scala-internals/9bT7clmkLYk/discussion

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 7, 2012

@harrah said:
Reopening due to the mentioned thread and copying the example here:

I see the following spurious warning frequently, which drowns out useful unchecked warnings:

sealed trait A[T]
final class B[T] extends A[T]

object ParsedAxis {
   def x(a: A[Int]) =
      a match {
         case b: B[Int] => 3
      }
}
A.scala:7: non-variable type argument Int in type pattern B[Int] is unchecked since it is eliminated by erasure
         case b: B[Int] => 3
                 ^

In this case, the warning can be resolved by using a type variable:

         case b: B[i] => 3

but this is less useful when reading the code and isn't possible when B[Int] is inside a type alias like type BI = B[Int]. Similarly, Int @unchecked can be used, but it is less safe than the type variable approach, where the compiler still knows that i=Int is the only possibility. In fact, if another type argument is used, the compiler complains:

pattern type is incompatible with expected type;
  found   : B[String]
  required: A[Int]
         case b: B[String] => 3
                 ^
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 7, 2012

@harrah said:
Copied from Paul's comment on #6338:

I'm not really working on 2.10 anymore, but if anyone wants it, this warning and other issues are handled here: https://github.com/paulp/scala/tree/topic/210-quieter-warnings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.