Skip to content
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

No global memory allocator error when testing no_std crate that does link to std #54010

Closed
IsaacWoods opened this issue Sep 6, 2018 · 9 comments · Fixed by #68357
Closed
Labels
T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@IsaacWoods
Copy link
Contributor

I have a no_std crate that imports std when testing:

#![no_std]
#![feature(alloc)]

extern crate alloc;

#[cfg(test)]
#[macro_use]
extern crate std;

However, I get this error during testing:

error: no global memory allocator found but one is required; link to std or add #[global_allocator] t
o a static item that implements the GlobalAlloc trait.

Allocating on the heap during testing does appear to work, so I'm assuming this is just a case of the warning still being emitted.

@zesterer
Copy link
Contributor

zesterer commented Oct 9, 2018

I have the same issue. Any update?

@sfackler
Copy link
Member

sfackler commented Oct 9, 2018

cargo test builds the crate both as a library (for doctests) and as a test executable. You only pull in std for the test executable, so the error message could be coming from building as a library.

@cbeck88
Copy link

cbeck88 commented Jan 15, 2019

I have the same issue, error message seems to be spurious

@dvdhrm
Copy link
Contributor

dvdhrm commented Apr 16, 2019

Yeah, the error message is emitted when building the doctest library. Funnily enough, it does not cause the build to fail. That is, cargo test still continues running and then exits with success (0).

Sadly, there is currently no way to link in a default allocator (or std for that matter) for doctests. There is the unstable doc_cfg feature that provides cfg(rustdoc), but I don't see how to achieve something similar without relying on unstable features.

I simply use doctest = false until this is fixed.

@RalfJung
Copy link
Member

Related: #52243

bors added a commit to rust-lang/hashbrown that referenced this issue Jul 3, 2019
Don't use std crate at all

Fixes #95

We're running into rust-lang/rust#54010 but I don't think it's a problem.
@jethrogb
Copy link
Contributor

jethrogb commented Jul 3, 2019

@rustbot modify labels: +T-rustdoc

@rustbot
Copy link
Collaborator

rustbot commented Jul 3, 2019

Error: Parsing label command in comment failed: ...ify labels|error: must have : or to as label starter at >| +T-rustdo...

Please let @rust-lang/release know if you're having trouble with this bot.

@rustbot rustbot added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Jul 3, 2019
tmfink added a commit to capstone-rust/capstone-rs that referenced this issue Jul 12, 2019
With 1.36.0, alloc crate is now stable, so we always use no_std for
capstone-rs. There is no longer a "std" feature.

Remove alloc_system feature, since the default allocator is the system
allocator.

Minor bug: doc-tests fail
See: rust-lang/rust#54010
tmfink added a commit to capstone-rust/capstone-rs that referenced this issue Jul 12, 2019
With 1.36.0, alloc crate is now stable, so we always use no_std for
capstone-rs. There is no longer a "std" feature.

Remove alloc_system feature, since the default allocator is the system
allocator.

Update CI tests to not use features.

Minor bug: doc-tests fail
See: rust-lang/rust#54010
tmfink added a commit to capstone-rust/capstone-rs that referenced this issue Jul 14, 2019
With 1.36.0, alloc crate is now stable, so we always use no_std for
capstone-rs. There is no longer a "std" feature.

Remove alloc_system feature, since the default allocator is the system
allocator.

Update CI tests to not use features.

Minor bug: doc-tests fail
See: rust-lang/rust#54010
martindisch added a commit to martindisch/oscore that referenced this issue Jul 22, 2019
Having them activated causes this message:
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.

The tests still succeed, but it's annoying. Therefore disabled until
we have any doctests.

rust-lang/rust#54010
@Nemo157
Copy link
Member

Nemo157 commented Aug 11, 2019

Sadly, there is currently no way to link in a default allocator (or std for that matter) for doctests.

If you don't put #[no_std] inside the doctest it will be linked against std even when testing a #[no_std] crate, you can think of each doctest as a separate crate. If you do want a different allocator you can add a #[global_allocator] static like in a normal binary. Even though this error message is printed the doc tests should still be running.

@RalfJung
Copy link
Member

Also see #52243 (comment): the error is basically spurious.

LukasKalbertodt added a commit to LukasKalbertodt/stable-vec that referenced this issue Aug 22, 2019
This crate only needs `core` and `alloc`, so we can mark it as
`#![no_std]`. In theory, more people can use this crate now.

Currently, running `cargo test --doc` still prints an error that
doesn't make any sense, but this is not a problem. It's a known bug:
rust-lang/rust#54010
martindisch added a commit to martindisch/coap-lite that referenced this issue Sep 7, 2019
Having them activated causes this message:
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.

The tests still succeed, but it's annoying. Therefore disabled until
we have any doctests.

rust-lang/rust#54010
@bors bors closed this as completed in cd4652a Jan 20, 2020
Dr-Emann added a commit to Dr-Emann/stowaway that referenced this issue Jan 22, 2020
Because of rust-lang/rust#54010, `cargo test`
currently gives a spurious, harmless error in all but a very recent
nightly.
Dr-Emann added a commit to Dr-Emann/stowaway that referenced this issue Jan 22, 2020
Because of rust-lang/rust#54010, `cargo test`
currently gives a spurious, harmless error in all but a very recent
nightly.
rrbutani added a commit to ut-utp/core that referenced this issue Mar 9, 2020
On stable rust, actually running the tests for this crate succeeds but yells about there no being a global memory allocator.
This is an error that'll eventually be fixed (rust-lang/rust#54010); for now running without the no_std feature is a workaround.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants