Skip to content

panic::Location Debug implementation now prints a byte array #142279

Closed

Description

@tgross35

Since #135054, the Debug implementation for std::panic::Location just prints the byte bag:

fn main() {
    dbg!(std::panic::Location::caller());
}

Stable:

[src/main.rs:2:5] std::panic::Location::caller() = Location {
    file: "src/main.rs",
    line: 2,
    col: 10,
}

Nightly:

[src/main.rs:2:5] std::panic::Location::caller() = Location {
    file_bytes_with_nul: [
        115,
        114,
        99,
        47,
        109,
        97,
        105,
        110,
        46,
        114,
        115,
        0,
    ],
    line: 2,
    col: 10,
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=698066c5427dd43cb12df0d2fb5b9300

Originally noticed at #142216.

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jun 10, 2025
  2. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jun 10, 2025
  3. tgross35 commented on Jun 10, 2025

    @tgross35
    MemberAuthor

    @cramertj is there a reason file_bytes_with_nul wasn't made a CStr in #135054? Other than .file() becoming O(n) once it becomes a thin pointer.

    Might just need a handwritten debug implementation otherwise.

  4. tgross35 commented on Jun 10, 2025

    @tgross35
    MemberAuthor

    Cc @m-ou-se from the review ^

  5. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jun 11, 2025
  6. added
    P-highHigh priority
    and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jun 11, 2025
  7. added
    C-bugCategory: This is a bug.
    and removed
    C-bugCategory: This is a bug.
    on Jun 11, 2025
  8. self-assigned this
    on Jun 11, 2025
  9. nealsid commented on Jun 12, 2025

    @nealsid
    Contributor

    FYI, I was continuing to work on #142216 and wanted to make proposals based on after this change, so I merged it in, and, this might be a small issue but I noticed that backslashes in the path are printed twice on Windows (this has debug_refcell enabled in library/core):

    PS C:\Users\neals\refcell-test> cargo +stage1 clean ; cargo +stage1 run
         Removed 28 files, 4.4MiB total
       Compiling refcell-test v0.1.0 (C:\Users\neals\refcell-test)
        Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.39s
         Running `target\debug\refcell-test.exe`
    Hello, world! RefCell { value: 5 }
    
    thread 'main' panicked at src\main.rs:10:15:
    already borrowed: BorrowMutError { location: Location { file: "src\\main.rs", line: 7, column: 15 } }
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    error: process didn't exit successfully: `target\debug\refcell-test.exe` (exit code: 101)
    PS C:\Users\neals\refcell-test> cargo +stage1 clean ; cargo +stage1 run
    

    Without this change at HEAD, if you look at the bytes printed there is one backslash (92) in the byte array:

    PS C:\Users\neals\refcell-test> cargo +stage1 clean ; cargo +stage1 run
         Removed 28 files, 4.4MiB total
       Compiling refcell-test v0.1.0 (C:\Users\neals\refcell-test)
        Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.61s
         Running `target\debug\refcell-test.exe`
    Hello, world! RefCell { value: 5 }
    
    thread 'main' panicked at src\main.rs:10:15:
    already borrowed: BorrowMutError { location: Location { file_bytes_with_nul: [115, 114, 99, 92, 109, 97, 105, 110, 46, 114, 115, 0], line: 7, col: 15 } }
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    error: process didn't exit successfully: `target\debug\refcell-test.exe` (exit code: 101)
    
  10. tgross35 commented on Jun 13, 2025

    @tgross35
    MemberAuthor

    @nealsid isn't that the same way it printed before the regression to printing bytes?

    Usually the Debug implementations are intended so you could copy+paste them to source, which means strings are escaped. The Display implementation shouldn't have this.

  11. nealsid commented on Jun 13, 2025

    @nealsid
    Contributor

    @tgross35 you're right, building at 61413ae (the parent of the commit that caused the regression) shows that it was escaped previously as well. Thanks.

  12. added a commit that references this issue on Jun 16, 2025
  13. added a commit that references this issue on Jun 16, 2025
  14. added a commit that references this issue on Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.P-highHigh priorityT-libsRelevant to the library team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions