rust-lang / rust-lang/rustfmt

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

Open
#6,290 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-imports C-feature-request P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

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;

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing rustfmt's import reordering behavior for group_imports = "StdExternalCrate", using the alloc, core, and std example as the expected case. Determine when semantic ordering should replace lexicographic ordering, then verify the formatted output against the issue's example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.