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

Fix reachability by retaining spaces of Prod params #14118

Merged
merged 1 commit into from Dec 19, 2021

Conversation

dwijnand
Copy link
Member

When intersecting a product type space with a type space, if the two
types aren't related, we must retain the spaces of the parameters,
otherwise the intersection will represent a bigger space of values that
it should (leading to a false positive unreachability warning).

I actually think that the first condition, isSubType(tp1, tp2), should
be true, because Bar is a subtype of Foo[X], provided X is
instantiated to Nothing. Maybe that's not isSubType but something
else (because we want forSome X, not forAll X). But, either way, fixing
that the intersection doesn't overpromise (i.e. lie) is a good fix
anyways.

When intersecting a product type space with a type space, if the two
types aren't related, we must retain the spaces of the parameters,
otherwise the intersection will represent a bigger space of values that
it should (leading to a false positive unreachability warning).

I actually think that the first condition, `isSubType(tp1, tp2)`, should
be true, because `Bar` is a subtype of `Foo[X]`, provided `X` is
instantiated to `Nothing`.  Maybe that's not isSubType but something
else (because we want forSome X, not forAll X).  But, either way, fixing
that the intersection doesn't overpromise (i.e. lie) is a good fix
anyways.
@dwijnand dwijnand linked an issue Dec 15, 2021 that may be closed by this pull request
@dwijnand dwijnand marked this pull request as ready for review December 16, 2021 09:05
@dwijnand
Copy link
Member Author

This is what the log looks like for the part I mean. I've fixed this now by making the intersection return Prod(Bar & Foo[X], [1 (Typ((1 : Int)))]). But ideally I would want to somehow make Bar <:< Foo[X] return true. To do that I think I need to replace the instantiated X for a fresh variable, allowing it to become instantiated to Nothing. @odersky do you know how to do that mapping?

[log elimOpaque]   ==> Bar(1) & _: Foo[X]?
[log elimOpaque]     ==> Bar <:< Foo[X]?
[log elimOpaque]       ==> isSubType Bar <:< Foo[X]?
[log elimOpaque]         ==> isSubType Foo[Nothing] <:< Foo[X] (left is approximated)?
[log elimOpaque]           ==> isSubType X <:< Nothing?
[log elimOpaque]             ==> isSubType Any <:< Nothing (left is approximated)?
[log elimOpaque]             <== isSubType Any <:< Nothing (left is approximated) = false
[log elimOpaque]           <== isSubType X <:< Nothing = false
[log elimOpaque]         <== isSubType Foo[Nothing] <:< Foo[X] (left is approximated) = false
[log elimOpaque]       <== isSubType Bar <:< Foo[X] = false
[log elimOpaque]     <== Bar <:< Foo[X] = false
[log elimOpaque]     ==> Foo[X] <:< Bar?
[log elimOpaque]       ==> isSubType Foo[X] <:< Bar?
[log elimOpaque]       <== isSubType Foo[X] <:< Bar = false
[log elimOpaque]     <== Foo[X] <:< Bar = false
[log elimOpaque]   <== Bar(1) & _: Foo[X] = Bar(1) (Prod(Bar & Foo[X], [1 (Typ((1 : Int)))])

@dwijnand
Copy link
Member Author

This fixes the issue, so it's mergeable as it is, but I'm asking as I would like to see if I can improve it further.

@odersky
Copy link
Contributor

odersky commented Dec 19, 2021

Creating new type variables looks like GADT territory. Maybe @abgruszecki can comment. For the space logic it looks OK to me, but I am no expert. @liufengyun could you take a quick look?

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

I am not an expert, so I can't comment on the logic. It looks reasonable to me and I found no other problems.

Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the fix @dwijnand 👍

if (!isSameUnapply(fun1, fun2)) intersectUnrelatedAtomicTypes(tp1, tp2)
if (!isSameUnapply(fun1, fun2)) intersectUnrelatedAtomicTypes(tp1, tp2) match
case Typ(tp, _) => Prod(tp, fun1, ss1)
case sp => sp
Copy link
Contributor

Choose a reason for hiding this comment

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

Aside: We could experiment by introducing a space called Unknown(tp: Type) for this case later.

Edited: Sorry, I meant the case above (the 1st case).

Copy link
Member Author

Choose a reason for hiding this comment

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

The case is only for Empty, when the two types are provably disjoint, so I'm not sure what we'd want to preserve the type for. But I'm not against the idea.

@dwijnand dwijnand merged commit 0f6df73 into scala:master Dec 19, 2021
@dwijnand dwijnand deleted the contrareach branch December 19, 2021 16:47
@dwijnand
Copy link
Member Author

Btw, I sort of introduced this regression when I made the change in 202b63e without realising I was forgetting the subpatterns of the product spaces (the components/params).

@Kordyjan Kordyjan added this to the 3.1.2 milestone Aug 2, 2023
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.

Spurious "Unreachable case" warning
4 participants