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

compilation error for ^= operator #10077

Closed
scabug opened this issue Nov 25, 2016 · 6 comments
Closed

compilation error for ^= operator #10077

scabug opened this issue Nov 25, 2016 · 6 comments

Comments

@scabug
Copy link

scabug commented Nov 25, 2016

object Main {
    val arr1 = new Array[Byte](10)
    val arr2 = new Array[Byte](10)

    arr1(4) ^= arr2(5)
}

Expected: successful compilation
Actual:

[error] /tmp/1/Main.scala:5: type mismatch;
[error]  found   : Int
[error]  required: Byte
[error]     arr1(4) ^= arr2(5)
[error]             ^
[error] one error found
@scabug
Copy link
Author

scabug commented Nov 25, 2016

Imported From: https://issues.scala-lang.org/browse/SI-10077?orig=1
Reporter: Sergey Alaev (salaev)
Affected Versions: 2.12.0
See #9834
Attachments:

@scabug
Copy link
Author

scabug commented Nov 25, 2016

@som-snytt said:
This is because x ^ y is an Int, not a Byte.

scala> arr1(4) = (arr1(4) ^ arr2(5)).toByte

scala> arr1.update(4, (arr1(4) ^ arr2(5)).toByte)

scala> implicit class cv(val as: Array[Byte]) extends AnyVal {
     | def update(i: Int, v: Int): Unit = as.update(i, v.toByte)
     | }
defined class cv

scala> arr1(4) ^= arr2(5)

Maybe it's possible to spec it in such a way that the conversions are inserted.

Anyway, the proposed fix for #9834 is slightly obscurantistical:

scala> arr1(4) ^= arr2(5)
<console>:14: error: value ^= is not a member of Byte
  Expression does not convert to assignment because:
    type mismatch;
     found   : Int
     required: Byte
       arr1(4) ^= arr2(5)
               ^

@scabug
Copy link
Author

scabug commented Dec 1, 2016

@SethTisue said:
You don't need Array to demonstrate the issue here:

scala> var b1 = 0.toByte; b1 ^= b1
<console>:13: error: type mismatch;
 found   : Int
 required: Byte
        b1 ^= b1
           ^

equivalent Java code compiles and the bytecode includes the necessary i2b instruction.

@scabug
Copy link
Author

scabug commented Dec 1, 2016

@SethTisue said:
I'm ambivalent about whether it's desirable for Scala to further paper over the lack of support for bytewise operations at the bytecode (ironic! "byte"code! hahaha!) level, especially since it would require additional language in the spec. (JLS 15.26.2 has "the result of the binary operation is converted to the type of the left-hand variable")

@scabug
Copy link
Author

scabug commented Dec 1, 2016

@som-snytt said:
The error message improvement now shows the expansion, which can be tricky to visualize.

scala> var b1 = 0.toByte ; b1 ^= b1
<console>:13: error: value ^= is not a member of Byte
  Expression does not convert to assignment because:
    type mismatch;
     found   : Int
     required: Byte
    expansion: this.b1 = this.b1.^(b1)
        b1 ^= b1
           ^

scala> val as, bs = new Array[Byte](10)
as: Array[Byte] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
bs: Array[Byte] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

scala> as(4) ^= bs(5)
<console>:14: error: value ^= is not a member of Byte
  Expression does not convert to assignment because:
    type mismatch;
     found   : Int
     required: Byte
    expansion: as.update(4, as.apply(4).^(bs(5)))
       as(4) ^= bs(5)
             ^

It would be nice to highlight the error in the expansion without adding a caret line.

@scabug scabug added this to the Backlog milestone Apr 7, 2017
@som-snytt
Copy link

Closing as duplicate of the linked ticket by which the error message was improved.

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2023
@SethTisue SethTisue removed this from the Backlog milestone Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants