Skip to content

Support #[track_caller] on EII declarations#158535

Open
cezarbbb wants to merge 1 commit into
rust-lang:mainfrom
cezarbbb:eii-track-caller
Open

Support #[track_caller] on EII declarations#158535
cezarbbb wants to merge 1 commit into
rust-lang:mainfrom
cezarbbb:eii-track-caller

Conversation

@cezarbbb

@cezarbbb cezarbbb commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Tracking issue: #125418
Zulip thread: Zulip

#[track_caller] is currently rejected on EII declarations by the catch-all in split_attrs. This routes it onto the foreign item and
derives it onto every implementation during codegen.

The foreign item is the symbol callers link against, so it must carry the flag for call sites to append the caller location. It is routed onto the foreign item only, not the default impl — check_attr already rejects #[track_caller] on EII implementations (EiiWithTrackCaller), since the attribute is part of the ABI.

The flag is then derived onto each implementation in codegen_attrs: if the foreign item is #[track_caller], every impl gets the flag too. The shim in add_function_aliases takes its ABI from the impl's fn_abi, so if any impl lacked the flag the caller-location argument would be silently dropped rather than reported as a type error. Deriving it keeps default and explicit impls in sync without requiring users to repeat the attribute.

#[track_caller] on a foreign item is an already-supported path (see tests/ui/rfcs/rfc-2091-track-caller/track-caller-ffi.rs); this just wires it into EII. Statics are unaffected — #[track_caller] isn't a valid target on statics and is rejected as before.

r? @jdonszelmann @bjorn3

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) 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 Jun 29, 2026
@rustbot

rustbot commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

jdonszelmann is currently at their maximum review capacity.
They may take a while to respond.

@rust-log-analyzer

This comment has been minimized.

@cezarbbb

cezarbbb commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Summarizing the Zulip thread for context, there are two directions came up. One requires #[track_caller] symmetrically on both decl and impl, rejecting mismatches. The other propagates the flag from the decl onto every impl during codegen (mirroring how trait impls already inherit #[track_caller] from the trait declaration via should_inherit_track_caller, without the impl writing it), with a possible trampoline follow-up for the decl-without/impl-with case.

The discussion landed on propagation, which is what this PR implements. Adding/removing #[track_caller] isn't a semver break today (the compiler handles it transparently), so there's no need to treat it as part of the signature that decl and impl must explicitly match. The trampoline was agreed to be a follow-up, not part of this PR.

@jdonszelmann whenever you have capacity, could you take a look?

// its ABI from the impl's `fn_abi`, so every impl must agree on whether the
// caller-location argument is present, otherwise it would be silently dropped.
if tcx
.body_codegen_attrs(foreign_item)

@bjorn3 bjorn3 Jul 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign_item would never be a const or global asm block if this code is reached, right? So you should be able to use codegen_fn_attrs directly.

View changes since the review

@bjorn3 bjorn3 added the F-extern_item_impls `#![feature(extern_item_impls)]` label Jul 8, 2026
@bjorn3

bjorn3 commented Jul 8, 2026

Copy link
Copy Markdown
Member

r? bjorn3

@rustbot rustbot assigned bjorn3 and unassigned jdonszelmann Jul 8, 2026
@cezarbbb cezarbbb force-pushed the eii-track-caller branch from e1e75ea to 5c33d50 Compare July 9, 2026 01:20
@bjorn3

bjorn3 commented Jul 9, 2026

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 5c33d50 has been approved by bjorn3

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 Jul 9, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 9, 2026
Support `#[track_caller]` on EII declarations

Tracking issue: rust-lang#125418
Zulip thread: [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/Questions.20about.20support.20.60.23.5Btrack_caller.5D.60.20on.20EII.20declaration)

`#[track_caller]` is currently rejected on EII declarations by the catch-all in `split_attrs`. This routes it onto the foreign item and
derives it onto every implementation during codegen.

The foreign item is the symbol callers link against, so it must carry the flag for call sites to append the caller location. It is routed onto the foreign item only, not the default impl — `check_attr` already rejects `#[track_caller]` on EII implementations (`EiiWithTrackCaller`), since the attribute is part of the ABI.

The flag is then derived onto each implementation in `codegen_attrs`: if the foreign item is `#[track_caller]`, every impl gets the flag too. The shim in `add_function_aliases` takes its ABI from the impl's `fn_abi`, so if any impl lacked the flag the caller-location argument would be silently dropped rather than reported as a type error. Deriving it keeps default and explicit impls in sync without requiring users to repeat the attribute.

`#[track_caller]` on a foreign item is an already-supported path (see `tests/ui/rfcs/rfc-2091-track-caller/track-caller-ffi.rs`); this just wires it into EII. Statics are unaffected — `#[track_caller]` isn't a valid target on statics and is rejected as before.

r? @jdonszelmann @bjorn3
rust-bors Bot pushed a commit that referenced this pull request Jul 9, 2026
…uwer

Rollup of 17 pull requests

Successful merges:

 - #158510 (Enable `static_position_independent_executables` on all gnu and musl targets)
 - #158541 (Move `std::io::Write` to `core::io`)
 - #158899 (Fix `unaligned_volatile_store` by removing `MemFlags::UNALIGNED`)
 - #155429 (Support `u128`/`i128` c-variadic arguments)
 - #156370 (Reject linked dylib EII default overrides)
 - #157153 (allow `Allocator`s to be used as `#[global_allocator]`s)
 - #158535 (Support `#[track_caller]` on EII declarations)
 - #158617 (allow mGCA const arguments to fall back to anon consts)
 - #158645 (Fix splat ICEs and ban it in closures)
 - #158988 (Redo `TokenStreamIter`)
 - #158347 (Improve generic parameters handling for #[diagnostic::on_const])
 - #158722 (delegation: do not always inherit `ConstArgHasType` predicates)
 - #158739 (view-types: HIR lowering)
 - #158883 (tests: fix enum-match.rs to handle LLVM 23)
 - #158886 (Add documentation for the `no_std` attribute)
 - #158951 (Merge three `MaxUniverse`s into one)
 - #158961 (Reapply "LLVM 23: Run AssignGUIDPass in some places")
@JonathanBrouwer

Copy link
Copy Markdown
Contributor

@bors try jobs=aarch64-apple

@JonathanBrouwer

Copy link
Copy Markdown
Contributor

@bors r-

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 9, 2026
Support `#[track_caller]` on EII declarations


try-job: aarch64-apple
@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 9, 2026
@rust-bors

rust-bors Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

View changes since this unapproval

@rust-bors

rust-bors Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

💔 Test for 69d1e2b failed: CI. Failed job:

@rust-log-analyzer

This comment has been minimized.

@cezarbbb

Copy link
Copy Markdown
Contributor Author

On Apple target, using #[eii] on a static property with a value will throw an error, and remove FOO directly from the scope, resulting in a cannot find value FOO error when println!("{FOO}") is executed. The solution is to remove that static test case from the test. It shouldn't be managed by this EII test in the first place—the track_caller cannot be placed on static properties for general attribute validation, which has already been tested in another test (tests/ui/attributes/issue-105594-invalid-attr-validation.rs).

@cezarbbb

Copy link
Copy Markdown
Contributor Author

I don't have an Apple device, so I overlooked this. If anyone has permission, could you please help me rerun a CI test with aarch64-apple?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) F-extern_item_impls `#![feature(extern_item_impls)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

6 participants