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

Backport "Update unreducible match types error reporting" to LTS #21024

Closed
wants to merge 7 commits into from

Conversation

WojciechMazur
Copy link
Contributor

Backports #19954 to the LTS branch.

PR submitted by the release tooling.
[skip ci]

EugeneFlesselle and others added 7 commits July 4, 2024 17:51
This reverts commit 9ae1598

Note that the changes in Typer:
```
val unsimplifiedType = result.tpe
simplify(result, pt, locked)
result.tpe.stripTypeVar match
  case e: ErrorType if !unsimplifiedType.isErroneous =>
    errorTree(xtree, e.msg, xtree.srcPos)
  case _ => result
```
cannot be reverted yet since the MatchReducer now also reduces to an `ErrorType` for MatchTypeLegacyPatterns, introduced after 9ae1598.

[Cherry-picked 7460ab3][modified]
i18488.scala was only passing because of the bug in the MatchReducer,
as we can see in the subtyping trace:
```
==> isSubType TableQuery[BaseCrudRepository.this.EntityTable] <:< Query[BaseCrudRepository.this.EntityTable, E[Option]]?
  ==> isSubType Query[BaseCrudRepository.this.EntityTable, Extract[BaseCrudRepository.this.EntityTable]] <:<
                Query[BaseCrudRepository.this.EntityTable, E[Option]] (left is approximated)?
    ==> isSubType E[Option] <:< Extract[BaseCrudRepository.this.EntityTable]?
      ==> isSubType [T[_$1]] =>> Any <:< Extract?
        ==> isSubType Any <:< Extract[T]?
          ==> isSubType Any <:< T match { case AbstractTable[t] => t } <: t (right is approximated)?
            ==> isSubType Any <:< <error Match type reduction failed since selector T
                                   matches none of the cases
                                   case AbstractTable[t] => t> (right is approximated)?
            <== isSubType Any <:< <error Match type reduction failed since selector T
                                   matches none of the cases
                                   case AbstractTable[t] => t> (right is approximated) = true
          <== isSubType Any <:< T match { case AbstractTable[t] => t } <: t (right is approximated) = true
        <== isSubType Any <:< Extract[T] = true
      <== isSubType [T[_$1]] =>> Any <:< Extract = true
      ...
    <== isSubType Extract[BaseCrudRepository.this.EntityTable] <:< E[Option] = true
  <== isSubType Query[BaseCrudRepository.this.EntityTable, Extract[BaseCrudRepository.this.EntityTable]] <:<
                Query[BaseCrudRepository.this.EntityTable, E[Option]] (left is approximated) = true
<== isSubType TableQuery[BaseCrudRepository.this.EntityTable] <:< Query[BaseCrudRepository.this.EntityTable, E[Option]] = true
```

[Cherry-picked 5becaac]
Modify the MatchReducer to return NoType in the case of no matches, rather than throwing a MatchTypeReductionError.
This makes it consistent with the other match type reduction failures, where being stuck does not result in an error, but simply in an unreduced match type.

We still get the explanations of the underlying error in the MatchTypeTrace, but in positions which need the reduction for conformance, rather than at application site of the match type.

[Cherry-picked d7946bf][modified]
The diff in neg/10349.scala is quite interesting.
With a few intermediate values:
```scala
type First[X] = X match
 case Map[_, v] => First[Option[v]]

def first[X](x: X): First[X] = x match
 case x: Map[k, v] =>
 val hdOpt: Option[v] = x.values.headOption
 first(hdOpt): First[Option[v]] // error only before changes
```
This now type-checks but will fail at runtime because of the in-exhaustivity of the match expression.
Perhaps we should add some additional condition in `isMatchTypeShaped` to account for this, or at least emit a warning ?

[Cherry-picked 61c2832]
`recurseWith` can be called with the same scrutinee (even if match type reduction is cached) if it is an applied match alias

For example, `Tuple.Head[Tuple.Tail[T]]` will attempt to reduce `Tuple.Tail[T]` twice:
- once as an argument of the match alias `Head`, and
- once as a scrutinee in body of `Head` (after the substitution).

[Cherry-picked 2329236][modified]
If a match type pattern is an opaque type, we use its bounds when checking the validity of the pattern.
Following the ElimOpaque phase however, the pattern is beta-reduced (as normal applied type aliases), which may result in an illegal pattern.

[Cherry-picked 2beb67e][modified]
@WojciechMazur
Copy link
Contributor Author

Can't be backported to the LTS - courses regression in multiple compilation tests, probably due to missing dependant PRs backports

@WojciechMazur WojciechMazur deleted the lts-19954 branch July 4, 2024 16:50
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