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

Don't require extern crate #57288

Open
mark-i-m opened this issue Jan 2, 2019 · 22 comments
Open

Don't require extern crate #57288

mark-i-m opened this issue Jan 2, 2019 · 22 comments
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@mark-i-m
Copy link
Member

mark-i-m commented Jan 2, 2019

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.

@cramertj
Copy link
Member

cramertj commented Jan 2, 2019

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.

@ehuss
Copy link
Contributor

ehuss commented Jan 2, 2019

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).

@Centril Centril added C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jan 2, 2019
@mark-i-m
Copy link
Member Author

mark-i-m commented Jan 2, 2019

@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.

@cramertj
Copy link
Member

cramertj commented Jan 2, 2019

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.

@dekellum
Copy link

dekellum commented Jan 2, 2019

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?

@cramertj
Copy link
Member

cramertj commented Jan 2, 2019

@dekellum Indeed-- same issue.

@Nemo157
Copy link
Member

Nemo157 commented Jan 3, 2019

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.

@mark-i-m
Copy link
Member Author

mark-i-m commented Jan 3, 2019

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

@petrochenkov
Copy link
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.

@petrochenkov
Copy link
Contributor

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

@Nemo157
Copy link
Member

Nemo157 commented Jan 3, 2019

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).

@petrochenkov
Copy link
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.)

@mark-i-m
Copy link
Member Author

mark-i-m commented Jan 3, 2019

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.

@alercah
Copy link
Contributor

alercah commented Jan 9, 2019

I just ran into this in a proc macro crate. Notwithstanding the possibility that proc-macro libraries import proc_macro by default, my user instinct would be the following stanza in my Cargo.toml:

[dependencies]
proc_macro = { builtin = true }

I know it's not technically builtin, it's actually provided by the distribution. But to a user it feels like it's a builtin.

@cramertj
Copy link
Member

cramertj commented Jan 9, 2019

@alercah This isn't only for builtin crates, but for anything that should be pulled in from library search directories (could be arbitrary crates).

@Nemo157
Copy link
Member

Nemo157 commented Jan 9, 2019

What is the next required step for this, does it require an RFC, or can it be implemented as an unstable feature in Cargo without that? If the latter, should there be a separate Cargo issue opened to track actually getting some feature implemented there that will solve this?

(Somewhat related to this issue, should there be a tracking issue for what is happening around meta if that is the intended solution for proc-macros?)

@Centril Centril changed the title [tracking issue] no more extern crate for sysroot crates Tracking issue for no more extern crate for sysroot crates Jan 13, 2019
@aturon
Copy link
Member

aturon commented Jan 17, 2019

cc @rust-lang/cargo

@Centril Centril added T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. and removed I-nominated labels Jan 17, 2019
@SimonSapin
Copy link
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.

@Lokathor
Copy link
Contributor

Lokathor commented Jul 4, 2019

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.

@ehuss
Copy link
Contributor

ehuss commented Sep 4, 2019

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.

Centril added a commit to Centril/rust that referenced this issue Nov 7, 2019
Stabilize --extern flag without a path.

This stabilizes the `--extern` flag without a path, implemented in rust-lang#54116.

This flag is used to add a crate that may be found in the search path to the extern prelude. The intent of stabilizing this now is to change Cargo to emit this flag for `proc_macro` when building a proc-macro crate. This will allow the ability to elide `extern crate proc_macro;` for proc-macros, one of the few places where it is still necessary.

It is intended that Cargo may also use this flag for other cases in the future as part of the [std-aware work](https://github.com/rust-lang/wg-cargo-std-aware/). There will likely be some kind of syntax where users may declare dependencies on other crates (such as `alloc`), and Cargo will use this flag so that they may be used like any other crate. At this time there are no short-term plans to use it for anything other than proc-macro.

This will not help for non-proc-macro crates that use `proc_macro`, which I believe is not too common?

An alternate approach for proc-macro is to use the `meta` crate, but from my inquiries there doesn't appear to be anyone interested in pushing that forward. The `meta` crate also doesn't help with things like `alloc` or `test`.

cc rust-lang#57288
Centril added a commit to Centril/rust that referenced this issue Nov 7, 2019
Stabilize --extern flag without a path.

This stabilizes the `--extern` flag without a path, implemented in rust-lang#54116.

This flag is used to add a crate that may be found in the search path to the extern prelude. The intent of stabilizing this now is to change Cargo to emit this flag for `proc_macro` when building a proc-macro crate. This will allow the ability to elide `extern crate proc_macro;` for proc-macros, one of the few places where it is still necessary.

It is intended that Cargo may also use this flag for other cases in the future as part of the [std-aware work](https://github.com/rust-lang/wg-cargo-std-aware/). There will likely be some kind of syntax where users may declare dependencies on other crates (such as `alloc`), and Cargo will use this flag so that they may be used like any other crate. At this time there are no short-term plans to use it for anything other than proc-macro.

This will not help for non-proc-macro crates that use `proc_macro`, which I believe is not too common?

An alternate approach for proc-macro is to use the `meta` crate, but from my inquiries there doesn't appear to be anyone interested in pushing that forward. The `meta` crate also doesn't help with things like `alloc` or `test`.

cc rust-lang#57288
bors added a commit that referenced this issue Nov 8, 2019
Stabilize --extern flag without a path.

This stabilizes the `--extern` flag without a path, implemented in #54116.

This flag is used to add a crate that may be found in the search path to the extern prelude. The intent of stabilizing this now is to change Cargo to emit this flag for `proc_macro` when building a proc-macro crate. This will allow the ability to elide `extern crate proc_macro;` for proc-macros, one of the few places where it is still necessary.

It is intended that Cargo may also use this flag for other cases in the future as part of the [std-aware work](https://github.com/rust-lang/wg-cargo-std-aware/). There will likely be some kind of syntax where users may declare dependencies on other crates (such as `alloc`), and Cargo will use this flag so that they may be used like any other crate. At this time there are no short-term plans to use it for anything other than proc-macro.

This will not help for non-proc-macro crates that use `proc_macro`, which I believe is not too common?

An alternate approach for proc-macro is to use the `meta` crate, but from my inquiries there doesn't appear to be anyone interested in pushing that forward. The `meta` crate also doesn't help with things like `alloc` or `test`.

cc #57288
@joshtriplett joshtriplett removed the C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. label Jun 15, 2022
@Mark-Simulacrum Mark-Simulacrum removed T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. needs-rfc This change is large or controversial enough that it should have an RFC accepted before doing it. labels Jun 15, 2022
@Mark-Simulacrum Mark-Simulacrum changed the title Tracking issue for no more extern crate for sysroot crates Don't require extern crate Jun 15, 2022
@tgross35
Copy link
Contributor

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

@ehuss
Copy link
Contributor

ehuss commented Oct 17, 2023

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
Labels
T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests