Skip to content

Prefer not to early return from derived visitables #124263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions compiler/rustc_macros/src/type_visitable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,29 @@ pub fn type_visitable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2:
}

s.add_bounds(synstructure::AddBounds::Generics);
let body_visit = s.each(|bind| {
quote! {
match ::rustc_ast_ir::visit::VisitorResult::branch(
::rustc_middle::ty::visit::TypeVisitable::visit_with(#bind, __visitor)
) {
::core::ops::ControlFlow::Continue(()) => {},
::core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
},
let body_visit =
s.each_variant(|vi| {
if let Some((last, rest)) = vi.bindings().split_last() {
// Avoid early returning after visiting the final binding, so that LLVM can TCO the call.
rest.iter()
.map(|bind| quote! {
match ::rustc_ast_ir::visit::VisitorResult::branch(
::rustc_middle::ty::visit::TypeVisitable::visit_with(#bind, __visitor)
) {
::core::ops::ControlFlow::Continue(()) => {},
::core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
},
}
})
.chain(std::iter::once(quote! {
::rustc_middle::ty::visit::TypeVisitable::visit_with(#last, __visitor)
}))
.collect()
} else {
quote! { <__V::Result as ::rustc_ast_ir::visit::VisitorResult>::output() }
}
}
});
});
s.bind_with(|_| synstructure::BindStyle::Move);

s.bound_impl(
Expand All @@ -54,7 +65,6 @@ pub fn type_visitable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2:
__visitor: &mut __V
) -> __V::Result {
match *self { #body_visit }
<__V::Result as ::rustc_ast_ir::visit::VisitorResult>::output()
}
},
)
Expand Down
Loading