Skip to content
Open
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
55 changes: 53 additions & 2 deletions crates/macros/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,9 @@ fn expr_to_php_stub(expr: &Expr) -> String {
"\"{}\"",
s.value().replace('\\', "\\\\").replace('"', "\\\"")
),
syn::Lit::Int(i) => i.to_string(),
syn::Lit::Float(f) => f.to_string(),
// Use base10_digits() to strip Rust type suffixes like _usize, _i32, etc.
syn::Lit::Int(i) => i.base10_digits().to_string(),
syn::Lit::Float(f) => f.base10_digits().to_string(),
syn::Lit::Bool(b) => if b.value { "true" } else { "false" }.to_string(),
syn::Lit::Char(c) => format!("\"{}\"", c.value()),
_ => expr.to_token_stream().to_string(),
Expand Down Expand Up @@ -1033,3 +1034,53 @@ pub fn type_is_nullable(ty: &Type) -> Result<bool> {
_ => bail!(ty => "Unsupported argument type."),
})
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_expr_to_php_stub_strips_numeric_suffixes() {
// Test integer suffixes are stripped (issue #492)
let expr: Expr = syn::parse_quote!(42_usize);
assert_eq!(expr_to_php_stub(&expr), "42");

let expr: Expr = syn::parse_quote!(42_i32);
assert_eq!(expr_to_php_stub(&expr), "42");

let expr: Expr = syn::parse_quote!(42_u64);
assert_eq!(expr_to_php_stub(&expr), "42");

// Test float suffixes are stripped
let expr: Expr = syn::parse_quote!(3.14_f64);
assert_eq!(expr_to_php_stub(&expr), "3.14");

let expr: Expr = syn::parse_quote!(3.14_f32);
assert_eq!(expr_to_php_stub(&expr), "3.14");

// Test literals without suffixes still work
let expr: Expr = syn::parse_quote!(42);
assert_eq!(expr_to_php_stub(&expr), "42");

let expr: Expr = syn::parse_quote!(3.14);
assert_eq!(expr_to_php_stub(&expr), "3.14");
}

#[test]
fn test_expr_to_php_stub_negative_numbers() {
let expr: Expr = syn::parse_quote!(-42_i32);
assert_eq!(expr_to_php_stub(&expr), "-42");

let expr: Expr = syn::parse_quote!(-3.14_f64);
assert_eq!(expr_to_php_stub(&expr), "-3.14");
}

#[test]
fn test_expr_to_php_stub_none_and_some() {
let expr: Expr = syn::parse_quote!(None);
assert_eq!(expr_to_php_stub(&expr), "null");

let expr: Expr = syn::parse_quote!(Some(42_usize));
assert_eq!(expr_to_php_stub(&expr), "42");
}
}
Loading