Skip to content

Don't require extern crate #57288

Open
Open

Description

@mark-i-m

Breaking out from #53128

Currently, extern crate is still required for sysroot crates, like test, proc_macro, core or std. One needs to extern crate them for use in no_std crates unless they are implicitly imported (like std is without #![no_std], or core with it).

  • proc_macro today is automatically imported by Cargo for proc-macro declared crates
  • alloc and std need extern for no_std (or std crates)
  • core is automatically extern'd in for all crates

This is the tracking issue for making them not require extern crate.

Activity

  1. cramertj commented on Jan 2, 2019

    @cramertj
    Member

    Requiring extern crate std; in #![no_std] crates is an intentional restriction. Otherwise, it would be easy to accidentally introduce a dependency on std. If this issue is meant to refer to the ability to specify that std should be available based on a cargo feature, then this is identical to the issue about passing --extern <cratename> (w/ no =<path>) from cargo.

  2. ehuss commented on Jan 2, 2019

    @ehuss
    Contributor

    then this is identical to the issue about passing --extern <cratename>

    Is there a tracking issue for this? It seems like there is some design work that needs to be done, but it's not clear who is responsible (cargo, lang, or compiler).

  3. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-langRelevant to the language team
    on Jan 2, 2019
  4. mark-i-m commented on Jan 2, 2019

    @mark-i-m
    ContributorAuthor

    @cramertj Are you saying this is more blocked on some sort of std-aware cargo? Also, I get that concern for std, but what about core? I don't think I have ever written a crate where I didn't want core around.

  5. cramertj commented on Jan 2, 2019

    @cramertj
    Member

    No, std-aware cargo is completely unrelated. This is about exposing the existing, but unstable, --extern <cratename> flag from cargo. #53166 explains the why/how, #53130 (comment) is the "what", and #54116 is the implementation of the flag in rustc. What's wanted is the ability to make cargo pass --extern std iff an std feature is enabled. That is the cargo issue.

  6. dekellum commented on Jan 2, 2019

    @dekellum

    Also please include tracking here for removing the need for extern crate test in 2018 edition, sysroot libtest benchmarks as was originally brought up in #55133, also closed without the last bit of cargo support. I would guess (without full understanding) that cargo bench and cargo build --benches, etc. needs to be updated to pass the --extern test?

  7. cramertj commented on Jan 2, 2019

    @cramertj
    Member

    @dekellum Indeed-- same issue.

  8. Nemo157 commented on Jan 3, 2019

    @Nemo157
    Contributor

    Also, I get that concern for std, but what about core?

    You only need to extern crate core; when you're using no_core, which makes as much sense as needing extern crate std; for no_std.

    Automatically injecting proc_macro (or meta or whatever it's going to be called) for crate-type = "proc-macro" is I think one of the more important variations of this, since that's most likely to affect stable users.

    EDIT: Actually, rechecking #54116 it appears that meta is already injected, it just needs to be added as a real crate in the sysroot to get that functionality working.

  9. mark-i-m commented on Jan 3, 2019

    @mark-i-m
    ContributorAuthor

    For whatever reason, I have needed to do extern crate core to get cross-compiled crates working. Also, there is liballoc and libtest.

  10. petrochenkov commented on Jan 3, 2019

    @petrochenkov
    Contributor

    extern crate core to get cross-compiled crates working

    Could you give a reproduction?
    As @Nemo157 said above, core is always in extern prelude unless no_core is specified.
    The only reason to use extern crate core that I can think of is this ICE - #56935.

  11. petrochenkov commented on Jan 3, 2019

    @petrochenkov
    Contributor

    The issue should probably use proc_macro or alloc as an example rather than std/core.

  12. Nemo157 commented on Jan 3, 2019

    @Nemo157
    Contributor

    Also, there is liballoc and libtest.

    These are both unstable crates so IMO lower priority than meta or std. However, as @cramertj notes it should be possible to handle them via the same mechanism that will allow std to be injected from Cargo.toml (sysroot-aware Cargo, maybe? EDIT: nope, @petrochenkov shot that down pretty quickly).

  13. petrochenkov commented on Jan 3, 2019

    @petrochenkov
    Contributor

    Also, this is not only about sysroot, but about any crates in library search directories.
    (Non-sysroot search directories are used, for example, by our test suite. I'm sure there are other scenarios as well.)

  14. mark-i-m commented on Jan 3, 2019

    @mark-i-m
    ContributorAuthor

    For whatever reason, I have needed to do extern crate core to get cross-compiled crates working. Also, there is liballoc and libtest.

    Could you give a reproduction?

    Ah, my mistake I went back and looked at the code, and it was liballoc.

  15. 6 remaining items

  16. added
    T-cargoRelevant to the cargo team, which will review and decide on the PR/issue.
    needs-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.
    and removed on Jan 17, 2019
  17. SimonSapin commented on Mar 5, 2019

    @SimonSapin
    Contributor

    As far as I understand this issue applies to the test and proc_macro crates, independently of #![no_std]. I’ve edited the issue description accordingly.

  18. Lokathor commented on Jul 4, 2019

    @Lokathor
    Contributor

    Tracking issue note: alloc just became stable (1.36) and you need to say extern crate alloc;, so it can go in the issue list now I guess.

    At the same time, you don't need to say extern crate core; if you're using normal rust code any more, so core can be removed from the list.

    EDIT: you need to say extern crate core; for ![no_core] crates, but not for ![no_std] or "normal" crates, which should be clarified in the listing.

  19. ehuss commented on Sep 4, 2019

    @ehuss
    Contributor

    Would people be for or against stabilizing the bare --extern flag now? My intent is to automatically issue that flag for proc_macro for proc-macro crates to avoid the need for extern crate. There hasn't been any visible progress on the transition to the meta crate, so I suspect it will be a long time for that to happen.

    This doesn't immediately help for other crates, but would pave the way for future changes in cargo that might allow making these dependencies explicit.

  20. added 2 commits that reference this issue on Nov 7, 2019
  21. added a commit that references this issue on Nov 8, 2019
  22. removed
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Jun 15, 2022
  23. removed
    T-cargoRelevant to the cargo team, which will review and decide on the PR/issue.
    needs-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.
    on Jun 15, 2022
  24. changed the title [-]Tracking issue for no more `extern crate` for sysroot crates[/-] [+]Don't require `extern crate`[/+] on Jun 15, 2022
  25. tgross35 commented on Oct 16, 2023

    @tgross35
    Member

    What is the status of this now that --extern works?

  26. ehuss commented on Oct 17, 2023

    @ehuss
    Contributor

    The status is largely unchanged.

    Cargo uses --extern proc_macro for proc-macro crates so you don't have to do extern proc_macro;.

    extern crate test; is still required, but all items in the test crate are unstable, and the path to stability for anything there is unclear.

    extern crate std; and extern crate alloc; is still required if you have #![no_std], but want to conditionally include std or alloc. Cargo does not provide a mechanism to indicate that dependency.

    extern crate core; is still required if you have #![no_core], same as std. I don't think this is a particular use-case that we should worry about.

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-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions