-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
Uh, I think the job failed due to some rate limits and needs to be restarted? |
There was a problem hiding this 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!
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 |
//! Test that users are able to use stable mir APIs to retrieve type information from a crate item | ||
//! definition. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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
rust/compiler/rustc_middle/src/ty/sty.rs
Lines 98 to 109 in 14863ea
/// 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.
@bors r+ rollup |
@bors r+ rollup |
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`
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
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`
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`
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
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`
☔ The latest upstream changes (presumably #141942) made this pull request unmergeable. Please resolve the merge conflicts. |
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
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
Resolves rust-lang/project-stable-mir#93
Discr
struct to stable mir as stable version of struct with same namediscriminant_for_variant
method toAdtDef
andCoroutineDef