From 8bc120af8286caae68742f8d9943681c5691effb Mon Sep 17 00:00:00 2001 From: Artem Agvanian Date: Fri, 16 Aug 2024 14:28:06 -0700 Subject: [PATCH 1/2] Add an ability to convert between `Span` and `visit::Location` --- compiler/stable_mir/src/mir/visit.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/compiler/stable_mir/src/mir/visit.rs b/compiler/stable_mir/src/mir/visit.rs index 50d7bae21db..f391a0f444a 100644 --- a/compiler/stable_mir/src/mir/visit.rs +++ b/compiler/stable_mir/src/mir/visit.rs @@ -465,6 +465,12 @@ pub fn span(&self) -> Span { } } +impl From for Location { + fn from(span: Span) -> Self { + Location(span) + } +} + /// Reference to a place used to represent a partial projection. pub struct PlaceRef<'a> { pub local: Local, From 515f5acefeed7880bd69d6ac5b5b222b48a283d9 Mon Sep 17 00:00:00 2001 From: Artem Agvanian Date: Fri, 23 Aug 2024 12:45:38 -0400 Subject: [PATCH 2/2] Introduce methods for obtaining `Location` for statements and terminators --- compiler/stable_mir/src/mir/visit.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/compiler/stable_mir/src/mir/visit.rs b/compiler/stable_mir/src/mir/visit.rs index f391a0f444a..aeae866e9d3 100644 --- a/compiler/stable_mir/src/mir/visit.rs +++ b/compiler/stable_mir/src/mir/visit.rs @@ -465,10 +465,20 @@ pub fn span(&self) -> Span { } } -impl From for Location { - fn from(span: Span) -> Self { - Location(span) - } +/// 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.