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

Unsound reduction of match type where the scrutinee's prefix is a param ref #20518

Open
smarter opened this issue Jun 3, 2024 · 3 comments
Open
Assignees
Labels
area:match-types itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)

Comments

@smarter
Copy link
Member

smarter commented Jun 3, 2024

Compiler version

3.5.1-RC1-bin-20240602-c6fbe6f-NIGHTLY

Minimized code

This is a variation of @EugeneFlesselle's #19746:

trait V:
  type X = this.type match
    case W[x] => x

class W[+Y] extends V

trait Outer[+T]:
  val t: T

object Test:
  extension (self: Any) def as[T]: T =
    def asX(o: Outer[W[Any]]): o.t.X /* <- reduces to Any */ = self
    asX(new Outer[W[T]] { val t = W[T]() }) /* <- reduces to T */

  @main def run =
    0.as[Boolean] // ClassCastException

The fix for #19746 in 0a3497b should be generalized to check is(Param) in prefixes too (and maybe that should also apply to TypeRef and not just TermRef).

@smarter smarter added itype:bug area:match-types itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) labels Jun 3, 2024
@EugeneFlesselle
Copy link
Contributor

Related to #20515

@smarter
Copy link
Member Author

smarter commented Jun 3, 2024

Yes indeed, #20515 is likely to require a more general fix.

@EugeneFlesselle
Copy link
Contributor

EugeneFlesselle commented Jun 3, 2024

This is mostly speculative but the problem seems to me to be assuming
(t: W[Any]).X =:= (t: W[Any]).type match {...} =:= W[Any] match {...}
when the second equality should really just be a subtyping relationship (IIRC the substitution happens before beginning match type reduction).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:match-types itype:bug itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException)
Projects
None yet
Development

No branches or pull requests

3 participants