about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--library/core/src/alloc/mod.rs5
-rw-r--r--src/test/ui/allocator/object-safe.rs13
2 files changed, 17 insertions, 1 deletions
diff --git a/library/core/src/alloc/mod.rs b/library/core/src/alloc/mod.rs
index 045eb58d013..9c2a0146e2c 100644
--- a/library/core/src/alloc/mod.rs
+++ b/library/core/src/alloc/mod.rs
@@ -342,7 +342,10 @@ pub unsafe trait Allocator {
     ///
     /// The returned adaptor also implements `Allocator` and will simply borrow this.
     #[inline(always)]
-    fn by_ref(&self) -> &Self {
+    fn by_ref(&self) -> &Self
+    where
+        Self: Sized,
+    {
         self
     }
 }
diff --git a/src/test/ui/allocator/object-safe.rs b/src/test/ui/allocator/object-safe.rs
new file mode 100644
index 00000000000..fae7ab7fe33
--- /dev/null
+++ b/src/test/ui/allocator/object-safe.rs
@@ -0,0 +1,13 @@
+// run-pass
+
+// Check that `Allocator` is object safe, this allows for polymorphic allocators
+
+#![feature(allocator_api)]
+
+use std::alloc::{Allocator, System};
+
+fn ensure_object_safe(_: &dyn Allocator) {}
+
+fn main() {
+    ensure_object_safe(&System);
+}