Skip to content

the #[track_caller] shim should not inherit #[no_mangle] #145724

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

folkertdev
Copy link
Contributor

fixes #143162

builds on #143293 which introduced a mechanism to strip attributes from shims.

I'm now stripping specifically the attributes that modify the symbol from the #[track_caller] shim. Maybe I missed some though, and maybe we really should be stripping more things from either the #[track_caller] shim, or shims in general.

cc @Jules-Bertholet @workingjubilee @bjorn3

@rustbot
Copy link
Collaborator

rustbot commented Aug 21, 2025

r? @fee1-dead

rustbot has assigned @fee1-dead.
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

@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 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 21, 2025

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@Jules-Bertholet
Copy link
Contributor

Jules-Bertholet commented Aug 21, 2025

I think this may be the exact opposite of what we want to do—the #[no_mangle] should probably apply only to the shim. (I think we actually need the lang team to weigh in on this… #143162 (comment))

@rust-log-analyzer

This comment has been minimized.

@folkertdev folkertdev force-pushed the track-caller-drop-no-mangle branch from d57bec3 to 2901108 Compare August 21, 2025 21:27
@folkertdev
Copy link
Contributor Author

folkertdev commented Aug 21, 2025

Having the shim "steal" the attribute breaks this code:

https://github.com/rust-lang/rust/blob/master/tests/ui/rfcs/rfc-2091-track-caller/track-caller-ffi.rs

extern "Rust" {
    #[track_caller]
    fn rust_track_caller_ffi_test_tracked() -> &'static Location<'static>;
    fn rust_track_caller_ffi_test_untracked() -> &'static Location<'static>;
}

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()
    }
}

in other words, it is current behavior that you are able to export a symbol that takes the implicit location argument.

The #[track_caller] attribute is only allowed on functions that use the (unstable) rust ABI, so only interop with other rust code is relevant, and then it does make sense to me that you'd want to be able to export the "tracked" version. Users can always write their own shim if they want to export a symbol that does not have the implicit location argument.

edit: so basically I implemented what Björn suggests at #143162 (comment).

edit2: also Jubilee did not think that T-lang needed to be involved #143162 (comment). Given the above that seems right to me: if we want to keep existing code working, we need the shim to drop these attributes. That seems fine, because only extern "Rust" can be used in combination with #[track_caller].

@Jules-Bertholet
Copy link
Contributor

Jules-Bertholet commented Aug 21, 2025

in other words, it is current behavior that you are able to export a symbol that takes the implicit location argument.

Ah, I didn’t know that was actually intended to work and that we had a test for it.

also Jubilee did not think that T-lang needed to be involved #143162 (comment).

Yes, but she was advocating for the exact opposite resolution than you are proposing here! So I think it would actually have confirmed that they did need to be involved. Except that, at the time, neither she nor I knew that the lang team had already made this decision. So yes, in light of this new-to-me information, there’s no need for lang involvement, and we should just merge this PR.

@Jules-Bertholet
Copy link
Contributor

You should also add a test that casting a #[track_caller] fn from an extern block to a function pointer works:

mod inner {
    #[unsafe(no_mangle)]
    extern "Rust" fn foo() {
        println!("hello world");
    }
}

unsafe extern "Rust" {
    #[track_caller]
    safe fn foo();
}

fn main() {
    foo();
    let fnptr = foo as fn();
    fnptr();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"symbol is already defined" when combining track_caller with no_mangle
5 participants