Skip to content

fix: resolve LogSource for getClass results on Scala 3 - #3609

Open
pjfanning wants to merge 1 commit into
apache:mainfrom
pjfanning:fix-logsource-scala3-1538
Open

pjfanning wants to merge 1 commit into
apache:mainfrom
pjfanning:fix-logsource-scala3-1538

Conversation

@pjfanning

Copy link
Copy Markdown
Member

Motivation

On Scala 3, Logging(system, getClass) fails to compile with Cannot find LogSource for Class[? <: Foo]. getClass returns a wildcard class type, which Scala 3 does not match against LogSource.fromAnyClass[T]: LogSource[Class[T]]. Scala 2.13 accepts the same code.

Modification

  • object LogSource now extends a new LogSourceCompat trait.
  • The Scala 3 LogSourceCompat adds a lower priority fromAnySubClass[T]: LogSource[Class[? <: T]] implicit. The Scala 2.13 version is empty, because Scala 2 reports that implicit as ambiguous with fromAnyClass.
  • fromAnyClass is unchanged. Changing its signature to LogSource[Class[? <: T]] also works, but MiMa reports an IncompatibleSignatureProblem.
  • Added LogSourceSpec.

Result

Logging(system, getClass) and LogSource(getClass, system) compile on Scala 3.

Tests

  • actor-tests/Test/testOnly org.apache.pekko.event.LogSourceSpec on Scala 3.3.8 without the fix: compile error Cannot find LogSource for Class[? <: ...LogsWithGetClass]
  • actor-tests/Test/testOnly org.apache.pekko.event.LogSourceSpec org.apache.pekko.event.LoggerSpec with the fix: passed on 2.13.18 and 3.3.8
  • actor/mimaReportBinaryIssues: passed on 2.13.18 and 3.3.8
  • scalafmt run on changed files

References

Fixes #1538

Motivation:
On Scala 3, `Logging(system, getClass)` fails to compile with
"Cannot find LogSource for Class[? <: Foo]". `getClass` returns a
wildcard class type, which Scala 3 does not match against
`LogSource.fromAnyClass[T]: LogSource[Class[T]]`. Scala 2.13 accepts it.

Modification:
- Make `object LogSource` extend a new `LogSourceCompat` trait.
- The Scala 3 `LogSourceCompat` adds a lower priority
  `fromAnySubClass[T]: LogSource[Class[? <: T]]` implicit; the Scala 2.13
  version is empty because Scala 2 reports that implicit as ambiguous
  with `fromAnyClass`.
- `fromAnyClass` is unchanged; changing its signature instead fails MiMa
  with an IncompatibleSignatureProblem.
- Add `LogSourceSpec`.

Result:
`Logging(system, getClass)` and `LogSource(getClass, system)` compile
on Scala 3.

Tests:
- `actor-tests/Test/testOnly org.apache.pekko.event.LogSourceSpec` on
  Scala 3.3.8 without the fix: compile error "Cannot find LogSource for
  Class[? <: ...LogsWithGetClass]"
- `actor-tests/Test/testOnly org.apache.pekko.event.LogSourceSpec
  org.apache.pekko.event.LoggerSpec` with the fix: passed on 2.13.18
  and 3.3.8
- `actor/mimaReportBinaryIssues`: passed on 2.13.18 and 3.3.8
- scalafmt run on changed files

References:
Fixes apache#1538
@pjfanning pjfanning added this to the 2.0.0-M5 milestone Oct 9, 2026
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.

LoggingAdapter with ActorSystem and class broken in scala 3

1 participant