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

Approximate MatchTypes with lub of case bodies, if non-recursive #19761

Merged
merged 7 commits into from
Mar 4, 2024

Conversation

dwijnand
Copy link
Member

Fixes #19710

@dwijnand
Copy link
Member Author

@mbovel @EugeneFlesselle @sjrd cc @nicolasstucki Do you remember why we don't approximate match types with the lub of their bodies? I thought I had done this experiment before, before Seb's reimplementation, proving how it's wrong. Doing the experiment now, it seems to work. So, is this wrong? I.e. unsound?

@nicolasstucki
Copy link
Contributor

I do not remember.

@sjrd
Copy link
Member

sjrd commented Feb 23, 2024

At the very least, it does not correspond to anything that was ever specified or documented. Doing that would require a dedicated rule in the type system.

Off the top of my head, I don't see why it would be unsound. But that's also assuming that type bounds are correctly checked in the bodies, which IIUC is not done yet?

@dwijnand
Copy link
Member Author

But that's also assuming that type bounds are correctly checked in the bodies, which IIUC is not done yet?

I believe so:

https://github.com/lampepfl/dotty/blob/98efdab3c3edaac379c07b979f9ff103bf13cd07/compiler/src/dotty/tools/dotc/typer/Typer.scala#L2378-L2379

@EugeneFlesselle
Copy link
Contributor

@dwijnand Some issues had been discussed in #8085, perhaps filtering for recursive match-types is enough to avoid then though.

@dwijnand
Copy link
Member Author

I like the approach of caching the lub once during typing, much smarter. I'll try that change.

@EugeneFlesselle
Copy link
Contributor

EugeneFlesselle commented Feb 23, 2024

I like the approach of caching the lub once during typing, much smarter. I'll try that change.

I'm wondering however if doing the check during subtyping might make use of a narrowed context ?
Or more generally, when it could be useful to recompute the bound. For example if a match type is inherited and the scrutinee has been refined.

In any case, I think this would be a great improvement.

@@ -2863,6 +2863,8 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
disjointnessBoundary(tp.effectiveBounds.hi)
case tp: ErrorType =>
defn.AnyType
case NoType => // ParamRef#superTypeNormalized can return NoType
Copy link
Member

Choose a reason for hiding this comment

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

That seems to be a problem with ParamRef#superTypeNormalized rather than with disjointnessBoundary.

Copy link
Member Author

Choose a reason for hiding this comment

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

What should superTypeNormalized do?

Copy link
Member

Choose a reason for hiding this comment

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

It should return the declared type of the corresponding param, which is supposed to be its underlying. I don't see a scenario in which superTypeNormalized should ever return NoType, except possibly for AnyKind itself.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

That sort of explains it, although it's still concerning that we're attempting to read it from provablyDisjoint when it hasn't been initialized yet. Looks like a time bomb waiting to explode later due to weird compilation order issues.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, and this is not the only case, I ran into a similar issue where normalisation relies on the underlying type, which is not always set as expected. For a TypeRef, it goes through the denotation info, of which the completion seemed a bit unpredictable to me (heavily influenced by small changes in where things happen to be reduced).

Also (maybe) unrelated the normalization depends on the gadt ctx which doesn't seem to be reliably populated when using NamedTypes.

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess I should've opted for AnyKind rather than Any, as the more conservative approach.

Copy link
Member

Choose a reason for hiding this comment

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

I would still be less uncomfortable if we specifically handled ParamRef to deal with its superTypeNormalized returning NoType; instead of having this "catch-all" NoType. That would limit the blast radius of this workaround. We should also clearly comment that it is a workaround.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure thing.

@dwijnand dwijnand enabled auto-merge March 4, 2024 14:51
@dwijnand dwijnand merged commit de6a090 into scala:main Mar 4, 2024
15 checks passed
@dwijnand dwijnand deleted the mt/lub-rhss branch March 5, 2024 09:12
@Kordyjan Kordyjan added this to the 3.4.2 milestone Mar 28, 2024
WojciechMazur added a commit that referenced this pull request Jul 3, 2024
…rsive" to LTS (#20972)

Backports #19761 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
sjrd added a commit to sjrd/tasty-query that referenced this pull request Jul 24, 2024
WojciechMazur added a commit that referenced this pull request Jul 29, 2024
…ive" in 3.4.3 (#21268)

Reverts #19761 in 3.4.3-RC1 as discussed in #21258 and accepted by the
core compiler team.
WojciechMazur added a commit that referenced this pull request Jul 29, 2024
…ive" in 3.5.0 (#21266)

Reverts #19761 in 3.5.0-RC6 as discussed in #21258 and accepted by the
core compiler team.
WojciechMazur added a commit that referenced this pull request Jul 29, 2024
…ive" in 3.5.1 (#21267)

Reverts #19761 in 3.5.1-RC2 as discussed in #21258 and accepted by the
core compiler team.
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.

Regression in match types combined with type lambda
5 participants