Skip to content

Commit c56d8ed

Browse files
Rollup merge of rust-lang#105541 - compiler-errors:iter-comb, r=cjgillot
Simplify some iterator combinators Saw a `flat_map().next()` instead of a `find_map()` in some method probe code, so did a quick search for some similar usages.
2 parents 752c0f5 + 2baee88 commit c56d8ed

File tree

12 files changed

+98
-207
lines changed

12 files changed

+98
-207
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -527,26 +527,21 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
527527
// that are *partially* initialized by assigning to a field of an uninitialized
528528
// binding. We differentiate between them for more accurate wording here.
529529
"isn't fully initialized"
530-
} else if spans
531-
.iter()
532-
.filter(|i| {
533-
// We filter these to avoid misleading wording in cases like the following,
534-
// where `x` has an `init`, but it is in the same place we're looking at:
535-
// ```
536-
// let x;
537-
// x += 1;
538-
// ```
539-
!i.contains(span)
540-
// We filter these to avoid incorrect main message on `match-cfg-fake-edges.rs`
541-
&& !visitor
542-
.errors
543-
.iter()
544-
.map(|(sp, _)| *sp)
545-
.any(|sp| span < sp && !sp.contains(span))
546-
})
547-
.count()
548-
== 0
549-
{
530+
} else if !spans.iter().any(|i| {
531+
// We filter these to avoid misleading wording in cases like the following,
532+
// where `x` has an `init`, but it is in the same place we're looking at:
533+
// ```
534+
// let x;
535+
// x += 1;
536+
// ```
537+
!i.contains(span)
538+
// We filter these to avoid incorrect main message on `match-cfg-fake-edges.rs`
539+
&& !visitor
540+
.errors
541+
.iter()
542+
.map(|(sp, _)| *sp)
543+
.any(|sp| span < sp && !sp.contains(span))
544+
}) {
550545
show_assign_sugg = true;
551546
"isn't initialized"
552547
} else {

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+2-67
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
55
use rustc_data_structures::fx::FxIndexSet;
66
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
7+
use rustc_hir as hir;
78
use rustc_hir::def_id::DefId;
89
use rustc_hir::intravisit::Visitor;
9-
use rustc_hir::{self as hir, Item, ItemKind, Node};
1010
use rustc_infer::infer::{
1111
error_reporting::nice_region_error::{
1212
self, find_anon_type, find_param_with_region, suggest_adding_lifetime_params,
@@ -291,71 +291,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
291291
outlives_suggestion.add_suggestion(self);
292292
}
293293

294-
fn get_impl_ident_and_self_ty_from_trait(
295-
&self,
296-
def_id: DefId,
297-
trait_objects: &FxIndexSet<DefId>,
298-
) -> Option<(Ident, &'tcx hir::Ty<'tcx>)> {
299-
let tcx = self.infcx.tcx;
300-
match tcx.hir().get_if_local(def_id) {
301-
Some(Node::ImplItem(impl_item)) => {
302-
match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id()).def_id)
303-
{
304-
Some(Node::Item(Item {
305-
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
306-
..
307-
})) => Some((impl_item.ident, self_ty)),
308-
_ => None,
309-
}
310-
}
311-
Some(Node::TraitItem(trait_item)) => {
312-
let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
313-
match tcx.hir().find_by_def_id(trait_did.def_id) {
314-
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
315-
// The method being called is defined in the `trait`, but the `'static`
316-
// obligation comes from the `impl`. Find that `impl` so that we can point
317-
// at it in the suggestion.
318-
let trait_did = trait_did.to_def_id();
319-
match tcx
320-
.hir()
321-
.trait_impls(trait_did)
322-
.iter()
323-
.filter_map(|&impl_did| {
324-
match tcx.hir().get_if_local(impl_did.to_def_id()) {
325-
Some(Node::Item(Item {
326-
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
327-
..
328-
})) if trait_objects.iter().all(|did| {
329-
// FIXME: we should check `self_ty` against the receiver
330-
// type in the `UnifyReceiver` context, but for now, use
331-
// this imperfect proxy. This will fail if there are
332-
// multiple `impl`s for the same trait like
333-
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
334-
// In that case, only the first one will get suggestions.
335-
let mut traits = vec![];
336-
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
337-
hir_v.visit_ty(self_ty);
338-
!traits.is_empty()
339-
}) =>
340-
{
341-
Some(self_ty)
342-
}
343-
_ => None,
344-
}
345-
})
346-
.next()
347-
{
348-
Some(self_ty) => Some((trait_item.ident, self_ty)),
349-
_ => None,
350-
}
351-
}
352-
_ => None,
353-
}
354-
}
355-
_ => None,
356-
}
357-
}
358-
359294
/// Report an error because the universal region `fr` was required to outlive
360295
/// `outlived_fr` but it is not known to do so. For example:
361296
///
@@ -850,7 +785,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
850785
visitor.visit_ty(param.param_ty);
851786

852787
let Some((ident, self_ty)) =
853-
self.get_impl_ident_and_self_ty_from_trait(instance.def_id(), &visitor.0) else {return};
788+
NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, instance.def_id(), &visitor.0) else { return; };
854789

855790
self.suggest_constrain_dyn_trait_in_impl(diag, &visitor.0, ident, self_ty);
856791
}

compiler/rustc_codegen_gcc/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
12441244
) -> RValue<'gcc> {
12451245
// FIXME(antoyo): remove when having a proper API.
12461246
let gcc_func = unsafe { std::mem::transmute(func) };
1247-
let call = if self.functions.borrow().values().find(|value| **value == gcc_func).is_some() {
1247+
let call = if self.functions.borrow().values().any(|value| *value == gcc_func) {
12481248
self.function_call(func, args, funclet)
12491249
}
12501250
else {

compiler/rustc_codegen_gcc/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
253253

254254
pub fn rvalue_as_function(&self, value: RValue<'gcc>) -> Function<'gcc> {
255255
let function: Function<'gcc> = unsafe { std::mem::transmute(value) };
256-
debug_assert!(self.functions.borrow().values().find(|value| **value == function).is_some(),
256+
debug_assert!(self.functions.borrow().values().any(|value| *value == function),
257257
"{:?} ({:?}) is not a function", value, value.get_type());
258258
function
259259
}

compiler/rustc_hir_typeck/src/lib.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,8 @@ fn typeck_with_fallback<'tcx>(
240240
}),
241241
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), .. })
242242
| Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), .. }) => {
243-
let operand_ty = asm
244-
.operands
245-
.iter()
246-
.filter_map(|(op, _op_sp)| match op {
243+
let operand_ty =
244+
asm.operands.iter().find_map(|(op, _op_sp)| match op {
247245
hir::InlineAsmOperand::Const { anon_const }
248246
if anon_const.hir_id == id =>
249247
{
@@ -259,8 +257,7 @@ fn typeck_with_fallback<'tcx>(
259257
}))
260258
}
261259
_ => None,
262-
})
263-
.next();
260+
});
264261
operand_ty.unwrap_or_else(fallback)
265262
}
266263
_ => fallback(),

compiler/rustc_hir_typeck/src/method/prelude2021.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
341341
// Find an identifier with which this trait was imported (note that `_` doesn't count).
342342
let any_id = import_items
343343
.iter()
344-
.filter_map(|item| if item.ident.name != Underscore { Some(item.ident) } else { None })
345-
.next();
344+
.find_map(|item| if item.ident.name != Underscore { Some(item.ident) } else { None });
346345
if let Some(any_id) = any_id {
347346
if any_id.name == Empty {
348347
// Glob import, so just use its name.

compiler/rustc_hir_typeck/src/method/probe.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11111111
// a raw pointer
11121112
!step.self_ty.references_error() && !step.from_unsafe_deref
11131113
})
1114-
.flat_map(|step| {
1114+
.find_map(|step| {
11151115
let InferOk { value: self_ty, obligations: _ } = self
11161116
.fcx
11171117
.probe_instantiate_query_response(
@@ -1147,7 +1147,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11471147
})
11481148
})
11491149
})
1150-
.next()
11511150
}
11521151

11531152
/// For each type `T` in the step list, this attempts to find a method where

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
257257
self.tcx
258258
.inherent_impls(adt_def.did())
259259
.iter()
260-
.filter_map(|def_id| self.associated_value(*def_id, item_name))
261-
.count()
262-
>= 1
260+
.any(|def_id| self.associated_value(*def_id, item_name).is_some())
263261
} else {
264262
false
265263
}

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+57-82
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
239239
let mut v = TraitObjectVisitor(FxIndexSet::default());
240240
v.visit_ty(param.param_ty);
241241
if let Some((ident, self_ty)) =
242-
self.get_impl_ident_and_self_ty_from_trait(item_def_id, &v.0)
242+
NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, item_def_id, &v.0)
243243
&& self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty)
244244
{
245245
override_error_code = Some(ident.name);
@@ -309,19 +309,12 @@ pub fn suggest_new_region_bound(
309309
let did = item_id.owner_id.to_def_id();
310310
let ty = tcx.mk_opaque(did, ty::InternalSubsts::identity_for_item(tcx, did));
311311

312-
if let Some(span) = opaque
313-
.bounds
314-
.iter()
315-
.filter_map(|arg| match arg {
316-
GenericBound::Outlives(Lifetime {
317-
res: LifetimeName::Static,
318-
ident,
319-
..
320-
}) => Some(ident.span),
321-
_ => None,
322-
})
323-
.next()
324-
{
312+
if let Some(span) = opaque.bounds.iter().find_map(|arg| match arg {
313+
GenericBound::Outlives(Lifetime {
314+
res: LifetimeName::Static, ident, ..
315+
}) => Some(ident.span),
316+
_ => None,
317+
}) {
325318
if let Some(explicit_static) = &explicit_static {
326319
err.span_suggestion_verbose(
327320
span,
@@ -338,20 +331,14 @@ pub fn suggest_new_region_bound(
338331
Applicability::MaybeIncorrect,
339332
);
340333
}
341-
} else if opaque
342-
.bounds
343-
.iter()
344-
.filter_map(|arg| match arg {
345-
GenericBound::Outlives(Lifetime { ident, .. })
346-
if ident.name.to_string() == lifetime_name =>
347-
{
348-
Some(ident.span)
349-
}
350-
_ => None,
351-
})
352-
.next()
353-
.is_some()
354-
{
334+
} else if opaque.bounds.iter().any(|arg| match arg {
335+
GenericBound::Outlives(Lifetime { ident, .. })
336+
if ident.name.to_string() == lifetime_name =>
337+
{
338+
true
339+
}
340+
_ => false,
341+
}) {
355342
} else {
356343
err.span_suggestion_verbose(
357344
fn_return.span.shrink_to_hi(),
@@ -403,66 +390,54 @@ pub fn suggest_new_region_bound(
403390
}
404391

405392
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
406-
fn get_impl_ident_and_self_ty_from_trait(
407-
&self,
393+
pub fn get_impl_ident_and_self_ty_from_trait(
394+
tcx: TyCtxt<'tcx>,
408395
def_id: DefId,
409396
trait_objects: &FxIndexSet<DefId>,
410397
) -> Option<(Ident, &'tcx hir::Ty<'tcx>)> {
411-
let tcx = self.tcx();
412-
match tcx.hir().get_if_local(def_id) {
413-
Some(Node::ImplItem(impl_item)) => {
414-
match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id()).def_id)
398+
match tcx.hir().get_if_local(def_id)? {
399+
Node::ImplItem(impl_item) => {
400+
let impl_did = tcx.hir().get_parent_item(impl_item.hir_id());
401+
if let hir::OwnerNode::Item(Item {
402+
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
403+
..
404+
}) = tcx.hir().owner(impl_did)
415405
{
416-
Some(Node::Item(Item {
417-
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
418-
..
419-
})) => Some((impl_item.ident, self_ty)),
420-
_ => None,
406+
Some((impl_item.ident, self_ty))
407+
} else {
408+
None
421409
}
422410
}
423-
Some(Node::TraitItem(trait_item)) => {
424-
let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
425-
match tcx.hir().find_by_def_id(trait_did.def_id) {
426-
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
427-
// The method being called is defined in the `trait`, but the `'static`
428-
// obligation comes from the `impl`. Find that `impl` so that we can point
429-
// at it in the suggestion.
430-
let trait_did = trait_did.to_def_id();
431-
match tcx
432-
.hir()
433-
.trait_impls(trait_did)
434-
.iter()
435-
.filter_map(|&impl_did| {
436-
match tcx.hir().get_if_local(impl_did.to_def_id()) {
437-
Some(Node::Item(Item {
438-
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
439-
..
440-
})) if trait_objects.iter().all(|did| {
441-
// FIXME: we should check `self_ty` against the receiver
442-
// type in the `UnifyReceiver` context, but for now, use
443-
// this imperfect proxy. This will fail if there are
444-
// multiple `impl`s for the same trait like
445-
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
446-
// In that case, only the first one will get suggestions.
447-
let mut traits = vec![];
448-
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
449-
hir_v.visit_ty(self_ty);
450-
!traits.is_empty()
451-
}) =>
452-
{
453-
Some(self_ty)
454-
}
455-
_ => None,
456-
}
457-
})
458-
.next()
459-
{
460-
Some(self_ty) => Some((trait_item.ident, self_ty)),
461-
_ => None,
462-
}
411+
Node::TraitItem(trait_item) => {
412+
let trait_id = tcx.hir().get_parent_item(trait_item.hir_id());
413+
debug_assert_eq!(tcx.def_kind(trait_id.def_id), hir::def::DefKind::Trait);
414+
// The method being called is defined in the `trait`, but the `'static`
415+
// obligation comes from the `impl`. Find that `impl` so that we can point
416+
// at it in the suggestion.
417+
let trait_did = trait_id.to_def_id();
418+
tcx.hir().trait_impls(trait_did).iter().find_map(|&impl_did| {
419+
if let Node::Item(Item {
420+
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
421+
..
422+
}) = tcx.hir().find_by_def_id(impl_did)?
423+
&& trait_objects.iter().all(|did| {
424+
// FIXME: we should check `self_ty` against the receiver
425+
// type in the `UnifyReceiver` context, but for now, use
426+
// this imperfect proxy. This will fail if there are
427+
// multiple `impl`s for the same trait like
428+
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
429+
// In that case, only the first one will get suggestions.
430+
let mut traits = vec![];
431+
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
432+
hir_v.visit_ty(self_ty);
433+
!traits.is_empty()
434+
})
435+
{
436+
Some((trait_item.ident, *self_ty))
437+
} else {
438+
None
463439
}
464-
_ => None,
465-
}
440+
})
466441
}
467442
_ => None,
468443
}
@@ -493,7 +468,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
493468

494469
// Get the `Ident` of the method being called and the corresponding `impl` (to point at
495470
// `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called).
496-
let Some((ident, self_ty)) = self.get_impl_ident_and_self_ty_from_trait(instance.def_id(), &v.0) else {
471+
let Some((ident, self_ty)) = NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, instance.def_id(), &v.0) else {
497472
return false;
498473
};
499474

0 commit comments

Comments
 (0)