-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
unused_imports
shouldn't fire for alloc
types included in the std
prelude
#121362
Comments
Oftentimes, conditional #![cfg_attr(not(feature = "std"), no_std)] But if I understood your issue correctly, you can work around the warnings by instead doing: #![no_std]
#[cfg(feature = "std")]
extern crate std; Which consistently results in the std prelude not being used. |
Lol, what sorcery is this? Is |
|
@MaxGraey Heap and collections are available if you have access to "Stack overflow protection" and "runs init code before main" are properties of the whole binary and its main entrypoint, not of your specific library. I believe it is actually better, and not less capable or meaningfully different, to write I can't judge whether the warnings are a regression, though. |
I still can't figure out how to implement graceful switching between "std" and "core" when "std" is optional for a working crate. Previous solution was: #![cfg_attr(not(feature = "std"), no_std)]
#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std as alloc; If replace |
You shouldn't be using the std prelude in You will have to write extra imports for things that are missing from the #[cfg(feature = "std")]
use std::dbg;
#[cfg(not(feature = "std"))]
use fallback_dbg as dbg;
#[allow(unused_macros)]
macro_rules! fallback_dbg {
($x:expr) => { $x };
}; |
Ungated imports from alloc are incredibly common in existing code: https://github.com/search?q=%22use+alloc%3A%3A%22+language%3ARust&type=code The change to this lint will result in a huge amount of new warnings across countless projects. |
@Systemcluster The problem was the conditional usage of the For example the linux repository – which was the first result in your search for me – will not run into this issue. But you're still right in that this issue will probably occur in a lot of crates: github search |
cc #121708 |
This issue needs to be reopened. Up until @kadiwa4 updated the docs last week, the canonical guidance was: // In lib.rs
#![cfg_attr(not(feature = "std"), no_std)] The new guidance isn't a drop-in replacement. For example, it causes this code (which with the old #![no_std]
#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "std")]
mod foo {
fn foo() -> Box<()> {
Box::new(())
}
} Almost all of my crates are |
in #![no_std]
#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "std")]
mod foo {
use std::boxed::Box;
fn foo() -> Box<()> {
Box::new(())
}
} alternatively, if you rely on |
I understand this. It's why the updated 'conditional
That doesn't change existing conditional |
i don't think it was meant to be a drop-in replacement. definitely agree that the rapid change of guidance from also, the code i showed that fixes your example only adds 1 line. i feel that is maybe not so bad. i guess you can always suppress the lint until and unless you want to change your conditional 🤷♀️ |
See - rust-lang/rust#121362 the recommendation for how for no_std libraries has changed. To avoid warnings around unncessary imports (due to the prelude when compiling tests) make the tests also no_std and explicitly import the require test dependencies. Signed-off-by: Rob Bradford <rbradford@rivosinc.com>
See - rust-lang/rust#121362 the recommendation for how for no_std libraries has changed. To avoid warnings around unncessary imports (due to the prelude when compiling tests) make the tests also no_std and explicitly import the require test dependencies. Signed-off-by: Rob Bradford <rbradford@rivosinc.com>
See - rust-lang/rust#121362 the recommendation for how for no_std libraries has changed. To avoid warnings around unncessary imports (due to the prelude when compiling tests) make the tests also no_std and explicitly import the require test dependencies. Signed-off-by: Rob Bradford <rbradford@rivosinc.com>
When working with libraries that can or can not be
#[no_std]
, it is common to simplyextern crate alloc;
and then manually import types likealloc::vec::Vec
. In this way everything works regardless of the configuration.However, the latest versions of the compiler are warning such approach.
In the above example, if
use alloc::vec::Vec;
is removed#[no_std]
breaks. If not removed, the warning persists 😑Maybe related: #121312 #121331 #121330 #120422
The text was updated successfully, but these errors were encountered: