optics-dev / optics-dev/Monocle

[Focus] Compilation fails in Scala 3 in nested case class/object

Open
#1,293 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.7k
Forks
207
Avg merge
7h 59m
Merged PRs (30d)
5

Description

With Monocle 3.1.0,

class OpticsSuite extends DisciplineSuite {

  final case class Nested(a: Int)
  object Nested {
    val a: Lens[Nested, Int] = Focus[Nested](_.a)
  }
}

fails with

[error] -- Error: (...)/OpticsSuite.scala:37:44 
[error] 37 |    val a: Lens[Nested, Int] = Focus[Nested](_.a)
[error]    |                               ^^^^^^^^^^^^^^^^^^
[error]    |Exception occurred while executing macro expansion.
[error]    |java.lang.ClassCastException: class dotty.tools.dotc.ast.Trees$This cannot be cast to class dotty.tools.dotc.ast.Trees$RefTree (dotty.tools.dotc.ast.Trees$This and dotty.tools.dotc.ast.Trees$RefTree are in unnamed module of loader sbt.internal.classpath.ClassLoaderCache$Key$CachedClassLoader @7df31f1b)
[error]    |    at scala.quoted.runtime.impl.QuotesImpl.scala$quoted$runtime$impl$QuotesImpl$reflect$Ref$$$_$apply$$anonfun$6(QuotesImpl.scala:436)
[error]    |    at scala.quoted.runtime.impl.QuotesImpl$reflect$.scala$quoted$runtime$impl$QuotesImpl$reflect$$$withDefaultPos(QuotesImpl.scala:2904)
[error]    |    at scala.quoted.runtime.impl.QuotesImpl$reflect$Ref$.apply(QuotesImpl.scala:436)
[error]    |Testat scala.quoted.runtime.impl.QuotesImpl$reflect$Ref$.apply(QuotesImpl.scala:434)
[error]    |    at monocle.internal.focus.features.selectonlyfield.SelectOnlyFieldParser.getCompanionObject$$anonfun$1(SelectOnlyFieldParser.scala:36)
[error]    |    at scala.util.Either.map(Either.scala:382)
[error]    |    at monocle.internal.focus.features.selectonlyfield.SelectOnlyFieldParser.getCompanionObject(SelectOnlyFieldParser.scala:36)
[error]    |    at monocle.internal.focus.features.selectonlyfield.SelectOnlyFieldParser.getFieldAction$$anonfun$1(SelectOnlyFieldParser.scala:28)
[error]    |    at scala.util.Either.flatMap(Either.scala:352)
[error]    |    at monocle.internal.focus.features.selectonlyfield.SelectOnlyFieldParser.monocle$internal$focus$features$selectonlyfield$SelectOnlyFieldParser$$getFieldAction(SelectOnlyFieldParser.scala:30)
[error]    |    at monocle.internal.focus.features.selectonlyfield.SelectOnlyFieldParser$SelectOnlyField$.unapply(SelectOnlyFieldParser.scala:17)
[error]    |    at monocle.internal.focus.features.ParserLoop.loop$1(ParserLoop.scala:56)
[error]    |    at monocle.internal.focus.features.ParserLoop.parseFocusActions(ParserLoop.scala:64)
[error]    |    at monocle.internal.focus.features.ParserLoop.parseFocusActions$(ParserLoop.scala:27)
[error]    |    at monocle.internal.focus.FocusImpl.parseFocusActions(FocusImpl.scala:7)
[error]    |    at monocle.internal.focus.FocusImpl.$anonfun$1(FocusImpl.scala:20)
[error]    |    at scala.util.Either.flatMap(Either.scala:352)
[error]    |    at monocle.internal.focus.FocusImpl.run(FocusImpl.scala:22)
[error]    |    at monocle.internal.focus.FocusImpl$.apply(FocusImpl.scala:33)
[error]    |
[error]    |----------------------------------------------------------------------------
[error]    |Inline stack trace
[error]    |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[error]    |This location contains code that was inlined from Focus.scala:35
[error]     ----------------------------------------------------------------------------
[error] one error found

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the nested case class/object example and inspect monocle/internal/focus/features/selectonlyfield/SelectOnlyFieldParser.scala, especially getCompanionObject at line 36, along with the Focus.scala expansion path shown in the trace. Done means the Scala 3 example compiles without the ClassCastException and the existing optics behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.