Skip to content

Commit 3c16447

Browse files
Rollup merge of rust-lang#121576 - Jarcho:visitor3, r=oli-obk
Convert the rest of the visitors to use `VisitorResult` Continuing from rust-lang#121256.
2 parents 8a3dc83 + e1e02a2 commit 3c16447

File tree

68 files changed

+498
-591
lines changed

Some content is hidden

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

68 files changed

+498
-591
lines changed

Cargo.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3970,6 +3970,7 @@ version = "0.0.0"
39703970
dependencies = [
39713971
"itertools 0.11.0",
39723972
"rustc_ast",
3973+
"rustc_ast_ir",
39733974
"rustc_attr",
39743975
"rustc_data_structures",
39753976
"rustc_errors",
@@ -4038,6 +4039,7 @@ dependencies = [
40384039
name = "rustc_infer"
40394040
version = "0.0.0"
40404041
dependencies = [
4042+
"rustc_ast_ir",
40414043
"rustc_data_structures",
40424044
"rustc_errors",
40434045
"rustc_fluent_macro",
@@ -4632,6 +4634,7 @@ dependencies = [
46324634
"bitflags 2.4.2",
46334635
"itertools 0.11.0",
46344636
"rustc_ast",
4637+
"rustc_ast_ir",
46354638
"rustc_attr",
46364639
"rustc_data_structures",
46374640
"rustc_errors",
@@ -4670,6 +4673,7 @@ name = "rustc_transmute"
46704673
version = "0.0.0"
46714674
dependencies = [
46724675
"itertools 0.11.0",
4676+
"rustc_ast_ir",
46734677
"rustc_data_structures",
46744678
"rustc_hir",
46754679
"rustc_infer",
@@ -4685,6 +4689,7 @@ name = "rustc_ty_utils"
46854689
version = "0.0.0"
46864690
dependencies = [
46874691
"itertools 0.11.0",
4692+
"rustc_ast_ir",
46884693
"rustc_data_structures",
46894694
"rustc_errors",
46904695
"rustc_fluent_macro",

compiler/rustc_ast/src/visit.rs

+3-65
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
1616
use crate::ast::*;
1717

18-
use core::ops::ControlFlow;
19-
2018
use rustc_span::symbol::Ident;
2119
use rustc_span::Span;
2220

21+
pub use rustc_ast_ir::visit::VisitorResult;
22+
pub use rustc_ast_ir::{try_visit, visit_opt, walk_list, walk_visitable_list};
23+
2324
#[derive(Copy, Clone, Debug, PartialEq)]
2425
pub enum AssocCtxt {
2526
Trait,
@@ -101,51 +102,6 @@ pub enum LifetimeCtxt {
101102
GenericArg,
102103
}
103104

104-
/// Similar to the `Try` trait, but also implemented for `()`.
105-
pub trait VisitorResult {
106-
type Residual;
107-
fn output() -> Self;
108-
fn from_residual(residual: Self::Residual) -> Self;
109-
fn branch(self) -> ControlFlow<Self::Residual>;
110-
}
111-
112-
impl VisitorResult for () {
113-
type Residual = !;
114-
115-
fn output() -> Self {}
116-
fn from_residual(_: !) -> Self {}
117-
fn branch(self) -> ControlFlow<!> {
118-
ControlFlow::Continue(())
119-
}
120-
}
121-
122-
impl<T> VisitorResult for ControlFlow<T> {
123-
type Residual = T;
124-
125-
fn output() -> Self {
126-
ControlFlow::Continue(())
127-
}
128-
fn from_residual(residual: Self::Residual) -> Self {
129-
ControlFlow::Break(residual)
130-
}
131-
fn branch(self) -> ControlFlow<T> {
132-
self
133-
}
134-
}
135-
136-
#[macro_export]
137-
macro_rules! try_visit {
138-
($e:expr) => {
139-
match $crate::visit::VisitorResult::branch($e) {
140-
core::ops::ControlFlow::Continue(()) => (),
141-
#[allow(unreachable_code)]
142-
core::ops::ControlFlow::Break(r) => {
143-
return $crate::visit::VisitorResult::from_residual(r);
144-
}
145-
}
146-
};
147-
}
148-
149105
/// Each method of the `Visitor` trait is a hook to be potentially
150106
/// overridden. Each method's default implementation recursively visits
151107
/// the substructure of the input via the corresponding `walk` method;
@@ -316,24 +272,6 @@ pub trait Visitor<'ast>: Sized {
316272
}
317273
}
318274

319-
#[macro_export]
320-
macro_rules! walk_list {
321-
($visitor: expr, $method: ident, $list: expr $(, $($extra_args: expr),* )?) => {
322-
for elem in $list {
323-
$crate::try_visit!($visitor.$method(elem $(, $($extra_args,)* )?));
324-
}
325-
}
326-
}
327-
328-
#[macro_export]
329-
macro_rules! visit_opt {
330-
($visitor: expr, $method: ident, $opt: expr $(, $($extra_args: expr),* )?) => {
331-
if let Some(x) = $opt {
332-
$crate::try_visit!($visitor.$method(x $(, $($extra_args,)* )?));
333-
}
334-
}
335-
}
336-
337275
pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) -> V::Result {
338276
walk_list!(visitor, visit_item, &krate.items);
339277
walk_list!(visitor, visit_attribute, &krate.attrs);

compiler/rustc_ast_ir/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
#![cfg_attr(feature = "nightly", feature(never_type))]
12
#![cfg_attr(feature = "nightly", feature(rustc_attrs))]
23
#![cfg_attr(feature = "nightly", allow(internal_features))]
34

45
#[cfg(feature = "nightly")]
56
#[macro_use]
67
extern crate rustc_macros;
78

9+
pub mod visit;
10+
811
/// The movability of a coroutine / closure literal:
912
/// whether a coroutine contains self-references, causing it to be `!Unpin`.
1013
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]

compiler/rustc_ast_ir/src/visit.rs

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use core::ops::ControlFlow;
2+
3+
/// Similar to the `Try` trait, but also implemented for `()`.
4+
pub trait VisitorResult {
5+
type Residual;
6+
fn output() -> Self;
7+
fn from_residual(residual: Self::Residual) -> Self;
8+
fn from_branch(b: ControlFlow<Self::Residual>) -> Self;
9+
fn branch(self) -> ControlFlow<Self::Residual>;
10+
}
11+
12+
impl VisitorResult for () {
13+
#[cfg(feature = "nightly")]
14+
type Residual = !;
15+
16+
#[cfg(not(feature = "nightly"))]
17+
type Residual = core::ops::Infallible;
18+
19+
fn output() -> Self {}
20+
fn from_residual(_: Self::Residual) -> Self {}
21+
fn from_branch(_: ControlFlow<Self::Residual>) -> Self {}
22+
fn branch(self) -> ControlFlow<Self::Residual> {
23+
ControlFlow::Continue(())
24+
}
25+
}
26+
27+
impl<T> VisitorResult for ControlFlow<T> {
28+
type Residual = T;
29+
30+
fn output() -> Self {
31+
ControlFlow::Continue(())
32+
}
33+
fn from_residual(residual: Self::Residual) -> Self {
34+
ControlFlow::Break(residual)
35+
}
36+
fn from_branch(b: Self) -> Self {
37+
b
38+
}
39+
fn branch(self) -> Self {
40+
self
41+
}
42+
}
43+
44+
#[macro_export]
45+
macro_rules! try_visit {
46+
($e:expr) => {
47+
match $crate::visit::VisitorResult::branch($e) {
48+
core::ops::ControlFlow::Continue(()) => (),
49+
#[allow(unreachable_code)]
50+
core::ops::ControlFlow::Break(r) => {
51+
return $crate::visit::VisitorResult::from_residual(r);
52+
}
53+
}
54+
};
55+
}
56+
57+
#[macro_export]
58+
macro_rules! visit_opt {
59+
($visitor: expr, $method: ident, $opt: expr $(, $($extra_args: expr),* )?) => {
60+
if let Some(x) = $opt {
61+
$crate::try_visit!($visitor.$method(x $(, $($extra_args,)* )?));
62+
}
63+
}
64+
}
65+
66+
#[macro_export]
67+
macro_rules! walk_list {
68+
($visitor: expr, $method: ident, $list: expr $(, $($extra_args: expr),* )?) => {
69+
for elem in $list {
70+
$crate::try_visit!($visitor.$method(elem $(, $($extra_args,)* )?));
71+
}
72+
}
73+
}
74+
75+
#[macro_export]
76+
macro_rules! walk_visitable_list {
77+
($visitor: expr, $list: expr $(, $($extra_args: expr),* )?) => {
78+
for elem in $list {
79+
$crate::try_visit!(elem.visit_with($visitor $(, $($extra_args,)* )?));
80+
}
81+
}
82+
}

compiler/rustc_ast_passes/src/ast_validation.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
use itertools::{Either, Itertools};
1010
use rustc_ast::ptr::P;
11-
use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
12-
use rustc_ast::walk_list;
11+
use rustc_ast::visit::{walk_list, AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
1312
use rustc_ast::*;
1413
use rustc_ast_pretty::pprust::{self, State};
1514
use rustc_data_structures::fx::FxIndexMap;

compiler/rustc_builtin_macros/src/deriving/default.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::errors;
44
use rustc_ast as ast;
5-
use rustc_ast::{attr, walk_list, EnumDef, VariantData};
5+
use rustc_ast::visit::walk_list;
6+
use rustc_ast::{attr, EnumDef, VariantData};
67
use rustc_expand::base::{Annotatable, DummyResult, ExtCtxt};
78
use rustc_span::symbol::Ident;
89
use rustc_span::symbol::{kw, sym};

compiler/rustc_const_eval/src/interpret/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ where
3030
}
3131

3232
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for UsedParamsNeedInstantiationVisitor<'tcx> {
33-
type BreakTy = FoundParam;
33+
type Result = ControlFlow<FoundParam>;
3434

35-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
35+
fn visit_ty(&mut self, ty: Ty<'tcx>) -> Self::Result {
3636
if !ty.has_param() {
3737
return ControlFlow::Continue(());
3838
}
@@ -64,7 +64,7 @@ where
6464
}
6565
}
6666

67-
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
67+
fn visit_const(&mut self, c: ty::Const<'tcx>) -> Self::Result {
6868
match c.kind() {
6969
ty::ConstKind::Param(..) => ControlFlow::Break(FoundParam),
7070
_ => c.super_visit_with(self),

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_trait_selection::traits::{self, ObligationCauseCode, ObligationCtxt};
1717
use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitor};
1818

1919
use std::mem;
20-
use std::ops::{ControlFlow, Deref};
20+
use std::ops::Deref;
2121

2222
use super::ops::{self, NonConstOp, Status};
2323
use super::qualifs::{self, HasMutInterior, NeedsDrop, NeedsNonConstDrop};
@@ -164,9 +164,9 @@ struct LocalReturnTyVisitor<'ck, 'mir, 'tcx> {
164164
}
165165

166166
impl<'ck, 'mir, 'tcx> TypeVisitor<TyCtxt<'tcx>> for LocalReturnTyVisitor<'ck, 'mir, 'tcx> {
167-
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
167+
fn visit_ty(&mut self, t: Ty<'tcx>) {
168168
match t.kind() {
169-
ty::FnPtr(_) => ControlFlow::Continue(()),
169+
ty::FnPtr(_) => {}
170170
ty::Ref(_, _, hir::Mutability::Mut) => {
171171
self.checker.check_op(ops::ty::MutRef(self.kind));
172172
t.super_visit_with(self)

compiler/rustc_expand/src/expand.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use rustc_ast::mut_visit::*;
1414
use rustc_ast::ptr::P;
1515
use rustc_ast::token::{self, Delimiter};
1616
use rustc_ast::tokenstream::TokenStream;
17-
use rustc_ast::visit::{self, AssocCtxt, Visitor, VisitorResult};
18-
use rustc_ast::{try_visit, walk_list};
17+
use rustc_ast::visit::{self, try_visit, walk_list, AssocCtxt, Visitor, VisitorResult};
1918
use rustc_ast::{AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, ExprKind};
2019
use rustc_ast::{ForeignItemKind, HasAttrs, HasNodeId};
2120
use rustc_ast::{Inline, ItemKind, MacStmtStyle, MetaItemKind, ModKind};

compiler/rustc_hir/src/intravisit.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@
6565
//! example coroutine inference, and possibly also HIR borrowck.
6666
6767
use crate::hir::*;
68-
use rustc_ast::visit::VisitorResult;
69-
use rustc_ast::{try_visit, visit_opt, walk_list};
68+
use rustc_ast::visit::{try_visit, visit_opt, walk_list, VisitorResult};
7069
use rustc_ast::{Attribute, Label};
7170
use rustc_span::def_id::LocalDefId;
7271
use rustc_span::symbol::{Ident, Symbol};

compiler/rustc_hir_analysis/src/check/check.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1474,15 +1474,14 @@ fn opaque_type_cycle_error(
14741474
closures: Vec<DefId>,
14751475
}
14761476
impl<'tcx> ty::visit::TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector {
1477-
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1477+
fn visit_ty(&mut self, t: Ty<'tcx>) {
14781478
match *t.kind() {
14791479
ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
14801480
self.opaques.push(def);
1481-
ControlFlow::Continue(())
14821481
}
14831482
ty::Closure(def_id, ..) | ty::Coroutine(def_id, ..) => {
14841483
self.closures.push(def_id);
1485-
t.super_visit_with(self)
1484+
t.super_visit_with(self);
14861485
}
14871486
_ => t.super_visit_with(self),
14881487
}

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_trait_selection::regions::InferCtxtRegionExt;
1212
use rustc_trait_selection::traits::{
1313
elaborate, normalize_param_env_or_error, outlives_bounds::InferCtxtExt, ObligationCtxt,
1414
};
15-
use std::ops::ControlFlow;
1615

1716
/// Check that an implementation does not refine an RPITIT from a trait method signature.
1817
pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
@@ -211,9 +210,7 @@ struct ImplTraitInTraitCollector<'tcx> {
211210
}
212211

213212
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitCollector<'tcx> {
214-
type BreakTy = !;
215-
216-
fn visit_ty(&mut self, ty: Ty<'tcx>) -> std::ops::ControlFlow<Self::BreakTy> {
213+
fn visit_ty(&mut self, ty: Ty<'tcx>) {
217214
if let ty::Alias(ty::Projection, proj) = *ty.kind()
218215
&& self.tcx.is_impl_trait_in_trait(proj.def_id)
219216
{
@@ -223,12 +220,11 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitCollector<'tcx> {
223220
.explicit_item_bounds(proj.def_id)
224221
.iter_instantiated_copied(self.tcx, proj.args)
225222
{
226-
pred.visit_with(self)?;
223+
pred.visit_with(self);
227224
}
228225
}
229-
ControlFlow::Continue(())
230226
} else {
231-
ty.super_visit_with(self)
227+
ty.super_visit_with(self);
232228
}
233229
}
234230
}

compiler/rustc_hir_analysis/src/check/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//!
77
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/borrow_check.html
88
9-
use rustc_ast::walk_list;
9+
use rustc_ast::visit::walk_list;
1010
use rustc_data_structures::fx::FxHashSet;
1111
use rustc_hir as hir;
1212
use rustc_hir::def_id::DefId;

0 commit comments

Comments
 (0)