1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Collection literal conversion and type predicates for ExprConverter
//!
//! Handles list, dict, tuple, set, frozenset literals and type checking helpers.
use crate::hir::*;
use anyhow::{bail, Result};
use syn::parse_quote;
use super::ExprConverter;
impl<'a> ExprConverter<'a> {
pub(super) fn convert_list(&self, elts: &[HirExpr]) -> Result<syn::Expr> {
let elt_exprs: Vec<syn::Expr> = elts
.iter()
.map(|e| self.convert(e))
.collect::<Result<Vec<_>>>()?;
// DEPYLER-0780: Always use vec![] for list literals
// Array literals [T; N] are incompatible with &Vec<T> parameters
// Python lists map to Vec<T> in Rust, so consistently use vec![]
Ok(parse_quote! { vec![#(#elt_exprs),*] })
}
pub(super) fn convert_dict(&self, items: &[(HirExpr, HirExpr)]) -> Result<syn::Expr> {
// DEPYLER-1166: Check if dict has mixed value types and needs DepylerValue wrapping
let has_mixed_types = self.dict_has_mixed_value_types(items);
if has_mixed_types {
// Use DepylerValue wrapping for mixed-type dicts
// Use String keys since most Python dicts have string keys
let nested_type = Type::Dict(Box::new(Type::String), Box::new(Type::Unknown));
return self.convert_dict_to_depyler_value(
items,
&std::collections::HashMap::new(),
&nested_type,
);
}
let insert_exprs: Vec<syn::Expr> = items
.iter()
.map(|(k, v)| {
let key = self.convert(k)?;
let val = self.convert(v)?;
Ok(parse_quote! { map.insert(#key, #val) })
})
.collect::<Result<Vec<_>>>()?;
// DEPYLER-0623: Use fully qualified path to avoid missing import
Ok(parse_quote! {
{
let mut map = std::collections::HashMap::new();
#(#insert_exprs;)*
map
}
})
}
/// DEPYLER-1166: Check if dict literal has mixed value types (requires DepylerValue)
pub(super) fn dict_has_mixed_value_types(&self, items: &[(HirExpr, HirExpr)]) -> bool {
if items.len() <= 1 {
return false; // Single or empty dict, no mixing
}
// Quick check: if any value is a dict or list, it's potentially mixed
let has_dict_or_list = items
.iter()
.any(|(_, v)| matches!(v, HirExpr::Dict(_) | HirExpr::List(_)));
if has_dict_or_list {
return true;
}
// Check if literal types are mixed
let value_type = |v: &HirExpr| -> u8 {
match v {
HirExpr::Literal(Literal::Int(_)) => 1,
HirExpr::Literal(Literal::Float(_)) => 2,
HirExpr::Literal(Literal::String(_)) => 3,
HirExpr::Literal(Literal::Bool(_)) => 4,
HirExpr::Literal(Literal::None) => 5,
HirExpr::Var(_) => 6, // Variables have unknown type
_ => 7, // Other expressions
}
};
let first_type = value_type(&items[0].1);
// If any value has a different type, it's mixed
// Variables (type 6) and other expressions (type 7) always count as different
items.iter().any(|(_, v)| {
let t = value_type(v);
t != first_type || t >= 6
})
}
/// DEPYLER-1122: Convert dict literal with DepylerValue wrapping
/// Used when returning a dict from a method with bare `dict` return type,
/// which maps to HashMap<DepylerValue, DepylerValue>
/// DEPYLER-1166: Also handles Dict[str, Any] which has String keys
pub(super) fn convert_dict_to_depyler_value(
&self,
items: &[(HirExpr, HirExpr)],
class_field_types: &std::collections::HashMap<String, Type>,
ret_type: &Type,
) -> Result<syn::Expr> {
// DEPYLER-1166: Determine if keys should be String (Dict[str, Any]) or DepylerValue (bare dict)
// DEPYLER-1213: Bare dict annotation stored as Custom("dict") should use String keys
// (produces HashMap<String, DepylerValue>)
let use_string_keys = match ret_type {
// Dict[str, Any] or Dict[str, unknown] - use String keys
Type::Dict(k, _) => matches!(k.as_ref(), Type::String | Type::Unknown),
// Bare `dict` annotation - also use String keys (common case Dict[str, Any])
Type::Custom(name) => name == "dict" || name == "Dict",
_ => false,
};
let insert_exprs: Vec<syn::Expr> = items
.iter()
.map(|(k, v)| {
let key_raw = self.convert(k)?;
let val_raw = self.convert(v)?;
// DEPYLER-1166: For Dict[str, Any], use raw string keys; for bare dict, wrap in DepylerValue
let key: syn::Expr = if use_string_keys {
// Dict[str, Any] - use String keys directly
key_raw.clone()
} else {
// Bare dict - wrap key in DepylerValue
match k {
HirExpr::Literal(Literal::Int(_)) => parse_quote! { DepylerValue::Int(#key_raw as i64) },
HirExpr::Literal(Literal::Float(_)) => parse_quote! { DepylerValue::Float(#key_raw as f64) },
HirExpr::Literal(Literal::String(_)) => parse_quote! { DepylerValue::Str(#key_raw.to_string()) },
HirExpr::Literal(Literal::Bool(_)) => parse_quote! { DepylerValue::Bool(#key_raw) },
_ => parse_quote! { DepylerValue::Str(format!("{:?}", #key_raw)) },
}
};
// Wrap value in DepylerValue based on its type
// DEPYLER-99MODE-S9: String literals need .to_string() for DepylerValue::Str(String)
let val: syn::Expr = match v {
HirExpr::Literal(Literal::Int(_)) => parse_quote! { DepylerValue::Int(#val_raw as i64) },
HirExpr::Literal(Literal::Float(_)) => parse_quote! { DepylerValue::Float(#val_raw as f64) },
HirExpr::Literal(Literal::String(_)) => parse_quote! { DepylerValue::Str(#val_raw.to_string()) },
HirExpr::Literal(Literal::Bool(_)) => parse_quote! { DepylerValue::Bool(#val_raw) },
HirExpr::Literal(Literal::None) => parse_quote! { DepylerValue::None },
HirExpr::Attribute { value, attr } => {
// self.field access - check field type
if let HirExpr::Var(name) = value.as_ref() {
if name == "self" {
if let Some(field_type) = class_field_types.get(attr) {
return Ok(match field_type {
Type::Int => parse_quote! { map.insert(#key, DepylerValue::Int(#val_raw as i64)) },
Type::Float => parse_quote! { map.insert(#key, DepylerValue::Float(#val_raw as f64)) },
Type::String => parse_quote! { map.insert(#key, DepylerValue::Str(#val_raw.to_string())) },
Type::Bool => parse_quote! { map.insert(#key, DepylerValue::Bool(#val_raw)) },
Type::List(_) => parse_quote! { map.insert(#key, DepylerValue::List(#val_raw.iter().map(|x| DepylerValue::from(x.clone())).collect())) },
_ => parse_quote! { map.insert(#key, DepylerValue::Str(format!("{:?}", #val_raw))) },
});
}
}
}
parse_quote! { DepylerValue::Str(format!("{:?}", #val_raw)) }
}
HirExpr::Var(name) => {
// Check if we know the variable type from class fields
if let Some(field_type) = class_field_types.get(name) {
match field_type {
Type::Int => parse_quote! { DepylerValue::Int(#val_raw as i64) },
Type::Float => parse_quote! { DepylerValue::Float(#val_raw as f64) },
Type::String => parse_quote! { DepylerValue::Str(#val_raw.to_string()) },
Type::Bool => parse_quote! { DepylerValue::Bool(#val_raw) },
_ => parse_quote! { DepylerValue::Str(format!("{:?}", #val_raw)) },
}
} else {
parse_quote! { DepylerValue::Str(format!("{:?}", #val_raw)) }
}
}
// DEPYLER-1166: Recursively handle nested Dict values
HirExpr::Dict(nested_items) => {
// Create a nested HashMap with DepylerValue keys and values
// For bare dict return types, inner dicts also have DepylerValue keys
let nested_type = Type::Dict(Box::new(Type::Unknown), Box::new(Type::Unknown));
let nested_dict = self.convert_dict_to_depyler_value(nested_items, class_field_types, &nested_type)?;
// The recursive call already produces HashMap<DepylerValue, DepylerValue>, just wrap it
parse_quote! { DepylerValue::Dict(#nested_dict) }
}
// DEPYLER-1166: Handle nested List values
HirExpr::List(items) => {
// Convert each list element to DepylerValue
let list_elements: Vec<syn::Expr> = items
.iter()
.map(|item| {
let item_raw = self.convert(item)?;
let wrapped: syn::Expr = match item {
HirExpr::Literal(Literal::Int(_)) => parse_quote! { DepylerValue::Int(#item_raw as i64) },
HirExpr::Literal(Literal::Float(_)) => parse_quote! { DepylerValue::Float(#item_raw as f64) },
HirExpr::Literal(Literal::String(_)) => parse_quote! { DepylerValue::Str(#item_raw.to_string()) },
HirExpr::Literal(Literal::Bool(_)) => parse_quote! { DepylerValue::Bool(#item_raw) },
HirExpr::Literal(Literal::None) => parse_quote! { DepylerValue::None },
_ => parse_quote! { DepylerValue::Str(format!("{:?}", #item_raw)) },
};
Ok(wrapped)
})
.collect::<Result<Vec<_>>>()?;
parse_quote! { DepylerValue::List(vec![#(#list_elements),*]) }
}
_ => parse_quote! { DepylerValue::Str(format!("{:?}", #val_raw)) },
};
Ok(parse_quote! { map.insert(#key, #val) })
})
.collect::<Result<Vec<_>>>()?;
Ok(parse_quote! {
{
let mut map = std::collections::HashMap::new();
#(#insert_exprs;)*
map
}
})
}
pub(super) fn convert_tuple(&self, elts: &[HirExpr]) -> Result<syn::Expr> {
let elt_exprs: Vec<syn::Expr> = elts
.iter()
.map(|e| self.convert(e))
.collect::<Result<Vec<_>>>()?;
Ok(parse_quote! { (#(#elt_exprs),*) })
}
pub(super) fn convert_set(&self, elts: &[HirExpr]) -> Result<syn::Expr> {
let insert_exprs: Vec<syn::Expr> = elts
.iter()
.map(|e| {
let elem = self.convert(e)?;
Ok(parse_quote! { set.insert(#elem) })
})
.collect::<Result<Vec<_>>>()?;
// DEPYLER-0623: Use fully qualified path to avoid missing import
Ok(parse_quote! {
{
let mut set = std::collections::HashSet::new();
#(#insert_exprs;)*
set
}
})
}
pub(super) fn convert_frozenset(&self, elts: &[HirExpr]) -> Result<syn::Expr> {
let insert_exprs: Vec<syn::Expr> = elts
.iter()
.map(|e| {
let elem = self.convert(e)?;
Ok(parse_quote! { set.insert(#elem) })
})
.collect::<Result<Vec<_>>>()?;
// DEPYLER-0623: Use fully qualified path to avoid missing import
Ok(parse_quote! {
{
let mut set = std::collections::HashSet::new();
#(#insert_exprs;)*
std::sync::Arc::new(set)
}
})
}
pub(super) fn is_set_expr(&self, expr: &HirExpr) -> bool {
match expr {
HirExpr::Set(_) | HirExpr::FrozenSet(_) => true,
HirExpr::Call { func, .. } if func == "set" || func == "frozenset" => true,
HirExpr::Var(name) => {
// DEPYLER-99MODE-S9: Check param_types for set-typed variables
// to correctly handle set subtraction (s1 - s2 → s1.difference(&s2))
if let Some(var_type) = self.param_types.get(name) {
matches!(var_type, Type::Set(_))
} else {
false
}
}
_ => false,
}
}
/// DEPYLER-0601: Detect if expression is likely a string type.
/// Used to generate `.contains()` instead of `.contains_key()` for `in` operator.
pub(super) fn is_string_expr(&self, expr: &HirExpr) -> bool {
match expr {
// String literals are obviously strings
HirExpr::Literal(Literal::String(_)) => true,
// F-strings produce strings
HirExpr::FString { .. } => true,
// Method calls that return strings
HirExpr::MethodCall { method, .. } => {
matches!(
method.as_str(),
"lower"
| "upper"
| "strip"
| "lstrip"
| "rstrip"
| "replace"
| "join"
| "format"
| "capitalize"
| "title"
| "swapcase"
| "center"
| "ljust"
| "rjust"
| "zfill"
| "expandtabs"
| "encode"
| "decode"
)
}
// Variables with common string-like names
HirExpr::Var(name) => {
matches!(
name.as_str(),
"s" | "url"
| "path"
| "text"
| "remaining"
| "query_string"
| "host"
| "scheme"
| "fragment"
| "name"
| "message"
| "line"
| "content"
| "data"
| "result"
| "output"
| "input"
| "string"
| "str"
| "pair"
| "email"
| "domain_part"
| "local_part"
| "normalized"
)
}
// Calls to str() produce strings
HirExpr::Call { func, .. } if func == "str" => true,
// DEPYLER-0752: Handle attribute access for known string fields
// Examples: r.stdout, result.stderr, response.text
HirExpr::Attribute { attr, .. } => {
matches!(
attr.as_str(),
"stdout" | "stderr" | "text" | "output" | "message" | "name"
)
}
_ => false,
}
}
/// DEPYLER-0742: Detect if expression is a deque type.
/// Used to generate VecDeque methods instead of Vec methods.
pub(super) fn is_deque_expr(&self, expr: &HirExpr) -> bool {
match expr {
// Call to deque() constructor
HirExpr::Call { func, .. } if func == "deque" || func == "collections.deque" => true,
// Variables with deque-like names
HirExpr::Var(name) => {
matches!(
name.as_str(),
"d" | "dq" | "deque" | "queue" | "buffer" | "deck"
)
}
_ => false,
}
}
/// DEPYLER-0832: Detect if expression is a tuple (for `in` operator).
/// Tuples should use `.contains()` on an array, not `.contains_key()`.
pub(super) fn is_tuple_or_list_expr(&self, expr: &HirExpr) -> bool {
matches!(expr, HirExpr::Tuple(_) | HirExpr::List(_))
}
/// DEPYLER-0960: Detect if expression is a dict/HashMap type.
/// Used to ensure `key in dict` generates `.contains_key()` not `.contains()`.
pub(super) fn is_dict_expr(&self, expr: &HirExpr) -> bool {
match expr {
// Dict literal
HirExpr::Dict { .. } => true,
// Variables with common dict-like names or typed as dict
HirExpr::Var(name) => {
// DEPYLER-99MODE-S9: Check if variable is explicitly typed as a set FIRST
// This prevents set variables named "visited", "seen", etc. from being
// misidentified as dicts by the name heuristic below
if let Some(t) = self.param_types.get(name) {
if matches!(t, Type::Set(_)) {
return false;
}
if matches!(t, Type::Dict(_, _)) {
return true;
}
}
if let Some(t) = self.class_field_types.get(name) {
if matches!(t, Type::Set(_)) {
return false;
}
if matches!(t, Type::Dict(_, _)) {
return true;
}
}
let n = name.as_str();
n.contains("dict")
|| n.contains("map")
|| n.contains("hash")
|| n == "config"
|| n == "settings"
|| n == "params"
|| n == "options"
|| n == "env"
|| n == "data"
|| n == "result"
|| n == "cache"
|| n == "d"
|| n == "m"
// DEPYLER-99MODE: Common algorithm dict names
|| n == "memo"
|| n == "counts"
|| n == "freq"
|| n == "frequency"
|| n == "lookup"
|| n == "index"
|| n == "graph"
|| n == "adj"
|| n == "dp"
|| n.ends_with("_map")
|| n.ends_with("_dict")
|| n.ends_with("_cache")
|| n.ends_with("_index")
|| n.ends_with("_lookup")
}
// Calls to dict() or functions returning dicts
HirExpr::Call { func, .. } => {
func == "dict"
|| func.contains("json")
|| func.contains("config")
|| func.contains("load")
|| func.contains("parse")
}
_ => false,
}
}
pub(super) fn convert_set_operation(
&self,
op: BinOp,
left: syn::Expr,
right: syn::Expr,
) -> Result<syn::Expr> {
match op {
BinOp::BitAnd => Ok(parse_quote! {
#left.intersection(&#right).cloned().collect()
}),
BinOp::BitOr => Ok(parse_quote! {
#left.union(&#right).cloned().collect()
}),
BinOp::Sub => Ok(parse_quote! {
#left.difference(&#right).cloned().collect()
}),
BinOp::BitXor => Ok(parse_quote! {
#left.symmetric_difference(&#right).cloned().collect()
}),
_ => bail!("Invalid set operator"),
}
}
}