Skip to content

global_allocator can not not be defined inside module #44113

Closed
@rkapl

Description

@rkapl

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 containing use 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:

#![feature(global_allocator, allocator_api)]                                                                                                                                                                       
                                                                                                                                                                                                                   
struct TAlloc;                                                                                                                                                                                                     
                                                                                                                                                                                                                   
unsafe impl<'a> std::heap::Alloc for &'a TAlloc{                                                                                                                                                                   
    unsafe fn alloc(&mut self, _layout: std::heap::Layout) -> std::result::Result<*mut u8, std::heap::AllocErr> {                                                                                                  
        return Err(std::heap::AllocErr::Unsupported{details: "Stub allocator"});                                                                                                                                   
    }                                                                                                                                                                                                              
    unsafe fn dealloc(&mut self, _ptr: *mut u8, _layout: std::heap::Layout) {                                                                                                                                      
    }                                                                                                                                                                                                              
}                                                                                                                                                                                                                  
                                                                                                                                                                                                                   
mod breaks{                                                                                                                                                                                                        
    #[global_allocator]                                                                                                                                                                                            
    static ALLOCATOR: ::TAlloc = ::TAlloc;                                                                                                                                                                         
}                                                                                                                                                                                                                  
                                                                                                                                                                                                                   
pub fn main() {                                                                                                                                                                                                    
}

causes:

error[E0432]: unresolved import `super`
  --> alloc.rs:15:5
   |
15 |     static ALLOCATOR: ::TAlloc = ::TAlloc;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `ALLOCATOR` in the root

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsC-bugCategory: This is a bug.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions