Skip to content

Expose discriminant values in stable_mir #141639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

NotLebedev
Copy link
Contributor

Resolves rust-lang/project-stable-mir#93

  • Added Discr struct to stable mir as stable version of struct with same name
  • Added discriminant_for_variant method to AdtDef and CoroutineDef

@rustbot
Copy link
Collaborator

rustbot commented May 27, 2025

r? @celinval

rustbot has assigned @celinval.
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

@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 May 27, 2025
@rust-log-analyzer

This comment has been minimized.

@NotLebedev
Copy link
Contributor Author

Uh, I think the job failed due to some rate limits and needs to be restarted?

Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

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

Nice! Can you please add a test for this? Can you please make sure you cover generic and monomorphic code? Thanks!

@NotLebedev
Copy link
Contributor Author

Nice! Can you please add a test for this? Can you please make sure you cover generic and monomorphic code? Thanks!

Added a test for enum discriminant. I'm not sure how to best make tests for coroutines as I only have high level understanding of async/await not of the underlying coroutine mechanism. I would really appreciate some advice what I need to look at to create a test for CoroutineDef::discriminant_for_variant

Comment on lines 2 to 3
//! Test that users are able to use stable mir APIs to retrieve type information from a crate item
//! definition.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Could you please rewrite this doc? It doesn't match the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Forgot to change when copy-pasting. Fixed

Copy link
Contributor

@makai410 makai410 left a comment

Choose a reason for hiding this comment

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

For testing coroutine, I think we should wait for this PR to land before we can retrieve the body of a closure, since rustc would generate closures to implement state machines for async functions.

For example:

async fn simple_async() -> i32 {
    let a = async { 1 }.await;
    let b = async { 2 }.await;
    a + b
}

where the optimized mir is like:

fn simple_async() -> {async fn body of simple_async()} {
// ...
}

fn simple_async::{closure#0}(_1: Pin<&mut {async fn body of simple_async()}>, _2: &mut Context<'_>) -> Poll<i32> {
// ...
}

fn simple_async::{closure#0}::{closure#0}(_1: Pin<&mut {async block@src/lib.rs:2:13: 2:18}>, _2: &mut Context<'_>) -> Poll<i32> {
// ...
}

fn simple_async::{closure#0}::{closure#1}(_1: Pin<&mut {async block@src/lib.rs:3:13: 3:18}>, _2: &mut Context<'_>) -> Poll<i32> {
// ...
}

{closure#0}::{closure#0} and {closure#0}::{closure#1} correspond to the two async blocks.

And what we might need to visit are something like

bb0: {
    _24 = deref_copy (_1.0: &mut {async fn body of simple_async()});
    _23 = discriminant((*_24));
    switchInt(move _23) -> [0: bb1, 1: bb24, 2: bb23, 3: bb21, 4: bb22, otherwise: bb6];
}

i.e., the discriminant and switchInt operations here are used to determine which state the async state machine is in.

Btw in that case, it would be handy if we could also add a discriminants() for retrieving all discriminants, something like

/// The set of all discriminants for the coroutine, enumerated with their
/// variant indices.
#[inline]
fn discriminants(
self,
def_id: DefId,
tcx: TyCtxt<'tcx>,
) -> impl Iterator<Item = (VariantIdx, Discr<'tcx>)> {
self.variant_range(def_id, tcx).map(move |index| {
(index, Discr { val: index.as_usize() as u128, ty: self.discr_ty(tcx) })
})
}

it can be done as a follow-up though.

@oli-obk
Copy link
Contributor

oli-obk commented Jun 10, 2025

@oli-obk oli-obk closed this Jun 10, 2025
@oli-obk
Copy link
Contributor

oli-obk commented Jun 10, 2025

@bors r+ rollup

@oli-obk oli-obk reopened this Jun 10, 2025
@oli-obk
Copy link
Contributor

oli-obk commented Jun 10, 2025

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 10, 2025

📌 Commit 443e45c has been approved by oli-obk

It is now in the queue for this repository.

@bors bors 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 Jun 10, 2025
fmease added a commit to fmease/rust that referenced this pull request Jun 10, 2025
Expose discriminant values in stable_mir

Resolves rust-lang/project-stable-mir#93
* Added `Discr` struct to stable mir as stable version of struct with same name
* Added `discriminant_for_variant`  method to `AdtDef` and `CoroutineDef`
bors added a commit that referenced this pull request Jun 11, 2025
Rollup of 13 pull requests

Successful merges:

 - #128425 (Make `missing_fragment_specifier` an unconditional error)
 - #141639 (Expose discriminant values in stable_mir)
 - #141967 (Configure bootstrap backport nominations through triagebot)
 - #142042 (Make E0621 missing lifetime suggestion verbose)
 - #142176 (tests: Split dont-shuffle-bswaps along opt-levels and arches)
 - #142235 (Build rustc with assertions in `dist-alt` jobs)
 - #142248 (Add supported asm types for LoongArch32)
 - #142272 (tests: Change ABIs in tests to more future-resilient ones)
 - #142282 (Only run `citool` tests on the `auto` branch)
 - #142285 (tests: Do not run afoul of asm.validity.non-exhaustive in input-stats)
 - #142297 (Implement `//@ needs-target-std` compiletest directive)
 - #142298 (Make loongarch-none target maintainers more easily pingable)
 - #142306 (Dont unwrap and re-wrap typing envs)

Failed merges:

 - #141942 (Implement representation options to smir)

r? `@ghost`
`@rustbot` modify labels: rollup
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 11, 2025
Expose discriminant values in stable_mir

Resolves rust-lang/project-stable-mir#93
* Added `Discr` struct to stable mir as stable version of struct with same name
* Added `discriminant_for_variant`  method to `AdtDef` and `CoroutineDef`
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Jun 11, 2025
Expose discriminant values in stable_mir

Resolves rust-lang/project-stable-mir#93
* Added `Discr` struct to stable mir as stable version of struct with same name
* Added `discriminant_for_variant`  method to `AdtDef` and `CoroutineDef`
bors added a commit that referenced this pull request Jun 11, 2025
Rollup of 13 pull requests

Successful merges:

 - #134841 (Look at proc-macro attributes when encountering unknown attribute)
 - #141639 (Expose discriminant values in stable_mir)
 - #141967 (Configure bootstrap backport nominations through triagebot)
 - #142042 (Make E0621 missing lifetime suggestion verbose)
 - #142176 (tests: Split dont-shuffle-bswaps along opt-levels and arches)
 - #142248 (Add supported asm types for LoongArch32)
 - #142272 (tests: Change ABIs in tests to more future-resilient ones)
 - #142282 (Only run `citool` tests on the `auto` branch)
 - #142297 (Implement `//@ needs-target-std` compiletest directive)
 - #142298 (Make loongarch-none target maintainers more easily pingable)
 - #142306 (Dont unwrap and re-wrap typing envs)
 - #142324 (Remove unneeded `FunctionCx` from some codegen methods)
 - #142328 (feat: Add `bit_width` for unsigned integer types)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 11, 2025
Expose discriminant values in stable_mir

Resolves rust-lang/project-stable-mir#93
* Added `Discr` struct to stable mir as stable version of struct with same name
* Added `discriminant_for_variant`  method to `AdtDef` and `CoroutineDef`
@bors
Copy link
Collaborator

bors commented Jun 11, 2025

☔ The latest upstream changes (presumably #141942) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 11, 2025
bors added a commit that referenced this pull request Jun 11, 2025
Rollup of 9 pull requests

Successful merges:

 - #141967 (Configure bootstrap backport nominations through triagebot)
 - #142042 (Make E0621 missing lifetime suggestion verbose)
 - #142272 (tests: Change ABIs in tests to more future-resilient ones)
 - #142282 (Only run `citool` tests on the `auto` branch)
 - #142297 (Implement `//@ needs-target-std` compiletest directive)
 - #142298 (Make loongarch-none target maintainers more easily pingable)
 - #142306 (Dont unwrap and re-wrap typing envs)
 - #142324 (Remove unneeded `FunctionCx` from some codegen methods)
 - #142328 (feat: Add `bit_width` for unsigned integer types)

Failed merges:

 - #141639 (Expose discriminant values in stable_mir)

r? `@ghost`
`@rustbot` modify labels: rollup
github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Jun 12, 2025
Rollup of 9 pull requests

Successful merges:

 - rust-lang/rust#141967 (Configure bootstrap backport nominations through triagebot)
 - rust-lang/rust#142042 (Make E0621 missing lifetime suggestion verbose)
 - rust-lang/rust#142272 (tests: Change ABIs in tests to more future-resilient ones)
 - rust-lang/rust#142282 (Only run `citool` tests on the `auto` branch)
 - rust-lang/rust#142297 (Implement `//@ needs-target-std` compiletest directive)
 - rust-lang/rust#142298 (Make loongarch-none target maintainers more easily pingable)
 - rust-lang/rust#142306 (Dont unwrap and re-wrap typing envs)
 - rust-lang/rust#142324 (Remove unneeded `FunctionCx` from some codegen methods)
 - rust-lang/rust#142328 (feat: Add `bit_width` for unsigned integer types)

Failed merges:

 - rust-lang/rust#141639 (Expose discriminant values in stable_mir)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose discriminant values in stable_mir
7 participants