Skip to content

transpile: Refactor binary operand handling and handle complex numbers - #1835

Open
Rua wants to merge 3 commits into
immunant:masterfrom
Rua:expected_operand_types
Open

Rua wants to merge 3 commits into
immunant:masterfrom
Rua:expected_operand_types

Conversation

@Rua

@Rua Rua commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Both a refactor and a functional improvement. Complex numbers aren't supported yet, so I haven't included any tests for that, but this is needed to support them later.

@Rua
Rua force-pushed the expected_operand_types branch 3 times, most recently from b78f827 to d93cde7 Compare June 2, 2026 13:59
@Rua
Rua force-pushed the expected_operand_types branch 3 times, most recently from 854e317 to 7efb825 Compare June 13, 2026 15:43
@Rua Rua changed the title transpile: Split off CBinOp::expected_operand_types and handle pointers/complex numbers transpile: Refactor binary operand handling and handle pointers/complex numbers Jun 13, 2026
@Rua
Rua force-pushed the expected_operand_types branch 5 times, most recently from e62b79c to 85e05aa Compare June 19, 2026 10:32
@Rua
Rua force-pushed the expected_operand_types branch 3 times, most recently from d79be12 to fa2af73 Compare July 7, 2026 10:21

@ahomescu ahomescu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test or two for this (unit and/or snapshot)?

@Rua

Rua commented Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

Not really sure what to test for, that isn't already tested?

@Rua
Rua force-pushed the expected_operand_types branch 4 times, most recently from 46b0ce9 to 31c8fbb Compare July 26, 2026 10:18
@Rua
Rua force-pushed the expected_operand_types branch from 31c8fbb to e5c1d4e Compare August 1, 2026 15:29
@ahomescu

ahomescu commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Okay we can land this as is since it's a pretty small change, but it would be nice in the future if changes were split between 1) refactorings (no tests needed), and 2) actual functional changes, that would need a test or two to handle those specific corner cases.

Comment thread c2rust-transpile/src/c_ast/mod.rs Outdated
// See if a `Complex` version of the new result type exists.
if matches!(result_type_kind, CTypeKind::Complex(..)) {
let new_result_type_kind =
CTypeKind::Complex(result_type_id.ctype);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if result_type_id is already a Complex? Would something like double f(double _Complex a, double _Complex b) { ... a + b ... } panic here? That could be a new test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's already a Complex then it would either stay the same, or be replaced with the complex version of one of the PULLBACK_KINDS, same as happens with other numeric types right now. The code here only applies to binary operators, so it won't modify a function call.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point was: is it incorrect to create a type like CTypeKind::Complex(Complex(...))?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In C, yes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check if the following C functions cause transpiler panics?

double _Complex f(double _Complex a, double _Complex b) {
    return a * b;
}

double _Complex g(double a, double _Complex b) {
    return a * b;
}

typedef double real;

double _Complex h(double _Complex a, real b) {
    return a * b;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it does, and I see the problem now. I managed to fix the first two cases, but the third one still panics because of the typedef. No real _Complex type exists. I need to think of a way around that...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've completely rewritten that part of the code. It should be good now?

Comment thread c2rust-transpile/src/c_ast/conversion.rs
@Rua
Rua force-pushed the expected_operand_types branch from e5c1d4e to 8847410 Compare August 22, 2026 08:29
@Rua
Rua force-pushed the expected_operand_types branch from 8847410 to eddb91a Compare September 2, 2026 07:55
@Rua Rua changed the title transpile: Refactor binary operand handling and handle pointers/complex numbers transpile: Refactor binary operand handling and handle complex numbers Sep 18, 2026
@Rua
Rua force-pushed the expected_operand_types branch from eddb91a to ced080a Compare September 20, 2026 09:14
@Rua
Rua force-pushed the expected_operand_types branch 2 times, most recently from 921a686 to f5895cd Compare October 8, 2026 11:29
@Rua
Rua force-pushed the expected_operand_types branch from f5895cd to d4efdf5 Compare October 12, 2026 04:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants