Skip to content

Commit

Permalink
Rollup merge of #129170 - artemagvanian:span-to-location, r=celinval
Browse files Browse the repository at this point in the history
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```
  • Loading branch information
workingjubilee committed Aug 29, 2024
2 parents 4c8c9e0 + 515f5ac commit 2572e0e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions compiler/stable_mir/src/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2572e0e

Please sign in to comment.