-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Reject tail calls of #[track_caller]
functions
#144762
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
base: master
Are you sure you want to change the base?
Conversation
r? @SparrowLii rustbot has assigned @SparrowLii. Use |
Hm. Alternatively, instead of rejecting callee being |
nit in PR description: s/monopolization/monomorphization |
This is what we have to do, because adding |
@Jules-Bertholet I think "must not" is too strict. We probably don't want it to be, but it could be a breaking change. |
It was an explicit goal of the |
Also see this comment: #88302 (comment)
Though, I feel like this guarantee is not really properly documented for users, for example, there is no mention of this in: https://doc.rust-lang.org/reference/attributes/codegen.html#r-attributes.codegen.track_caller |
I'm trying to figure
As all functions with tail calls would need to have #[track_caller] this would allow to track the top level caller function starting the recursion. (Nested #[track_caller] functions also track the top level caller.) I can see this being useful sometimes. Am I missing something? The ___location info would just be an argument that is passed through untouched. Even if it is spilled on the stack this would not cause a growing stack per call. Is this correct?
Playing this through with all relevant combinations I get this following table.
So, to my understanding we are dealing with exactly the same issues as for normal calls which track the caller. I would guess we could even use the same functionality. However, the main issue is that we need to special case the check for matching arguments? Also there could be a hidden performance cost, adding and dropping the ___location information, which seems to be a documentation / lint issue. There would not be a unexpected stack growth as we do not track all callers in the call stack but only one caller. I'm not saying we need to support this fully, just trying to understand the topic. |
For portability reasons we have to ensure that the ABI of the caller and callee match exactly when doing a tail call. Adding or removing an argument for the caller ___location may result in the amount of stack space for arguments changing, which can't be implemented on caller-pops calling conventions (like we use on most (all?) architectures as that is what the C ABI has to do to support varargs) for tail calls. The caller of the first tail-call would expect to pop X bytes of the stack, but the last tail-call may have Y bytes on the stack that need to be popped if the ABI of the caller and callee don't exactly match. Changing the amount of arguments can only work when you either ensure all arguments stay in registers or you use a callee-pop calling convention (as eg Wasmtime does for wasm tail calls). The former restriction is even worse that not allowing |
Ah, I see, for some reason I was thinking that this would use callee cleanup. Thank you for the answer. The discussion around the calling convention also still seems to be quite active, looking at recent comments in the RFC PR. |
I've open #144865 with al alternative approach. |
To make tail calls possible either both functions need to be
#[track_caller]
, or neither can. For simplicity I propose to just disallow using tail calling#[track_caller]
functions / tail calling from#[track_caller]
functions. I don't think#[track_caller]
is useful with tail calls anyway.However, we do need to have a quite late check, after monopolization (as otherwise you don't necessarily know if the callee has
#[track_caller]
).I'm not very happy with this implementation -- error'ing in the backend seems like a hack and will probably lead to disagreements between backends. Ideally we'd have a mir specific check (that would also probably make getting the span easier...).
Fixes #144755