diff options
| author | Jubilee <workingjubilee@gmail.com> | 2024-08-28 19:12:50 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-28 19:12:50 -0700 |
| commit | 2572e0e8c9d5d671eccb1d5791e55c929c4720f4 (patch) | |
| tree | 98f6f39fc4245b4a356bc1355c4c0d097f25b326 | |
| parent | 4c8c9e092d006aae7fecc93c432fe3bce1f46d04 (diff) | |
| parent | 515f5acefeed7880bd69d6ac5b5b222b48a283d9 (diff) | |
| download | rust-2572e0e8c9d5d671eccb1d5791e55c929c4720f4.tar.gz rust-2572e0e8c9d5d671eccb1d5791e55c929c4720f4.zip | |
Rollup merge of #129170 - artemagvanian:span-to-location, r=celinval
Add an ability to convert between `Span` and `visit::Location` AFAIK, there is no way to create a `Location` from a `Span` because its only field is private. This makes it impossible to use visitor methods like `visit_statement` or `visit_terminator`. This PR adds an implementation for`From<Span>` for `Location` to fix this. r? ```@celinval```
| -rw-r--r-- | compiler/stable_mir/src/mir/visit.rs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/compiler/stable_mir/src/mir/visit.rs b/compiler/stable_mir/src/mir/visit.rs index 50d7bae21db..aeae866e9d3 100644 --- a/compiler/stable_mir/src/mir/visit.rs +++ b/compiler/stable_mir/src/mir/visit.rs @@ -465,6 +465,22 @@ impl Location { } } +/// Location of the statement at the given index for a given basic block. Assumes that `stmt_idx` +/// and `bb_idx` are valid for a given body. +pub fn statement_location(body: &Body, bb_idx: &BasicBlockIdx, stmt_idx: usize) -> Location { + let bb = &body.blocks[*bb_idx]; + let stmt = &bb.statements[stmt_idx]; + Location(stmt.span) +} + +/// Location of the terminator for a given basic block. Assumes that `bb_idx` is valid for a given +/// body. +pub fn terminator_location(body: &Body, bb_idx: &BasicBlockIdx) -> Location { + let bb = &body.blocks[*bb_idx]; + let terminator = &bb.terminator; + Location(terminator.span) +} + /// Reference to a place used to represent a partial projection. pub struct PlaceRef<'a> { pub local: Local, |
