Skip to content

Commit e9149ca

Browse files
committed
Auto merge of #69759 - Centril:rollup-0m0yh1l, r=Centril
Rollup of 6 pull requests Successful merges: - #69656 (Use .next() instead of .nth(0) on iterators.) - #69674 (Rename DefKind::Method and TraitItemKind::Method ) - #69676 (Pass correct place to `discriminant_switch_effect`) - #69706 (Use subslice patterns in slice methods) - #69714 (Make PlaceRef take just one lifetime) - #69727 (Avoid using `unwrap()` in suggestions) Failed merges: - #69589 (ast: `Mac`/`Macro` -> `MacCall`) - #69680 (rustc_expand: Factor out `Annotatable::into_tokens` to a separate method) r? @ghost
2 parents 4a1b69d + 7f53c32 commit e9149ca

File tree

78 files changed

+215
-222
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+215
-222
lines changed

src/libcore/slice/mod.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<T> [T] {
103103
#[stable(feature = "rust1", since = "1.0.0")]
104104
#[inline]
105105
pub fn first(&self) -> Option<&T> {
106-
self.get(0)
106+
if let [first, ..] = self { Some(first) } else { None }
107107
}
108108

109109
/// Returns a mutable pointer to the first element of the slice, or `None` if it is empty.
@@ -121,7 +121,7 @@ impl<T> [T] {
121121
#[stable(feature = "rust1", since = "1.0.0")]
122122
#[inline]
123123
pub fn first_mut(&mut self) -> Option<&mut T> {
124-
self.get_mut(0)
124+
if let [first, ..] = self { Some(first) } else { None }
125125
}
126126

127127
/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
@@ -139,7 +139,7 @@ impl<T> [T] {
139139
#[stable(feature = "slice_splits", since = "1.5.0")]
140140
#[inline]
141141
pub fn split_first(&self) -> Option<(&T, &[T])> {
142-
if self.is_empty() { None } else { Some((&self[0], &self[1..])) }
142+
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
143143
}
144144

145145
/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
@@ -159,12 +159,7 @@ impl<T> [T] {
159159
#[stable(feature = "slice_splits", since = "1.5.0")]
160160
#[inline]
161161
pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
162-
if self.is_empty() {
163-
None
164-
} else {
165-
let split = self.split_at_mut(1);
166-
Some((&mut split.0[0], split.1))
167-
}
162+
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
168163
}
169164

170165
/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
@@ -182,8 +177,7 @@ impl<T> [T] {
182177
#[stable(feature = "slice_splits", since = "1.5.0")]
183178
#[inline]
184179
pub fn split_last(&self) -> Option<(&T, &[T])> {
185-
let len = self.len();
186-
if len == 0 { None } else { Some((&self[len - 1], &self[..(len - 1)])) }
180+
if let [init @ .., last] = self { Some((last, init)) } else { None }
187181
}
188182

189183
/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
@@ -203,13 +197,7 @@ impl<T> [T] {
203197
#[stable(feature = "slice_splits", since = "1.5.0")]
204198
#[inline]
205199
pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
206-
let len = self.len();
207-
if len == 0 {
208-
None
209-
} else {
210-
let split = self.split_at_mut(len - 1);
211-
Some((&mut split.1[0], split.0))
212-
}
200+
if let [init @ .., last] = self { Some((last, init)) } else { None }
213201
}
214202

215203
/// Returns the last element of the slice, or `None` if it is empty.
@@ -226,8 +214,7 @@ impl<T> [T] {
226214
#[stable(feature = "rust1", since = "1.0.0")]
227215
#[inline]
228216
pub fn last(&self) -> Option<&T> {
229-
let last_idx = self.len().checked_sub(1)?;
230-
self.get(last_idx)
217+
if let [.., last] = self { Some(last) } else { None }
231218
}
232219

233220
/// Returns a mutable pointer to the last item in the slice.
@@ -245,8 +232,7 @@ impl<T> [T] {
245232
#[stable(feature = "rust1", since = "1.0.0")]
246233
#[inline]
247234
pub fn last_mut(&mut self) -> Option<&mut T> {
248-
let last_idx = self.len().checked_sub(1)?;
249-
self.get_mut(last_idx)
235+
if let [.., last] = self { Some(last) } else { None }
250236
}
251237

252238
/// Returns a reference to an element or subslice depending on the type of

src/librustc/hir/map/blocks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl MaybeFnLike for hir::ImplItem<'_> {
6060
impl MaybeFnLike for hir::TraitItem<'_> {
6161
fn is_fn_like(&self) -> bool {
6262
match self.kind {
63-
hir::TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => true,
63+
hir::TraitItemKind::Fn(_, hir::TraitMethod::Provided(_)) => true,
6464
_ => false,
6565
}
6666
}
@@ -239,7 +239,7 @@ impl<'a> FnLikeNode<'a> {
239239
_ => bug!("item FnLikeNode that is not fn-like"),
240240
},
241241
Node::TraitItem(ti) => match ti.kind {
242-
hir::TraitItemKind::Method(ref sig, hir::TraitMethod::Provided(body)) => {
242+
hir::TraitItemKind::Fn(ref sig, hir::TraitMethod::Provided(body)) => {
243243
method(ti.hir_id, ti.ident, sig, None, body, ti.span, &ti.attrs)
244244
}
245245
_ => bug!("trait method FnLikeNode that is not fn-like"),

src/librustc/hir/map/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'hir> Entry<'hir> {
5151
},
5252

5353
Node::TraitItem(ref item) => match item.kind {
54-
TraitItemKind::Method(ref sig, _) => Some(&sig.decl),
54+
TraitItemKind::Fn(ref sig, _) => Some(&sig.decl),
5555
_ => None,
5656
},
5757

@@ -77,7 +77,7 @@ impl<'hir> Entry<'hir> {
7777
},
7878

7979
Node::TraitItem(item) => match &item.kind {
80-
TraitItemKind::Method(sig, _) => Some(sig),
80+
TraitItemKind::Fn(sig, _) => Some(sig),
8181
_ => None,
8282
},
8383

@@ -101,7 +101,7 @@ impl<'hir> Entry<'hir> {
101101

102102
Node::TraitItem(item) => match item.kind {
103103
TraitItemKind::Const(_, Some(body))
104-
| TraitItemKind::Method(_, TraitMethod::Provided(body)) => Some(body),
104+
| TraitItemKind::Fn(_, TraitMethod::Provided(body)) => Some(body),
105105
_ => None,
106106
},
107107

@@ -326,12 +326,12 @@ impl<'hir> Map<'hir> {
326326
},
327327
Node::TraitItem(item) => match item.kind {
328328
TraitItemKind::Const(..) => DefKind::AssocConst,
329-
TraitItemKind::Method(..) => DefKind::Method,
329+
TraitItemKind::Fn(..) => DefKind::AssocFn,
330330
TraitItemKind::Type(..) => DefKind::AssocTy,
331331
},
332332
Node::ImplItem(item) => match item.kind {
333333
ImplItemKind::Const(..) => DefKind::AssocConst,
334-
ImplItemKind::Method(..) => DefKind::Method,
334+
ImplItemKind::Method(..) => DefKind::AssocFn,
335335
ImplItemKind::TyAlias(..) => DefKind::AssocTy,
336336
ImplItemKind::OpaqueTy(..) => DefKind::AssocOpaqueTy,
337337
},
@@ -472,7 +472,7 @@ impl<'hir> Map<'hir> {
472472
| Node::AnonConst(_) => BodyOwnerKind::Const,
473473
Node::Ctor(..)
474474
| Node::Item(&Item { kind: ItemKind::Fn(..), .. })
475-
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Method(..), .. })
475+
| Node::TraitItem(&TraitItem { kind: TraitItemKind::Fn(..), .. })
476476
| Node::ImplItem(&ImplItem { kind: ImplItemKind::Method(..), .. }) => BodyOwnerKind::Fn,
477477
Node::Item(&Item { kind: ItemKind::Static(_, m, _), .. }) => BodyOwnerKind::Static(m),
478478
Node::Expr(&Expr { kind: ExprKind::Closure(..), .. }) => BodyOwnerKind::Closure,
@@ -800,7 +800,7 @@ impl<'hir> Map<'hir> {
800800
_ => false,
801801
},
802802
Node::TraitItem(ti) => match ti.kind {
803-
TraitItemKind::Method(..) => true,
803+
TraitItemKind::Fn(..) => true,
804804
_ => false,
805805
},
806806
Node::ImplItem(ii) => match ii.kind {
@@ -1311,7 +1311,7 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
13111311
Some(Node::TraitItem(ti)) => {
13121312
let kind = match ti.kind {
13131313
TraitItemKind::Const(..) => "assoc constant",
1314-
TraitItemKind::Method(..) => "trait method",
1314+
TraitItemKind::Fn(..) => "trait method",
13151315
TraitItemKind::Type(..) => "assoc type",
13161316
};
13171317

src/librustc/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ pub enum EvalResult {
250250
fn skip_stability_check_due_to_privacy(tcx: TyCtxt<'_>, mut def_id: DefId) -> bool {
251251
// Check if `def_id` is a trait method.
252252
match tcx.def_kind(def_id) {
253-
Some(DefKind::Method) | Some(DefKind::AssocTy) | Some(DefKind::AssocConst) => {
253+
Some(DefKind::AssocFn) | Some(DefKind::AssocTy) | Some(DefKind::AssocConst) => {
254254
if let ty::TraitContainer(trait_def_id) = tcx.associated_item(def_id).container {
255255
// Trait methods do not declare visibility (even
256256
// for visibility info in cstore). Use containing

src/librustc/mir/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ impl<'tcx> Debug for TerminatorKind<'tcx> {
14461446
match successor_count {
14471447
0 => Ok(()),
14481448

1449-
1 => write!(fmt, " -> {:?}", self.successors().nth(0).unwrap()),
1449+
1 => write!(fmt, " -> {:?}", self.successors().next().unwrap()),
14501450

14511451
_ => {
14521452
write!(fmt, " -> [")?;
@@ -1827,9 +1827,9 @@ rustc_index::newtype_index! {
18271827
}
18281828

18291829
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1830-
pub struct PlaceRef<'a, 'tcx> {
1830+
pub struct PlaceRef<'tcx> {
18311831
pub local: Local,
1832-
pub projection: &'a [PlaceElem<'tcx>],
1832+
pub projection: &'tcx [PlaceElem<'tcx>],
18331833
}
18341834

18351835
impl<'tcx> Place<'tcx> {
@@ -1864,7 +1864,7 @@ impl<'tcx> Place<'tcx> {
18641864
self.as_ref().as_local()
18651865
}
18661866

1867-
pub fn as_ref(&self) -> PlaceRef<'_, 'tcx> {
1867+
pub fn as_ref(&self) -> PlaceRef<'tcx> {
18681868
PlaceRef { local: self.local, projection: &self.projection }
18691869
}
18701870
}
@@ -1875,7 +1875,7 @@ impl From<Local> for Place<'_> {
18751875
}
18761876
}
18771877

1878-
impl<'a, 'tcx> PlaceRef<'a, 'tcx> {
1878+
impl<'tcx> PlaceRef<'tcx> {
18791879
/// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
18801880
/// a single deref of a local.
18811881
//

src/librustc/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ impl<'tcx> TypeckTables<'tcx> {
611611
}
612612

613613
match self.type_dependent_defs().get(expr.hir_id) {
614-
Some(Ok((DefKind::Method, _))) => true,
614+
Some(Ok((DefKind::AssocFn, _))) => true,
615615
_ => false,
616616
}
617617
}

src/librustc/ty/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl AssocItem {
230230
pub fn def_kind(&self) -> DefKind {
231231
match self.kind {
232232
AssocKind::Const => DefKind::AssocConst,
233-
AssocKind::Method => DefKind::Method,
233+
AssocKind::Method => DefKind::AssocFn,
234234
AssocKind::Type => DefKind::AssocTy,
235235
AssocKind::OpaqueTy => DefKind::AssocOpaqueTy,
236236
}
@@ -2872,7 +2872,7 @@ impl<'tcx> TyCtxt<'tcx> {
28722872
}
28732873
} else {
28742874
match self.def_kind(def_id).expect("no def for `DefId`") {
2875-
DefKind::AssocConst | DefKind::Method | DefKind::AssocTy => true,
2875+
DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy => true,
28762876
_ => false,
28772877
}
28782878
};
@@ -3051,7 +3051,7 @@ impl<'tcx> TyCtxt<'tcx> {
30513051
/// `DefId` of the impl that the method belongs to; otherwise, returns `None`.
30523052
pub fn impl_of_method(self, def_id: DefId) -> Option<DefId> {
30533053
let item = if def_id.krate != LOCAL_CRATE {
3054-
if let Some(DefKind::Method) = self.def_kind(def_id) {
3054+
if let Some(DefKind::AssocFn) = self.def_kind(def_id) {
30553055
Some(self.associated_item(def_id))
30563056
} else {
30573057
None

src/librustc/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl<'tcx> TyCtxt<'tcx> {
357357
let mut dtor_did = None;
358358
let ty = self.type_of(adt_did);
359359
self.for_each_relevant_impl(drop_trait, ty, |impl_did| {
360-
if let Some(item) = self.associated_items(impl_did).in_definition_order().nth(0) {
360+
if let Some(item) = self.associated_items(impl_did).in_definition_order().next() {
361361
if validate(self, impl_did).is_ok() {
362362
dtor_did = Some(item.def_id);
363363
}

src/librustc_ast_lowering/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -761,13 +761,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
761761
let names = self.lower_fn_params_to_names(&sig.decl);
762762
let (generics, sig) =
763763
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
764-
(generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Required(names)))
764+
(generics, hir::TraitItemKind::Fn(sig, hir::TraitMethod::Required(names)))
765765
}
766766
AssocItemKind::Fn(_, ref sig, ref generics, Some(ref body)) => {
767767
let body_id = self.lower_fn_body_block(i.span, &sig.decl, Some(body));
768768
let (generics, sig) =
769769
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
770-
(generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Provided(body_id)))
770+
(generics, hir::TraitItemKind::Fn(sig, hir::TraitMethod::Provided(body_id)))
771771
}
772772
AssocItemKind::TyAlias(_, ref generics, ref bounds, ref default) => {
773773
let ty = default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::disallowed()));

src/librustc_ast_lowering/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
7575
ParenthesizedGenericArgs::Ok
7676
}
7777
// `a::b::Trait(Args)::TraitItem`
78-
Res::Def(DefKind::Method, _)
78+
Res::Def(DefKind::AssocFn, _)
7979
| Res::Def(DefKind::AssocConst, _)
8080
| Res::Def(DefKind::AssocTy, _)
8181
if i + 2 == proj_start =>

src/librustc_codegen_ssa/mir/analyze.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
9797

9898
fn process_place(
9999
&mut self,
100-
place_ref: &mir::PlaceRef<'_, 'tcx>,
100+
place_ref: &mir::PlaceRef<'tcx>,
101101
context: PlaceContext,
102102
location: Location,
103103
) {

src/librustc_codegen_ssa/mir/operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
364364
fn maybe_codegen_consume_direct(
365365
&mut self,
366366
bx: &mut Bx,
367-
place_ref: mir::PlaceRef<'_, 'tcx>,
367+
place_ref: mir::PlaceRef<'tcx>,
368368
) -> Option<OperandRef<'tcx, Bx::Value>> {
369369
debug!("maybe_codegen_consume_direct(place_ref={:?})", place_ref);
370370

@@ -408,7 +408,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
408408
pub fn codegen_consume(
409409
&mut self,
410410
bx: &mut Bx,
411-
place_ref: mir::PlaceRef<'_, 'tcx>,
411+
place_ref: mir::PlaceRef<'tcx>,
412412
) -> OperandRef<'tcx, Bx::Value> {
413413
debug!("codegen_consume(place_ref={:?})", place_ref);
414414

src/librustc_codegen_ssa/mir/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
408408
pub fn codegen_place(
409409
&mut self,
410410
bx: &mut Bx,
411-
place_ref: mir::PlaceRef<'_, 'tcx>,
411+
place_ref: mir::PlaceRef<'tcx>,
412412
) -> PlaceRef<'tcx, Bx::Value> {
413413
debug!("codegen_place(place_ref={:?})", place_ref);
414414
let cx = self.cx;
@@ -497,7 +497,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
497497
result
498498
}
499499

500-
pub fn monomorphized_place_ty(&self, place_ref: mir::PlaceRef<'_, 'tcx>) -> Ty<'tcx> {
500+
pub fn monomorphized_place_ty(&self, place_ref: mir::PlaceRef<'tcx>) -> Ty<'tcx> {
501501
let tcx = self.cx.tcx();
502502
let place_ty = mir::Place::ty_from(place_ref.local, place_ref.projection, *self.mir, tcx);
503503
self.monomorphize(&place_ty.ty)

src/librustc_hir/def.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub enum DefKind {
7272
Static,
7373
/// Refers to the struct or enum variant's constructor.
7474
Ctor(CtorOf, CtorKind),
75-
Method,
75+
AssocFn,
7676
AssocConst,
7777

7878
// Macro namespace
@@ -107,7 +107,8 @@ impl DefKind {
107107
DefKind::Union => "union",
108108
DefKind::Trait => "trait",
109109
DefKind::ForeignTy => "foreign type",
110-
DefKind::Method => "method",
110+
// FIXME: Update the description to "assoc fn"
111+
DefKind::AssocFn => "method",
111112
DefKind::Const => "constant",
112113
DefKind::AssocConst => "associated constant",
113114
DefKind::TyParam => "type parameter",
@@ -150,7 +151,7 @@ impl DefKind {
150151
| DefKind::ConstParam
151152
| DefKind::Static
152153
| DefKind::Ctor(..)
153-
| DefKind::Method
154+
| DefKind::AssocFn
154155
| DefKind::AssocConst => ns == Namespace::ValueNS,
155156

156157
DefKind::Macro(..) => ns == Namespace::MacroNS,

src/librustc_hir/hir.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1863,8 +1863,8 @@ pub enum TraitMethod<'hir> {
18631863
pub enum TraitItemKind<'hir> {
18641864
/// An associated constant with an optional value (otherwise `impl`s must contain a value).
18651865
Const(&'hir Ty<'hir>, Option<BodyId>),
1866-
/// A method with an optional body.
1867-
Method(FnSig<'hir>, TraitMethod<'hir>),
1866+
/// An associated function with an optional body.
1867+
Fn(FnSig<'hir>, TraitMethod<'hir>),
18681868
/// An associated type with (possibly empty) bounds and optional concrete
18691869
/// type.
18701870
Type(GenericBounds<'hir>, Option<&'hir Ty<'hir>>),
@@ -2699,7 +2699,7 @@ impl Node<'_> {
26992699

27002700
pub fn fn_decl(&self) -> Option<&FnDecl<'_>> {
27012701
match self {
2702-
Node::TraitItem(TraitItem { kind: TraitItemKind::Method(fn_sig, _), .. })
2702+
Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(fn_sig, _), .. })
27032703
| Node::ImplItem(ImplItem { kind: ImplItemKind::Method(fn_sig, _), .. })
27042704
| Node::Item(Item { kind: ItemKind::Fn(fn_sig, _, _), .. }) => Some(fn_sig.decl),
27052705
Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {

0 commit comments

Comments
 (0)