Rollup merge of #62735 - petrochenkov:galloc, r=alexcrichton

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 https://github.com/rust-lang/rust/issues/44113
Fixes https://github.com/rust-lang/rust/issues/58072
This commit is contained in:
Mazdak Farrokhzad 2019-07-25 23:21:00 +02:00 committed by GitHub
commit e1de70b045
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
40 changed files with 513 additions and 593 deletions

View file

@ -1,28 +0,0 @@
// Tests that it is possible to create a global allocator in a submodule, rather than in the crate
// root.
extern crate alloc;
use std::{
alloc::{GlobalAlloc, Layout},
ptr,
};
struct MyAlloc;
unsafe impl GlobalAlloc for MyAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
ptr::null_mut()
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {}
}
mod submod {
use super::MyAlloc;
#[global_allocator]
static MY_HEAP: MyAlloc = MyAlloc; //~ ERROR global_allocator
}
fn main() {}

View file

@ -1,8 +0,0 @@
error: `global_allocator` cannot be used in submodules
--> $DIR/allocator-submodule.rs:25:5
|
LL | static MY_HEAP: MyAlloc = MyAlloc;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error

View file

@ -0,0 +1,13 @@
use std::alloc::{GlobalAlloc, Layout};
struct A;
unsafe impl GlobalAlloc for A {
unsafe fn alloc(&self, _: Layout) -> *mut u8 { panic!() }
unsafe fn dealloc(&self, _: *mut u8, _: Layout) { panic!() }
}
#[global_allocator(malloc)] //~ ERROR malformed `global_allocator` attribute input
static S: A = A;
fn main() {}

View file

@ -0,0 +1,8 @@
error: malformed `global_allocator` attribute input
--> $DIR/allocator-args.rs:10:1
|
LL | #[global_allocator(malloc)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[global_allocator]`
error: aborting due to previous error

View file

@ -4,6 +4,6 @@ use std::alloc::System;
static A: System = System;
#[global_allocator]
static B: System = System;
//~^ ERROR: cannot define more than one `#[global_allocator]`
//~^ ERROR: cannot define multiple global allocators
fn main() {}

View file

@ -1,8 +1,14 @@
error: cannot define more than one `#[global_allocator]`
error: cannot define multiple global allocators
--> $DIR/two-allocators.rs:6:1
|
LL | static B: System = System;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the previous global allocator is defined here
--> $DIR/two-allocators.rs:4:1
|
LL | static A: System = System;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error

View file

@ -1,3 +1,13 @@
warning: attribute must be of the form `#[bench]`
--> $DIR/issue-43106-gating-of-bench.rs:15:1
|
LL | #![bench = "4100"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(ill_formed_attribute_input)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
error[E0601]: `main` function not found in crate `issue_43106_gating_of_bench`
|
= note: consider adding a `main` function to `$DIR/issue-43106-gating-of-bench.rs`

View file

@ -1,3 +1,13 @@
warning: attribute must be of the form `#[test]`
--> $DIR/issue-43106-gating-of-test.rs:10:1
|
LL | #![test = "4200"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(ill_formed_attribute_input)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #57571 <https://github.com/rust-lang/rust/issues/57571>
error[E0601]: `main` function not found in crate `issue_43106_gating_of_test`
|
= note: consider adding a `main` function to `$DIR/issue-43106-gating-of-test.rs`