Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2572e0e

Browse files
authoredAug 29, 2024
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```
2 parents 4c8c9e0 + 515f5ac commit 2572e0e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
 

‎compiler/stable_mir/src/mir/visit.rs

+16
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,22 @@ impl Location {
465465
}
466466
}
467467

468+
/// Location of the statement at the given index for a given basic block. Assumes that `stmt_idx`
469+
/// and `bb_idx` are valid for a given body.
470+
pub fn statement_location(body: &Body, bb_idx: &BasicBlockIdx, stmt_idx: usize) -> Location {
471+
let bb = &body.blocks[*bb_idx];
472+
let stmt = &bb.statements[stmt_idx];
473+
Location(stmt.span)
474+
}
475+
476+
/// Location of the terminator for a given basic block. Assumes that `bb_idx` is valid for a given
477+
/// body.
478+
pub fn terminator_location(body: &Body, bb_idx: &BasicBlockIdx) -> Location {
479+
let bb = &body.blocks[*bb_idx];
480+
let terminator = &bb.terminator;
481+
Location(terminator.span)
482+
}
483+
468484
/// Reference to a place used to represent a partial projection.
469485
pub struct PlaceRef<'a> {
470486
pub local: Local,

0 commit comments

Comments
 (0)
Failed to load comments.