Skip to content

Don't infer implicits from non-accessible companion - #25367

Merged
SolalPirelli merged 2 commits into
scala:mainfrom
lrytz:i25347
Jun 12, 2026
Merged

SolalPirelli merged 2 commits into
scala:mainfrom
lrytz:i25347

Conversation

@lrytz

@lrytz lrytz commented Feb 26, 2026

Copy link
Copy Markdown
Member

Implicits found in the companion object of a type should not be inferred if the companion object is not accessible.

Fixes #25347

How much have your relied on LLM-based tools in this contribution?

Asked it to forward-port scala/scala#11215, but its attempt to fix it was in the wrong spot. So the fix is manual, I used the unit test file it created.

How was the solution tested?

The test passes, we now get a type mismatch error instead of the non-accessible implicit being inferred.

I tried separate compilation with two scalac calls in sbt, but that did something strange, I ran into a different problem.

I tried scala3-bootstrapped/publishLocal to test it but that failed too with

[error] -- [E038] Declaration Error: /Users/luc/code/scala/scala3/library/src/scala/collection/mutable/AnyRefMap.scala:406:15
[error] 406 |  override def + [V1 >: V](elem1: (K, V1), elem2: (K, V1), elems: (K, V1)*): AnyRefMap[K, V1]^{} = {
[error]     |               ^
[error]     |    method + has a different signature than the overridden declaration
[error]     |
[error]     | longer explanation available when compiling with `-explain`

I feel a bit out of time to look further into this right now.

@lrytz

This comment was marked as resolved.

@sjrd

sjrd commented Mar 3, 2026

Copy link
Copy Markdown
Member

There are some CI failures, though.

@lrytz

lrytz commented Mar 3, 2026

Copy link
Copy Markdown
Member Author

There are some CI failures, though.

Yes, i'll fix them. Thanks for the review!

@lrytz

lrytz commented Mar 3, 2026

Copy link
Copy Markdown
Member Author

@jchyb I reverted #23126.

I believe the test case in that PR should not compile, the compiler infers the implicit argument for jam.brew to jam.brew(jam.internal.defaultJamConfig), but jam.internal is not accessible. The spec says

eligible are all identifiers x that can be accessed at the point of the method call

My PR will likely lead to compilation failures in the community build, but that should be addressed by updating the project's source code.

@jchyb jchyb left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

-- [E172] Type Error: tests/neg/i22593b/Test.scala:3:10 ------------------------
3 |  jam.brew // error
  |          ^
  |No given instance of type jam.internal.JamConfig was found for parameter config of method brew in package jam
  |
  |One of the following imports might fix the problem:
  |
  |  import jam.defaultJamConfig
  |  import jam.internal.defaultJamConfig
  |

The error there looks good now (previously without that reverted PR it was some incoherent type error).

@lrytz

lrytz commented Mar 4, 2026

Copy link
Copy Markdown
Member Author

So this should be good to go. Though perhaps we should wait for a minor, because I expect it will cause some existing code to fail to compile. @Gedochao

Can we run the community build before merge to check?

@Gedochao

Gedochao commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

Can we run the community build before merge to check?

@WojciechMazur

So this should be good to go. Though perhaps we should wait for a minor, because I expect it will cause some existing code to fail to compile. @Gedochao

Based on the community build results, I guess we could think whether this should go now in 3.8.3, or wait for 3.10.
While 3.9 is technically a new minor, I'd avoid putting in stuff we're not sure about (but maybe we'll gain confidence after the community build is run)

@WojciechMazur

WojciechMazur commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

Comparsion against 3.8.3-RC1
Comparsion against latest nightly 33 compilation errors (test errors can be ignored, nightly did not execute them)

Note: Red background - new failures, yellow background existing failures present in previous compiler version, there's a switch for filtering by failures reason, and to filter out only new failures, but vibed dashboard seems not to support combination of these filters.
Note2: RC versions execute tests so there's less of new failures visible. This PR was tested with runtime tests enabled

It seems to introduce regressions related to implicit or typer (missing implicit conversions?)

@Gedochao Gedochao added the needs-minor-release This PR cannot be merged until the next minor release label Mar 5, 2026
@Gedochao Gedochao added this to the 3.10.0 milestone Mar 5, 2026
@Gedochao

Gedochao commented Mar 5, 2026

Copy link
Copy Markdown
Contributor

Seems like we'll wait for 3.10 with this (and hopefully address the regressions in the meantime)

@lrytz

lrytz commented Mar 5, 2026

Copy link
Copy Markdown
Member Author

It seems to introduce regressions related to implicit or typer (missing implicit conversions?)

Yes, the PR changes implicit search to discard candidates found in non-accessible companions. As a small example, this used to compile, but now it fails with a type mismach error:

object t {
  class C
  private object C { implicit def c2s(c: C): String = "" }
}

val c = new t.C()
def m: String = c // found: C, required: String

(An explicit reference to the implicit def m: String = t.C.c2s(c) of course fails to compile in current Scala releases.)

Maybe it's possible to turn this into a deprecation warning in 3.8 / 3.9, that would be helpful. The problem is that the type mismatch / implicit not found errors don't make it easy to tell how the code used to compile before, which non-accessible implicit was inferred.

@lrytz

lrytz commented Mar 13, 2026

Copy link
Copy Markdown
Member Author

Deprecation first in PR #25516.

I changed this PR to revert the deprecation and change the implicit resolution.

@lrytz
lrytz force-pushed the i25347 branch 2 times, most recently from 7b12aea to 8d4868a Compare March 20, 2026 12:56
sjrd added a commit that referenced this pull request Mar 20, 2026
…5516)

Implicits found in the companion object of a type should not be inferred
if the companion object is not accessible.

Deprecation as a first step before actually changing implicit inference.

Ref #25347

The PR to change inference is #25367
@lrytz
lrytz force-pushed the i25347 branch 2 times, most recently from 06e8097 to 2b2ca70 Compare March 25, 2026 07:10
@SolalPirelli

Copy link
Copy Markdown
Contributor

This can now be merged since main is 3.10, correct? (Just double checking since there was a mention of regressions in the discussion here)

Implicits found in the companion object of a type should not
be inferred if the companion object is not accessible.
@Gedochao

Copy link
Copy Markdown
Contributor

Rebased.
If CI passes, I think we can merge.

@SolalPirelli
SolalPirelli merged commit 5321891 into scala:main Jun 12, 2026
45 checks passed
@kevin-lee kevin-lee mentioned this pull request Jun 23, 2026
8 tasks done
@WojciechMazur WojciechMazur added the release-notes Should be mentioned in the release notes label Jun 27, 2026
@WojciechMazur

Copy link
Copy Markdown
Contributor

Linking repository with 20 failures in OpenCB due to this change, minimized with LLMs: https://github.com/WojciechMazur/scala3-non-accessible-companion-25367

Also we'd need to address it as one of core changes of 3.10 - even right now it seems to have large impact on the community. At current point it seems we'd need help and informative campaign for the Scala OSS as new releases of core libraries would be required, and ideally should land before 3.10.0

To consider: maybe under -source:3.10-migration it should still be a warning to make localization of these issues easier? We'd potentially have more breaking changes in 3.10

@lrytz

lrytz commented Jul 1, 2026

Copy link
Copy Markdown
Member Author

Yeah, you're right. If someone wants to upgrade to 3.10 but they use a library that has such a "broken" API, they are blocked. apache/pekko-http#1056

SolalPirelli pushed a commit that referenced this pull request Sep 28, 2026
…rox from FunProtoTyped (#25662)

Reproduction case for #24824 as issue was solved by
#25367 (pass caller context -
details:
#24824 (comment)).

~~Address #24824: preserve `FunProtoTyped` in `wildApprox` and
`FunProtoTyped.map` to avoid assert from `collectParts` that leading to
a crash~~

## How much have you relied on LLM-based tools in this contribution?

moderately: I feed LLM with relevant issues, PRs; reiterated over
proposed solutions,
~then switched to work done by @Alex1005a as he had more mature
solution~

## How was the solution tested?

Tested locally added reproduction case:

```shell
sbt --client "testCompilation 24824"
```

---------

Signed-off-by: Piotr Paradzinski <dancingwithopenheart@gmail.com>
Co-authored-by: alex <asafofalexander@gmail.com>
alejandrohdezma pushed a commit to permutive-engineering/prometheus4cats that referenced this pull request Oct 15, 2026
Scala 3.10 no longer infers implicits from companions that are not
accessible from the call site (scala/scala3#25367). InitLast's companion
was private[prometheus4cats], so every chained .label[...] call in user
code failed with 'No given instance of type InitLast.Aux[...]'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-minor-release This PR cannot be merged until the next minor release release-notes Should be mentioned in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler crash when implicit infers a path to a private object

6 participants