Skip to content

Commit d951183

Browse files
authored
Unrolled build for rust-lang#127366
Rollup merge of rust-lang#127366 - oli-obk:falliblevisitor, r=compiler-errors Use `ControlFlow` results for visitors that are only looking for a single value These visitors all had a `Option<Value>` or `bool` field, that, once set, was never unset or modified again. They have been refactored by removing the field and returning `ControlFlow` directly from the visitor
2 parents 5c08cc7 + 7dca61b commit d951183

File tree

6 files changed

+59
-87
lines changed

6 files changed

+59
-87
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+23-38
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt;
3939
use rustc_trait_selection::traits::error_reporting::FindExprBySpan;
4040
use rustc_trait_selection::traits::{Obligation, ObligationCause, ObligationCtxt};
4141
use std::iter;
42+
use std::ops::ControlFlow;
4243

4344
use crate::borrow_set::TwoPhaseActivation;
4445
use crate::borrowck_errors;
@@ -784,20 +785,20 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
784785
/// binding declaration within every scope we inspect.
785786
struct Finder {
786787
hir_id: hir::HirId,
787-
found: bool,
788788
}
789789
impl<'hir> Visitor<'hir> for Finder {
790-
fn visit_pat(&mut self, pat: &'hir hir::Pat<'hir>) {
790+
type Result = ControlFlow<()>;
791+
fn visit_pat(&mut self, pat: &'hir hir::Pat<'hir>) -> Self::Result {
791792
if pat.hir_id == self.hir_id {
792-
self.found = true;
793+
return ControlFlow::Break(());
793794
}
794-
hir::intravisit::walk_pat(self, pat);
795+
hir::intravisit::walk_pat(self, pat)
795796
}
796-
fn visit_expr(&mut self, ex: &'hir hir::Expr<'hir>) {
797+
fn visit_expr(&mut self, ex: &'hir hir::Expr<'hir>) -> Self::Result {
797798
if ex.hir_id == self.hir_id {
798-
self.found = true;
799+
return ControlFlow::Break(());
799800
}
800-
hir::intravisit::walk_expr(self, ex);
801+
hir::intravisit::walk_expr(self, ex)
801802
}
802803
}
803804
// The immediate HIR parent of the moved expression. We'll look for it to be a call.
@@ -822,9 +823,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
822823
_ => continue,
823824
};
824825
if let Some(&hir_id) = local_hir_id {
825-
let mut finder = Finder { hir_id, found: false };
826-
finder.visit_expr(e);
827-
if finder.found {
826+
if (Finder { hir_id }).visit_expr(e).is_break() {
828827
// The current scope includes the declaration of the binding we're accessing, we
829828
// can't look up any further for loops.
830829
break;
@@ -839,9 +838,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
839838
hir::Node::Expr(hir::Expr {
840839
kind: hir::ExprKind::If(cond, ..), ..
841840
}) => {
842-
let mut finder = Finder { hir_id: expr.hir_id, found: false };
843-
finder.visit_expr(cond);
844-
if finder.found {
841+
if (Finder { hir_id: expr.hir_id }).visit_expr(cond).is_break() {
845842
// The expression where the move error happened is in a `while let`
846843
// condition Don't suggest clone as it will likely end in an
847844
// infinite loop.
@@ -1837,15 +1834,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
18371834

18381835
pub struct Holds<'tcx> {
18391836
ty: Ty<'tcx>,
1840-
holds: bool,
18411837
}
18421838

18431839
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for Holds<'tcx> {
18441840
type Result = std::ops::ControlFlow<()>;
18451841

18461842
fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
18471843
if t == self.ty {
1848-
self.holds = true;
1844+
return ControlFlow::Break(());
18491845
}
18501846
t.super_visit_with(self)
18511847
}
@@ -1863,9 +1859,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
18631859
&& rcvr_ty == ty
18641860
&& let ty::Ref(_, inner, _) = rcvr_ty.kind()
18651861
&& let inner = inner.peel_refs()
1866-
&& let mut v = (Holds { ty: inner, holds: false })
1867-
&& let _ = v.visit_ty(local_ty)
1868-
&& v.holds
1862+
&& (Holds { ty: inner }).visit_ty(local_ty).is_break()
18691863
&& let None = self.infcx.type_implements_trait_shallow(clone, inner, self.param_env)
18701864
{
18711865
err.span_label(
@@ -4325,15 +4319,14 @@ impl<'tcx> AnnotatedBorrowFnSignature<'tcx> {
43254319
}
43264320

43274321
/// Detect whether one of the provided spans is a statement nested within the top-most visited expr
4328-
struct ReferencedStatementsVisitor<'a>(&'a [Span], bool);
4322+
struct ReferencedStatementsVisitor<'a>(&'a [Span]);
43294323

4330-
impl<'a, 'v> Visitor<'v> for ReferencedStatementsVisitor<'a> {
4331-
fn visit_stmt(&mut self, s: &'v hir::Stmt<'v>) {
4324+
impl<'v> Visitor<'v> for ReferencedStatementsVisitor<'_> {
4325+
type Result = ControlFlow<()>;
4326+
fn visit_stmt(&mut self, s: &'v hir::Stmt<'v>) -> Self::Result {
43324327
match s.kind {
4333-
hir::StmtKind::Semi(expr) if self.0.contains(&expr.span) => {
4334-
self.1 = true;
4335-
}
4336-
_ => {}
4328+
hir::StmtKind::Semi(expr) if self.0.contains(&expr.span) => ControlFlow::Break(()),
4329+
_ => ControlFlow::Continue(()),
43374330
}
43384331
}
43394332
}
@@ -4375,9 +4368,7 @@ impl<'b, 'v, 'tcx> Visitor<'v> for ConditionVisitor<'b, 'tcx> {
43754368
hir::ExprKind::If(cond, body, None) => {
43764369
// `if` expressions with no `else` that initialize the binding might be missing an
43774370
// `else` arm.
4378-
let mut v = ReferencedStatementsVisitor(self.spans, false);
4379-
v.visit_expr(body);
4380-
if v.1 {
4371+
if ReferencedStatementsVisitor(self.spans).visit_expr(body).is_break() {
43814372
self.errors.push((
43824373
cond.span,
43834374
format!(
@@ -4394,11 +4385,9 @@ impl<'b, 'v, 'tcx> Visitor<'v> for ConditionVisitor<'b, 'tcx> {
43944385
hir::ExprKind::If(cond, body, Some(other)) => {
43954386
// `if` expressions where the binding is only initialized in one of the two arms
43964387
// might be missing a binding initialization.
4397-
let mut a = ReferencedStatementsVisitor(self.spans, false);
4398-
a.visit_expr(body);
4399-
let mut b = ReferencedStatementsVisitor(self.spans, false);
4400-
b.visit_expr(other);
4401-
match (a.1, b.1) {
4388+
let a = ReferencedStatementsVisitor(self.spans).visit_expr(body).is_break();
4389+
let b = ReferencedStatementsVisitor(self.spans).visit_expr(other).is_break();
4390+
match (a, b) {
44024391
(true, true) | (false, false) => {}
44034392
(true, false) => {
44044393
if other.span.is_desugaring(DesugaringKind::WhileLoop) {
@@ -4437,11 +4426,7 @@ impl<'b, 'v, 'tcx> Visitor<'v> for ConditionVisitor<'b, 'tcx> {
44374426
// arms might be missing an initialization.
44384427
let results: Vec<bool> = arms
44394428
.iter()
4440-
.map(|arm| {
4441-
let mut v = ReferencedStatementsVisitor(self.spans, false);
4442-
v.visit_arm(arm);
4443-
v.1
4444-
})
4429+
.map(|arm| ReferencedStatementsVisitor(self.spans).visit_arm(arm).is_break())
44454430
.collect();
44464431
if results.iter().any(|x| *x) && !results.iter().all(|x| *x) {
44474432
for (arm, seen) in arms.iter().zip(results) {

compiler/rustc_builtin_macros/src/deriving/default.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ fn has_a_default_variant(item: &Annotatable) -> bool {
240240
if v.attrs.iter().any(|attr| attr.has_name(kw::Default)) {
241241
ControlFlow::Break(())
242242
} else {
243-
// no need to subrecurse.
243+
// no need to walk the variant, we are only looking for top level variants
244244
ControlFlow::Continue(())
245245
}
246246
}

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+32-42
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ops::ControlFlow;
2+
13
use crate::middle::resolve_bound_vars as rbv;
24
use hir::{
35
intravisit::{self, Visitor},
@@ -87,14 +89,9 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
8789
let mut in_param_ty = false;
8890
for (_parent, node) in tcx.hir().parent_iter(hir_id) {
8991
if let Some(generics) = node.generics() {
90-
let mut visitor = AnonConstInParamTyDetector {
91-
in_param_ty: false,
92-
found_anon_const_in_param_ty: false,
93-
ct: hir_id,
94-
};
92+
let mut visitor = AnonConstInParamTyDetector { in_param_ty: false, ct: hir_id };
9593

96-
visitor.visit_generics(generics);
97-
in_param_ty = visitor.found_anon_const_in_param_ty;
94+
in_param_ty = visitor.visit_generics(generics).is_break();
9895
break;
9996
}
10097
}
@@ -460,50 +457,45 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<S
460457
struct LateBoundRegionsDetector<'tcx> {
461458
tcx: TyCtxt<'tcx>,
462459
outer_index: ty::DebruijnIndex,
463-
has_late_bound_regions: Option<Span>,
464460
}
465461

466462
impl<'tcx> Visitor<'tcx> for LateBoundRegionsDetector<'tcx> {
467-
fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) {
468-
if self.has_late_bound_regions.is_some() {
469-
return;
470-
}
463+
type Result = ControlFlow<Span>;
464+
fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) -> ControlFlow<Span> {
471465
match ty.kind {
472466
hir::TyKind::BareFn(..) => {
473467
self.outer_index.shift_in(1);
474-
intravisit::walk_ty(self, ty);
468+
let res = intravisit::walk_ty(self, ty);
475469
self.outer_index.shift_out(1);
470+
res
476471
}
477472
_ => intravisit::walk_ty(self, ty),
478473
}
479474
}
480475

481-
fn visit_poly_trait_ref(&mut self, tr: &'tcx hir::PolyTraitRef<'tcx>) {
482-
if self.has_late_bound_regions.is_some() {
483-
return;
484-
}
476+
fn visit_poly_trait_ref(&mut self, tr: &'tcx hir::PolyTraitRef<'tcx>) -> ControlFlow<Span> {
485477
self.outer_index.shift_in(1);
486-
intravisit::walk_poly_trait_ref(self, tr);
478+
let res = intravisit::walk_poly_trait_ref(self, tr);
487479
self.outer_index.shift_out(1);
480+
res
488481
}
489482

490-
fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) {
491-
if self.has_late_bound_regions.is_some() {
492-
return;
493-
}
494-
483+
fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) -> ControlFlow<Span> {
495484
match self.tcx.named_bound_var(lt.hir_id) {
496-
Some(rbv::ResolvedArg::StaticLifetime | rbv::ResolvedArg::EarlyBound(..)) => {}
485+
Some(rbv::ResolvedArg::StaticLifetime | rbv::ResolvedArg::EarlyBound(..)) => {
486+
ControlFlow::Continue(())
487+
}
497488
Some(rbv::ResolvedArg::LateBound(debruijn, _, _))
498-
if debruijn < self.outer_index => {}
489+
if debruijn < self.outer_index =>
490+
{
491+
ControlFlow::Continue(())
492+
}
499493
Some(
500494
rbv::ResolvedArg::LateBound(..)
501495
| rbv::ResolvedArg::Free(..)
502496
| rbv::ResolvedArg::Error(_),
503497
)
504-
| None => {
505-
self.has_late_bound_regions = Some(lt.ident.span);
506-
}
498+
| None => ControlFlow::Break(lt.ident.span),
507499
}
508500
}
509501
}
@@ -513,20 +505,15 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<S
513505
generics: &'tcx hir::Generics<'tcx>,
514506
decl: &'tcx hir::FnDecl<'tcx>,
515507
) -> Option<Span> {
516-
let mut visitor = LateBoundRegionsDetector {
517-
tcx,
518-
outer_index: ty::INNERMOST,
519-
has_late_bound_regions: None,
520-
};
508+
let mut visitor = LateBoundRegionsDetector { tcx, outer_index: ty::INNERMOST };
521509
for param in generics.params {
522510
if let GenericParamKind::Lifetime { .. } = param.kind {
523511
if tcx.is_late_bound(param.hir_id) {
524512
return Some(param.span);
525513
}
526514
}
527515
}
528-
visitor.visit_fn_decl(decl);
529-
visitor.has_late_bound_regions
516+
visitor.visit_fn_decl(decl).break_value()
530517
}
531518

532519
let decl = node.fn_decl()?;
@@ -536,26 +523,29 @@ fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<S
536523

537524
struct AnonConstInParamTyDetector {
538525
in_param_ty: bool,
539-
found_anon_const_in_param_ty: bool,
540526
ct: HirId,
541527
}
542528

543529
impl<'v> Visitor<'v> for AnonConstInParamTyDetector {
544-
fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) {
530+
type Result = ControlFlow<()>;
531+
532+
fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) -> Self::Result {
545533
if let GenericParamKind::Const { ty, default: _, is_host_effect: _, synthetic: _ } = p.kind
546534
{
547535
let prev = self.in_param_ty;
548536
self.in_param_ty = true;
549-
self.visit_ty(ty);
537+
let res = self.visit_ty(ty);
550538
self.in_param_ty = prev;
539+
res
540+
} else {
541+
ControlFlow::Continue(())
551542
}
552543
}
553544

554-
fn visit_anon_const(&mut self, c: &'v hir::AnonConst) {
545+
fn visit_anon_const(&mut self, c: &'v hir::AnonConst) -> Self::Result {
555546
if self.in_param_ty && self.ct == c.hir_id {
556-
self.found_anon_const_in_param_ty = true;
557-
} else {
558-
intravisit::walk_anon_const(self, c)
547+
return ControlFlow::Break(());
559548
}
549+
intravisit::walk_anon_const(self, c)
560550
}
561551
}

compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs

-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ struct NestedObligationsForSelfTy<'a, 'tcx> {
104104
}
105105

106106
impl<'a, 'tcx> ProofTreeVisitor<'tcx> for NestedObligationsForSelfTy<'a, 'tcx> {
107-
type Result = ();
108-
109107
fn span(&self) -> Span {
110108
self.root_cause.span
111109
}

compiler/rustc_trait_selection/src/traits/object_safety.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,11 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>(
805805
.unwrap()
806806
.contains(&data.trait_ref(self.tcx).def_id);
807807

808+
// only walk contained types if it's not a super trait
808809
if is_supertrait_of_current_trait {
809-
ControlFlow::Continue(()) // do not walk contained types, do not report error, do collect $200
810+
ControlFlow::Continue(())
810811
} else {
811-
t.super_visit_with(self) // DO walk contained types, POSSIBLY reporting an error
812+
t.super_visit_with(self) // POSSIBLY reporting an error
812813
}
813814
}
814815
_ => t.super_visit_with(self), // walk contained types, if any

compiler/rustc_trait_selection/src/traits/wf.rs

-2
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,6 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
640640
}
641641

642642
impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
643-
type Result = ();
644-
645643
fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
646644
debug!("wf bounds for t={:?} t.kind={:#?}", t, t.kind());
647645

0 commit comments

Comments
 (0)