Skip to content

Update the error message for a missing global allocator #51639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/missing-allocator.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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;
4 changes: 4 additions & 0 deletions src/test/ui/missing-allocator.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick
I'm not really happy with this output. I would prefer something closer to the following, but I'm not sure we can at that stage:

error: no global memory allocator found but one is required
note: link to `std` or add `#[global_allocator]` to a static item that implements the `GlobalAlloc` trait
note: for more information visit <BOOK 2.0 LINK>

error: aborting due to previous error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I output a note from this code?

As to including an URL, the closest we have at the moment is https://doc.rust-lang.org/nightly/std/alloc/index.html#the-global_allocator-attribute but as you can see it includes /nightly, since this change has not reached stable yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have too look at this, but honestly I would not block on the perfect output precisely because it's nightly. Let's just remember to revise this before stabilizing.