-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #70916 - Centril:track-caller-ffi, r=eddyb
Support `#[track_caller]` on functions in `extern "Rust" { ... }` Fixes #70830 which is the follow-up to @eddyb's suggestion in #69251 (comment) to allow `#[track_caller]` on `fn`s in FFI imports, that is, on functions in `extern "Rust" { ... }` blocks. This requires that the other side, the FFI export, also have the `#[track_caller]` attribute. Otherwise, undefined behavior is triggered and the blame lies, as usual, with the `unsafe { ... }` block which called the FFI imported function. After this PR, all forms of `fn` items with the right ABI (`"Rust"`) support `#[track_caller]`. As a drive-by, the PR also hardens the check rejecting `#[naked] #[track_caller]` such that methods and other forms of `fn` items are also considered. r? @eddyb cc @rust-lang/lang
- Loading branch information
Showing
8 changed files
with
81 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,21 @@ | ||
#![feature(naked_functions, track_caller)] | ||
|
||
#[track_caller] | ||
#[track_caller] //~ ERROR cannot use `#[track_caller]` with `#[naked]` | ||
#[naked] | ||
fn f() {} | ||
//~^^^ ERROR cannot use `#[track_caller]` with `#[naked]` | ||
|
||
struct S; | ||
|
||
impl S { | ||
#[track_caller] //~ ERROR cannot use `#[track_caller]` with `#[naked]` | ||
#[naked] | ||
fn g() {} | ||
} | ||
|
||
extern "Rust" { | ||
#[track_caller] //~ ERROR cannot use `#[track_caller]` with `#[naked]` | ||
#[naked] | ||
fn h(); | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// run-pass | ||
|
||
#![feature(track_caller)] | ||
|
||
use std::panic::Location; | ||
|
||
extern "Rust" { | ||
#[track_caller] | ||
fn rust_track_caller_ffi_test_tracked() -> &'static Location<'static>; | ||
fn rust_track_caller_ffi_test_untracked() -> &'static Location<'static>; | ||
} | ||
|
||
fn rust_track_caller_ffi_test_nested_tracked() -> &'static Location<'static> { | ||
unsafe { rust_track_caller_ffi_test_tracked() } | ||
} | ||
|
||
mod provides { | ||
use std::panic::Location; | ||
#[track_caller] // UB if we did not have this! | ||
#[no_mangle] | ||
fn rust_track_caller_ffi_test_tracked() -> &'static Location<'static> { | ||
Location::caller() | ||
} | ||
#[no_mangle] | ||
fn rust_track_caller_ffi_test_untracked() -> &'static Location<'static> { | ||
Location::caller() | ||
} | ||
} | ||
|
||
fn main() { | ||
let location = Location::caller(); | ||
assert_eq!(location.file(), file!()); | ||
assert_eq!(location.line(), 31); | ||
assert_eq!(location.column(), 20); | ||
|
||
let tracked = unsafe { rust_track_caller_ffi_test_tracked() }; | ||
assert_eq!(tracked.file(), file!()); | ||
assert_eq!(tracked.line(), 36); | ||
assert_eq!(tracked.column(), 28); | ||
|
||
let untracked = unsafe { rust_track_caller_ffi_test_untracked() }; | ||
assert_eq!(untracked.file(), file!()); | ||
assert_eq!(untracked.line(), 26); | ||
assert_eq!(untracked.column(), 9); | ||
|
||
let contained = rust_track_caller_ffi_test_nested_tracked(); | ||
assert_eq!(contained.file(), file!()); | ||
assert_eq!(contained.line(), 14); | ||
assert_eq!(contained.column(), 14); | ||
} |