Skip to content

Commit c7b5536

Browse files
committed
Unify walk_ty
1 parent 0575403 commit c7b5536

File tree

1 file changed

+81
-103
lines changed

1 file changed

+81
-103
lines changed

compiler/rustc_ast/src/visitors.rs

Lines changed: 81 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,16 @@ macro_rules! make_ast_visitor {
416416
}
417417
}
418418

419+
macro_rules! visit_safety {
420+
($vis: expr, $safety: expr) => {
421+
if_mut_expr!(
422+
visit_safety($vis, $safety)
423+
,
424+
// assign to _ to prevent unused_variable warnings
425+
{ let _ = (&$vis, &$safety); }
426+
);
427+
}
428+
}
419429

420430
// FIXME: should only exist while Visitor::visit_ident
421431
// doesn't receives a reference
@@ -1075,6 +1085,77 @@ macro_rules! make_ast_visitor {
10751085
return_result!(V)
10761086
}
10771087

1088+
pub fn walk_ty<$($lt,)? V: $trait$(<$lt>)?>(
1089+
vis: &mut V,
1090+
ty: ref_t!(P!(Ty))
1091+
) -> result!(V) {
1092+
let Ty { id, kind, span, tokens } = deref_P!(ty);
1093+
try_v!(visit_id!(vis, id));
1094+
match kind {
1095+
TyKind::Err(_guar) => {}
1096+
TyKind::Infer
1097+
| TyKind::ImplicitSelf
1098+
| TyKind::Dummy
1099+
| TyKind::Never
1100+
| TyKind::CVarArgs => {}
1101+
TyKind::Slice(ty) => {
1102+
try_v!(vis.visit_ty(ty));
1103+
}
1104+
TyKind::Ptr(mt) => {
1105+
try_v!(vis.visit_mt(mt));
1106+
}
1107+
TyKind::Ref(lt, mt) => {
1108+
visit_o!(lt, |lt| vis.visit_lifetime(lt, LifetimeCtxt::Ref));
1109+
try_v!(vis.visit_mt(mt));
1110+
}
1111+
TyKind::BareFn(bft) => {
1112+
let BareFnTy { safety, ext: _, generic_params, decl, decl_span } = & $($mut)? **bft;
1113+
visit_safety!(vis, safety);
1114+
visit_list!(vis, visit_generic_param, flat_map_generic_param, generic_params);
1115+
try_v!(vis.visit_fn_decl(decl));
1116+
try_v!(visit_span!(vis, decl_span));
1117+
}
1118+
TyKind::Tup(tys) => {
1119+
visit_list!(vis, visit_ty, tys);
1120+
}
1121+
TyKind::Paren(ty) => {
1122+
try_v!(vis.visit_ty(ty))
1123+
}
1124+
TyKind::Pat(ty, pat) => {
1125+
try_v!(vis.visit_ty(ty));
1126+
try_v!(vis.visit_pat(pat));
1127+
}
1128+
TyKind::Path(qself, path) => {
1129+
try_v!(vis.visit_qself(qself));
1130+
try_v!(vis.visit_path(path, *id));
1131+
}
1132+
TyKind::Array(ty, length) => {
1133+
try_v!(vis.visit_ty(ty));
1134+
try_v!(vis.visit_anon_const(length));
1135+
}
1136+
TyKind::Typeof(expr) => {
1137+
try_v!(vis.visit_anon_const(expr));
1138+
},
1139+
TyKind::TraitObject(bounds, _syntax) => {
1140+
visit_list!(vis, visit_param_bound, bounds; BoundKind::TraitObject);
1141+
}
1142+
TyKind::ImplTrait(id, bounds) => {
1143+
try_v!(visit_id!(vis, id));
1144+
visit_list!(vis, visit_param_bound, bounds; BoundKind::Impl);
1145+
}
1146+
TyKind::MacCall(mac) => {
1147+
try_v!(vis.visit_mac_call(mac))
1148+
}
1149+
TyKind::AnonStruct(id, fields) | TyKind::AnonUnion(id, fields) => {
1150+
try_v!(visit_id!(vis, id));
1151+
visit_list!(vis, visit_field_def, flat_map_field_def, fields);
1152+
}
1153+
}
1154+
visit_lazy_tts!(vis, tokens);
1155+
try_v!(visit_span!(vis, span));
1156+
return_result!(V)
1157+
}
1158+
10781159
derive_copy_clone!{
10791160
#[derive(Debug)]
10801161
pub enum FnKind<'a> {
@@ -1327,54 +1408,6 @@ pub mod visit {
13271408
}
13281409
}
13291410

1330-
pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) -> V::Result {
1331-
let Ty { id, kind, span: _, tokens: _ } = typ;
1332-
match kind {
1333-
TyKind::Slice(ty) | TyKind::Paren(ty) => try_visit!(visitor.visit_ty(ty)),
1334-
TyKind::Ptr(mt) => try_visit!(visitor.visit_mt(mt)),
1335-
TyKind::Ref(opt_lifetime, mt) => {
1336-
visit_opt!(visitor, visit_lifetime, opt_lifetime, LifetimeCtxt::Ref);
1337-
try_visit!(visitor.visit_mt(mt));
1338-
}
1339-
TyKind::Tup(tuple_element_types) => {
1340-
walk_list!(visitor, visit_ty, tuple_element_types);
1341-
}
1342-
TyKind::BareFn(function_declaration) => {
1343-
let BareFnTy { safety: _, ext: _, generic_params, decl, decl_span: _ } =
1344-
&**function_declaration;
1345-
walk_list!(visitor, visit_generic_param, generic_params);
1346-
try_visit!(visitor.visit_fn_decl(decl));
1347-
}
1348-
TyKind::Path(maybe_qself, path) => {
1349-
try_visit!(visitor.visit_qself(maybe_qself));
1350-
try_visit!(visitor.visit_path(path, *id));
1351-
}
1352-
TyKind::Pat(ty, pat) => {
1353-
try_visit!(visitor.visit_ty(ty));
1354-
try_visit!(visitor.visit_pat(pat));
1355-
}
1356-
TyKind::Array(ty, length) => {
1357-
try_visit!(visitor.visit_ty(ty));
1358-
try_visit!(visitor.visit_anon_const(length));
1359-
}
1360-
TyKind::TraitObject(bounds, _syntax) => {
1361-
walk_list!(visitor, visit_param_bound, bounds, BoundKind::TraitObject);
1362-
}
1363-
TyKind::ImplTrait(_id, bounds) => {
1364-
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
1365-
}
1366-
TyKind::Typeof(expression) => try_visit!(visitor.visit_anon_const(expression)),
1367-
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Dummy => {}
1368-
TyKind::Err(_guar) => {}
1369-
TyKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
1370-
TyKind::Never | TyKind::CVarArgs => {}
1371-
TyKind::AnonStruct(_id, ref fields) | TyKind::AnonUnion(_id, ref fields) => {
1372-
walk_list!(visitor, visit_field_def, fields);
1373-
}
1374-
}
1375-
V::Result::output()
1376-
}
1377-
13781411
pub fn walk_assoc_item_constraint<'a, V: Visitor<'a>>(
13791412
visitor: &mut V,
13801413
constraint: &'a AssocItemConstraint,
@@ -1927,61 +1960,6 @@ pub mod mut_visit {
19271960
vis.visit_span(span);
19281961
}
19291962

1930-
pub fn walk_ty<T: MutVisitor>(vis: &mut T, ty: &mut P<Ty>) {
1931-
let Ty { id, kind, span, tokens } = ty.deref_mut();
1932-
vis.visit_id(id);
1933-
match kind {
1934-
TyKind::Err(_guar) => {}
1935-
TyKind::Infer
1936-
| TyKind::ImplicitSelf
1937-
| TyKind::Dummy
1938-
| TyKind::Never
1939-
| TyKind::CVarArgs => {}
1940-
TyKind::Slice(ty) => vis.visit_ty(ty),
1941-
TyKind::Ptr(mt) => vis.visit_mt(mt),
1942-
TyKind::Ref(lt, mt) => {
1943-
visit_opt(lt, |lt| vis.visit_lifetime(lt, LifetimeCtxt::Ref));
1944-
vis.visit_mt(mt);
1945-
}
1946-
TyKind::BareFn(bft) => {
1947-
let BareFnTy { safety, ext: _, generic_params, decl, decl_span } = bft.deref_mut();
1948-
visit_safety(vis, safety);
1949-
generic_params.flat_map_in_place(|param| vis.flat_map_generic_param(param));
1950-
vis.visit_fn_decl(decl);
1951-
vis.visit_span(decl_span);
1952-
}
1953-
TyKind::Tup(tys) => visit_thin_vec(tys, |ty| vis.visit_ty(ty)),
1954-
TyKind::Paren(ty) => vis.visit_ty(ty),
1955-
TyKind::Pat(ty, pat) => {
1956-
vis.visit_ty(ty);
1957-
vis.visit_pat(pat);
1958-
}
1959-
TyKind::Path(qself, path) => {
1960-
vis.visit_qself(qself);
1961-
vis.visit_path(path, *id);
1962-
}
1963-
TyKind::Array(ty, length) => {
1964-
vis.visit_ty(ty);
1965-
vis.visit_anon_const(length);
1966-
}
1967-
TyKind::Typeof(expr) => vis.visit_anon_const(expr),
1968-
TyKind::TraitObject(bounds, _syntax) => {
1969-
visit_vec(bounds, |bound| vis.visit_param_bound(bound, BoundKind::TraitObject))
1970-
}
1971-
TyKind::ImplTrait(id, bounds) => {
1972-
vis.visit_id(id);
1973-
visit_vec(bounds, |bound| vis.visit_param_bound(bound, BoundKind::Impl));
1974-
}
1975-
TyKind::MacCall(mac) => vis.visit_mac_call(mac),
1976-
TyKind::AnonStruct(id, fields) | TyKind::AnonUnion(id, fields) => {
1977-
vis.visit_id(id);
1978-
fields.flat_map_in_place(|field| vis.flat_map_field_def(field));
1979-
}
1980-
}
1981-
visit_lazy_tts(vis, tokens);
1982-
vis.visit_span(span);
1983-
}
1984-
19851963
fn walk_foreign_mod<T: MutVisitor>(vis: &mut T, foreign_mod: &mut ForeignMod) {
19861964
let ForeignMod { safety, abi: _, items } = foreign_mod;
19871965
visit_safety(vis, safety);

0 commit comments

Comments
 (0)