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 overcompilation due to unstable context bound desugaring #18280

Merged
merged 2 commits into from
Jul 25, 2023

Conversation

smarter
Copy link
Member

@smarter smarter commented Jul 24, 2023

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.

@@ -0,0 +1,9 @@
> compile
Copy link
Member

Choose a reason for hiding this comment

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

may be worth noting here the predicted order of typer visiting definitions in run 1 vs run 2, to illustrate how before the change it actually had unstable names between runs.

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.
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 scala#7661.

Fixes scala#18080.
@smarter smarter enabled auto-merge July 25, 2023 13:21
@smarter smarter merged commit 1dc9761 into scala:main Jul 25, 2023
15 checks passed
@smarter smarter deleted the stable-ctx-bounds branch July 25, 2023 16:49
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Kordyjan added a commit that referenced this pull request Dec 8, 2023
…g" to LTS (#19132)

Backports #18280 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
@Kordyjan Kordyjan modified the milestones: 3.4.0, 3.3.2 Dec 14, 2023
@mkurz
Copy link
Contributor

mkurz commented Feb 27, 2024

This change is causing compatibility issues when running a Play project with Scala 3.3.1 with Play artifacts build with Scala 3.3.2:

@smarter Can you take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants