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 specifity comparison for extensions in polymorphic givens #13509

Merged
merged 3 commits into from Sep 16, 2021

Conversation

smarter
Copy link
Member

@smarter smarter commented Sep 11, 2021

When we compare polymorphic methods for specificity, we replace their
type parameters by type variables constrained in the current
context (see isAsSpecific), but for extension methods in polymorphic
givens, the comparison was done with a constraint set that does not
include the type parameters of the givens, this lead to ambiguity errors
as experienced in
typelevel/spotted-leopards#2.

We fix this by ensuring the TyperState we use for the comparison
contains any additional constraint specific to the TyperState of either
alternative. This required generalizing TyperState#mergeConstraintWith
to handle this not being committable (because in that case this does
not need to take ownership of the type variables owned by that,
therefore that itself is allowed to be committable).

- Add Context#withTyperState
- Add a committable parameter to TyperState#fresh
Previously, the code in the closure passed to `comparing` could have
been using the wrong context. I don't think this happened in practice so
far because comparingCtx was always equal to ctx but it will matter in the
next commit.
When we compare polymorphic methods for specificity, we replace their
type parameters by type variables constrained in the current
context (see isAsSpecific), but for extension methods in polymorphic
givens, the comparison was done with a constraint set that does not
include the type parameters of the givens, this lead to ambiguity errors
as experienced in
typelevel/spotted-leopards#2.

We fix this by ensuring the TyperState we use for the comparison
contains any additional constraint specific to the TyperState of either
alternative. This required generalizing TyperState#mergeConstraintWith
to handle `this` not being committable (because in that case `this` does
not need to take ownership of the type variables owned by `that`,
therefore `that` itself is allowed to be committable).
Copy link
Member

@dwijnand dwijnand left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me

@odersky
Copy link
Contributor

odersky commented Sep 16, 2021

test performance please

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.

LGTM, with just a question about performance implications.

// alternatives correctly we need a TyperState that includes
// constraints from both sides, see
// tests/*/extension-specificity2.scala for test cases.
val constraintsIn1 = alt1.tstate.constraint ne ctx.typerState.constraint
Copy link
Contributor

Choose a reason for hiding this comment

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

Is constraintsIn1/2 expected to be rarely true? So, no performance implications?

Copy link
Member Author

@smarter smarter Sep 16, 2021

Choose a reason for hiding this comment

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

I'd expect this codepath to be rarely hit since it's already in a fallback path and will only concern polymorphic givens with matching extension methods.

@dottybot
Copy link
Member

performance test scheduled: 1 job(s) in queue, 0 running.

@dottybot
Copy link
Member

Performance test finished successfully:

Visit https://dotty-bench.epfl.ch/13509/ to see the changes.

Benchmarks is based on merging with master (8d9542c)

@smarter smarter merged commit dd6da26 into scala:master Sep 16, 2021
@smarter smarter deleted the fix-spotted-leopards branch September 16, 2021 18:40
@Kordyjan Kordyjan added this to the 3.1.1 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.

None yet

5 participants