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 #21868, #21869, and #21870: handle CapsOf in more places #21875

Merged
merged 5 commits into from
Nov 21, 2024

Conversation

noti0na1
Copy link
Member

@noti0na1 noti0na1 commented Nov 1, 2024

Fix #21868, #21869, and #21870

@@ -198,7 +198,7 @@ extension (tp: Type)
|| tp.isRootCapability
) && !tp.symbol.isOneOf(UnstableValueFlags)
case tp: TypeRef =>
tp.symbol.isAbstractOrParamType && tp.derivesFrom(defn.Caps_CapSet)
Copy link
Member Author

Choose a reason for hiding this comment

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

@odersky Do you remember why we require the arguments of CapsOf to be abstract before? I think any type derived from CapSet should work.

@noti0na1 noti0na1 changed the title Fix #21869: Handle CapsOf in more places Fix #21869 and #21870: handle CapsOf in more places Nov 1, 2024
@noti0na1 noti0na1 self-assigned this Nov 4, 2024
@noti0na1 noti0na1 requested a review from bracevac November 19, 2024 17:05
@noti0na1 noti0na1 marked this pull request as ready for review November 19, 2024 17:05
@bracevac
Copy link
Contributor

bracevac commented Nov 20, 2024

Strange, we're consistently getting a CI failure Errors found on incorrect row numbers when compiling tests/vulpix-tests/unit/negNoPositionAnnots.scala. That does not look related at all to the changes.
Edit: it's actually the new test case test/neg-custom-args/captures/i21868.scala. The log output is difficult to read.

@bracevac bracevac changed the title Fix #21869 and #21870: handle CapsOf in more places Fix #21868, #21869, and #21870: handle CapsOf in more places Nov 20, 2024
If we do not strip the capture set at this point,
then the type will be transformed twice and
we'll get the same error message twice.
def boom(): Unit^{C^} // error

trait Abstract:
type C <: CapSet^
Copy link
Member Author

Choose a reason for hiding this comment

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

Idealy, we'd like to retrict the capture parameter and member bounded by >: CapSet^{} <: CapSet^, which can forbid instantiating to meaningless types, like Nothing.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's certainly cleaner. The question is how far we want to take it for now. A fully general solution would also have to deal with the presence of explicit lower bounds, which could be themselves type variables, etc.

trait Abstract[L >: CapSet^{}]:
    type T >: L <: CapSet^

Maybe let's track this in a separate issue? The point of the current fix is to prevent the uncaught exception and compiler crash.

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 don't think this is an issue, as long as L is also bounded by CapSet.

Copy link
Contributor

Choose a reason for hiding this comment

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

As the below examples show, there's a still a few quirks. I'm also somewhat uneasy with automatically presuming that the lower bound of a CapSet^-bounded type variable is CapSet^{}. E.g., should this always be the case, or only with cc enabled?

Since the specific issues are resolved, let's merge this and have a separate issue to make capture set intervals and subtyping airtight.

@bracevac
Copy link
Contributor

At some point, we should probably make intersection and unions work as well:

class Concrete2 extends Abstract:
    type C = CapSet^{} & CapSet^{}
    def boom() = () // error

class Concrete3 extends Abstract:
    type C = CapSet^{} | CapSet^{}
    def boom() = () // error

It's not unreasonable to expect CapSet^c1 | CapSet^c2 <:> CapSet^(c1 | c2) and CapSet^c1 & CapSet^c2 <:> CapSet^(c1 & c2).

@bracevac
Copy link
Contributor

bracevac commented Nov 21, 2024

OK, so this is weird:

import language.experimental.captureChecking
import caps.*

trait Abstract[X >: Nothing]: // Concrete2 and 3 fail with this lower bound, but work with X >: CapSet^{}
    type C >: X <: CapSet^
    def boom(): Unit^{C^}

class Concrete extends Abstract[CapSet^{}]:
    type C = CapSet^{}
    def boom() = () // ok

class Concrete2 extends Abstract[CapSet^{}]:
    type C = CapSet^{} & CapSet^{}
    def boom() = () // error

class Concrete3 extends Abstract[CapSet^{}]:
    type C = CapSet^{} | CapSet^{}
    def boom() = () // error

We get (now properly reported as an error) "Illegal capture reference ..." as long as X has lower bound Nothing. It will magically work if it's lower-bounded by CapSet^{}.

Copy link
Contributor

@bracevac bracevac left a comment

Choose a reason for hiding this comment

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

LGTM!

@bracevac bracevac enabled auto-merge November 21, 2024 15:24
@bracevac bracevac added cc-experiment Intended to be merged with cc-experiment branch on origin area:experimental:cc Capture checking related labels Nov 21, 2024
@bracevac bracevac merged commit 5d1d274 into scala:main Nov 21, 2024
27 checks passed
@bracevac bracevac deleted the fix-21870 branch November 21, 2024 15:43
@WojciechMazur WojciechMazur added this to the 3.6.3 milestone Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:experimental:cc Capture checking related cc-experiment Intended to be merged with cc-experiment branch on origin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Instantiating abstract types bounded by CapSet with Nothing crashes the capture checker
3 participants