Skip to content

Commit a06160d

Browse files
authored
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``
2 parents 8209715 + ed300d8 commit a06160d

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ pub enum LifetimeCtxt {
121121
/// explicitly, you need to override each method. (And you also need
122122
/// to monitor future changes to `Visitor` in case a new method with a
123123
/// new default implementation gets introduced.)
124+
///
125+
/// Every `walk_*` method uses deconstruction to access fields of structs and
126+
/// enums. This will result in a compile error if a field is added, which makes
127+
/// it more likely the appropriate visit call will be added for it.
124128
pub trait Visitor<'ast>: Sized {
125129
/// The result type of the `visit_*` methods. Can be either `()`,
126130
/// or `ControlFlow<T>`.

compiler/rustc_hir/src/intravisit.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ use nested_filter::NestedFilter;
200200
/// explicitly, you need to override each method. (And you also need
201201
/// to monitor future changes to `Visitor` in case a new method with a
202202
/// new default implementation gets introduced.)
203+
///
204+
/// Every `walk_*` method uses deconstruction to access fields of structs and
205+
/// enums. This will result in a compile error if a field is added, which makes
206+
/// it more likely the appropriate visit call will be added for it.
203207
pub trait Visitor<'v>: Sized {
204208
// This type should not be overridden, it exists for convenient usage as `Self::MaybeTyCtxt`.
205209
type MaybeTyCtxt: HirTyCtxt<'v> = <Self::NestedFilter as NestedFilter<'v>>::MaybeTyCtxt;
@@ -1201,7 +1205,6 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(
12011205
visitor: &mut V,
12021206
trait_item: &'v TraitItem<'v>,
12031207
) -> V::Result {
1204-
// N.B., deliberately force a compilation error if/when new fields are added.
12051208
let TraitItem { ident, generics, ref defaultness, ref kind, span, owner_id: _ } = *trait_item;
12061209
let hir_id = trait_item.hir_id();
12071210
try_visit!(visitor.visit_ident(ident));
@@ -1240,7 +1243,6 @@ pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(
12401243
visitor: &mut V,
12411244
trait_item_ref: &'v TraitItemRef,
12421245
) -> V::Result {
1243-
// N.B., deliberately force a compilation error if/when new fields are added.
12441246
let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
12451247
try_visit!(visitor.visit_nested_trait_item(id));
12461248
try_visit!(visitor.visit_ident(ident));
@@ -1251,7 +1253,6 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(
12511253
visitor: &mut V,
12521254
impl_item: &'v ImplItem<'v>,
12531255
) -> V::Result {
1254-
// N.B., deliberately force a compilation error if/when new fields are added.
12551256
let ImplItem {
12561257
owner_id: _,
12571258
ident,
@@ -1286,7 +1287,6 @@ pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
12861287
visitor: &mut V,
12871288
foreign_item_ref: &'v ForeignItemRef,
12881289
) -> V::Result {
1289-
// N.B., deliberately force a compilation error if/when new fields are added.
12901290
let ForeignItemRef { id, ident, span: _ } = *foreign_item_ref;
12911291
try_visit!(visitor.visit_nested_foreign_item(id));
12921292
visitor.visit_ident(ident)
@@ -1296,7 +1296,6 @@ pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(
12961296
visitor: &mut V,
12971297
impl_item_ref: &'v ImplItemRef,
12981298
) -> V::Result {
1299-
// N.B., deliberately force a compilation error if/when new fields are added.
13001299
let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
13011300
try_visit!(visitor.visit_nested_impl_item(id));
13021301
try_visit!(visitor.visit_ident(ident));

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use super::{
33
Pat, PatKind, Stmt, StmtKind, Thir,
44
};
55

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

0 commit comments

Comments
 (0)