Skip to content

Commit b651986

Browse files
noahgiftclaude
andcommitted
fix(codegen): Convert string literals in tuples to owned Strings (Refs DEPYLER-0682)
Python tuple literals containing strings like [(1, "a"), (2, "b")] were generating inconsistent Rust types - some elements had .to_string() and others were raw &str literals. This caused type mismatch errors when the tuples were used in Vec collections. Changes: - Updated convert_tuple() to apply .to_string() to string literals - Ensures all string elements in tuples become owned Strings - Maintains type consistency in Vec<(i32, i32, String)> patterns Example transformation: signs = [(1, 20, "Capricorn"), (2, 19, "Aquarius")] Now consistently generates: vec![(1, 20, "Capricorn".to_string()), (2, 19, "Aquarius".to_string())] 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent af99f41 commit b651986

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

crates/depyler-core/src/rust_gen/expr_gen.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13457,9 +13457,20 @@ impl<'a, 'b> ExpressionConverter<'a, 'b> {
1345713457
}
1345813458

1345913459
fn convert_tuple(&mut self, elts: &[HirExpr]) -> Result<syn::Expr> {
13460+
// DEPYLER-0682: Convert string literals in tuples to owned Strings
13461+
// When tuples are used in lists (e.g., Vec<(i32, i32, String)>), string
13462+
// elements need to be owned Strings, not &str references.
13463+
// This ensures type consistency across all tuple elements in a Vec.
1346013464
let elt_exprs: Vec<syn::Expr> = elts
1346113465
.iter()
13462-
.map(|e| e.to_rust_expr(self.ctx))
13466+
.map(|e| {
13467+
let mut expr = e.to_rust_expr(self.ctx)?;
13468+
// Convert string literals to .to_string() for owned String
13469+
if matches!(e, HirExpr::Literal(Literal::String(_))) {
13470+
expr = parse_quote! { #expr.to_string() };
13471+
}
13472+
Ok(expr)
13473+
})
1346313474
.collect::<Result<Vec<_>>>()?;
1346413475
Ok(parse_quote! { (#(#elt_exprs),*) })
1346513476
}

0 commit comments

Comments
 (0)