# Source code locations from MIR

**URL:** <https://internals.rust-lang.org/t/source-code-locations-from-mir/17046>\
**Category:** compiler\
**Created:** [July 20, 2022, 2:26pm UTC](https://internals.rust-lang.org/t/source-code-locations-from-mir/17046 "2022-07-20T14:26:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dstephens](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@dstephens](https://internals.rust-lang.org/u/dstephens)\
**Post date:** [July 20, 2022, 2:26pm UTC](https://internals.rust-lang.org/t/source-code-locations-from-mir/17046/1 "2022-07-20T14:26:41Z")

</div>

I am working on a project to mark the source code of simple Rust programs with the Live, Dead, borrow, move, drop locations of each local. I believe this information is available in the MIR but I am struggling to extract the source code locations. For each function, I iterate through the local\_decls and get the declaration location from span. However, the associated locations for live, borrow, move, and drop are basic block locations. The source code locations are included in the playground mir but not part of the body returned from optimized\_mir. How do I get the source code location mapping of StorageLive, StorageDead, move and drop from the function body returned from optimized\_mir()? I am new to Rust so apologize if I missed something obvious. Any suggestions, examples, links, repos are welcome and appreciated.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [July 20, 2022, 2:59pm UTC](https://internals.rust-lang.org/t/source-code-locations-from-mir/17046/2 "2022-07-20T14:59:07Z")

</div>

> [@dstephens](#):
>
> How do I get the source code location mapping of StorageLive, StorageDead, move and drop from the function body returned from optimized\_mir()?

For `StorageLive`, `StorageDead` and moves the `mir::Statement` type has a `source_info` field which in turn has a `span` field representing the location of the statement. As for `Drop`, this is a MIR terminator and as such you have a `mir::Terminator`, which also has a `source_info` field.

---

<div class="post-metadata">

**Author:** ![vakaras](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/vakaras/32/2156_2.png) [@vakaras](https://internals.rust-lang.org/u/vakaras)\
**Post date:** [July 20, 2022, 7:16pm UTC](https://internals.rust-lang.org/t/source-code-locations-from-mir/17046/3 "2022-07-20T19:16:10Z")

</div>

[GitHub - willcrichton/flowistry: Flowistry is an IDE plugin for Rust that helps you focus on relevant code.](https://github.com/willcrichton/flowistry) is likely to have code snippets you need.

---

<div class="post-metadata">

**Author:** ![eddyb](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/eddyb/32/8411_2.png) [@eddyb](https://internals.rust-lang.org/u/eddyb)\
**Post date:** [July 21, 2022, 1:32am UTC](https://internals.rust-lang.org/t/source-code-locations-from-mir/17046/4 "2022-07-21T01:32:03Z")

</div>

> [@dstephens](#):
>
> The source code locations are included in the playground mir but not part of the body returned from optimized\_mir.

It is there (via `SourceInfo`), as @bjorn3 pointed out.

The playground uses `--emit=mir` which pretty-prints `optimized_mir` - if you see something in textual MIR, it means it's available to the compiler.

---

<div class="post-metadata">

**Author:** ![dstephens](https://avatars.discourse-cdn.com/v4/letter/d/7bcc69/32.png) [@dstephens](https://internals.rust-lang.org/u/dstephens)\
**Post date:** [July 21, 2022, 7:37pm UTC](https://internals.rust-lang.org/t/source-code-locations-from-mir/17046/5 "2022-07-21T19:37:18Z")

</div>

Awesome. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 19, 2022, 7:37pm UTC](https://internals.rust-lang.org/t/source-code-locations-from-mir/17046/6 "2022-10-19T19:37:51Z")

</div>

Closed on October 19, 2022, 7:37pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
