Skip to content

Commit

Permalink
fix bug in deltaunit offset checking (#432)
Browse files Browse the repository at this point in the history
* fix bug in deltaunit offset checking

* pay the mima troll
  • Loading branch information
erikerlandson authored Feb 2, 2023
1 parent 6b61c47 commit 97c5f5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
38 changes: 14 additions & 24 deletions core/src/main/scala/coulomb/infra/meta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,13 @@ object meta:
Quotes
)(u: quotes.reflect.TypeRepr, b: quotes.reflect.TypeRepr): Rational =
import quotes.reflect.*
given sigmode: SigMode = SigMode.Simplify
// given sigmode: SigMode = SigMode.Simplify
u match
case deltaunit(offset, db) =>
if (matchingdelta(db, b)) offset
else
report.error(s"bad DeltaUnit in offset: ${typestr(u)}")
Rational.const0
case baseunit() if convertible(u, b) => Rational.const0
case derivedunit(_, _) if convertible(u, b) => Rational.const0
case _ => {
report.error(
s"unknown unit expression in offset: ${typestr(u)}"
); Rational.const0
}

def matchingdelta(using
Quotes
)(db: quotes.reflect.TypeRepr, b: quotes.reflect.TypeRepr): Boolean =
import quotes.reflect.*
// units of db and b should cancel, and leave only a constant behind
simplify(TypeRepr.of[/].appliedTo(List(db, b))) match
case rationalTE(_) => true
case _ => false
case deltaunit(offset, d) if convertible(d, b) => offset
case _ if convertible(u, b) => Rational.const0
case _ =>
report.error(s"bad DeltaUnit in offset: ${typestr(u)}")
Rational.const0

def convertible(using
Quotes
Expand All @@ -152,6 +136,12 @@ object meta:
val (_, rsig) = cansig(TypeRepr.of[/].appliedTo(List(u1, u2)))
rsig == Nil

@deprecated("unused, keeping this to satisfy MIMA")
def matchingdelta(using
Quotes
)(db: quotes.reflect.TypeRepr, b: quotes.reflect.TypeRepr): Boolean =
false

// returns tuple: (expr-for-coef, type-of-Res)
def cansig(using qq: Quotes, mode: SigMode)(
uu: quotes.reflect.TypeRepr
Expand Down Expand Up @@ -331,12 +321,12 @@ object meta:
)
) match
case iss: ImplicitSearchSuccess =>
val AppliedType(_, List(_, b, o, _, _)) =
val AppliedType(_, List(_, d, o, _, _)) =
iss.tree.tpe.baseType(
TypeRepr.of[DeltaUnit].typeSymbol
): @unchecked
val rationalTE(offset) = o: @unchecked
Some((offset, b))
Some((offset, d))
case _ => None

def unifyOp(using Quotes)(
Expand Down
9 changes: 9 additions & 0 deletions units/src/test/scala/coulomb/time.scala
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,20 @@ class JavaTimeSuite extends CoulombSuite:
assertEquals(et.toInstant.toString, "1969-07-20T00:00:00Z")
}

test("toInstant Milli * Seconds") {
import coulomb.units.javatime.conversions.explicit.given

// verify it handles arbitrary unit types that are compatible
val et = (1000L).withEpochTime[Milli * Second]
assertEquals(et.toInstant.toString, "1970-01-01T00:00:01Z")
}

test("toInstant YearsBC") {
import coulomb.units.javatime.conversions.explicit.given

import bc.{*, given}

// verify it handles negative directionality
val et = 100.withEpochTime[YearsBC]
assertEquals(et.toInstant.toString, "-0099-05-18T00:00:00Z")
}
Expand Down

0 comments on commit 97c5f5a

Please sign in to comment.