Skip to content

Commit d6602a4

Browse files
committed
Move visitor utils to rustc_ast_ir
1 parent 5257aee commit d6602a4

File tree

20 files changed

+116
-76
lines changed

20 files changed

+116
-76
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3471,6 +3471,7 @@ version = "0.0.0"
34713471
dependencies = [
34723472
"itertools 0.11.0",
34733473
"rustc_ast",
3474+
"rustc_ast_ir",
34743475
"rustc_ast_pretty",
34753476
"rustc_attr",
34763477
"rustc_data_structures",
@@ -3557,6 +3558,7 @@ name = "rustc_builtin_macros"
35573558
version = "0.0.0"
35583559
dependencies = [
35593560
"rustc_ast",
3561+
"rustc_ast_ir",
35603562
"rustc_ast_pretty",
35613563
"rustc_attr",
35623564
"rustc_data_structures",
@@ -3831,6 +3833,7 @@ name = "rustc_expand"
38313833
version = "0.0.0"
38323834
dependencies = [
38333835
"rustc_ast",
3836+
"rustc_ast_ir",
38343837
"rustc_ast_passes",
38353838
"rustc_ast_pretty",
38363839
"rustc_attr",
@@ -3887,6 +3890,7 @@ dependencies = [
38873890
"odht",
38883891
"rustc_arena",
38893892
"rustc_ast",
3893+
"rustc_ast_ir",
38903894
"rustc_data_structures",
38913895
"rustc_index",
38923896
"rustc_macros",
@@ -3904,6 +3908,7 @@ dependencies = [
39043908
"itertools 0.11.0",
39053909
"rustc_arena",
39063910
"rustc_ast",
3911+
"rustc_ast_ir",
39073912
"rustc_attr",
39083913
"rustc_data_structures",
39093914
"rustc_errors",
@@ -4086,6 +4091,7 @@ name = "rustc_lint"
40864091
version = "0.0.0"
40874092
dependencies = [
40884093
"rustc_ast",
4094+
"rustc_ast_ir",
40894095
"rustc_ast_pretty",
40904096
"rustc_attr",
40914097
"rustc_data_structures",
@@ -4465,6 +4471,7 @@ dependencies = [
44654471
"pulldown-cmark 0.9.6",
44664472
"rustc_arena",
44674473
"rustc_ast",
4474+
"rustc_ast_ir",
44684475
"rustc_ast_pretty",
44694476
"rustc_attr",
44704477
"rustc_data_structures",

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
1616
use crate::ast::*;
1717

18-
use core::ops::ControlFlow;
19-
18+
use rustc_ast_ir::visit::VisitorResult;
19+
use rustc_ast_ir::{try_visit, visit_opt, walk_list};
2020
use rustc_span::symbol::Ident;
2121
use rustc_span::Span;
2222

@@ -101,51 +101,6 @@ pub enum LifetimeCtxt {
101101
GenericArg,
102102
}
103103

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-
149104
/// Each method of the `Visitor` trait is a hook to be potentially
150105
/// overridden. Each method's default implementation recursively visits
151106
/// the substructure of the input via the corresponding `walk` method;
@@ -316,24 +271,6 @@ pub trait Visitor<'ast>: Sized {
316271
}
317272
}
318273

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-
337274
pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) -> V::Result {
338275
walk_list!(visitor, visit_item, &krate.items);
339276
walk_list!(visitor, visit_attribute, &krate.attrs);

compiler/rustc_ast_ir/src/lib.rs

Lines changed: 3 additions & 0 deletions
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

Lines changed: 82 additions & 0 deletions
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/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
# tidy-alphabetical-start
88
itertools = "0.11"
99
rustc_ast = { path = "../rustc_ast" }
10+
rustc_ast_ir = { path = "../rustc_ast_ir" }
1011
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1112
rustc_attr = { path = "../rustc_attr" }
1213
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use itertools::{Either, Itertools};
1010
use rustc_ast::ptr::P;
1111
use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
12-
use rustc_ast::walk_list;
1312
use rustc_ast::*;
13+
use rustc_ast_ir::walk_list;
1414
use rustc_ast_pretty::pprust::{self, State};
1515
use rustc_data_structures::fx::FxIndexMap;
1616
use rustc_feature::Features;

compiler/rustc_builtin_macros/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ doctest = false
99
[dependencies]
1010
# tidy-alphabetical-start
1111
rustc_ast = { path = "../rustc_ast" }
12+
rustc_ast_ir = { path = "../rustc_ast_ir" }
1213
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1314
rustc_attr = { path = "../rustc_attr" }
1415
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_builtin_macros/src/deriving/default.rs

Lines changed: 2 additions & 1 deletion
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::{attr, EnumDef, VariantData};
6+
use rustc_ast_ir::walk_list;
67
use rustc_expand::base::{Annotatable, DummyResult, ExtCtxt};
78
use rustc_span::symbol::Ident;
89
use rustc_span::symbol::{kw, sym};

compiler/rustc_expand/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ doctest = false
1010
[dependencies]
1111
# tidy-alphabetical-start
1212
rustc_ast = { path = "../rustc_ast" }
13+
rustc_ast_ir = { path = "../rustc_ast_ir" }
1314
rustc_ast_passes = { path = "../rustc_ast_passes" }
1415
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1516
rustc_attr = { path = "../rustc_attr" }

compiler/rustc_expand/src/expand.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ 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, AssocCtxt, Visitor};
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};
2221
use rustc_ast::{NestedMetaItem, NodeId, PatKind, StmtKind, TyKind};
22+
use rustc_ast_ir::visit::VisitorResult;
23+
use rustc_ast_ir::{try_visit, walk_list};
2324
use rustc_ast_pretty::pprust;
2425
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
2526
use rustc_data_structures::sync::Lrc;

compiler/rustc_hir/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = "2021"
88
odht = { version = "0.3.1", features = ["nightly"] }
99
rustc_arena = { path = "../rustc_arena" }
1010
rustc_ast = { path = "../rustc_ast" }
11+
rustc_ast_ir = { path = "../rustc_ast_ir" }
1112
rustc_data_structures = { path = "../rustc_data_structures" }
1213
rustc_index = { path = "../rustc_index" }
1314
rustc_macros = { path = "../rustc_macros" }

compiler/rustc_hir/src/intravisit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
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};
7068
use rustc_ast::{Attribute, Label};
69+
use rustc_ast_ir::visit::VisitorResult;
70+
use rustc_ast_ir::{try_visit, visit_opt, walk_list};
7171
use rustc_span::def_id::LocalDefId;
7272
use rustc_span::symbol::{Ident, Symbol};
7373
use rustc_span::Span;

compiler/rustc_hir_analysis/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ doctest = false
1212
itertools = "0.11"
1313
rustc_arena = { path = "../rustc_arena" }
1414
rustc_ast = { path = "../rustc_ast" }
15+
rustc_ast_ir = { path = "../rustc_ast_ir" }
1516
rustc_attr = { path = "../rustc_attr" }
1617
rustc_data_structures = { path = "../rustc_data_structures" }
1718
rustc_errors = { path = "../rustc_errors" }

compiler/rustc_hir_analysis/src/check/region.rs

Lines changed: 1 addition & 1 deletion
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_ir::walk_list;
1010
use rustc_data_structures::fx::FxHashSet;
1111
use rustc_hir as hir;
1212
use rustc_hir::def_id::DefId;

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! the types in HIR to identify late-bound lifetimes and assign their Debruijn indices. This file
77
//! is also responsible for assigning their semantics to implicit lifetimes in trait objects.
88
9-
use rustc_ast::walk_list;
9+
use rustc_ast_ir::walk_list;
1010
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
1111
use rustc_errors::{codes::*, struct_span_code_err};
1212
use rustc_hir as hir;

compiler/rustc_lint/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
# tidy-alphabetical-start
88
rustc_ast = { path = "../rustc_ast" }
9+
rustc_ast_ir = { path = "../rustc_ast_ir" }
910
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1011
rustc_attr = { path = "../rustc_attr" }
1112
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_lint/src/early.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use crate::context::{EarlyContext, LintContext, LintStore};
1818
use crate::passes::{EarlyLintPass, EarlyLintPassObject};
1919
use rustc_ast::ptr::P;
2020
use rustc_ast::visit::{self as ast_visit, Visitor};
21-
use rustc_ast::{self as ast, walk_list, HasAttrs};
21+
use rustc_ast_ir::walk_list;
22+
use rustc_ast::{self as ast, HasAttrs};
2223
use rustc_data_structures::stack::ensure_sufficient_stack;
2324
use rustc_feature::Features;
2425
use rustc_middle::ty::RegisteredTools;

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
33
use crate::query::LocalCrate;
44
use crate::ty::TyCtxt;
55
use rustc_ast as ast;
6-
use rustc_ast::visit::VisitorResult;
7-
use rustc_ast::walk_list;
6+
use rustc_ast_ir::visit::VisitorResult;
7+
use rustc_ast_ir::walk_list;
88
use rustc_data_structures::fingerprint::Fingerprint;
99
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1010
use rustc_data_structures::svh::Svh;

compiler/rustc_resolve/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bitflags = "2.4.1"
99
pulldown-cmark = { version = "0.9.6", default-features = false }
1010
rustc_arena = { path = "../rustc_arena" }
1111
rustc_ast = { path = "../rustc_ast" }
12+
rustc_ast_ir = { path = "../rustc_ast_ir" }
1213
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1314
rustc_attr = { path = "../rustc_attr" }
1415
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_resolve/src/late.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{ResolutionError, Resolver, Segment, UseError};
1515
use rustc_ast::ptr::P;
1616
use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor};
1717
use rustc_ast::*;
18+
use rustc_ast_ir::walk_list;
1819
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
1920
use rustc_errors::{
2021
codes::*, struct_span_code_err, Applicability, DiagArgValue, IntoDiagnosticArg, StashKey,

0 commit comments

Comments
 (0)