Skip to content

Backport "Fix overcompilation due to unstable context bound desugaring" to LTS #19132

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

Merged
merged 2 commits into from
Dec 8, 2023

Conversation

Kordyjan
Copy link
Contributor

@Kordyjan Kordyjan commented Dec 1, 2023

Backports #18280 to the LTS branch.

PR submitted by the release tooling.
[skip ci]

There are various places in the compiler and assorted tools where we assume
that an EvidenceParamName is the name of a context bound, but in practice we
also used the same NameKind in other cases such as for inferred contextual
functions. This commit cleans things up by replacing EvidenceParamName by:
- ContextBoundParamName
- ContextFunctionParamName
- CanThrowEvidenceParamName
- and the existing WildcardParamName

Note that Scala 2 also uses "evidence$" prefixes to represent context bounds,
this is why some pretty-printing code that aims to resugar context bounds
coming from both Scala 2 and 3 does a syntactic check for
`ContextBoundParamName.separator` instead of a semantic check on the NameKind
itself, this could perhaps be handled in a nicer way using unmangle in the
Scala2Unpickler.

[Cherry-picked 77be114]
Context bounds are desugared into term parameters `evidence$N` and before this
commit, the `N` was chosen to be unique in the current compilation unit. This
isn't great because it means that adding a new definition with a context bound
in the middle of a file would change the desugaring of subsequent definitions
in the same file.

Even worse, when using incremental compilation we could end up with the same
context bound desugared with a different value of `N` on different compilation
runs because the order in which a compilation unit is traversed during Typer is
not fixed but depends on the how the units that are jointly compiled depend on
each other (as demonstrated by the `stable-ctx-bounds` test). This issue
affects all fresh names generated during Typer, but it is especially
problematic for context bounds because they're part of the API and renaming
a method parameter forces the recompilation of all files calling that method.

To fix this, we now only require context bounds parameters to have unique names
among all the parameters of the method. This matches how we already desugar
`def foo(using A, B)` into `def foo(using x$1: A, x$2: B)` regardless of the
context.

Note that fresh names used in other situations are still problematic for
deterministic compilation. Most of the time they're not part of the API checked
by Zinc, but they can still lead to overcompilation if they appear in an
`inline def` since the entire body of the `inline def` constitutes its API. In
the future, we should follow Scala 2's lead and only require names to be fresh
at the method level: scala/scala#6300 (The Scala 2
logic is slightly more complex to handle macros, but I don't think that applies
to Scala 3 macros), see #7661.

Fixes #18080.

[Cherry-picked f322b7b]
Base automatically changed from lts-18284 to release-3.3.2 December 8, 2023 15:21
@Kordyjan
Copy link
Contributor Author

Kordyjan commented Dec 8, 2023

No regressions detected in the community build up to lts-16941.

Reference

@Kordyjan Kordyjan merged commit b3886c3 into release-3.3.2 Dec 8, 2023
@Kordyjan Kordyjan deleted the lts-18280 branch December 8, 2023 15:21
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