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

Avoid erasure/preErasure issues around Any in transformIsInstanceOf #21647

Merged
merged 3 commits into from
Nov 2, 2024

Conversation

dwijnand
Copy link
Member

The testType Any is erased to Object, but the expr type Int isn't erased
to Integer, so then it fails as Int !<: Object. We avoid the problem by
feeding in AnyVal, leading to a (possibly elided) non-null test only.

Fixes #21544

The testType Any is erased to Object, but the expr type Int isn't erased
to Integer, so then it fails as Int !<: Object.  We avoid the problem by
feeding in AnyVal, leading to a (possibly elided) non-null test only.
@dwijnand dwijnand marked this pull request as ready for review September 25, 2024 22:27
@dwijnand dwijnand requested a review from sjrd September 25, 2024 22:27
Comment on lines 362 to 363
if testType.isAny && expr.tpe.isPrimitiveValueType then
defn.AnyValType
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that going to trigger with the same issue with any other type that erases to Object? Like ... AnyVal itself?

There's supposed to already be logic in transformIsInstanceOf to avoid a type test when expr.tpe <:< testType:

if (expr.tpe <:< testType) && inMatch then
if expr.tpe.isNotNull then constant(expr, Literal(Constant(true)))
else expr.testNotNull

The problem is that it tests on erased types, instead of the original types. Addressing that at the root should be more robust, shouldn't it?

@dwijnand dwijnand requested a review from sjrd October 31, 2024 18:44
class Test():
def m1(xs: List[Boolean]) = for (x: Any) <- xs yield x
def m2(xs: List[Boolean]) = for (x: AnyVal) <- xs yield x
def m3(xs: List[Boolean]) = for (x: Matchable) <- xs yield x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few more suggestions for tests:

  def m1(xs: List[AnyVal]) = for (x: Any) <- xs yield x
  def m1(xs: List[AnyVal]) = for (x: Matchable) <- xs yield x
  def m1(xs: List[Matchable]) = for (x: Any) <- xs yield x
  def m1(xs: List[Any]) = for (x: Any) <- xs yield x

@dwijnand dwijnand merged commit 01288d2 into scala:main Nov 2, 2024
29 checks passed
@dwijnand dwijnand deleted the 21544-forcomp-type-cast branch November 2, 2024 19:50
@WojciechMazur WojciechMazur added this to the 3.6.3 milestone Dec 9, 2024
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.

For comprehension bug when using type casting
3 participants