Skip to content

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

Closed

Description

@IsaacWoods

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.

Activity

  1. zesterer commented on Oct 9, 2018

    @zesterer
    Contributor

    I have the same issue. Any update?

  2. sfackler commented on Oct 9, 2018

    @sfackler
    Member

    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.

  3. cbeck88 commented on Jan 15, 2019

    @cbeck88

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

  4. dvdhrm commented on Apr 16, 2019

    @dvdhrm
    Contributor

    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.

  5. RalfJung commented on Apr 18, 2019

    @RalfJung
    Member

    Related: #52243

  6. added a commit that references this issue on Jul 3, 2019
  7. jethrogb commented on Jul 3, 2019

    @jethrogb
    Contributor

    @rustbot modify labels: +T-rustdoc

  8. rustbot commented on Jul 3, 2019

    @rustbot
    Collaborator

    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.

  9. added
    T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.
    on Jul 3, 2019
  10. Nemo157 commented on Aug 11, 2019

    @Nemo157
    Contributor

    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.

  11. RalfJung commented on Aug 11, 2019

    @RalfJung
    Member

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

  12. added a commit that references this issue on Aug 22, 2019
  13. added a commit that references this issue on Jan 20, 2020
    cd4652a
  14. added a commit that references this issue on Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions