Skip to content

Fix Issue 6470: Preserve Space after colon if followed by absolute path declaration (::) #6475

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
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add more testcase
  • Loading branch information
FwP-IDN committed Feb 17, 2025
commit 62230c1c60072c563e2c24db3cd84812b11e70f0
37 changes: 28 additions & 9 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_ast::token::{Delimiter, Lit, LitKind};
use rustc_ast::{ForLoopKind, MatchKind, ast, ptr, token};
use rustc_span::{BytePos, Span};
use tracing::debug;

use tracing::field::debug;
use crate::chains::rewrite_chain;
use crate::closures;
use crate::comment::{
Expand All @@ -30,11 +30,7 @@ use crate::spanned::Spanned;
use crate::stmt;
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,
semicolon_for_expr, unicode_str_width, wrap_str,
};
use crate::utils::{colon_spaces, contains_skip, count_newlines, filtered_str_fits, first_line_ends_with, inner_attributes, is_absolute_decl_path, last_line_extendable, last_line_width, mk_sp, outer_attributes, semicolon_for_expr, unicode_str_width, wrap_str};
use crate::vertical::rewrite_with_alignment;
use crate::visitor::FmtVisitor;

Expand Down Expand Up @@ -1880,8 +1876,27 @@ pub(crate) fn wrap_struct_field(
}
}

pub(crate) fn struct_lit_field_separator(config: &Config) -> &str {
colon_spaces(config, false)
pub(crate) fn struct_lit_field_separator(config: &Config, force_space_after_colon: bool) -> &str {
colon_spaces(config, force_space_after_colon)
}

fn extract_ast_path_from_expr(expr: &ast::Expr) -> Option<&ast::Path> {
match &expr.kind {
ast::ExprKind::Call(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr),
ast::ExprKind::MethodCall(box_method_call, ..) => extract_ast_path_from_expr(&*box_method_call.receiver),
ast::ExprKind::Binary(_, left_expr, ..) => extract_ast_path_from_expr(&*left_expr),
ast::ExprKind::Cast(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr),
ast::ExprKind::Type(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr),
ast::ExprKind::Field(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr),
ast::ExprKind::Index(ptr_expr, ..) => extract_ast_path_from_expr(&*ptr_expr),
ast::ExprKind::Range(Some(start_expr), ..) => extract_ast_path_from_expr(&*start_expr),
ast::ExprKind::Path(_, path, ..) => Some(&path),
ast::ExprKind::MacCall(mac, ..) => Some(&(*mac).path),
ast::ExprKind::Struct(ptr_struct_expr, ..) => {
Some(&(*ptr_struct_expr).path)
}
_ => None,
}
}

pub(crate) fn rewrite_field(
Expand All @@ -1901,7 +1916,11 @@ pub(crate) fn rewrite_field(
if field.is_shorthand {
Ok(attrs_str + name)
} else {
let mut separator = String::from(struct_lit_field_separator(context.config));
let force_space_after_colon = match extract_ast_path_from_expr(&field.expr) {
Some(path) => is_absolute_decl_path(path),
_ => false,
};
let mut separator = String::from(struct_lit_field_separator(context.config, force_space_after_colon));
for _ in 0..prefix_max_width.saturating_sub(name.len()) {
separator.push(' ');
}
Expand Down
15 changes: 1 addition & 14 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Rewrite for ast::Local {

if let Some(ref ty) = self.ty {
let force_space_after_colon =
is_ty_kind_with_absolute_decl(&ty.clone().into_inner().kind);
is_ty_kind_with_absolute_decl(&(*ty).kind);
let separator = type_annotation_separator(context.config, force_space_after_colon);

let ty_shape = if pat_str.contains('\n') {
Expand Down Expand Up @@ -2087,19 +2087,6 @@ impl<'a> StaticParts<'a> {
}
}

fn is_ty_kind_with_absolute_decl(ty_kind: &ast::TyKind) -> bool {
match ty_kind {
ast::TyKind::Path(None, ast_path) => {
let segments = &ast_path.segments;
match segments.first() {
Some(path_segment) => path_segment.ident.name == symbol::kw::PathRoot,
None => false,
}
}
_ => false,
}
}

fn rewrite_static(
context: &RewriteContext<'_>,
static_parts: &StaticParts<'_>,
Expand Down
20 changes: 18 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use rustc_ast::ast::{
};
use rustc_ast::ptr;
use rustc_ast_pretty::pprust;
use rustc_span::{BytePos, LocalExpnId, Span, Symbol, SyntaxContext, sym, symbol};
use rustc_span::{sym, symbol, BytePos, LocalExpnId, Span, Symbol, SyntaxContext};
use unicode_width::UnicodeWidthStr;

use crate::comment::{CharClasses, FullCodeCharKind, LineClasses, filter_normal_code};
use crate::comment::{filter_normal_code, CharClasses, FullCodeCharKind, LineClasses};
use crate::config::{Config, StyleEdition};
use crate::rewrite::RewriteContext;
use crate::shape::{Indent, Shape};
Expand Down Expand Up @@ -715,3 +715,19 @@ mod test {
);
}
}


pub fn is_absolute_decl_path(path :&ast::Path) -> bool {
let segments = &path.segments;
match segments.first() {
Some(path_segment) => path_segment.ident.name == symbol::kw::PathRoot,
None => false,
}
}

pub fn is_ty_kind_with_absolute_decl(ty_kind: &ast::TyKind) -> bool {
match ty_kind {
ast::TyKind::Path(None, ast_path) => is_absolute_decl_path(ast_path),
_ => false,
}
}
147 changes: 147 additions & 0 deletions tests/source/issue-6470/case-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ struct SomeStruct {
field8 :i32,
field9: i32,
field10 : i32,

field11:&::some_crate::Thing,
field12: &::some_crate::Thing,
field13 :&::some_crate::Thing,
field14 : &::some_crate::Thing,
}

const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default();
Expand All @@ -28,6 +33,31 @@ const THING8 :i32 = 0;
const THING9: i32 = 0;
const THING10 : i32 = 0;

const THING11:&::some_crate::SomeType = ::some_crate::SomeType::default();
const THING12: &::some_crate::SomeType = ::some_crate::SomeType::default();
const THING13 :&::some_crate::SomeType = ::some_crate::SomeType::default();
const THING14 : &::some_crate::SomeType = ::some_crate::SomeType::default();



static STATIC1: ::some_crate::SomeType = ::some_crate::SomeType::default();
static STATIC2 : ::some_crate::SomeType = ::some_crate::SomeType::default();

static STATIC3: some_crate::SomeType = some_crate::SomeType::default();
static STATIC4 :some_crate::SomeType = some_crate::SomeType::default();
static STATIC5: some_crate::SomeType = some_crate::SomeType::default();
static STATIC6 : some_crate::SomeType = some_crate::SomeType::default();

static STATIC7: i32 = 0;
static STATIC8 :i32 = 0;
static STATIC9: i32 = 0;
static STATIC10 : i32 = 0;

static STATIC11:&::some_crate::SomeType = ::some_crate::SomeType::default();
static STATIC12: &::some_crate::SomeType = ::some_crate::SomeType::default();
static STATIC13 :&::some_crate::SomeType = ::some_crate::SomeType::default();
static STATIC14 : &::some_crate::SomeType = ::some_crate::SomeType::default();

fn main() {
let x1: ::some_crate::SomeType = ::some_crate::SomeType::default();
let x2 : ::some_crate::SomeType = ::some_crate::SomeType::default();
Expand All @@ -41,4 +71,121 @@ fn main() {
let x8 :i32 = 0;
let x9: i32 = 0;
let x10 : i32 = 0;

let x11:&::some_crate::SomeType = ::some_crate::SomeType::default();
let x12 :&::some_crate::SomeType = ::some_crate::SomeType::default();
let x13: &::some_crate::SomeType = ::some_crate::SomeType::default();
let x14 : &::some_crate::SomeType = ::some_crate::SomeType::default();


let y_call = SomeStruct {
field1: ::some_crate::Thing::default(),
field2 : ::some_crate::Thing::default(),

field3:some_crate::Thing::default(),
field4 :some_crate::Thing::default(),
field5: some_crate::Thing::default(),
field6 : some_crate::Thing::default(),

field7:12,
field8 :12,
field9: 12,
field10 : 12,

field11:&::some_crate::Thing::default(),
field12: &::some_crate::Thing::default(),
field13 :&::some_crate::Thing::default(),
field14 : &::some_crate::Thing::default(),
};

let y_method_call = SomeStruct {
field1: ::some_crate::Thing::Default.call(),
field2 : ::some_crate::Thing::Default.call(),

..y_call
};

let y_binary = SomeStruct {
field1: ::some_crate::Thing::Default+ 12,
field2 : ::some_crate::Thing::Default + 12,

..y_call
};

let y_cast = SomeStruct {
field1: ::some_crate::Thing::Default as i32,
field2 : ::some_crate::Thing::Default as i32,

..y_call
};

let y_type = SomeStruct {
field7: ::some_crate::Thing::Default,
field8 : ::some_crate::Thing::Default,

..y_call
};

let y_field = SomeStruct {
field1: ::some_crate::Thing::Default.some_field,
field2 : ::some_crate::Thing::Default.some_field,

..y_call
};

let y_index = SomeStruct {
field1: ::some_crate::Thing::Default[0],
field2 : ::some_crate::Thing::Default[0],

..y_call
};

let y_range = SomeStruct {
field1: ::some_crate::Thing::DefaultStart..12,
field2 : ::some_crate::Thing::DefaultStart..12,

..y_call
};

let y_path = SomeStruct {
field1: ::some_crate::Thing::Default,
field2 : ::some_crate::Thing::Default,

..y_call
};

let y_mac_call = SomeStruct {
field1: ::some_crate::macr!(),
field2 : ::some_crate::macr!(),

..y_call
};

let y_struct = SomeStruct {
field1: ::some_crate::Thing::SomeStruct{
fieldA1: 123,
fieldA2: 123,
},
field2 : ::some_crate::Thing::SomeStruct{
fieldA1: 123,
fieldA2: 123,
},

..y_call
};
}

fn func1(x: ::some_crate::SomeType) {}
fn func2(x : ::some_crate::SomeType) {}
fn func3(x:some_crate::SomeType) {}
fn func4(x :some_crate::SomeType) {}
fn func5(x: some_crate::SomeType) {}
fn func6(x : some_crate::SomeType) {}
fn func7(x:i32) {}
fn func8(x: i32) {}
fn func9(x :i32) {}
fn func10(x : i32) {}
fn func11(x:&::some_crate::SomeType) {}
fn func12(x :&::some_crate::SomeType) {}
fn func13(x: &::some_crate::SomeType) {}
fn func14(x : &::some_crate::SomeType) {}
75 changes: 75 additions & 0 deletions tests/source/issue-6470/case-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
struct SomeStruct {
field1: ::some_crate::Thing,
field2 : ::some_crate::Thing,
field1_enum: ::some_crate::Thing,
field2_enum : ::some_crate::Thing,

field3:some_crate::Thing,
field4 :some_crate::Thing,
Expand All @@ -13,6 +15,11 @@ struct SomeStruct {
field8 :i32,
field9: i32,
field10 : i32,

field11:&::some_crate::Thing,
field12: &::some_crate::Thing,
field13 :&::some_crate::Thing,
field14 : &::some_crate::Thing,
}

const THING1: ::some_crate::SomeType = ::some_crate::SomeType::default();
Expand All @@ -28,6 +35,31 @@ const THING8 :i32 = 0;
const THING9: i32 = 0;
const THING10 : i32 = 0;

const THING11:&::some_crate::SomeType = ::some_crate::SomeType::default();
const THING12: &::some_crate::SomeType = ::some_crate::SomeType::default();
const THING13 :&::some_crate::SomeType = ::some_crate::SomeType::default();
const THING14 : &::some_crate::SomeType = ::some_crate::SomeType::default();



static STATIC1: ::some_crate::SomeType = ::some_crate::SomeType::default();
static STATIC2 : ::some_crate::SomeType = ::some_crate::SomeType::default();

static STATIC3: some_crate::SomeType = some_crate::SomeType::default();
static STATIC4 :some_crate::SomeType = some_crate::SomeType::default();
static STATIC5: some_crate::SomeType = some_crate::SomeType::default();
static STATIC6 : some_crate::SomeType = some_crate::SomeType::default();

static STATIC7: i32 = 0;
static STATIC8 :i32 = 0;
static STATIC9: i32 = 0;
static STATIC10 : i32 = 0;

static STATIC11:&::some_crate::SomeType = ::some_crate::SomeType::default();
static STATIC12: &::some_crate::SomeType = ::some_crate::SomeType::default();
static STATIC13 :&::some_crate::SomeType = ::some_crate::SomeType::default();
static STATIC14 : &::some_crate::SomeType = ::some_crate::SomeType::default();

fn main() {
let x1: ::some_crate::SomeType = ::some_crate::SomeType::default();
let x2 : ::some_crate::SomeType = ::some_crate::SomeType::default();
Expand All @@ -41,4 +73,47 @@ fn main() {
let x8 :i32 = 0;
let x9: i32 = 0;
let x10 : i32 = 0;

let x11:&::some_crate::SomeType = ::some_crate::SomeType::default();
let x12 :&::some_crate::SomeType = ::some_crate::SomeType::default();
let x13: &::some_crate::SomeType = ::some_crate::SomeType::default();
let x14 : &::some_crate::SomeType = ::some_crate::SomeType::default();

let y = SomeStruct {
field1: ::some_crate::Thing::default(),
field2 : ::some_crate::Thing::default(),
field1_enum: ::some_crate::Thing::Enum1,
field2_enum : ::some_crate::Thing::Enum1,


field3:some_crate::Thing::default(),
field4 :some_crate::Thing::default(),
field5: some_crate::Thing::default(),
field6 : some_crate::Thing::default(),

field7:12,
field8 :12,
field9: 12,
field10 : 12,

field11:&::some_crate::Thing::default(),
field12: &::some_crate::Thing::default(),
field13 :&::some_crate::Thing::default(),
field14 : &::some_crate::Thing::default(),
};
}

fn func1(x: ::some_crate::SomeType) {}
fn func2(x : ::some_crate::SomeType) {}
fn func3(x:some_crate::SomeType) {}
fn func4(x :some_crate::SomeType) {}
fn func5(x: some_crate::SomeType) {}
fn func6(x : some_crate::SomeType) {}
fn func7(x:i32) {}
fn func8(x: i32) {}
fn func9(x :i32) {}
fn func10(x : i32) {}
fn func11(x:&::some_crate::SomeType) {}
fn func12(x :&::some_crate::SomeType) {}
fn func13(x: &::some_crate::SomeType) {}
fn func14(x : &::some_crate::SomeType) {}
Loading
Loading