[Scala 3] port ValueEnum (top-level valNameImpl)#883
Draft
halotukozak wants to merge 3 commits into
Draft
Conversation
Verbatim port from origin/master:core/src/main/scala-3/com/avsystem/commons/misc/ValueEnum.scala.
- Top-level `def valNameImpl` (NOT in MiscMacros — Pattern 5 enclosing-symbol walk via
`Symbol.spliceOwner.owner` + `omitAnonClass`; Pitfall 5 — `.owner` required, not bare
`spliceOwner`).
- `Ctx` registration machinery (synchronized + `awaitingRegister` flag dance + `finished`
flag + `lazy val values`) preserved verbatim per Pitfall 8 (SI-7046-style init-order trap).
- `given (valName: ValName) => EnumCtx = new Ctx(...)` replaces the Phase-1 `implicit def
valName: ValName = ???` stub.
- `inline protected given ValName = ${ valNameImpl[Value, ValName, this.type]('{ ValName(_) }) }`
wires call sites to the top-level macro.
- Rule-3 auto-fix: added local `import scala.quoted.{Expr, Quotes, Type}` because our
`CommonAliases.scala` on this branch base lacks the fork's `export scala.quoted.*` line
(matches slice 5.3/5.5 precedent).
- Rule-3 auto-fix: bundled new `ValueEnumCompanionCompat.scala` (single trait, no extra
deps — only needs `summon[Ordering[T]]` which is provided by `given Ordering[T]` in
ValueEnum.scala). Fork keeps it in `compat.scala` alongside many other compat traits;
we extract just the ValueEnum-relevant one to avoid dragging in unported feature compat
surface (Boxing/Opt/Timestamp/TypeString/JavaClassName/NamedEnumCompanion/OrderedEnum).
Synced verbatim from origin/master:core/src/test/scala-3/com/avsystem/commons/misc/ValueEnumTest.scala. - "value enum test" un-wrapped + green: validates ordinals (0..4) + names (One/Two/Three/Four/Five_?) + values collection ordering — confirms Ctx synchronized + awaitingRegister flag dance produces correct results (no IllegalStateException at startup, Pitfall 8 cleared). - "enum constant member validation" stays `ignore`d per fork (`assertCompiles`/ `assertDoesNotCompile` of the macro's compile-time error contract is harder to reproduce in Scala 3 toolbox — fork keeps it ignored too).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Restores
misc.ValueEnumfamily from Phase-1???stub to a real Scala 3 macro implementation. Cribbed verbatim from forkorigin/master:core/src/main/scala-3/com/avsystem/commons/misc/ValueEnum.scala.What changed
ValueEnum.scala:def valNameImpl[T <: ValueEnum: Type, ValName: Type, Owner: Type]— Pattern 5 enclosing-symbol walk viaSymbol.spliceOwner.owner+omitAnonClasshelper. Validates that the macro splice site is a public, final, non-lazyvalin the companion object.ValueEnumCompanion:protected[this] implicit def valName: ValName = ???is nowinline protected given ValName = $${ valNameImpl[Value, ValName, this.type]('{ ValName(_) }) }.Ctxregistration machinery preserved verbatim (synchronized+awaitingRegister/finishedflag dance +lazy val values) — initialization order matches Scala 2 for the SI-7046-style trap.AbstractValueEnum(implicit val enumCtx: EnumCtx)switched toAbstractValueEnum(using protected val enumCtx: EnumCtx).Givens:
implicit final val ordering: Ordering[T]is nowgiven ordering: Ordering[T] = Ordering.by(_.ordinal). Public name preserved.implicit final def ordered(value: T): Ordered[T]modernized togiven ordered: Conversion[T, Ordered[T]] = Ordered.orderingToOrdered(_). DownstreamenumA < enumBkeeps compiling without an extraimport scala.math.Ordered.orderingToOrdered.Tests
ValueEnumTestun-wrapped.value enum testgreen — validatesvalues == List(One, Two, Three, Four, Five_?), ordinals 0..4, names match declaration.enum constant member validationstaysignored (matches fork — compile-timeassertCompiles/assertDoesNotCompileof macro error contract is harder to reproduce in Scala 3 toolbox).MIGRATION.md
§3 core — misc ValueEnum (slice 5.7)entry — full reshape rationale.ValueEnumCompanion.valNameremoved.Slice 5.7 / Phase 5 leaf-feature-restoration.