answer the numeric literals question: a bare number is a magnitude with no axis
This commit is contained in:
@@ -610,6 +610,15 @@ fn cg_expr(expr: Map<String, Any>) -> String {
|
|||||||
record_call(state_get("__cg_current_fn"), "temporal:instant_plus_instant")
|
record_call(state_get("__cg_current_fn"), "temporal:instant_plus_instant")
|
||||||
return "0 /* TIME_TYPE_ERROR: Instant + Instant */"
|
return "0 /* TIME_TYPE_ERROR: Instant + Instant */"
|
||||||
}
|
}
|
||||||
|
// A bare literal is a MAGNITUDE WITH NO AXIS. Duration + Int
|
||||||
|
// was already refused because an Int carries no unit; adding
|
||||||
|
// one to a POINT is worse, because it silently moves the
|
||||||
|
// instant by an unspecified amount. The asymmetry had no
|
||||||
|
// justification -- it was simply never written.
|
||||||
|
if is_int_expr(right) {
|
||||||
|
record_call(state_get("__cg_current_fn"), "temporal:instant_plus_int")
|
||||||
|
return "0 /* TIME_TYPE_ERROR: Instant + Int */"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if left_is_dur {
|
if left_is_dur {
|
||||||
if right_is_inst {
|
if right_is_inst {
|
||||||
|
|||||||
@@ -967,3 +967,23 @@ test "test-keyword-is-still-reserved" {
|
|||||||
let out: String = compile_capture(src)
|
let out: String = compile_capture(src)
|
||||||
assert str_contains(out, "int main"), "the suite still compiles, which requires test to remain a keyword"
|
assert str_contains(out, "int main"), "the suite still compiles, which requires test to remain a keyword"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── A bare literal is a magnitude with no axis ───────────────────────────────
|
||||||
|
//
|
||||||
|
// Duration + Int was already refused because an Int carries no unit. Adding one
|
||||||
|
// to a POINT is worse: it moves the instant by an unspecified amount. The
|
||||||
|
// asymmetry had no justification; it was simply never written.
|
||||||
|
|
||||||
|
test "instant-plus-bare-int-is-refused" {
|
||||||
|
let src: String = "fn main() { let t: Instant = now() let u: Instant = t + 3 println(\"x\") }"
|
||||||
|
let out: String = compile_capture(src)
|
||||||
|
assert str_contains(out, "TIME_TYPE_ERROR: Instant + Int"), "3 of what?"
|
||||||
|
}
|
||||||
|
|
||||||
|
test "instant-plus-unit-suffix-is-allowed" {
|
||||||
|
// .hour supplies the axis, so the magnitude becomes a displacement.
|
||||||
|
let src: String = "fn main() { let t: Instant = now() let u: Instant = t + 1.hour println(\"x\") }"
|
||||||
|
let out: String = compile_capture(src)
|
||||||
|
assert str_contains(out, "el_instant_add_dur"), "a unit suffix makes it a Duration"
|
||||||
|
assert !str_contains(out, "TIME_TYPE_ERROR"), "and the addition is legal"
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
# <kind> means <message>
|
# <kind> means <message>
|
||||||
|
|
||||||
instant_plus_instant means Instant + Instant is not allowed — a point plus a point is not a point. Subtract them for a Duration, or add a Duration.
|
instant_plus_instant means Instant + Instant is not allowed — a point plus a point is not a point. Subtract them for a Duration, or add a Duration.
|
||||||
|
instant_plus_int means Instant + Int is not allowed — a bare literal is a magnitude with no axis. 3 of what? Adding it to a point moves the instant by an unspecified amount. Use a Duration.
|
||||||
duration_plus_int means Duration + Int is not allowed — an Int carries no unit. Use duration_seconds(n) or N.seconds.
|
duration_plus_int means Duration + Int is not allowed — an Int carries no unit. Use duration_seconds(n) or N.seconds.
|
||||||
duration_minus_int means Duration - Int is not allowed — an Int carries no unit.
|
duration_minus_int means Duration - Int is not allowed — an Int carries no unit.
|
||||||
instant_cmp_duration means Instant < Duration is not allowed — a point and a displacement are not on the same scale.
|
instant_cmp_duration means Instant < Duration is not allowed — a point and a displacement are not on the same scale.
|
||||||
|
|||||||
Reference in New Issue
Block a user