From a9a0f4cdd156c228ec8873d8eef4161c4d53f289 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 19 Jun 2018 19:44:18 +0200 Subject: [PATCH 1/2] Add a UI test for the missing allocator error message --- src/test/ui/missing-allocator.rs | 26 ++++++++++++++++++++++++++ src/test/ui/missing-allocator.stderr | 4 ++++ 2 files changed, 30 insertions(+) create mode 100644 src/test/ui/missing-allocator.rs create mode 100644 src/test/ui/missing-allocator.stderr diff --git a/src/test/ui/missing-allocator.rs b/src/test/ui/missing-allocator.rs new file mode 100644 index 0000000000000..24282631b7eea --- /dev/null +++ b/src/test/ui/missing-allocator.rs @@ -0,0 +1,26 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -C panic=abort +// no-prefer-dynamic + +#![no_std] +#![crate_type = "staticlib"] +#![feature(panic_implementation, lang_items, alloc)] + +#[panic_implementation] +fn panic(_: &core::panic::PanicInfo) -> ! { + loop {} +} + +#[lang = "oom"] +fn oom() {} + +extern crate alloc; diff --git a/src/test/ui/missing-allocator.stderr b/src/test/ui/missing-allocator.stderr new file mode 100644 index 0000000000000..6fd21d850e96e --- /dev/null +++ b/src/test/ui/missing-allocator.stderr @@ -0,0 +1,4 @@ +error: no #[default_lib_allocator] found but one is required; is libstd not linked? + +error: aborting due to previous error + From d2fe6c452790b8d9e986777694390f2f378c58cb Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Tue, 19 Jun 2018 20:10:56 +0200 Subject: [PATCH 2/2] Update the error message for a missing global allocator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/librustc_metadata/creader.rs | 6 ++++-- src/test/ui/missing-allocator.stderr | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index e41b3f5f53b25..6c1991457433f 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -986,8 +986,10 @@ impl<'a> CrateLoader<'a> { }, None => { if !attr::contains_name(&krate.attrs, "default_lib_allocator") { - self.sess.err("no #[default_lib_allocator] found but one is \ - required; is libstd not linked?"); + self.sess.err("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."); return; } self.sess.allocator_kind.set(Some(AllocatorKind::DefaultLib)); diff --git a/src/test/ui/missing-allocator.stderr b/src/test/ui/missing-allocator.stderr index 6fd21d850e96e..11e0085d1ce2e 100644 --- a/src/test/ui/missing-allocator.stderr +++ b/src/test/ui/missing-allocator.stderr @@ -1,4 +1,4 @@ -error: no #[default_lib_allocator] found but one is required; is libstd not linked? +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. error: aborting due to previous error