Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b7560f5

Browse files
committedNov 20, 2024·
Use visit_item instead of flat_map_item in test_harness.rs
1 parent 1dc1236 commit b7560f5

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed
 

Diff for: ‎compiler/rustc_builtin_macros/src/test_harness.rs

+21-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Code that generates a test runner to run all the tests in a crate
22

3-
use std::{iter, mem};
3+
use std::mem;
44

55
use rustc_ast as ast;
66
use rustc_ast::entry::EntryPointType;
@@ -19,7 +19,7 @@ use rustc_span::hygiene::{AstPass, SyntaxContext, Transparency};
1919
use rustc_span::symbol::{Ident, Symbol, sym};
2020
use rustc_span::{DUMMY_SP, Span};
2121
use rustc_target::spec::PanicStrategy;
22-
use smallvec::{SmallVec, smallvec};
22+
use smallvec::smallvec;
2323
use thin_vec::{ThinVec, thin_vec};
2424
use tracing::debug;
2525

@@ -129,8 +129,9 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
129129
c.items.push(mk_main(&mut self.cx));
130130
}
131131

132-
fn flat_map_item(&mut self, mut i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
133-
let item = &mut *i;
132+
fn visit_item(&mut self, item: &mut P<ast::Item>) {
133+
let item = &mut **item;
134+
134135
if let Some(name) = get_test_name(&item) {
135136
debug!("this is a test item");
136137

@@ -158,7 +159,6 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
158159
// But in those cases, we emit a lint to warn the user of these missing tests.
159160
walk_item(&mut InnerItemLinter { sess: self.cx.ext_cx.sess }, &item);
160161
}
161-
smallvec![i]
162162
}
163163
}
164164

@@ -198,40 +198,30 @@ struct EntryPointCleaner<'a> {
198198
}
199199

200200
impl<'a> MutVisitor for EntryPointCleaner<'a> {
201-
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
201+
fn visit_item(&mut self, item: &mut P<ast::Item>) {
202202
self.depth += 1;
203-
let item = walk_flat_map_item(self, i).expect_one("noop did something");
203+
ast::mut_visit::walk_item(self, item, ());
204204
self.depth -= 1;
205205

206206
// Remove any #[rustc_main] or #[start] from the AST so it doesn't
207207
// clash with the one we're going to add, but mark it as
208208
// #[allow(dead_code)] to avoid printing warnings.
209-
let item = match entry_point_type(&item, self.depth == 0) {
209+
match entry_point_type(&item, self.depth == 0) {
210210
EntryPointType::MainNamed | EntryPointType::RustcMainAttr | EntryPointType::Start => {
211-
item.map(|ast::Item { id, ident, attrs, kind, vis, span, tokens }| {
212-
let allow_dead_code = attr::mk_attr_nested_word(
213-
&self.sess.psess.attr_id_generator,
214-
ast::AttrStyle::Outer,
215-
ast::Safety::Default,
216-
sym::allow,
217-
sym::dead_code,
218-
self.def_site,
219-
);
220-
let attrs = attrs
221-
.into_iter()
222-
.filter(|attr| {
223-
!attr.has_name(sym::rustc_main) && !attr.has_name(sym::start)
224-
})
225-
.chain(iter::once(allow_dead_code))
226-
.collect();
227-
228-
ast::Item { id, ident, attrs, kind, vis, span, tokens }
229-
})
211+
let allow_dead_code = attr::mk_attr_nested_word(
212+
&self.sess.psess.attr_id_generator,
213+
ast::AttrStyle::Outer,
214+
ast::Safety::Default,
215+
sym::allow,
216+
sym::dead_code,
217+
self.def_site,
218+
);
219+
item.attrs
220+
.retain(|attr| !attr.has_name(sym::rustc_main) && !attr.has_name(sym::start));
221+
item.attrs.push(allow_dead_code);
230222
}
231-
EntryPointType::None | EntryPointType::OtherMain => item,
223+
EntryPointType::None | EntryPointType::OtherMain => {}
232224
};
233-
234-
smallvec![item]
235225
}
236226
}
237227

@@ -292,7 +282,7 @@ fn generate_test_harness(
292282
/// Most of the Ident have the usual def-site hygiene for the AST pass. The
293283
/// exception is the `test_const`s. These have a syntax context that has two
294284
/// opaque marks: one from the expansion of `test` or `test_case`, and one
295-
/// generated in `TestHarnessGenerator::flat_map_item`. When resolving this
285+
/// generated in `TestHarnessGenerator::visit_item`. When resolving this
296286
/// identifier after failing to find a matching identifier in the root module
297287
/// we remove the outer mark, and try resolving at its def-site, which will
298288
/// then resolve to `test_const`.

0 commit comments

Comments
 (0)
Please sign in to comment.