Skip to content

Commit ac8d1f7

Browse files
committed
Auto merge of #51639 - SimonSapin:missing-alloc, r=rkruppe
Update the error message for a missing global allocator Don’t mention `#[default_lib_allocator]` (which is an implementation detail irrelevant to most users) and instead suggest using `#[global_allocator]`, which is often the correct fix.
2 parents f28c7ae + d2fe6c4 commit ac8d1f7

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/librustc_metadata/creader.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,10 @@ impl<'a> CrateLoader<'a> {
986986
},
987987
None => {
988988
if !attr::contains_name(&krate.attrs, "default_lib_allocator") {
989-
self.sess.err("no #[default_lib_allocator] found but one is \
990-
required; is libstd not linked?");
989+
self.sess.err("no global memory allocator found but one is \
990+
required; link to std or \
991+
add #[global_allocator] to a static item \
992+
that implements the GlobalAlloc trait.");
991993
return;
992994
}
993995
self.sess.allocator_kind.set(Some(AllocatorKind::DefaultLib));

src/test/ui/missing-allocator.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -C panic=abort
12+
// no-prefer-dynamic
13+
14+
#![no_std]
15+
#![crate_type = "staticlib"]
16+
#![feature(panic_implementation, lang_items, alloc)]
17+
18+
#[panic_implementation]
19+
fn panic(_: &core::panic::PanicInfo) -> ! {
20+
loop {}
21+
}
22+
23+
#[lang = "oom"]
24+
fn oom() {}
25+
26+
extern crate alloc;

src/test/ui/missing-allocator.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: no global memory allocator found but one is required; link to std or add #[global_allocator] to a static item that implements the GlobalAlloc trait.
2+
3+
error: aborting due to previous error
4+

0 commit comments

Comments
 (0)