Skip to content

Commit 496f14b

Browse files
committed
Convert SpannedTypeVisitor to use VisitorResult
1 parent e9b4f60 commit 496f14b

File tree

3 files changed

+23
-32
lines changed

3 files changed

+23
-32
lines changed

compiler/rustc_privacy/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,8 @@ impl<'tcx> TypePrivacyVisitor<'tcx> {
10691069
}
10701070

10711071
impl<'tcx> rustc_ty_utils::sig_types::SpannedTypeVisitor<'tcx> for TypePrivacyVisitor<'tcx> {
1072-
type BreakTy = ();
1073-
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> ControlFlow<()> {
1072+
type Result = ControlFlow<()>;
1073+
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> Self::Result {
10741074
self.span = span;
10751075
value.visit_with(&mut self.skeleton())
10761076
}

compiler/rustc_ty_utils/src/opaque_types.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
88
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
99
use rustc_span::Span;
1010
use rustc_trait_selection::traits::check_args_compatible;
11-
use std::ops::ControlFlow;
1211

1312
use crate::errors::{DuplicateArg, NotParam};
1413

@@ -185,9 +184,8 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
185184

186185
impl<'tcx> super::sig_types::SpannedTypeVisitor<'tcx> for OpaqueTypeCollector<'tcx> {
187186
#[instrument(skip(self), ret, level = "trace")]
188-
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> ControlFlow<!> {
187+
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) {
189188
self.visit_spanned(span, value);
190-
ControlFlow::Continue(())
191189
}
192190
}
193191

@@ -278,13 +276,11 @@ struct ImplTraitInAssocTypeCollector<'tcx>(OpaqueTypeCollector<'tcx>);
278276

279277
impl<'tcx> super::sig_types::SpannedTypeVisitor<'tcx> for ImplTraitInAssocTypeCollector<'tcx> {
280278
#[instrument(skip(self), ret, level = "trace")]
281-
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> ControlFlow<!> {
279+
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) {
282280
let old = self.0.span;
283281
self.0.span = Some(span);
284282
value.visit_with(self);
285283
self.0.span = old;
286-
287-
ControlFlow::Continue(())
288284
}
289285
}
290286

+19-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
//! This module contains helpers for walking all types of
22
//! a signature, while preserving spans as much as possible
33
4-
use std::ops::ControlFlow;
5-
64
use rustc_hir::{def::DefKind, def_id::LocalDefId};
75
use rustc_middle::ty::{self, TyCtxt};
86
use rustc_span::Span;
9-
use rustc_type_ir::visit::TypeVisitable;
7+
use rustc_type_ir::try_visit;
8+
use rustc_type_ir::visit::{TypeVisitable, VisitorResult};
109

1110
pub trait SpannedTypeVisitor<'tcx> {
12-
type BreakTy = !;
13-
fn visit(
14-
&mut self,
15-
span: Span,
16-
value: impl TypeVisitable<TyCtxt<'tcx>>,
17-
) -> ControlFlow<Self::BreakTy>;
11+
type Result: VisitorResult = ();
12+
fn visit(&mut self, span: Span, value: impl TypeVisitable<TyCtxt<'tcx>>) -> Self::Result;
1813
}
1914

2015
pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
2116
tcx: TyCtxt<'tcx>,
2217
item: LocalDefId,
2318
visitor: &mut V,
24-
) -> ControlFlow<V::BreakTy> {
19+
) -> V::Result {
2520
let kind = tcx.def_kind(item);
2621
trace!(?kind);
2722
match kind {
@@ -30,12 +25,12 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
3025
let ty_sig = tcx.fn_sig(item).instantiate_identity();
3126
let hir_sig = tcx.hir_node_by_def_id(item).fn_decl().unwrap();
3227
// Walk over the inputs and outputs manually in order to get good spans for them.
33-
visitor.visit(hir_sig.output.span(), ty_sig.output());
28+
try_visit!(visitor.visit(hir_sig.output.span(), ty_sig.output()));
3429
for (hir, ty) in hir_sig.inputs.iter().zip(ty_sig.inputs().iter()) {
35-
visitor.visit(hir.span, ty.map_bound(|x| *x))?;
30+
try_visit!(visitor.visit(hir.span, ty.map_bound(|x| *x)));
3631
}
3732
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
38-
visitor.visit(span, pred)?;
33+
try_visit!(visitor.visit(span, pred));
3934
}
4035
}
4136
// Walk over the type behind the alias
@@ -44,32 +39,32 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
4439
DefKind::Static(_) | DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => {
4540
if let Some(ty) = tcx.hir_node_by_def_id(item).ty() {
4641
// Associated types in traits don't necessarily have a type that we can visit
47-
visitor.visit(ty.span, tcx.type_of(item).instantiate_identity())?;
42+
try_visit!(visitor.visit(ty.span, tcx.type_of(item).instantiate_identity()));
4843
}
4944
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
50-
visitor.visit(span, pred)?;
45+
try_visit!(visitor.visit(span, pred));
5146
}
5247
}
5348
DefKind::OpaqueTy => {
5449
for (pred, span) in tcx.explicit_item_bounds(item).instantiate_identity_iter_copied() {
55-
visitor.visit(span, pred)?;
50+
try_visit!(visitor.visit(span, pred));
5651
}
5752
}
5853
// Look at field types
5954
DefKind::Struct | DefKind::Union | DefKind::Enum => {
6055
let span = tcx.def_ident_span(item).unwrap();
6156
let ty = tcx.type_of(item).instantiate_identity();
62-
visitor.visit(span, ty);
57+
try_visit!(visitor.visit(span, ty));
6358
let ty::Adt(def, args) = ty.kind() else {
6459
span_bug!(span, "invalid type for {kind:?}: {:#?}", ty.kind())
6560
};
6661
for field in def.all_fields() {
6762
let span = tcx.def_ident_span(field.did).unwrap();
6863
let ty = field.ty(tcx, args);
69-
visitor.visit(span, ty);
64+
try_visit!(visitor.visit(span, ty));
7065
}
7166
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
72-
visitor.visit(span, pred)?;
67+
try_visit!(visitor.visit(span, pred));
7368
}
7469
}
7570
// These are not part of a public API, they can only appear as hidden types, and there
@@ -80,20 +75,20 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
8075
if of_trait {
8176
let span = tcx.hir_node_by_def_id(item).expect_item().expect_impl().of_trait.unwrap().path.span;
8277
let args = &tcx.impl_trait_ref(item).unwrap().instantiate_identity().args[1..];
83-
visitor.visit(span, args)?;
78+
try_visit!(visitor.visit(span, args));
8479
}
8580
let span = match tcx.hir_node_by_def_id(item).ty() {
8681
Some(ty) => ty.span,
8782
_ => tcx.def_span(item),
8883
};
89-
visitor.visit(span, tcx.type_of(item).instantiate_identity());
84+
try_visit!(visitor.visit(span, tcx.type_of(item).instantiate_identity()));
9085
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
91-
visitor.visit(span, pred)?;
86+
try_visit!(visitor.visit(span, pred));
9287
}
9388
}
9489
DefKind::TraitAlias | DefKind::Trait => {
9590
for (pred, span) in tcx.predicates_of(item).instantiate_identity(tcx) {
96-
visitor.visit(span, pred)?;
91+
try_visit!(visitor.visit(span, pred));
9792
}
9893
}
9994
| DefKind::Variant
@@ -116,5 +111,5 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>(
116111
| DefKind::Mod
117112
| DefKind::Use => {}
118113
}
119-
ControlFlow::Continue(())
114+
V::Result::output()
120115
}

0 commit comments

Comments
 (0)