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

Implement version-sort for imports in style_edition 2024 #6284

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config_proc_macro/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use quote::{ToTokens, quote};

pub fn fold_quote<F, I, T>(input: impl Iterator<Item = I>, f: F) -> TokenStream
where
Expand Down
12 changes: 6 additions & 6 deletions src/attr.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//! Format attributes and meta items.

use rustc_ast::ast;
use rustc_ast::HasAttrs;
use rustc_span::{symbol::sym, Span};
use rustc_ast::ast;
use rustc_span::{Span, symbol::sym};

use self::doc_comment::DocCommentFormatter;
use crate::comment::{contains_comment, rewrite_doc_comment, CommentStyle};
use crate::config::lists::*;
use crate::comment::{CommentStyle, contains_comment, rewrite_doc_comment};
use crate::config::IndentStyle;
use crate::config::lists::*;
use crate::expr::rewrite_literal;
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use crate::lists::{ListFormatting, Separator, definitive_tactic, itemize_list, write_list};
use crate::overflow;
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
use crate::shape::Shape;
use crate::source_map::SpanUtils;
use crate::types::{rewrite_path, PathContext};
use crate::types::{PathContext, rewrite_path};
use crate::utils::{count_newlines, mk_sp};

mod doc_comment;
Expand Down
8 changes: 4 additions & 4 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(rustc_private)]

use anyhow::{format_err, Result};
use anyhow::{Result, format_err};

use io::Error as IoError;
use thiserror::Error;
Expand All @@ -11,15 +11,15 @@ use tracing_subscriber::EnvFilter;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{self, stdout, Read, Write};
use std::io::{self, Read, Write, stdout};
use std::path::{Path, PathBuf};
use std::str::FromStr;

use getopts::{Matches, Options};

use crate::rustfmt::{
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName,
FormatReportFormatterBuilder, Input, Session, StyleEdition, Verbosity, Version,
CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName,
FormatReportFormatterBuilder, Input, Session, StyleEdition, Verbosity, Version, load_config,
};

const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rustfmt/issues/new?labels=bug";
Expand Down
4 changes: 2 additions & 2 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ use std::borrow::Cow;
use std::cmp::min;

use rustc_ast::{ast, ptr};
use rustc_span::{symbol, BytePos, Span};
use rustc_span::{BytePos, Span, symbol};

use crate::comment::{rewrite_comment, CharClasses, FullCodeCharKind, RichChar};
use crate::comment::{CharClasses, FullCodeCharKind, RichChar, rewrite_comment};
use crate::config::{IndentStyle, StyleEdition};
use crate::expr::rewrite_call;
use crate::lists::extract_pre_comment;
Expand Down
6 changes: 3 additions & 3 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ use rustc_span::Span;
use thin_vec::thin_vec;

use crate::attr::get_attrs_from_stmt;
use crate::config::lists::*;
use crate::config::StyleEdition;
use crate::config::lists::*;
use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
use crate::items::{span_hi_for_param, span_lo_for_param};
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use crate::lists::{ListFormatting, Separator, definitive_tactic, itemize_list, write_list};
use crate::overflow::OverflowableItem;
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
use crate::shape::Shape;
use crate::source_map::SpanUtils;
use crate::types::rewrite_bound_params;
use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};
use crate::utils::{NodeIdExt, last_line_width, left_most_sub_expr, stmt_expr};

// This module is pretty messy because of the rules around closures and blocks:
// FIXME - the below is probably no longer true in full.
Expand Down
4 changes: 2 additions & 2 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

use std::{borrow::Cow, iter};

use itertools::{multipeek, MultiPeek};
use itertools::{MultiPeek, multipeek};
use rustc_span::Span;

use crate::config::Config;
use crate::rewrite::{RewriteContext, RewriteErrorExt, RewriteResult};
use crate::shape::{Indent, Shape};
use crate::string::{rewrite_string, StringFormat};
use crate::string::{StringFormat, rewrite_string};
use crate::utils::{
count_newlines, first_line_width, last_line_width, trim_left_preserve_layout,
trimmed_last_line_width, unicode_str_width,
Expand Down
2 changes: 1 addition & 1 deletion src/config/file_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{cmp, fmt, iter, str};

use rustc_data_structures::sync::Lrc;
use rustc_span::SourceFile;
use serde::{ser, Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Deserializer, Serialize, Serializer, ser};
use serde_json as json;
use thiserror::Error;

Expand Down
4 changes: 2 additions & 2 deletions src/config/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_imports)]

use std::collections::{hash_set, HashSet};
use std::collections::{HashSet, hash_set};
use std::fmt;
use std::path::{Path, PathBuf};
use std::str::FromStr;
Expand All @@ -11,10 +11,10 @@ use serde::de::{SeqAccess, Visitor};
use serde::ser::SerializeSeq;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::config::Config;
use crate::config::file_lines::FileLines;
use crate::config::lists::*;
use crate::config::macro_names::MacroSelectors;
use crate::config::Config;

#[config_type]
pub enum NewlineStyle {
Expand Down
2 changes: 1 addition & 1 deletion src/emitter/checkstyle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use self::xml::XmlEscaped;
use super::*;
use crate::rustfmt_diff::{make_diff, DiffLine, Mismatch};
use crate::rustfmt_diff::{DiffLine, Mismatch, make_diff};

mod xml;

Expand Down
2 changes: 1 addition & 1 deletion src/emitter/json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::rustfmt_diff::{make_diff, DiffLine, Mismatch};
use crate::rustfmt_diff::{DiffLine, Mismatch, make_diff};
use serde::Serialize;
use serde_json::to_string as to_json_string;

Expand Down
2 changes: 1 addition & 1 deletion src/emitter/modified_lines.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use crate::rustfmt_diff::{make_diff, ModifiedLines};
use crate::rustfmt_diff::{ModifiedLines, make_diff};

#[derive(Debug, Default)]
pub(crate) struct ModifiedLinesEmitter;
Expand Down
18 changes: 9 additions & 9 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@ use std::cmp::min;

use itertools::Itertools;
use rustc_ast::token::{Delimiter, Lit, LitKind};
use rustc_ast::{ast, ptr, token, ForLoopKind, MatchKind};
use rustc_ast::{ForLoopKind, MatchKind, ast, ptr, token};
use rustc_span::{BytePos, Span};

use crate::chains::rewrite_chain;
use crate::closures;
use crate::comment::{
combine_strs_with_missing_comments, contains_comment, recover_comment_removed, rewrite_comment,
rewrite_missing_comment, CharClasses, FindUncommented,
CharClasses, FindUncommented, combine_strs_with_missing_comments, contains_comment,
recover_comment_removed, rewrite_comment, rewrite_missing_comment,
};
use crate::config::lists::*;
use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, StyleEdition};
use crate::lists::{
definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape,
struct_lit_tactic, write_list, ListFormatting, Separator,
ListFormatting, Separator, definitive_tactic, itemize_list, shape_for_tactic,
struct_lit_formatting, struct_lit_shape, struct_lit_tactic, write_list,
};
use crate::macros::{rewrite_macro, MacroPosition};
use crate::macros::{MacroPosition, rewrite_macro};
use crate::matches::rewrite_match;
use crate::overflow::{self, IntoOverflowableItem, OverflowableItem};
use crate::pairs::{rewrite_all_pairs, rewrite_pair, PairParts};
use crate::pairs::{PairParts, rewrite_all_pairs, rewrite_pair};
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
use crate::shape::{Indent, Shape};
use crate::source_map::{LineRangeUtils, SpanUtils};
use crate::spanned::Spanned;
use crate::stmt;
use crate::string::{rewrite_string, StringFormat};
use crate::types::{rewrite_path, PathContext};
use crate::string::{StringFormat, rewrite_string};
use crate::types::{PathContext, rewrite_path};
use crate::utils::{
colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with,
inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
Expand Down
2 changes: 1 addition & 1 deletion src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::parse::parser::{DirectoryOwnership, Parser, ParserError};
use crate::parse::session::ParseSess;
use crate::utils::{contains_skip, count_newlines};
use crate::visitor::FmtVisitor;
use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session};
use crate::{ErrorKind, FormatReport, Input, Session, modules, source_file};

mod generated;
mod newline_style;
Expand Down
2 changes: 1 addition & 1 deletion src/git-rustfmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustfmt_nightly as rustfmt;
use tracing_subscriber::EnvFilter;

use crate::rustfmt::{
load_config, CliOptions, FormatReportFormatterBuilder, Input, Session, Version,
CliOptions, FormatReportFormatterBuilder, Input, Session, Version, load_config,
};

fn prune_files(files: Vec<&str>) -> Vec<&str> {
Expand Down
46 changes: 26 additions & 20 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ use itertools::Itertools;

use rustc_ast::ast::{self, UseTreeKind};
use rustc_span::{
BytePos, DUMMY_SP, Span,
symbol::{self, sym},
BytePos, Span, DUMMY_SP,
};

use crate::comment::combine_strs_with_missing_comments;
use crate::config::lists::*;
use crate::config::ImportGranularity;
use crate::config::lists::*;
use crate::config::{Edition, IndentStyle, StyleEdition};
use crate::lists::{
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
ListFormatting, ListItem, Separator, definitive_tactic, itemize_list, write_list,
};
use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt, RewriteResult};
use crate::shape::Shape;
use crate::sort::version_sort;
use crate::source_map::SpanUtils;
use crate::spanned::Spanned;
use crate::utils::{is_same_visibility, mk_sp, rewrite_ident};
Expand Down Expand Up @@ -923,7 +924,7 @@ impl Ord for UseSegment {
| (Crate(ref a), Crate(ref b)) => match (a, b) {
(Some(sa), Some(sb)) => {
if self.style_edition >= StyleEdition::Edition2024 {
sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#"))
version_sort(sa.trim_start_matches("r#"), sb.trim_start_matches("r#"))
} else {
a.cmp(b)
}
Expand All @@ -937,20 +938,26 @@ impl Ord for UseSegment {
} else {
(pia.as_str(), pib.as_str())
};
// snake_case < CamelCase < UPPER_SNAKE_CASE
if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
return Ordering::Greater;
}
if ia.starts_with(char::is_lowercase) && ib.starts_with(char::is_uppercase) {
return Ordering::Less;
}
if is_upper_snake_case(ia) && !is_upper_snake_case(ib) {
return Ordering::Greater;
}
if !is_upper_snake_case(ia) && is_upper_snake_case(ib) {
return Ordering::Less;
}
let ident_ord = ia.cmp(ib);

let ident_ord = if self.style_edition >= StyleEdition::Edition2024 {
version_sort(ia, ib)
} else {
// snake_case < CamelCase < UPPER_SNAKE_CASE
if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) {
return Ordering::Greater;
}
if ia.starts_with(char::is_lowercase) && ib.starts_with(char::is_uppercase) {
return Ordering::Less;
}
if is_upper_snake_case(ia) && !is_upper_snake_case(ib) {
return Ordering::Greater;
}
if !is_upper_snake_case(ia) && is_upper_snake_case(ib) {
return Ordering::Less;
}
ia.cmp(ib)
};

if ident_ord != Ordering::Equal {
return ident_ord;
}
Expand All @@ -959,8 +966,7 @@ impl Ord for UseSegment {
(Some(_), None) => Ordering::Greater,
(Some(aas), Some(abs)) => {
if self.style_edition >= StyleEdition::Edition2024 {
aas.trim_start_matches("r#")
.cmp(abs.trim_start_matches("r#"))
version_sort(aas.trim_start_matches("r#"), abs.trim_start_matches("r#"))
} else {
aas.cmp(abs)
}
Expand Down
17 changes: 8 additions & 9 deletions src/items.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
// Formatting top-level items - functions, structs, enums, traits, impls.

use std::borrow::Cow;
use std::cmp::{max, min, Ordering};
use std::cmp::{Ordering, max, min};

use regex::Regex;
use rustc_ast::visit;
use rustc_ast::{ast, ptr};
use rustc_span::{symbol, BytePos, Span, DUMMY_SP};
use rustc_span::{BytePos, DUMMY_SP, Span, symbol};

use crate::attr::filter_inline_attrs;
use crate::comment::{
combine_strs_with_missing_comments, contains_comment, is_last_comment_block,
FindUncommented, combine_strs_with_missing_comments, contains_comment, is_last_comment_block,
recover_comment_removed, recover_missing_comment_in_span, rewrite_missing_comment,
FindUncommented,
};
use crate::config::lists::*;
use crate::config::{BraceStyle, Config, IndentStyle, StyleEdition};
use crate::expr::{
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, rewrite_let_else_block,
RhsAssignKind, RhsTactics,
RhsAssignKind, RhsTactics, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
rewrite_assign_rhs_with, rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments,
rewrite_let_else_block,
};
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use crate::macros::{rewrite_macro, MacroPosition};
use crate::lists::{ListFormatting, Separator, definitive_tactic, itemize_list, write_list};
use crate::macros::{MacroPosition, rewrite_macro};
use crate::overflow;
use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
use crate::shape::{Indent, Shape};
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ use crate::shape::Indent;
use crate::utils::indent_next_line;

pub use crate::config::{
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle,
Range, StyleEdition, Verbosity, Version,
CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle, Range,
StyleEdition, Verbosity, Version, load_config,
};

pub use crate::format_report_formatter::{FormatReportFormatter, FormatReportFormatterBuilder};
Expand Down Expand Up @@ -93,6 +93,7 @@ mod rewrite;
pub(crate) mod rustfmt_diff;
mod shape;
mod skip;
mod sort;
pub(crate) mod source_file;
pub(crate) mod source_map;
mod spanned;
Expand Down
2 changes: 1 addition & 1 deletion src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::iter::Peekable;

use rustc_span::BytePos;

use crate::comment::{find_comment_end, rewrite_comment, FindUncommented};
use crate::comment::{FindUncommented, find_comment_end, rewrite_comment};
use crate::config::lists::*;
use crate::config::{Config, IndentStyle};
use crate::rewrite::{RewriteContext, RewriteError, RewriteResult};
Expand Down
Loading
Loading