Rollup merge of #142007 - nnethercote:visitor-comments, r=chenyukang

Improve some `Visitor` comments.

For AST/HIR/THIR visitors, explain the use of deconstruction.

r? ``@BoxyUwU``
This commit is contained in:
Matthias Krüger
2025-06-04 16:24:12 +02:00
committed by GitHub
3 changed files with 11 additions and 5 deletions

View File

@@ -121,6 +121,10 @@ pub enum LifetimeCtxt {
/// explicitly, you need to override each method. (And you also need /// explicitly, you need to override each method. (And you also need
/// to monitor future changes to `Visitor` in case a new method with a /// to monitor future changes to `Visitor` in case a new method with a
/// new default implementation gets introduced.) /// new default implementation gets introduced.)
///
/// Every `walk_*` method uses deconstruction to access fields of structs and
/// enums. This will result in a compile error if a field is added, which makes
/// it more likely the appropriate visit call will be added for it.
pub trait Visitor<'ast>: Sized { pub trait Visitor<'ast>: Sized {
/// The result type of the `visit_*` methods. Can be either `()`, /// The result type of the `visit_*` methods. Can be either `()`,
/// or `ControlFlow<T>`. /// or `ControlFlow<T>`.

View File

@@ -200,6 +200,10 @@ use nested_filter::NestedFilter;
/// explicitly, you need to override each method. (And you also need /// explicitly, you need to override each method. (And you also need
/// to monitor future changes to `Visitor` in case a new method with a /// to monitor future changes to `Visitor` in case a new method with a
/// new default implementation gets introduced.) /// new default implementation gets introduced.)
///
/// Every `walk_*` method uses deconstruction to access fields of structs and
/// enums. This will result in a compile error if a field is added, which makes
/// it more likely the appropriate visit call will be added for it.
pub trait Visitor<'v>: Sized { pub trait Visitor<'v>: Sized {
// This type should not be overridden, it exists for convenient usage as `Self::MaybeTyCtxt`. // This type should not be overridden, it exists for convenient usage as `Self::MaybeTyCtxt`.
type MaybeTyCtxt: HirTyCtxt<'v> = <Self::NestedFilter as NestedFilter<'v>>::MaybeTyCtxt; type MaybeTyCtxt: HirTyCtxt<'v> = <Self::NestedFilter as NestedFilter<'v>>::MaybeTyCtxt;
@@ -1201,7 +1205,6 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(
visitor: &mut V, visitor: &mut V,
trait_item: &'v TraitItem<'v>, trait_item: &'v TraitItem<'v>,
) -> V::Result { ) -> V::Result {
// N.B., deliberately force a compilation error if/when new fields are added.
let TraitItem { ident, generics, ref defaultness, ref kind, span, owner_id: _ } = *trait_item; let TraitItem { ident, generics, ref defaultness, ref kind, span, owner_id: _ } = *trait_item;
let hir_id = trait_item.hir_id(); let hir_id = trait_item.hir_id();
try_visit!(visitor.visit_ident(ident)); try_visit!(visitor.visit_ident(ident));
@@ -1240,7 +1243,6 @@ pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(
visitor: &mut V, visitor: &mut V,
trait_item_ref: &'v TraitItemRef, trait_item_ref: &'v TraitItemRef,
) -> V::Result { ) -> V::Result {
// N.B., deliberately force a compilation error if/when new fields are added.
let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref; let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
try_visit!(visitor.visit_nested_trait_item(id)); try_visit!(visitor.visit_nested_trait_item(id));
try_visit!(visitor.visit_ident(ident)); try_visit!(visitor.visit_ident(ident));
@@ -1251,7 +1253,6 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(
visitor: &mut V, visitor: &mut V,
impl_item: &'v ImplItem<'v>, impl_item: &'v ImplItem<'v>,
) -> V::Result { ) -> V::Result {
// N.B., deliberately force a compilation error if/when new fields are added.
let ImplItem { let ImplItem {
owner_id: _, owner_id: _,
ident, ident,
@@ -1286,7 +1287,6 @@ pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
visitor: &mut V, visitor: &mut V,
foreign_item_ref: &'v ForeignItemRef, foreign_item_ref: &'v ForeignItemRef,
) -> V::Result { ) -> V::Result {
// N.B., deliberately force a compilation error if/when new fields are added.
let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref; let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref;
try_visit!(visitor.visit_nested_foreign_item(id)); try_visit!(visitor.visit_nested_foreign_item(id));
visitor.visit_ident(ident) visitor.visit_ident(ident)
@@ -1296,7 +1296,6 @@ pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(
visitor: &mut V, visitor: &mut V,
impl_item_ref: &'v ImplItemRef, impl_item_ref: &'v ImplItemRef,
) -> V::Result { ) -> V::Result {
// N.B., deliberately force a compilation error if/when new fields are added.
let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref; let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
try_visit!(visitor.visit_nested_impl_item(id)); try_visit!(visitor.visit_nested_impl_item(id));
try_visit!(visitor.visit_ident(ident)); try_visit!(visitor.visit_ident(ident));

View File

@@ -3,6 +3,9 @@ use super::{
Pat, PatKind, Stmt, StmtKind, Thir, Pat, PatKind, Stmt, StmtKind, Thir,
}; };
/// Every `walk_*` method uses deconstruction to access fields of structs and
/// enums. This will result in a compile error if a field is added, which makes
/// it more likely the appropriate visit call will be added for it.
pub trait Visitor<'thir, 'tcx: 'thir>: Sized { pub trait Visitor<'thir, 'tcx: 'thir>: Sized {
fn thir(&self) -> &'thir Thir<'tcx>; fn thir(&self) -> &'thir Thir<'tcx>;