Skip to content

Commit 76eb077

Browse files
ytmimicalebcartwright
authored andcommitted
Retain qualified path when rewriting struct literals
Fixes 5151 Details about the qualified path are now passed along so that rustfmt can include them when formatting struct literals.
1 parent 0b2fd9b commit 76eb077

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

Diff for: src/expr.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,21 @@ pub(crate) fn format_expr(
108108
ast::ExprKind::Unary(op, ref subexpr) => rewrite_unary_op(context, op, subexpr, shape),
109109
ast::ExprKind::Struct(ref struct_expr) => {
110110
let ast::StructExpr {
111-
fields, path, rest, ..
111+
qself,
112+
fields,
113+
path,
114+
rest,
112115
} = &**struct_expr;
113-
rewrite_struct_lit(context, path, fields, rest, &expr.attrs, expr.span, shape)
116+
rewrite_struct_lit(
117+
context,
118+
path,
119+
qself.as_ref(),
120+
fields,
121+
rest,
122+
&expr.attrs,
123+
expr.span,
124+
shape,
125+
)
114126
}
115127
ast::ExprKind::Tup(ref items) => {
116128
rewrite_tuple(context, items.iter(), expr.span, shape, items.len() == 1)
@@ -1511,6 +1523,7 @@ fn struct_lit_can_be_aligned(fields: &[ast::ExprField], has_base: bool) -> bool
15111523
fn rewrite_struct_lit<'a>(
15121524
context: &RewriteContext<'_>,
15131525
path: &ast::Path,
1526+
qself: Option<&ast::QSelf>,
15141527
fields: &'a [ast::ExprField],
15151528
struct_rest: &ast::StructRest,
15161529
attrs: &[ast::Attribute],
@@ -1527,7 +1540,7 @@ fn rewrite_struct_lit<'a>(
15271540

15281541
// 2 = " {".len()
15291542
let path_shape = shape.sub_width(2)?;
1530-
let path_str = rewrite_path(context, PathContext::Expr, None, path, path_shape)?;
1543+
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;
15311544

15321545
let has_base_or_rest = match struct_rest {
15331546
ast::StructRest::None if fields.is_empty() => return Some(format!("{} {{}}", path_str)),

Diff for: tests/target/issue-5151/minimum_example.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(more_qualified_paths)]
2+
3+
struct Struct {}
4+
5+
trait Trait {
6+
type Type;
7+
}
8+
9+
impl Trait for Struct {
10+
type Type = Self;
11+
}
12+
13+
fn main() {
14+
// keep the qualified path details
15+
let _ = <Struct as Trait>::Type {};
16+
}

0 commit comments

Comments
 (0)