-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
global_allocator can not not be defined inside module #44113
Comments
I looked into this a bit, writing some notes here so I don't forget what I found. The problem is here: rust/src/librustc_allocator/expand.rs Lines 94 to 97 in 0e6f4cf
The There may be a function hidden in the compiler somewhere that can help get a |
Another option is to generate stub impls of the global alloc functions in the AST but then fill them out later when there's more information available. |
To add to this, this is also broken: pub fn main() {
#[global_allocator]
static ALLOCATOR: ::TAlloc = ::TAlloc;
} |
Started working on this... It's very WIP at the moment, though. If anyone has feedback, let me know. |
Prohibit `global_allocator` in submodules Background: #44113 is caused by weird interactions with hygiene. Hygiene is hard. After a lot of playing around, we decided that the best path forward would be to prohibit `global_allocator`s from being in submodules for now. When somebody gets it working, we can re-enable it. This PR contains the following - Some hygiene "fixes" -- things I suspect are the correct thing to do that will make life easier in the future. This includes using call_site hygiene for the generated module and passing the correct crate name to the expansion config. - Comments and minor formatting fixes - Some debugging code - Code to prohibit `global_allocator` in submodules - Test checking that the proper error occurs. cc #44113 #49320 #51241 r? @alexcrichton
At least until rust-lang/rust#44113 is fixed.
We now have a good error for this:
So the real question is, is this something we want to enable in the future? If so, then this shouldn't be closed, and the bug would be tracking that. If we do not, then this should be closed. |
Personally, i would like for that to be possible. For example, when building an OS, it would be nice to be able to everything in a memory management module. Currently, you have to break that abstraction to put the allocator in thr crate root, which then requires exposing the implementation outside the memory management module. |
So I realized that I never gave an update here. The current status is as follows: About a year ago, I attempted to implement a fix, but we ran into a lot of weird hygiene issues (that even alexcrichton wasn't able to help with). Then, I changed course and implemented the error that Steve posted above. That's about all I know how to do, and that was the end of my endeavors in this space. Somebody with expertise in macro/hygiene wizardry and compiler internals is needed to fix the problems we ran into in #49320. |
For me, the error message is more important than solving the problem perfectly. In my use case, i have just moved the allocator to the root -- but the real problem was finding this solution. |
Adding submodule support may be useful when combining several crates with custom global allocator with c libraries. With several crates the ´´´__rg_alloc´´´ symbol is overlapping in the linking phase and linking is failing. Or is there a workaround already for this (besides allowing multidefs)? |
I think it is intentionally disallowed to have multiple global allocators. After all, they would no longer be global... |
In this case they are the same allocator as they are using the same global alloc crate. Still, as they are from different internal crates, they are seen as different allocators during linking. Multidefs is the workaround, but it of course may cause other issues elsewhere. But yes, I agree that maybe submodules is not the way to properly solve this problem. |
Fixed in #62735. |
Turn `#[global_allocator]` into a regular attribute macro It was a 99% macro with exception of some diagnostic details. As a result of the change, `#[global_allocator]` now works in nested modules and even in nameless blocks. Fixes rust-lang#44113 Fixes rust-lang#58072
Turn `#[global_allocator]` into a regular attribute macro It was a 99% macro with exception of some diagnostic details. As a result of the change, `#[global_allocator]` now works in nested modules and even in nameless blocks. Fixes rust-lang#44113 Fixes rust-lang#58072
Turn `#[global_allocator]` into a regular attribute macro It was a 99% macro with exception of some diagnostic details. As a result of the change, `#[global_allocator]` now works in nested modules and even in nameless blocks. Fixes rust-lang#44113 Fixes rust-lang#58072
Turn `#[global_allocator]` into a regular attribute macro It was a 99% macro with exception of some diagnostic details. As a result of the change, `#[global_allocator]` now works in nested modules and even in nameless blocks. Fixes rust-lang#44113 Fixes rust-lang#58072
When trying define a global allocator inside a module, you get a weird error message:
[E0432]: unresolved import super
. The global_allocator is expanded to code containinguse super::<varname>
but I don't know why rustc fails to resolve that in this case. Happens with both inline and file modules.Looks like the module that is created by expanding global_allocator thinks its parent is the root crate despite being in the
breaks
module.Example:
causes:
The text was updated successfully, but these errors were encountered: