Fix cast order for ToInt (#3485)

This commit is contained in:
Laurenz 2024-02-25 12:50:11 +01:00 committed by GitHub
parent 010da18d99
commit ca5d682edb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 14 additions and 7 deletions

View File

@ -803,8 +803,8 @@ pub struct ToArray(Array);
cast! {
ToArray,
v: Bytes => Self(v.iter().map(|&b| Value::Int(b.into())).collect()),
v: Array => Self(v),
v: Bytes => Self(v.iter().map(|&b| Value::Int(b.into())).collect()),
v: Version => Self(v.values().iter().map(|&v| Value::Int(v as i64)).collect())
}

View File

@ -107,6 +107,7 @@ pub struct ToFloat(f64);
cast! {
ToFloat,
v: f64 => Self(v),
v: bool => Self(v as i64 as f64),
v: i64 => Self(v as f64),
v: Ratio => Self(v.get()),
@ -114,7 +115,6 @@ cast! {
parse_float(v.clone().into())
.map_err(|_| eco_format!("invalid float: {}", v))?
),
v: f64 => Self(v),
}
fn parse_float(s: EcoString) -> Result<f64, ParseFloatError> {

View File

@ -229,10 +229,10 @@ pub struct ToInt(i64);
cast! {
ToInt,
v: i64 => Self(v),
v: bool => Self(v as i64),
v: f64 => Self(v as i64),
v: Str => Self(parse_int(&v).map_err(|_| eco_format!("invalid integer: {}", v))?),
v: i64 => Self(v),
}
fn parse_int(mut s: &str) -> Result<i64, ParseIntError> {

View File

@ -1916,8 +1916,8 @@ pub struct ChromaComponent(f32);
cast! {
ChromaComponent,
v: Ratio => Self((v.get() * 0.4) as f32),
v: f64 => Self(v as f32),
v: Ratio => Self((v.get() * 0.4) as f32),
}
/// An integer or ratio component.

View File

@ -0,0 +1,7 @@
// Test that integer -> integer conversion doesn't do a roundtrip through float.
// Ref: false
---
#let x = 9223372036854775800
#test(type(x), int)
#test(int(x), x)

View File

@ -28,5 +28,5 @@
#bytes((a: 1))
---
// Error: 8-15 expected bytes, array, or version, found string
// Error: 8-15 expected array, bytes, or version, found string
#array("hello")

View File

@ -53,11 +53,11 @@
#test(calc.round(calc.pi, digits: 2), 3.14)
---
// Error: 6-10 expected boolean, float, string, or integer, found length
// Error: 6-10 expected integer, boolean, float, or string, found length
#int(10pt)
---
// Error: 8-13 expected boolean, integer, ratio, string, or float, found type
// Error: 8-13 expected float, boolean, integer, ratio, or string, found type
#float(float)
---