Skip to content

Pattern-bound types with bad bounds lead to unsoundness #15578

Closed
@abgruszecki

Description

@abgruszecki
trait Foo[X >: Any <: Nothing]

@main def Test = (None: Option[Foo[?]]) match {
  case _: Option[Foo[t]] =>
    val unsound: Nothing = (5 : Any) : t
    (unsound : Unit => Unit).apply(())
}

The problem seems to be that pattern matching lets us name a type argument which does not correspond to a type member with known good bounds. In DOT, we would not be able to refer to t, so the issue would not show up.

Fixes we have thought of so far:

  1. Disallowing classes with bad bounds on type parameters
  2. Disallowing pat-mat from naming types with bad bounds / only letting pat-mat name types with bounds that are known to be good
  3. Disallowing pat-mat from naming type arguments which are not top-level

See also the discussion in #15571 and related PR #15577.

Metadata

Metadata

Assignees

Labels

area:pattern-matchingitype:soundnessSoundness bug (it lets us compile code that crashes at runtime with a ClassCastException)

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions