Skip to content

Commit 079e9fe

Browse files
bors[bot]matklad
andauthored
Merge #10512
10512: internal: add integrated test for token censoring r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents e9be41e + 634f047 commit 079e9fe

File tree

7 files changed

+105
-71
lines changed

7 files changed

+105
-71
lines changed

crates/base_db/src/fixture.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ fn test_proc_macros(proc_macros: &[String]) -> (Vec<ProcMacro>, String) {
270270
pub fn identity(_attr: TokenStream, item: TokenStream) -> TokenStream {
271271
item
272272
}
273+
#[proc_macro_derive(derive_identity)]
274+
pub fn derive_identity(item: TokenStream) -> TokenStream {
275+
item
276+
}
273277
#[proc_macro_attribute]
274278
pub fn input_replace(attr: TokenStream, _item: TokenStream) -> TokenStream {
275279
attr
@@ -285,6 +289,11 @@ pub fn mirror(input: TokenStream) -> TokenStream {
285289
kind: crate::ProcMacroKind::Attr,
286290
expander: Arc::new(IdentityProcMacroExpander),
287291
},
292+
ProcMacro {
293+
name: "derive_identity".into(),
294+
kind: crate::ProcMacroKind::CustomDerive,
295+
expander: Arc::new(IdentityProcMacroExpander),
296+
},
288297
ProcMacro {
289298
name: "input_replace".into(),
290299
kind: crate::ProcMacroKind::Attr,

crates/hir_def/src/macro_expansion_tests.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
mod mbe;
1313
mod builtin_fn_macro;
1414
mod builtin_derive_macro;
15+
mod proc_macros;
1516

1617
use std::{iter, ops::Range};
1718

@@ -28,7 +29,7 @@ use syntax::{
2829

2930
use crate::{
3031
db::DefDatabase, nameres::ModuleSource, resolver::HasResolver, src::HasSource, test_db::TestDB,
31-
AsMacroCall, Lookup,
32+
AdtId, AsMacroCall, Lookup, ModuleDefId,
3233
};
3334

3435
#[track_caller]
@@ -124,6 +125,16 @@ fn check(ra_fixture: &str, mut expect: Expect) {
124125
expanded_text.replace_range(range, &expn_text)
125126
}
126127

128+
for decl_id in def_map[local_id].scope.declarations() {
129+
if let ModuleDefId::AdtId(AdtId::StructId(struct_id)) = decl_id {
130+
let src = struct_id.lookup(&db).source(&db);
131+
if src.file_id.is_attr_macro(&db) || src.file_id.is_custom_derive(&db) {
132+
let pp = pretty_print_macro_expansion(src.value.syntax().clone());
133+
format_to!(expanded_text, "\n{}", pp)
134+
}
135+
}
136+
}
137+
127138
for impl_id in def_map[local_id].scope.impls() {
128139
let src = impl_id.lookup(&db).source(&db);
129140
if src.file_id.is_builtin_derive(&db).is_some() {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! Tests for user-defined procedural macros.
2+
//!
3+
//! Note `//- proc_macros: identity` fixture metas in tests -- we don't use real
4+
//! proc-macros here, as that would be slow. Instead, we use several hard-coded
5+
//! in-memory macros.
6+
use expect_test::expect;
7+
8+
use crate::macro_expansion_tests::check;
9+
10+
#[test]
11+
fn attribute_macro_attr_censoring() {
12+
cov_mark::check!(attribute_macro_attr_censoring);
13+
check(
14+
r#"
15+
//- proc_macros: identity
16+
#[attr1] #[proc_macros::identity] #[attr2]
17+
struct S;
18+
"#,
19+
expect![[r##"
20+
#[attr1] #[proc_macros::identity] #[attr2]
21+
struct S;
22+
23+
#[attr1]
24+
#[attr2] struct S;"##]],
25+
);
26+
}
27+
28+
#[test]
29+
fn derive_censoring() {
30+
cov_mark::check!(derive_censoring);
31+
check(
32+
r#"
33+
//- proc_macros: derive_identity
34+
#[attr1]
35+
#[derive(Foo)]
36+
#[derive(proc_macros::derive_identity)]
37+
#[derive(Bar)]
38+
#[attr2]
39+
struct S;
40+
"#,
41+
expect![[r##"
42+
#[attr1]
43+
#[derive(Foo)]
44+
#[derive(proc_macros::derive_identity)]
45+
#[derive(Bar)]
46+
#[attr2]
47+
struct S;
48+
49+
#[attr1]
50+
#[derive(Bar)]
51+
#[attr2] struct S;"##]],
52+
);
53+
}

crates/hir_expand/src/db.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,24 @@ fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<Sy
319319
(|| {
320320
let censor = match loc.kind {
321321
MacroCallKind::FnLike { .. } => return None,
322-
MacroCallKind::Derive { derive_attr_index, .. } => ast::Item::cast(node.clone())?
323-
.attrs()
324-
.take(derive_attr_index as usize + 1)
325-
.filter(|attr| attr.simple_name().as_deref() == Some("derive"))
326-
.map(|it| it.syntax().clone())
327-
.collect(),
328-
MacroCallKind::Attr { invoc_attr_index, .. } => ast::Item::cast(node.clone())?
329-
.attrs()
330-
.nth(invoc_attr_index as usize)
331-
.map(|attr| attr.syntax().clone())
332-
.into_iter()
333-
.collect(),
322+
MacroCallKind::Derive { derive_attr_index, .. } => {
323+
cov_mark::hit!(derive_censoring);
324+
ast::Item::cast(node.clone())?
325+
.attrs()
326+
.take(derive_attr_index as usize + 1)
327+
.filter(|attr| attr.simple_name().as_deref() == Some("derive"))
328+
.map(|it| it.syntax().clone())
329+
.collect()
330+
}
331+
MacroCallKind::Attr { invoc_attr_index, .. } => {
332+
cov_mark::hit!(attribute_macro_attr_censoring);
333+
ast::Item::cast(node.clone())?
334+
.attrs()
335+
.nth(invoc_attr_index as usize)
336+
.map(|attr| attr.syntax().clone())
337+
.into_iter()
338+
.collect()
339+
}
334340
};
335341
Some(censor)
336342
})()

crates/hir_expand/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ impl HirFileId {
175175
}
176176
}
177177

178+
pub fn is_custom_derive(&self, db: &dyn db::AstDatabase) -> bool {
179+
match self.0 {
180+
HirFileIdRepr::FileId(_) => false,
181+
HirFileIdRepr::MacroFile(macro_file) => {
182+
let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id);
183+
match loc.def.kind {
184+
MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _) => true,
185+
_ => false,
186+
}
187+
}
188+
}
189+
}
190+
178191
/// Return whether this file is an include macro
179192
pub fn is_include_macro(&self, db: &dyn db::AstDatabase) -> bool {
180193
match self.0 {

crates/mbe/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ mod syntax_bridge;
1212
mod tt_iter;
1313
mod subtree_source;
1414

15-
#[cfg(test)]
16-
mod tests;
17-
1815
#[cfg(test)]
1916
mod benchmark;
2017
mod token_map;

crates/mbe/src/tests.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)