Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an ability to convert between Span and visit::Location #129170

Merged
merged 2 commits into from
Aug 29, 2024

Conversation

artemagvanian
Copy link
Contributor

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 forFrom<Span> for Location to fix this.

r? @celinval

@rustbot
Copy link
Collaborator

rustbot commented Aug 16, 2024

This PR changes Stable MIR

cc @oli-obk, @celinval, @ouz-a

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 16, 2024
@celinval
Copy link
Contributor

celinval commented Aug 20, 2024

Sorry for the delay. I'm not sure we want to necessary convert Span to Location. Just to give a bit of background, the Location was basically added as a placeholder for us to expand later on based on the emerging use cases. So we decided to keep things opaque.

Span is not as precise as we may want the location to be.

The compiler MIR Visitor, for example, is a combination of the index of the basic block and the index of the instruction within the basic block [1].

I would be more comfortable creating an interface that allows you to create a Location, such as:

pub fn new(body: &Body, bb: BasicBlockIdx, idx: usize) -> Location {}

Maybe you can provide a bit more background on your use case and see if that would work for you.

@artemagvanian
Copy link
Contributor Author

Thanks for the detailed explanation!

I am working on an MIR transformation in Stable MIR, which could be significantly simplified if we traversed the basic block in the reverse direction. However, the only option to achieve that seems to be to implement visit_statement and visit_terminator methods and then call them manually while iterating over the statements in reverse. Not being able to create Location from Span or otherwise prevents calling those methods directly.

Currently, we were able to circumvent this by visiting the basic block to collect instructions that need instrumentation and then applying the instrumentation in reverse order. However, it seems that being able to call visit_statement and visit_terminator directly is something worth implementing at some point.

@celinval
Copy link
Contributor

celinval commented Aug 22, 2024

Would my suggestion work in this case? Maybe we can add a few methods:

/// Location of the first instruction of the BB
fn basic_block_location(body: &Body, bb: &BasicBlockIdx);
/// Location of the statement at the given index
fn statement_location(body: &Body, bb: &BasicBlockIdx, stmt_indx: usize);
/// Location of the terminator 
fn terminator_location(body: &Body, bb: &BasicBlockIdx);

Feel free to tweak with the idea / naming.

@artemagvanian
Copy link
Contributor Author

I think it would. I believe Location is only well-defined for the latter two methods as of now. I will make the changes to the PR.

@celinval
Copy link
Contributor

Awesome! Thanks

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Aug 28, 2024

📌 Commit 515f5ac has been approved by celinval

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 28, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 28, 2024
…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`
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 28, 2024
…iaskrgr

Rollup of 11 pull requests

Successful merges:

 - rust-lang#128166 (Improved `checked_isqrt` and `isqrt` methods)
 - rust-lang#129170 (Add an ability to convert between `Span` and `visit::Location`)
 - rust-lang#129366 (linker: Synchronize native library search in rustc and linker)
 - rust-lang#129467 (derive(SmartPointer): assume pointee from the single generic and better error messages)
 - rust-lang#129494 (format code in tests/ui/threads-sendsync)
 - rust-lang#129527 (Don't use `TyKind` in a lint)
 - rust-lang#129617 (Update books)
 - rust-lang#129673 (Add fmt::Debug to sync::Weak<T, A>)
 - rust-lang#129683 (copysign with sign being a NaN can have non-portable results)
 - rust-lang#129689 (Move `'tcx` lifetime off of impl and onto methods for `CrateMetadataRef`)
 - rust-lang#129695 (Fix path to run clippy on rustdoc)

r? `@ghost`
`@rustbot` modify labels: rollup
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Aug 29, 2024
…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``
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 29, 2024
…kingjubilee

Rollup of 14 pull requests

Successful merges:

 - rust-lang#128192 (rustc_target: Add various aarch64 features)
 - rust-lang#129170 (Add an ability to convert between `Span` and `visit::Location`)
 - rust-lang#129343 (Emit specific message for time<=0.3.35)
 - rust-lang#129378 (Clean up cfg-gating of ProcessPrng extern)
 - rust-lang#129401 (Partially stabilize `feature(new_uninit)`)
 - rust-lang#129467 (derive(SmartPointer): assume pointee from the single generic and better error messages)
 - rust-lang#129494 (format code in tests/ui/threads-sendsync)
 - rust-lang#129617 (Update books)
 - rust-lang#129673 (Add fmt::Debug to sync::Weak<T, A>)
 - rust-lang#129683 (copysign with sign being a NaN can have non-portable results)
 - rust-lang#129689 (Move `'tcx` lifetime off of impl and onto methods for `CrateMetadataRef`)
 - rust-lang#129695 (Fix path to run clippy on rustdoc)
 - rust-lang#129712 (Correct trusty targets to be tier 3)
 - rust-lang#129715 (Update `compiler_builtins` to `0.1.123`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 2572e0e into rust-lang:master Aug 29, 2024
6 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Aug 29, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 29, 2024
Rollup merge of rust-lang#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```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants