From b5d7da878fa329b37f06d37c6a1a4d049953e7a0 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 1 Mar 2024 13:00:06 +1100 Subject: [PATCH 1/2] Decouple `DummyAstNode` and `DummyResult`. They are two different ways of creating dummy results, with two different purposes. Their implementations are separate except for crates, where `DummyResult` depends on `DummyAstNode`. This commit removes that dependency, so they are now fully separate. It also expands the comment on `DummyAstNode`. --- compiler/rustc_ast/src/mut_visit.rs | 5 ++++- compiler/rustc_expand/src/base.rs | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index bbbb34f7f2f48..da4328d8ce267 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1604,7 +1604,10 @@ pub fn noop_visit_capture_by(capture_by: &mut CaptureBy, vis: &mu } } -/// Some value for the AST node that is valid but possibly meaningless. +/// Some value for the AST node that is valid but possibly meaningless. Similar +/// to `Default` but not intended for wide use. The value will never be used +/// meaningfully, it exists just to support unwinding in `visit_clobber` in the +/// case where its closure panics. pub trait DummyAstNode { fn dummy() -> Self; } diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 485f0e7e46deb..d8ac4a540378e 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -4,7 +4,6 @@ use crate::expand::{self, AstFragment, Invocation}; use crate::module::DirOwnership; use rustc_ast::attr::MarkedAttrs; -use rustc_ast::mut_visit::DummyAstNode; use rustc_ast::ptr::P; use rustc_ast::token::{self, Nonterminal}; use rustc_ast::tokenstream::TokenStream; @@ -582,6 +581,17 @@ impl DummyResult { tokens: None, }) } + + /// A plain dummy crate. + pub fn raw_crate() -> ast::Crate { + ast::Crate { + attrs: Default::default(), + items: Default::default(), + spans: Default::default(), + id: ast::DUMMY_NODE_ID, + is_placeholder: Default::default(), + } + } } impl MacResult for DummyResult { @@ -650,7 +660,7 @@ impl MacResult for DummyResult { } fn make_crate(self: Box) -> Option { - Some(DummyAstNode::dummy()) + Some(DummyResult::raw_crate()) } } From a9dff2d931ad6844344c1b81f343ccdd1f4abdd9 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Sat, 2 Mar 2024 08:24:45 +1100 Subject: [PATCH 2/2] Remove unused `impl DummyAstNode for Block`. --- compiler/rustc_ast/src/mut_visit.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index da4328d8ce267..c517c6138ceb6 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -1682,19 +1682,6 @@ impl DummyAstNode for Stmt { } } -impl DummyAstNode for Block { - fn dummy() -> Self { - Block { - stmts: Default::default(), - id: DUMMY_NODE_ID, - rules: BlockCheckMode::Default, - span: Default::default(), - tokens: Default::default(), - could_be_bare_literal: Default::default(), - } - } -} - impl DummyAstNode for Crate { fn dummy() -> Self { Crate {