Skip to content

Support creating float constants in rustc_public mir#159472

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
hkalbasi:rustc_public_float
Jul 21, 2026
Merged

Support creating float constants in rustc_public mir#159472
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
hkalbasi:rustc_public_float

Conversation

@hkalbasi

Copy link
Copy Markdown
Member

This PR adds a try_from_float similar to try_from_uint to the rustc_public::ty::MirConst.

It takes u128 and users need a .to_bits() as u128 to call this function. I did this because f64 couldn't capture a f128 const completely, and f128 needs to cast and a branch on the input types, making the code complex. Another alternative is adding 4 functions try_from_f16 to try_from_f128, which I don't like, but that's also an option.

@rustbot

rustbot commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

@rustbot rustbot added 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. labels Jul 17, 2026
@rustbot

rustbot commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

r? @oli-obk

rustbot has assigned @oli-obk.
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: project-rustc-public
  • project-rustc-public expanded to celinval, makai410, oli-obk, scottmcm
  • Random selection from celinval, makai410, oli-obk, scottmcm

@celinval celinval 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.

Thank you for the contribution! Do you mind adding or extending an existing test for the new function? Thanks!

View changes since this review

})
}

/// Create a new constant that represents the given value.

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.

Do you mind improving this doc a bit? I think we should state that the value is the binary representation of the float constant.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tried to improve the docs.

let mut tables = self.tables.borrow_mut();
let cx = &*self.cx.borrow();
let ty = cx.new_rigid_ty(RigidTy::Float(float_ty).internal(&mut *tables, cx.tcx));
cx.try_new_const_uint(value, ty).map(|cnst| cnst.stable(&mut *tables, cx))

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.

I think it might be worth creating a bridge for creating a float constant. This function eventually creates a Scalar::Integer. In practice it may not matter, but it's fragile.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I had such a function in my original patch, then I found out that it is identical to try_new_const_uint. I added some comment to explain uint usage is not a mistake here, but I can add a distinct function in the bridge or rename the existing one if it is desired.

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.

Sorry, I read this wrong. I think this is fine as is. Thanks

@celinval

Copy link
Copy Markdown
Contributor

r? @celinval

@rustbot rustbot assigned celinval and unassigned oli-obk Jul 19, 2026
@hkalbasi
hkalbasi force-pushed the rustc_public_float branch from 6872df7 to c4bda51 Compare July 20, 2026 18:18
@hkalbasi

Copy link
Copy Markdown
Member Author

Do you mind adding or extending an existing test for the new function?

IIUC there is no test for the sibling try_from_uint and from_bool. If everything goes well, I have plans to add some generative capabilities (in addition to the current analytical capabilities) to the rustc_public (I need to discuss it with your team to see if it is in the scope and for details) and with that API we could write a meaningful test that covers all these APIs.

For now, I'm focused on upstreaming what looks uncontroversial and symmetric to the rest of the codes, and internalize the rest of my patch, so that my code becomes buildable on nightly rustc. I can add a simple test, but I think a real test for this function and its siblings needs a generative API and we can defer the test if/when rustc_public gains generative capabilities (without generative capabilities all of these functions can be removed or become private).

@celinval celinval 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.

Interesting, I'm curious to hear more about it. Feel free to start a thread in our zulip channel #project-rustc-public or create an issue on the project repo.

I'd still prefer seeing this tested even in a follow up PR. So we can merge this as is, but can you please add tests in your next PR? I understand that similar functions currently don't have any test, but I don't want to keep making the situation worse..

Thanks

@bors r+ rollup

View changes since this review

@rust-bors

rust-bors Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

📌 Commit c4bda51 has been approved by celinval

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 20, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 21, 2026
Rollup of 14 pull requests

Successful merges:

 - #159307 (Improve cross-namespace name diagnostics)
 - #159543 (Remove extra semicolons in parsing item lists)
 - #157270 (ergonomic_clones_dotuse_capture_by_ref: Capture upvar by ref for `.use` in non-move closures)
 - #158496 (Move `check_rustc_pub_transparent` into the attribute parser)
 - #158547 (Move `std::io::buffered` to `alloc::io`)
 - #158808 (Filter host libstdc++ ABI flag in rustc_llvm cross builds)
 - #159362 (Add regression test for #120328)
 - #159472 (Support creating float constants in rustc_public mir)
 - #159505 (make rustdoc::bare_urls strip trailing periods from url)
 - #159568 (Suggest close compiler options)
 - #159578 (Extract coroutine closure helper functions)
 - #159601 (Make `TokenTreeCursor` private)
 - #159613 (Set the rustc lib path for unstable-book-gen)
 - #159616 (Clarify the comment about stage1/stage2 discrepancy in input-stats test)
@rust-bors
rust-bors Bot merged commit f651441 into rust-lang:main Jul 21, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 21, 2026
rust-timer added a commit that referenced this pull request Jul 21, 2026
Rollup merge of #159472 - hkalbasi:rustc_public_float, r=celinval

Support creating float constants in rustc_public mir

This PR adds a `try_from_float` similar to `try_from_uint` to the `rustc_public::ty::MirConst`.

It takes `u128` and users need a `.to_bits() as u128` to call this function. I did this because `f64` couldn't capture a `f128` const completely, and `f128` needs to cast and a branch on the input types, making the code complex. Another alternative is adding 4 functions `try_from_f16` to `try_from_f128`, which I don't like, but that's also an option.
pull Bot pushed a commit to xtqqczze/rust-lang-miri that referenced this pull request Jul 22, 2026
Rollup of 14 pull requests

Successful merges:

 - rust-lang/rust#159307 (Improve cross-namespace name diagnostics)
 - rust-lang/rust#159543 (Remove extra semicolons in parsing item lists)
 - rust-lang/rust#157270 (ergonomic_clones_dotuse_capture_by_ref: Capture upvar by ref for `.use` in non-move closures)
 - rust-lang/rust#158496 (Move `check_rustc_pub_transparent` into the attribute parser)
 - rust-lang/rust#158547 (Move `std::io::buffered` to `alloc::io`)
 - rust-lang/rust#158808 (Filter host libstdc++ ABI flag in rustc_llvm cross builds)
 - rust-lang/rust#159362 (Add regression test for rust-lang/rust#120328)
 - rust-lang/rust#159472 (Support creating float constants in rustc_public mir)
 - rust-lang/rust#159505 (make rustdoc::bare_urls strip trailing periods from url)
 - rust-lang/rust#159568 (Suggest close compiler options)
 - rust-lang/rust#159578 (Extract coroutine closure helper functions)
 - rust-lang/rust#159601 (Make `TokenTreeCursor` private)
 - rust-lang/rust#159613 (Set the rustc lib path for unstable-book-gen)
 - rust-lang/rust#159616 (Clarify the comment about stage1/stage2 discrepancy in input-stats test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants