Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .claude/skills/scala-code-style/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Push correctness into the types so the compiler rejects wrong code, and let it d

## Verification

- Make no completion claim without fresh command output whose exit code you've read. Run `sbt core3/test coreNext3/test core/test` at least, and `sbt test` for anything touching shared or platform code. Lookup macro changes need a clean build, since zinc keeps stale macro expansions.
- Make no completion claim without fresh command output whose exit code you've read. Run `sbt core3/test coreNext3/test coreLatest3/test core/test linkage/test` at least, and `sbt test` for anything touching shared or platform code. Lookup macro changes need a clean build, since zinc keeps stale macro expansions.
- "Bug fixed" means the failure was reproduced, then shown gone, then shown to come back when the fix is removed.
- Changing a shared helper means running its owner's tests and checking every call site.
- Say what the evidence doesn't prove.
Expand Down
4 changes: 3 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ formless is a generic programming library (HLists, records, tagged types) for Sc

## Build & test

sbt with `sbt-projectmatrix`. The root project aggregates every `core` and `docs` project, so `sbt test` runs them all; for a faster loop, pick one matrix project:
sbt with `sbt-projectmatrix`. The root project aggregates every `core` and `docs` project and `linkage`, so `sbt test` runs them all; for a faster loop, pick one matrix project:

| Project | Platform / Scala |
|---|---|
| `core` | JVM, 2.13 |
| `core3` | JVM, Scala 3 LTS (3.3.x, the default `scalaVersion`) |
| `coreNext3` | JVM, Scala 3 Next (3.8.x) — tested, not published |
| `coreLatest3` | JVM, the newest Scala 3 (3.10.x) — tested, not published |
| `coreJS`, `coreJS3` | Scala.js |
| `coreNative`, `coreNative3` | Scala Native |
| `docs3` | mdoc docs project |
| `linkage` | JVM, `core`'s JVM test suite compiled with the newest Scala 3 against `core3`'s main classes, as a user's build links against the published jar; catches signatures two compilers erase differently |

Common commands:

Expand Down
86 changes: 53 additions & 33 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ lazy val scala213 = "2.13.18"
// Scala 3.3.9 fixes the Scaladoc signature crash on JDK 25: https://github.com/scala/scala3/pull/25779
lazy val scala3 = "3.3.9"
lazy val scala3Next = "3.8.4"
lazy val scala3NextAxis = new VirtualAxis.WeakAxis {
val idSuffix = "Next"
val directorySuffix = "next"
// The newest Scala 3; it erases some signatures differently from the LTS, which the `linkage` tests check
lazy val scala3Latest = "3.10.1"
def testedScala3Axis(suffix: String) = new VirtualAxis.WeakAxis {
val idSuffix = suffix
val directorySuffix = suffix.toLowerCase
}
lazy val scala3NextAxis = testedScala3Axis("Next")
lazy val scala3LatestAxis = testedScala3Axis("Latest")
lazy val scalaVersions = Seq(scala213, scala3)

ThisBuild / scalaVersion := scala3
ThisBuild / version := "0.9.0"
ThisBuild / version := "0.9.1"

// GitHub Actions config
// formless builds and is tested on Java 17 and newer; the checks that need only one Java version run on the newest
Expand Down Expand Up @@ -177,7 +181,22 @@ lazy val baseSettings = Seq(

lazy val root = project.in(file("."))
.settings(baseSettings)
.aggregate((core.projectRefs ++ docs.projectRefs): _*)
.aggregate((core.projectRefs ++ docs.projectRefs :+ (linkage: ProjectReference)): _*)

// Runs `core`'s JVM test suite compiled with the newest Scala 3 against the Scala 3 LTS build of `core`'s main classes,
// the one that is published, as a user's build links against the released jar. `core`'s own test projects compile the
// library and its tests with one compiler, so they can't see two compilers disagree on how a method erases.
lazy val linkage = project.in(file("linkage"))
.settings(baseSettings ++ jvmSettings)
.settings(
scalaVersion := scala3Latest,
libraryDependencies ++= Seq(munit.value, scalacheck.value),
Test / unmanagedSourceDirectories := Seq("scala", "scala-3", "scalajvm").map(dir =>
(ThisBuild / baseDirectory).value / "core" / "src" / "test" / dir
),
Test / sourceGenerators += generatedTestSources.taskValue,
)
.dependsOn(core.jvm(scala3))

lazy val publishSettings = Seq(
publish / skip := false,
Expand All @@ -199,16 +218,35 @@ lazy val munit = Def.setting("org.scalameta" %%% "munit" % "1.3.6" % Test)
lazy val shapeless = Def.setting("com.chuusai" %%% "shapeless" % "2.3.13")
lazy val scalacheck = Def.setting("org.scalacheck" %%% "scalacheck" % "1.20.0" % Test)

lazy val core = projectMatrix.in(file("core"))
// Tested against Scala 3 Next too, but published only for the LTS
.jvmPlatform(
scalaVersions = Seq(scala3Next),
axisValues = Seq(scala3NextAxis),
settings = jvmSettings ++ Seq(
publish / skip := true,
mimaPreviousArtifacts := Set(),
),
lazy val generatedTestSources = Def.task {
val srcManaged = (Test / sourceManaged).value / "generated"

def gen(scalaF: String, generator: SourceGenerator) = {
println(s"Generating ${srcManaged / scalaF} with $generator")
IO.write(srcManaged / scalaF, generator())
srcManaged / scalaF
}

Seq(
gen("Util.scala", SourceGenerator.Util),
gen("HListSelectorTest.scala", SourceGenerator.HListSelectorTest),
gen("RecordSelectorTest.scala", SourceGenerator.RecordSelectorTest),
gen("UpdaterTest.scala", SourceGenerator.UpdaterTest),
gen("ModifierTest.scala", SourceGenerator.ModifierTest),
gen("RenamerTest.scala", SourceGenerator.RenamerTest),
gen("RemoverTest.scala", SourceGenerator.RemoverTest),
)
}

lazy val testedOnlySettings = jvmSettings ++ Seq(
publish / skip := true,
mimaPreviousArtifacts := Set(),
)

lazy val core = projectMatrix.in(file("core"))
// Tested against Scala 3 Next and the newest Scala 3 too, but published only for the LTS
.jvmPlatform(scalaVersions = Seq(scala3Next), axisValues = Seq(scala3NextAxis), settings = testedOnlySettings)
.jvmPlatform(scalaVersions = Seq(scala3Latest), axisValues = Seq(scala3LatestAxis), settings = testedOnlySettings)
.jvmPlatform(scalaVersions = scalaVersions, settings = jvmSettings)
.jsPlatform(scalaVersions = scalaVersions)
.nativePlatform(scalaVersions = scalaVersions)
Expand All @@ -229,25 +267,7 @@ lazy val core = projectMatrix.in(file("core"))
),
Seq(),
),
Test / sourceGenerators += Def.task {
val srcManaged = (Test / sourceManaged).value / "generated"

def gen(scalaF: String, generator: SourceGenerator) = {
println(s"Generating ${srcManaged / scalaF} with $generator")
IO.write(srcManaged / scalaF, generator())
srcManaged / scalaF
}

Seq(
gen("Util.scala", SourceGenerator.Util),
gen("HListSelectorTest.scala", SourceGenerator.HListSelectorTest),
gen("RecordSelectorTest.scala", SourceGenerator.RecordSelectorTest),
gen("UpdaterTest.scala", SourceGenerator.UpdaterTest),
gen("ModifierTest.scala", SourceGenerator.ModifierTest),
gen("RenamerTest.scala", SourceGenerator.RenamerTest),
gen("RemoverTest.scala", SourceGenerator.RemoverTest),
)
},
Test / sourceGenerators += generatedTestSources.taskValue,
)

// Compile-time regression tests: compiles generated workloads and checks the memory the compiler allocates. Not part of
Expand Down
16 changes: 12 additions & 4 deletions core/src/main/scala-3/formless/record/Lookup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -765,18 +765,26 @@ private[formless] object Lookup {

}

/**
* The runtime value of the evidence the lookup macros prove, `FieldAbsent` and `MaybeRecord`. Their opaque types are
* backed by this object, not by `Unit`, because Scala 3 compilers disagree on how a method returning an opaque type over
* `Unit` erases (`BoxedUnit` in 3.3 and 3.8, `void` in 3.10), so code compiled by a newer compiler couldn't link against the
* published jar.
*/
private[formless] object Proven

/**
* Evidence that no field `head` of the record `T` satisfies `Cmp[head, F]`, whatever its abstract types turn out to be.
* Unlike the absence of a lookup that succeeds, it does not resolve for a record with an abstract tail, with an element
* that might match once its type parameters are known, or with a field that satisfies `Cmp[head, F]` but that `FieldOp`
* can't use.
*/
@scala.annotation.implicitNotFound("Cannot prove that no element of ${T} matches ${F}")
private[formless] opaque type FieldAbsent[T <: HList, F, Cmp[_, _]] = Unit
private[formless] opaque type FieldAbsent[T <: HList, F, Cmp[_, _]] = Proven.type

object FieldAbsent {
// Inside the opaque type's scope, so the proof is made without a cast; the macro below is the only caller
private[formless] def proof[T <: HList, F, Cmp[_, _]]: FieldAbsent[T, F, Cmp] = ()
private[formless] def proof[T <: HList, F, Cmp[_, _]]: FieldAbsent[T, F, Cmp] = Proven

private def fieldAbsentImpl[T <: HList: Type, F: Type, Cmp[_, _]: Type](using Quotes): Expr[FieldAbsent[T, F, Cmp]] =
Lookup.guarded("prove a field absent from a record")(fieldAbsentImpl0[T, F, Cmp])
Expand Down Expand Up @@ -969,10 +977,10 @@ object ReplaceAt {
* `HList`s also have, require it, so using them on a plain `HList` explains why instead of reporting a missing field.
*/
@scala.annotation.implicitNotFound("${T} has no record fields, so this `apply`, `updateWith` or `replace` is the record syntax that importing formless.record adds to every HList, not the HList method of the same name. To call the HList method, write formless.hlist.HList.toHListOps(list).updateWith(...), or don't import the record syntax in this file.")
private[formless] opaque type MaybeRecord[T <: HList] = Unit
private[formless] opaque type MaybeRecord[T <: HList] = Proven.type

object MaybeRecord {
private[formless] inline def proof[T <: HList]: MaybeRecord[T] = ()
private[formless] inline def proof[T <: HList]: MaybeRecord[T] = Proven

private def maybeRecordImpl[T <: HList: Type](using Quotes): Expr[MaybeRecord[T]] =
Lookup.guarded("check for record fields")(maybeRecordImpl0[T])
Expand Down