@@ -97,6 +97,19 @@ impl<'c> Translation<'c> {
9797 // Ops like division and bitxor accept inputs of their expected result type.
9898 lhs_type_id. ctype = expr_type_id. ctype ;
9999 rhs_type_id. ctype = expr_type_id. ctype ;
100+
101+ // For complex arithmetic, complex and real values can be mixed.
102+ let expr_type_kind =
103+ & self . ast_context . resolve_type ( expr_type_id. ctype ) . kind ;
104+ if let & CTypeKind :: Complex ( result_scalar_type_id) = expr_type_kind {
105+ if !matches ! ( lhs_type_kind, CTypeKind :: Complex ( ..) ) {
106+ lhs_type_id. ctype = result_scalar_type_id;
107+ }
108+
109+ if !matches ! ( rhs_type_kind, CTypeKind :: Complex ( ..) ) {
110+ rhs_type_id. ctype = result_scalar_type_id;
111+ }
112+ }
100113 } else if op. input_types_same ( ) && lhs_type_kind != rhs_type_kind {
101114 // Ops like comparisons require argument types to match, but the result type
102115 // doesn't inform us what type to choose. Select a synthetic definition of a
@@ -284,7 +297,15 @@ impl<'c> Translation<'c> {
284297 {
285298 // If the rhs must match the lhs but doesn't, add a cast.
286299 // For compound assignment, use the compute type; for regular assignment, use lhs type.
287- let target_type_id = compute_lhs_type_id. unwrap_or ( lhs_type_id) ;
300+ let mut target_type_id = compute_lhs_type_id. unwrap_or ( lhs_type_id) ;
301+
302+ // For complex arithmetic, complex and real values can be mixed.
303+ if let & CTypeKind :: Complex ( lhs_scalar_type_id) = lhs_type_kind {
304+ if !matches ! ( rhs_type_kind, CTypeKind :: Complex ( ..) ) {
305+ target_type_id. ctype = lhs_scalar_type_id;
306+ }
307+ }
308+
288309 rhs_rs = self . make_cast ( ctx. used ( ) , rhs_type_id, target_type_id, rhs_rs) ?;
289310 }
290311 }
0 commit comments