Skip to content

Implement complex numbers and basic arithmetic#154040

Open
scimind2460 wants to merge 1 commit intorust-lang:mainfrom
scimind2460:complex-numbers
Open

Implement complex numbers and basic arithmetic#154040
scimind2460 wants to merge 1 commit intorust-lang:mainfrom
scimind2460:complex-numbers

Conversation

@scimind2460
Copy link
Contributor

@scimind2460 scimind2460 commented Mar 18, 2026

Related to #154023
This PR adds complex numbers as a lang item behind a libs feature gate and adds simple implementations for arithmetic.

@rustbot
Copy link
Collaborator

rustbot commented Mar 18, 2026

Some changes occurred in src/tools/cargo

cc @ehuss

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 18, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 18, 2026

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, joboet, scottmcm

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

Choose a reason for hiding this comment

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

add a new line on the end of this file

Copy link
Member

Choose a reason for hiding this comment

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

and this

@tgross35
Copy link
Contributor

tgross35 commented Mar 18, 2026

I'd recommend that the first PR only add Complex and do the compiler ABI portion. Do math in a followup.

A test in tests/codegen-llvm is needed for sure, also in tests/assembly-llvm would be ideal.

Or only do addition+subtraction first, add compiler support later

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@scimind2460 scimind2460 force-pushed the complex-numbers branch 2 times, most recently from 23eb6b8 to 7a44277 Compare March 18, 2026 12:35
@scimind2460
Copy link
Contributor Author

Or only do addition+subtraction first, add compiler support later

I'll do that. I'm not entirely sure how to add support for calling conventions that exactly match up with _Complex.

@rust-log-analyzer

This comment has been minimized.

@bjorn3
Copy link
Member

bjorn3 commented Mar 18, 2026

You can probably make it a lang item and check for it in the target specific call conv impls of rustc_target::callconv and handle it as necessary. Also make sure to annotate it with #[repr(C)] to ensure the correct memory layout. And finally is it intentional that Complex accepts arbitrary types rather than just float types? Presumably there is no standard ABI for non-float types here.

@scimind2460
Copy link
Contributor Author

You can probably make it a lang item and check for it in the target specific call conv impls of rustc_target::callconv and handle it as necessary. Also make sure to annotate it with #[repr(C)] to ensure the correct memory layout. And finally is it intentional that Complex accepts arbitrary types rather than just float types? Presumably there is no standard ABI for non-float types here.

Thanks for the tip @bjorn3! I was planning to go with the lang item approach and #[repr(c)]. It is intentional as AFAIK libs didn't want the overhead of a full trait, and I'm not sure how we could implement it for non-float types.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job tidy failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

/// This promises to be equivalent to the C type on each platform.
#[unstable(feature = "complex_numbers", issue = "154023")]
#[derive(Copy, Clone)]
pub struct Complex<T: Copy> {
Copy link
Member

Choose a reason for hiding this comment

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

It's extremely rare that we have bounds on types in the library. Why does this have the bound rather than just being

Suggested change
pub struct Complex<T: Copy> {
pub struct Complex<T> {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because it made it a bit easier to implement the functions themselves rather than think of whether they wilp be consumed or taken by reference (given that floats are Copy)

Comment on lines +14 to +18
impl<T> Complex<T> {
pub fn new(re: T, im: T) -> Self {
Self { re, im }
}
}
Copy link
Member

Choose a reason for hiding this comment

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

notably, this can't work as written with the bound on the type

@scottmcm scottmcm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 18, 2026
@scottmcm
Copy link
Member

I would suggest splitting the PRs to not do too much in one. For example, start with just adding a library type with no compiler changes. You can do another PR for lang items and such later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants