Skip to content

Change Location<'_> lifetime to 'static in Panic[Hook]Info - #146561

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
ijchen:panic_location_static
Jun 28, 2026
Merged

Change Location<'_> lifetime to 'static in Panic[Hook]Info#146561
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
ijchen:panic_location_static

Conversation

@ijchen

@ijchen ijchen commented Sep 14, 2025

Copy link
Copy Markdown
Contributor

View all comments

I'm writing a library that would benefit from being able to store a &'static str instead of a String for the file location of a panic. Currently, the API of std::panic::PanicHookInfo::location and core::panic::PanicInfo::location return a Location<'_> (and by extension, a file name &str) whose lifetime is tied to the borrow of the Panic[Hook]Info.

It is my understanding that the Location/&str will always be Location<'static>/&'static str, since the file name is embedded directly into the compiled binary (and I don't see a likely reason/way for that to ever change in the future). Since it seems unlikely to ever need to change, and since making the guarantee that the returned lifetime is 'static has a real benefit (allows users to avoid unnecessary allocations), I think changing to the returned Location<'_>'s lifetime to 'static would be a worthwhile change.

see also the recent #1320870, which made a similar change to std::panic::Location

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 14, 2025
@rustbot

rustbot commented Sep 14, 2025

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@jieyouxu jieyouxu added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Sep 15, 2025
@m-ou-se m-ou-se added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 15, 2025
@Amanieu

Amanieu commented Sep 16, 2025

Copy link
Copy Markdown
Member

Considering that the only way to construct a location is Location::caller which returns a &'static Location<'static> (this is used internally by the panic macros and compiler-generated panics), we could make these methods return a &'static Location<'static> as well. I don't see any value in a non-'static version of this unless we plan to allow constructing custom locations (which seems unlikely).

@ijchen
ijchen force-pushed the panic_location_static branch from 4d90b34 to e3b8df1 Compare September 16, 2025 21:49
@rustbot

rustbot commented Sep 16, 2025

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@ijchen

ijchen commented Sep 16, 2025

Copy link
Copy Markdown
Contributor Author

That's a great idea! I've updated accordingly.

@Amanieu

Amanieu commented Sep 17, 2025

Copy link
Copy Markdown
Member

@rfcbot merge

@rust-rfcbot

rust-rfcbot commented Sep 17, 2025

Copy link
Copy Markdown
Collaborator

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@Amanieu Amanieu removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Sep 17, 2025
@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 17, 2025
@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Sep 28, 2025
@Amanieu

Amanieu commented Oct 4, 2025

Copy link
Copy Markdown
Member

Restarting FCP to reflect updated team membership.

@rfcbot cancel
@rfcbot merge

@rust-rfcbot

Copy link
Copy Markdown
Collaborator

@Amanieu proposal cancelled.

@rust-rfcbot rust-rfcbot removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 4, 2025
@rust-rfcbot

rust-rfcbot commented Oct 4, 2025

Copy link
Copy Markdown
Collaborator

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Oct 4, 2025
@ijchen

ijchen commented Oct 19, 2025

Copy link
Copy Markdown
Contributor Author

Friendly ping for checkboxes here, in case this slipped under the radar: @BurntSushi @joshtriplett @the8472

@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Jan 29, 2026
@hanna-kruppe

Copy link
Copy Markdown
Contributor

I found this PR after also writing some code that could've made good use of getting a &'static Location in a panic hook. It would be great to get this un-stuck somehow. Some thoughts:

If there's appetite for having an accessor that returns a Cow or morally similar enum (e.g., None | &'a Location | &'static Location), this would have to be a new method. Changing the return type of the existing location() method would be very breaking. In that case, location() could be redefined to mean "the &'static one, if it exists" (it already reserves the right to return None). This isn't great since existing code that uses location() may sometimes miss out on location info that's available (just not 'static), and would have to be updated to a different API. But in principle, with some renames and deprecations, it's may not end up too different from e.g. payload() vs payload_as_str().

More fundamentally: it's not clear to me why "no &'static Location but std can create one dynamically" is something that's useful enough to block useful improvements to Panic[Hook]Info. I get the use case for opting out of the static Location data per panic location, that could be a measurable binary size benefit. Most projects probably won't want the trade-off w.r.t. ability to debug crashes remotely, but it's not bad to have the option. (Though I'll note that so far nobody was motivated to implement this opt-out.) That's no problem for this PR, as location() already reserves the right to return None. The question is just, are there use cases where you'd want to strip Locations from the binary and still can and want to get some panic location information?

If you want that binary size benefit so badly, you're not going to ship debug info with the binary, you'll use split debug info, not ship the debug info with the binary, and do symbolication of crashes out-of-process (often on a different machine entirely). You may not even want to pay the cost of embedding symbolication code in the binary (cc #139209). So it seems unlikely that a binary built with the "panic Location opt-out" flag will be able to actually recover any locations. I only see two cases where the debug info would nevertheless be available, but neither seems convincing to me:

  • The developers are debugging a local build of the binary and still have the split debug info lying around on disk for std to find it. But in this scenario they can also (re-)build in a configuration that's better for debugging and includes static Locations.
  • You got an optimized binary and then separately fetch debug info for it from a symbol server. This is useful for debuggers and profilers, but generally done out-of-process. It seems very unlikely to me that Rust's standard library would want to include all the complexity to fetch debug info this way, especially in the use cases where binary size is important enough that dropping Locations is interesting.

@Amanieu Amanieu added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label May 17, 2026
@Amanieu Amanieu added the relnotes Marks issues that should be documented in the release notes of the next release. label May 26, 2026
@Amanieu

Amanieu commented May 26, 2026

Copy link
Copy Markdown
Member

This was discussed in the @rust-lang/libs-api meeting. We stand by our previous decision to stabilize this change, essentially agreeing with @hanna-kruppe's assessment.

@bors r+

@rust-bors

rust-bors Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

📋 This PR cannot be approved because it currently has the following label: S-waiting-on-fcp.

@Amanieu Amanieu removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label May 26, 2026
@ijchen

ijchen commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

I believe this is now just waiting on code review, correct?

@ijchen

ijchen commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

pinging @Mark-Simulacrum as the current assignee - let me know if this is waiting on me for something, I'm new to the process.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. labels Jun 24, 2026
@Mark-Simulacrum

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

📌 Commit e3b8df1 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 Jun 28, 2026
rust-bors Bot pushed a commit that referenced this pull request Jun 28, 2026
…uwer

Rollup of 2 pull requests

Successful merges:

 - #156419 (Update itertools to 0.15)
 - #146561 (Change `Location<'_>` lifetime to `'static` in `Panic[Hook]Info`)
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jun 28, 2026
Change `Location<'_>` lifetime to `'static` in `Panic[Hook]Info`



I'm writing a library that would benefit from being able to store a `&'static str` instead of a `String` for the file location of a panic. Currently, the API of [`std::panic::PanicHookInfo::location`](https://doc.rust-lang.org/nightly/std/panic/struct.PanicHookInfo.html#method.location) and [`core::panic::PanicInfo::location`](https://doc.rust-lang.org/nightly/core/panic/struct.PanicInfo.html#method.location) return a `Location<'_>` (and by extension, a file name `&str`) whose lifetime is tied to the borrow of the `Panic[Hook]Info`.

It is my understanding that the `Location`/`&str` will always be `Location<'static>`/`&'static str`, since the file name is embedded directly into the compiled binary (and I don't see a likely reason/way for that to ever change in the future). Since it seems unlikely to ever need to change, and since making the guarantee that the returned lifetime is `'static` has a real benefit (allows users to avoid unnecessary allocations), I think changing to the returned `Location<'_>`'s lifetime to `'static` would be a worthwhile change.

see also the recent [#1320870](#132087), which made a similar change to [`std::panic::Location`](https://doc.rust-lang.org/nightly/std/panic/struct.Location.html)
@rust-bors rust-bors Bot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 28, 2026
@rust-bors

rust-bors Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

💔 Test for d77ab4f failed: CI. Failed job:

rust-bors Bot pushed a commit that referenced this pull request Jun 28, 2026
…uwer

Rollup of 2 pull requests

Successful merges:

 - #156419 (Update itertools to 0.15)
 - #146561 (Change `Location<'_>` lifetime to `'static` in `Panic[Hook]Info`)
@JonathanBrouwer

Copy link
Copy Markdown
Contributor

@bors retry

@rust-bors rust-bors Bot 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 Jun 28, 2026
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

A job failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

rust-timer added a commit that referenced this pull request Jun 28, 2026
Rollup merge of #146561 - ijchen:panic_location_static, r=Mark-Simulacrum

Change `Location<'_>` lifetime to `'static` in `Panic[Hook]Info`

I'm writing a library that would benefit from being able to store a `&'static str` instead of a `String` for the file location of a panic. Currently, the API of [`std::panic::PanicHookInfo::location`](https://doc.rust-lang.org/nightly/std/panic/struct.PanicHookInfo.html#method.location) and [`core::panic::PanicInfo::location`](https://doc.rust-lang.org/nightly/core/panic/struct.PanicInfo.html#method.location) return a `Location<'_>` (and by extension, a file name `&str`) whose lifetime is tied to the borrow of the `Panic[Hook]Info`.

It is my understanding that the `Location`/`&str` will always be `Location<'static>`/`&'static str`, since the file name is embedded directly into the compiled binary (and I don't see a likely reason/way for that to ever change in the future). Since it seems unlikely to ever need to change, and since making the guarantee that the returned lifetime is `'static` has a real benefit (allows users to avoid unnecessary allocations), I think changing to the returned `Location<'_>`'s lifetime to `'static` would be a worthwhile change.

see also the recent [#1320870](#132087), which made a similar change to [`std::panic::Location`](https://doc.rust-lang.org/nightly/std/panic/struct.Location.html)
@rust-bors
rust-bors Bot merged commit 6b1bbf9 into rust-lang:main Jun 28, 2026
10 of 11 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.