Skip to content

TypeAssigner: fix return type of clone() for arrays #501

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

Merged
merged 1 commit into from
Apr 28, 2015

Conversation

smarter
Copy link
Member

@smarter smarter commented Apr 25, 2015

Given the following code:

  val x: Array[String] = new Array[String](1)
  x(0) = "foo"
  println(x.clone().apply(0))

Before this commit, the last line was rewritten in Erasure to:

  println(x.clone().[]apply(0))

This is incorrect: clone() returns an Object, so a cast is necessary,
this resulted in the following failure at runtime:

  java.lang.VerifyError: Bad type on operand stack in aaload

After this commit, the last line is rewritten in Erasure to:

  println(x.clone().asInstanceOf[String[]].[]apply(0))

This corresponds to adding a "checkcast" instruction in the generated
bytecode, and is enough to fix the runtime error.

Review by @odersky .

@DarkDimius
Copy link
Contributor

I would add a comment saying why clone isn't handled here:
Java language spec says that Int[].clone return type is Int[]. But JVM spec does not. On the bytecode level, it returns Object so java compiler also inserts a cast.

@DarkDimius
Copy link
Contributor

Otherwise LGTM

@smarter
Copy link
Member Author

smarter commented Apr 27, 2015

I added a comment in the code, feel free to merge if you think it's OK.

@DarkDimius
Copy link
Contributor

Non-ansi chars present in comment: ŝ

Given the following code:
  val x: Array[String] = new Array[String](1)
  x(0) = "foo"
  println(x.clone().apply(0))

Before this commit, the last line was rewritten in Erasure to:
  println(x.clone().[]apply(0))
This is incorrect: clone() returns an Object, so a cast is necessary,
this resulted in the following failure at runtime:
  java.lang.VerifyError: Bad type on operand stack in aaload

After this commit, the last line is rewritten in Erasure to:
  println(x.clone().asInstanceOf[String[]].[]apply(0))
This corresponds to adding a "checkcast" instruction in the generated
bytecode, and is enough to fix the runtime error.
@smarter
Copy link
Member Author

smarter commented Apr 27, 2015

Whoops, fixed

DarkDimius added a commit that referenced this pull request Apr 28, 2015
TypeAssigner: fix return type of clone() for arrays
@DarkDimius DarkDimius merged commit d55b719 into scala:master Apr 28, 2015
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

Successfully merging this pull request may close these issues.

2 participants