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

Scala compiler reports an error on for a wrong expression #9636

Closed
scabug opened this issue Jan 30, 2016 · 4 comments
Closed

Scala compiler reports an error on for a wrong expression #9636

scabug opened this issue Jan 30, 2016 · 4 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jan 30, 2016

Scala 2.11.x for a function:

def isWrapper(is: FileInputStream): InputStream = {
  val pb = new PushbackInputStream(is, 2)
  val signature = new Array[Byte](2)
  pb.read(signature)
  pb.unread(signature)
  if (signature.sameElements(Array(0x1F, 0x8B))) {
    new GZIPInputStream(new BufferedInputStream(pb))
  } else {
    pb
  }
}

reports: a type was inferred to be AnyVal; this may indicate a programming error.

def isWrapper(is: FileInputStream): InputStream = {
                                               ^

Although an compilation problem corresponds to: signature.sameElements(Array(0x1F, 0x8B)) where second array is Array[Int] and not Array[Byte].

http://stackoverflow.com/questions/35104769/scala-anyval-inferred-instead-of-inputstream

@scabug
Copy link
Author

scabug commented Jan 30, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9636?orig=1
Reporter: Artem Oboturov (oboturov-at-gmail.com)
Affected Versions: 2.11.7
See #9211

@scabug
Copy link
Author

scabug commented Jan 30, 2016

@som-snytt said:
The caret wanders:

Welcome to Scala 2.12.0-M3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions for evaluation. Or try :help.

scala> (new Array[Byte](2)).sameElements(Array(0x1F, 0x8B))
<console>:12: warning: a type was inferred to be `AnyVal`; this may indicate a programming error.
       (new Array[Byte](2)).sameElements(Array(0x1F, 0x8B))
                                              ^
res0: Boolean = false

scala> def f = if ((new Array[Byte](2)).sameElements(Array(0x1F, 0x8B))) true else false
<console>:11: warning: a type was inferred to be `AnyVal`; this may indicate a programming error.
       def f = if ((new Array[Byte](2)).sameElements(Array(0x1F, 0x8B))) true else false
           ^
f: Boolean

@scabug
Copy link
Author

scabug commented Feb 1, 2016

@retronym said (edited on Feb 1, 2016 6:06:43 AM UTC):
methTypeArgs should accept an extra parameter with the position of the fn tree, rather than trying to reverse engineer the position by throwing darts, blindfolded, at context.tree.

      def argumentPosition(idx: Int): Position = context.tree match {
        case x: ValOrDefDef => x.rhs match {
          case Apply(fn, args) if idx < args.size => args(idx).pos
          case _                                  => context.tree.pos
        }
        case _ => context.tree.pos
      }
      if (settings.warnInferAny && context.reportErrors && canWarnAboutAny) {
        foreachWithIndex(targs) ((targ, idx) =>
          targ.typeSymbol match {
            case sym @ (AnyClass | AnyValClass) =>
              reporter.warning(argumentPosition(idx), s"a type was inferred to be `${sym.name}`; this may indicate a programming error.")
            case _ =>
          }
        )
      }

@scabug
Copy link
Author

scabug commented Nov 19, 2016

@som-snytt said:
scala/scala#5546

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

No branches or pull requests

2 participants