Skip to content

prefer core-alloc-std to alloc-core-std when reordering standard libraries imports #6290

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

Open
lo48576 opened this issue Aug 22, 2024 · 0 comments · May be fixed by #6548
Open

prefer core-alloc-std to alloc-core-std when reordering standard libraries imports #6290

lo48576 opened this issue Aug 22, 2024 · 0 comments · May be fixed by #6548

Comments

@lo48576
Copy link
Contributor

lo48576 commented Aug 22, 2024

Summary

Currently alloc, core, and std imports are always reordered lexicographically (when they will be reordered).
However, at least when a reordered group only contains these standard library crates, it feels semantically more natural to use the order core-alloc-std (or std-alloc-core), because they have inclusion relationships (core::*alloc::*std::*).

For example, if alloc is unavailable then std is always unavailable, and if std is available then core and alloc is always available. alloc-core-std ordering looks somewhat unintuitive because it does not reflect such obvious transitive relations.

I'm not sure exact condition of when these "semantic" order should be chosen over the lexicographic order, but I strongly feel the semantic order is preferred in some situations (for example group_imports = "StdExternalCrate" is set).

Example

rustfmt.toml:

group_imports = "StdExternalCrate"

(and maybe more settings other than StdExternalCrate or group_imports as well...)

Input (random order):

extern crate alloc;

use std::io::File;
use core::num::NonZero;
use alloc::borrow::Cow;

Expected (semantic: core-alloc-std):

extern crate alloc;

use core::num::NonZero;
use alloc::borrow::Cow;
use std::io::File;

Got (lexicographic: alloc-core-std):

extern crate alloc;

use alloc::borrow::Cow;
use core::num::NonZero;
use std::io::File;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants