Skip to content

Commit 281c97c

Browse files
committed
Auto merge of rust-lang#156518 - estebank:match-rustfmt, r=mejrs
[style] rustfmt `match`es with comments in or-patterns Using rust-lang/rustfmt#6893, I reformatted the whole codebase. The result is that `match`es that *should have* been formatted under normal circumstances but are getting skipped now got their expected format. These match expressions were being entirely skipped because they contain or-patterns with comments in between patterns, causing rustfmt to bail out entirely. The or-patterns with comments themselves remain untouched, but now the match arm bodies and other patterns without comments do get formatted under that PR. Because the fix in rustfmt isn't landed yet, I reworked some of the or-patterns with comments so that formatting doesn't regress. Tried doing this only in larger blocks that are more likely to regress in the meantime. (Introduced and) removed a bunch of stray backticks \` likely left after an editor autoclosed the intended closing \`, resulting in <code>\`name\`\`</code> in comments.
2 parents d3cd040 + 5cd51c0 commit 281c97c

39 files changed

Lines changed: 472 additions & 361 deletions

File tree

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,12 +1557,10 @@ impl Expr {
15571557
}
15581558

15591559
match &self.kind {
1560-
ExprKind::Closure(closure) => {
1561-
match closure.fn_decl.output {
1562-
FnRetTy::Default(_) => ExprPrecedence::Jump,
1563-
FnRetTy::Ty(_) => prefix_attrs_precedence(&self.attrs),
1564-
}
1565-
}
1560+
ExprKind::Closure(closure) => match closure.fn_decl.output {
1561+
FnRetTy::Default(_) => ExprPrecedence::Jump,
1562+
FnRetTy::Ty(_) => prefix_attrs_precedence(&self.attrs),
1563+
},
15661564

15671565
ExprKind::Break(_ /*label*/, value)
15681566
| ExprKind::Ret(value)
@@ -1584,18 +1582,16 @@ impl Expr {
15841582
ExprKind::Binary(op, ..) => op.node.precedence(),
15851583
ExprKind::Cast(..) => ExprPrecedence::Cast,
15861584

1587-
ExprKind::Assign(..) |
1588-
ExprKind::AssignOp(..) => ExprPrecedence::Assign,
1585+
ExprKind::Assign(..) | ExprKind::AssignOp(..) => ExprPrecedence::Assign,
15891586

15901587
// Unary, prefix
1591-
ExprKind::AddrOf(..)
1588+
ExprKind::AddrOf(..) => ExprPrecedence::Prefix,
1589+
15921590
// Here `let pats = expr` has `let pats =` as a "unary" prefix of `expr`.
15931591
// However, this is not exactly right. When `let _ = a` is the LHS of a binop we
15941592
// need parens sometimes. E.g. we can print `(let _ = a) && b` as `let _ = a && b`
15951593
// but we need to print `(let _ = a) < b` as-is with parens.
1596-
| ExprKind::Let(..)
1597-
| ExprKind::Move(..)
1598-
| ExprKind::Unary(..) => ExprPrecedence::Prefix,
1594+
ExprKind::Let(..) | ExprKind::Move(..) | ExprKind::Unary(..) => ExprPrecedence::Prefix,
15991595

16001596
// Need parens if and only if there are prefix attributes.
16011597
ExprKind::Array(_)

compiler/rustc_ast/src/token.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,10 @@ impl Token {
688688
Lifetime(..) | // labeled loop
689689
Pound => true, // expression attributes
690690
OpenInvisible(InvisibleOrigin::MetaVar(
691-
MetaVarKind::Block |
692-
MetaVarKind::Expr { .. } |
693-
MetaVarKind::Literal |
694-
MetaVarKind::Path
691+
MetaVarKind::Block
692+
| MetaVarKind::Expr { .. }
693+
| MetaVarKind::Literal
694+
| MetaVarKind::Path,
695695
)) => true,
696696
_ => false,
697697
}
@@ -717,12 +717,12 @@ impl Token {
717717
Shl => true, // path (double UFCS)
718718
Or => matches!(pat_kind, PatWithOr), // leading vert `|` or-pattern
719719
OpenInvisible(InvisibleOrigin::MetaVar(
720-
MetaVarKind::Expr { .. } |
721-
MetaVarKind::Literal |
722-
MetaVarKind::Meta { .. } |
723-
MetaVarKind::Pat(_) |
724-
MetaVarKind::Path |
725-
MetaVarKind::Ty { .. }
720+
MetaVarKind::Expr { .. }
721+
| MetaVarKind::Literal
722+
| MetaVarKind::Meta { .. }
723+
| MetaVarKind::Pat(_)
724+
| MetaVarKind::Path
725+
| MetaVarKind::Ty { .. },
726726
)) => true,
727727
_ => false,
728728
}
@@ -733,20 +733,19 @@ impl Token {
733733
match self.uninterpolate().kind {
734734
Ident(name, is_raw) =>
735735
ident_can_begin_type(name, self.span, is_raw), // type name or keyword
736-
OpenParen | // tuple
737-
OpenBracket | // array
738-
Bang | // never
739-
Star | // raw pointer
740-
And | // reference
741-
AndAnd | // double reference
742-
Question | // maybe bound in trait object
743-
Lifetime(..) | // lifetime bound in trait object
744-
Lt | Shl | // associated path
745-
PathSep => true, // global path
746-
OpenInvisible(InvisibleOrigin::MetaVar(
747-
MetaVarKind::Ty { .. } |
748-
MetaVarKind::Path
749-
)) => true,
736+
OpenParen // tuple
737+
| OpenBracket // array
738+
| Bang // never
739+
| Star // raw pointer
740+
| And // reference
741+
| AndAnd // double reference
742+
| Question // maybe bound in trait object
743+
| Lifetime(..) // lifetime bound in trait object
744+
| Lt | Shl // associated path
745+
| PathSep => true, // global path
746+
OpenInvisible(InvisibleOrigin::MetaVar(MetaVarKind::Ty { .. } | MetaVarKind::Path)) => {
747+
true
748+
}
750749
// For anonymous structs or unions, which only appear in specific positions
751750
// (type of struct fields or union fields), we don't consider them as regular types
752751
_ => false,

compiler/rustc_borrowck/src/def_use.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ pub(crate) fn categorize(context: PlaceContext) -> Option<DefUse> {
6060
PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect) |
6161
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy) |
6262
PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) |
63-
PlaceContext::MutatingUse(MutatingUseContext::Retag) =>
64-
Some(DefUse::Use),
63+
PlaceContext::MutatingUse(MutatingUseContext::Retag) => Some(DefUse::Use),
6564

6665
///////////////////////////////////////////////////////////////////////////
6766
// DROP USES
@@ -70,9 +69,7 @@ pub(crate) fn categorize(context: PlaceContext) -> Option<DefUse> {
7069
// call to `std::mem::drop()`). For the purposes of NLL,
7170
// uses in drop are special because `#[may_dangle]`
7271
// attributes can affect whether lifetimes must be live.
73-
74-
PlaceContext::MutatingUse(MutatingUseContext::Drop) =>
75-
Some(DefUse::Drop),
72+
PlaceContext::MutatingUse(MutatingUseContext::Drop) => Some(DefUse::Drop),
7673

7774
// Debug info is neither def nor use.
7875
PlaceContext::NonUse(NonUseContext::VarDebugInfo) => None,

compiler/rustc_borrowck/src/lib.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -833,19 +833,21 @@ impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a,
833833
NonDivergingIntrinsic::CopyNonOverlapping(..) => span_bug!(
834834
span,
835835
"Unexpected CopyNonOverlapping, should only appear after lower_intrinsics",
836-
)
837-
}
836+
),
837+
},
838838
// Only relevant for mir typeck
839-
StatementKind::AscribeUserType(..)
839+
StatementKind::AscribeUserType(..) => {}
840840
// Only relevant for liveness and unsafeck
841-
| StatementKind::PlaceMention(..)
841+
StatementKind::PlaceMention(..) => {}
842842
// Doesn't have any language semantics
843-
| StatementKind::Coverage(..)
843+
StatementKind::Coverage(..) => {}
844844
// These do not actually affect borrowck
845-
| StatementKind::ConstEvalCounter
846-
| StatementKind::StorageLive(..) => {}
845+
StatementKind::ConstEvalCounter | StatementKind::StorageLive(..) => {}
847846
// This does not affect borrowck
848-
StatementKind::BackwardIncompatibleDropHint { place, reason: BackwardIncompatibleDropReason::Edition2024 } => {
847+
StatementKind::BackwardIncompatibleDropHint {
848+
place,
849+
reason: BackwardIncompatibleDropReason::Edition2024,
850+
} => {
849851
self.check_backward_incompatible_drop(location, **place, state);
850852
}
851853
StatementKind::StorageDead(local) => {
@@ -857,8 +859,7 @@ impl<'a, 'tcx> ResultsVisitor<'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a,
857859
state,
858860
);
859861
}
860-
StatementKind::Nop
861-
| StatementKind::SetDiscriminant { .. } => {
862+
StatementKind::Nop | StatementKind::SetDiscriminant { .. } => {
862863
bug!("Statement not allowed in this MIR phase")
863864
}
864865
}
@@ -2238,15 +2239,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
22382239
// None case => assigning to `x` does not require `x` be initialized.
22392240
for (place_base, elem) in place.iter_projections().rev() {
22402241
match elem {
2241-
ProjectionElem::Index(_/*operand*/) |
2242-
ProjectionElem::OpaqueCast(_) |
2243-
ProjectionElem::ConstantIndex { .. } |
2242+
ProjectionElem::Index(_/*operand*/)
2243+
| ProjectionElem::OpaqueCast(_)
22442244
// assigning to P[i] requires P to be valid.
2245-
ProjectionElem::Downcast(_/*adt_def*/, _/*variant_idx*/) =>
2245+
| ProjectionElem::ConstantIndex { .. }
22462246
// assigning to (P->variant) is okay if assigning to `P` is okay
22472247
//
22482248
// FIXME: is this true even if P is an adt with a dtor?
2249-
{ }
2249+
| ProjectionElem::Downcast(_/*adt_def*/, _/*variant_idx*/) =>
2250+
{}
22502251

22512252
ProjectionElem::UnwrapUnsafeBinder(_) => {
22522253
check_parent_of_field(self, location, place_base, span, state);
@@ -2255,8 +2256,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
22552256
// assigning to (*P) requires P to be initialized
22562257
ProjectionElem::Deref => {
22572258
self.check_if_full_path_is_moved(
2258-
location, InitializationRequiringAction::Use,
2259-
(place_base, span), state);
2259+
location,
2260+
InitializationRequiringAction::Use,
2261+
(place_base, span),
2262+
state,
2263+
);
22602264
// (base initialized; no need to
22612265
// recur further)
22622266
break;
@@ -2275,8 +2279,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
22752279
match base_ty.kind() {
22762280
ty::Adt(def, _) if def.has_dtor(tcx) => {
22772281
self.check_if_path_or_subpath_is_moved(
2278-
location, InitializationRequiringAction::Assignment,
2279-
(place_base, span), state);
2282+
location,
2283+
InitializationRequiringAction::Assignment,
2284+
(place_base, span),
2285+
state,
2286+
);
22802287

22812288
// (base initialized; no need to
22822289
// recur further)

compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
5656
StatementKind::Intrinsic(NonDivergingIntrinsic::Assume(op)) => {
5757
self.consume_operand(location, op);
5858
}
59-
StatementKind::Intrinsic(NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping {
60-
src,
61-
dst,
62-
count,
63-
})) => {
59+
StatementKind::Intrinsic(NonDivergingIntrinsic::CopyNonOverlapping(
60+
CopyNonOverlapping { src, dst, count },
61+
)) => {
6462
self.consume_operand(location, src);
6563
self.consume_operand(location, dst);
6664
self.consume_operand(location, count);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
454454
OperandValue::Pair(lldata, llextra)
455455
}
456456
mir::CastKind::PointerCoercion(
457-
PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer, _
457+
PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer,
458+
_,
458459
) => {
459460
bug!("{kind:?} is for borrowck, and should never appear in codegen");
460461
}
461-
mir::CastKind::PtrToPtr
462-
if bx.cx().is_backend_scalar_pair(operand.layout) =>
463-
{
462+
mir::CastKind::PtrToPtr if bx.cx().is_backend_scalar_pair(operand.layout) => {
464463
if let OperandValue::Pair(data_ptr, meta) = operand.val {
465464
if bx.cx().is_backend_scalar_pair(cast) {
466465
OperandValue::Pair(data_ptr, meta)
@@ -483,7 +482,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
483482
// path as the other integer-to-X casts.
484483
| mir::CastKind::PointerWithExposedProvenance => {
485484
let imm = operand.immediate();
486-
let abi::BackendRepr::Scalar(from_scalar) = operand.layout.backend_repr else {
485+
let abi::BackendRepr::Scalar(from_scalar) = operand.layout.backend_repr
486+
else {
487487
bug!("Found non-scalar for operand {operand:?}");
488488
};
489489
let from_backend_ty = bx.cx().immediate_backend_type(operand.layout);
@@ -498,11 +498,18 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
498498
bug!("Found non-scalar for cast {cast:?}");
499499
};
500500

501-
self.cast_immediate(bx, imm, from_scalar, from_backend_ty, to_scalar, to_backend_ty)
502-
.map(OperandValue::Immediate)
503-
.unwrap_or_else(|| {
504-
bug!("Unsupported cast of {operand:?} to {cast:?}");
505-
})
501+
self.cast_immediate(
502+
bx,
503+
imm,
504+
from_scalar,
505+
from_backend_ty,
506+
to_scalar,
507+
to_backend_ty,
508+
)
509+
.map(OperandValue::Immediate)
510+
.unwrap_or_else(|| {
511+
bug!("Unsupported cast of {operand:?} to {cast:?}");
512+
})
506513
}
507514
mir::CastKind::Transmute | mir::CastKind::Subtype => {
508515
self.codegen_transmute_operand(bx, operand, cast)
@@ -553,6 +560,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
553560
move_annotation: None,
554561
}
555562
}
563+
556564
mir::Rvalue::BinaryOp(op, (ref lhs, ref rhs)) => {
557565
let lhs = self.codegen_operand(bx, lhs);
558566
let rhs = self.codegen_operand(bx, rhs);
@@ -666,7 +674,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
666674
};
667675
OperandRef { val: OperandValue::Immediate(static_), layout, move_annotation: None }
668676
}
677+
669678
mir::Rvalue::Use(ref operand, _) => self.codegen_operand(bx, operand),
679+
670680
mir::Rvalue::Repeat(ref elem, len_const) => {
671681
// All arrays have `BackendRepr::Memory`, so only the ZST cases
672682
// end up here. Anything else forces the destination local to be
@@ -682,6 +692,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
682692
move_annotation: None,
683693
}
684694
}
695+
685696
mir::Rvalue::Aggregate(ref kind, ref fields) => {
686697
let (variant_index, active_field_index) = match **kind {
687698
mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
@@ -719,12 +730,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
719730
}
720731
}
721732
}
733+
722734
mir::Rvalue::WrapUnsafeBinder(ref operand, binder_ty) => {
723735
let operand = self.codegen_operand(bx, operand);
724736
let binder_ty = self.monomorphize(binder_ty);
725737
let layout = bx.cx().layout_of(binder_ty);
726738
OperandRef { val: operand.val, layout, move_annotation: None }
727739
}
740+
728741
mir::Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"),
729742
}
730743
}

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ fn const_to_valtree_inner<'tcx>(
109109
// switch to the base type.
110110
place.layout = ecx.layout_of(*base).unwrap();
111111
ensure_sufficient_stack(|| const_to_valtree_inner(ecx, &place, num_nodes))
112-
},
113-
112+
}
114113

115114
ty::RawPtr(_, _) => {
116115
// Not all raw pointers are allowed, as we cannot properly test them for
@@ -137,23 +136,19 @@ fn const_to_valtree_inner<'tcx>(
137136
// agree with runtime equality tests.
138137
ty::FnPtr(..) => Err(ValTreeCreationError::NonSupportedType(ty)),
139138

140-
ty::Ref(_, _, _) => {
139+
ty::Ref(_, _, _) => {
141140
let derefd_place = ecx.deref_pointer(place).report_err()?;
142141
const_to_valtree_inner(ecx, &derefd_place, num_nodes)
143142
}
144143

145-
ty::Str | ty::Slice(_) | ty::Array(_, _) => {
146-
slice_branches(ecx, place, num_nodes)
147-
}
144+
ty::Str | ty::Slice(_) | ty::Array(_, _) => slice_branches(ecx, place, num_nodes),
148145
// Trait objects are not allowed in type level constants, as we have no concept for
149146
// resolving their backing type, even if we can do that at const eval time. We may
150147
// hypothetically be able to allow `dyn StructuralPartialEq` trait objects in the future,
151148
// but it is unclear if this is useful.
152149
ty::Dynamic(..) => Err(ValTreeCreationError::NonSupportedType(ty)),
153150

154-
ty::Tuple(elem_tys) => {
155-
branches(ecx, place, elem_tys.len(), None, num_nodes)
156-
}
151+
ty::Tuple(elem_tys) => branches(ecx, place, elem_tys.len(), None, num_nodes),
157152

158153
ty::Adt(def, _) => {
159154
if def.is_union() {
@@ -163,22 +158,30 @@ fn const_to_valtree_inner<'tcx>(
163158
}
164159

165160
let variant = ecx.read_discriminant(place).report_err()?;
166-
branches(ecx, place, def.variant(variant).fields.len(), def.is_enum().then_some(variant), num_nodes)
161+
branches(
162+
ecx,
163+
place,
164+
def.variant(variant).fields.len(),
165+
def.is_enum().then_some(variant),
166+
num_nodes,
167+
)
167168
}
168169

170+
// FIXME(oli-obk): we could look behind opaque types
171+
ty::Alias(..) => Err(ValTreeCreationError::NonSupportedType(ty)),
172+
173+
// FIXME(oli-obk): we can probably encode closures just like structs
174+
ty::Closure(..) => Err(ValTreeCreationError::NonSupportedType(ty)),
175+
169176
ty::Never
170177
| ty::Error(_)
171178
| ty::Foreign(..)
172179
| ty::Infer(ty::FreshIntTy(_))
173180
| ty::Infer(ty::FreshFloatTy(_))
174-
// FIXME(oli-obk): we could look behind opaque types
175-
| ty::Alias(..)
176181
| ty::Param(_)
177182
| ty::Bound(..)
178183
| ty::Placeholder(..)
179184
| ty::Infer(_)
180-
// FIXME(oli-obk): we can probably encode closures just like structs
181-
| ty::Closure(..)
182185
| ty::CoroutineClosure(..)
183186
| ty::Coroutine(..)
184187
| ty::CoroutineWitness(..)

0 commit comments

Comments
 (0)