Skip to content

Commit

Permalink
Auto merge of #17967 - Veykril:mbe-tests, r=Veykril
Browse files Browse the repository at this point in the history
internal: Lay basic ground work for standalone mbe tests

Most of our mbe hir-def tests don't actually do anything name res relevant, we can (and should) move those down the stack into `mbe/hir-expand`.
  • Loading branch information
bors committed Sep 1, 2024
2 parents bae4951 + 4502a60 commit 779d9ee
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 76 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 20 additions & 18 deletions crates/hir-def/src/macro_expansion_tests/mbe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ m! { foo# bar }
m! { Foo,# Bar }
"#,
expect![[r##"
expect![[r#"
macro_rules! m {
($($i:ident),*) => ($(mod $i {} )*);
($($i:ident)#*) => ($(fn $i() {} )*);
Expand All @@ -404,27 +404,29 @@ fn bar() {}
struct Foo;
struct Bar;
"##]],
"#]],
);
}

#[test]
fn test_match_group_pattern_with_multiple_defs() {
// FIXME: The pretty printer breaks by leaving whitespace here, +syntaxctxt is used to avoid that
check(
r#"
macro_rules! m {
($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
}
// +syntaxctxt
m! { foo, bar }
"#,
expect![[r#"
macro_rules! m {
($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
}
impl Bar {
fn foo() {}
fn bar() {}
}
impl#\1# Bar#\1# {#\1#
fn#\1# foo#\0#(#\1#)#\1# {#\1#}#\1#
fn#\1# bar#\0#(#\1#)#\1# {#\1#}#\1#
}#\1#
"#]],
);
}
Expand Down Expand Up @@ -480,12 +482,12 @@ macro_rules! m {
}
m!{#abc}
"#,
expect![[r##"
expect![[r#"
macro_rules! m {
($($i:ident)* #abc) => ( fn baz() { $($i ();)* } );
}
fn baz() {}
"##]],
"#]],
)
}

Expand Down Expand Up @@ -1189,13 +1191,13 @@ macro_rules! m {
m! { cfg(target_os = "windows") }
m! { hello::world }
"#,
expect![[r##"
expect![[r#"
macro_rules! m {
($m:meta) => ( #[$m] fn bar() {} )
}
#[cfg(target_os = "windows")] fn bar() {}
#[hello::world] fn bar() {}
"##]],
"#]],
);
}

Expand All @@ -1213,15 +1215,15 @@ m! {
*/
}
"#,
expect![[r##"
expect![[r#"
macro_rules! m {
($(#[$m:meta])+) => ( $(#[$m])+ fn bar() {} )
}
#[doc = r" Single Line Doc 1"]
#[doc = r"
MultiLines Doc
"] fn bar() {}
"##]],
"#]],
);
}

Expand All @@ -1234,12 +1236,12 @@ macro_rules! m {
}
m! { #[doc = concat!("The `", "bla", "` lang item.")] }
"#,
expect![[r##"
expect![[r#"
macro_rules! m {
(#[$m:meta]) => ( #[$m] fn bar() {} )
}
#[doc = concat!("The `", "bla", "` lang item.")] fn bar() {}
"##]],
"#]],
);
}

Expand All @@ -1257,15 +1259,15 @@ m! {
*/
}
"#,
expect![[r##"
expect![[r#"
macro_rules! m {
($(#[$ m:meta])+) => ( $(#[$m])+ fn bar() {} )
}
#[doc = r" 錦瑟無端五十弦,一弦一柱思華年。"]
#[doc = r"
莊生曉夢迷蝴蝶,望帝春心託杜鵑。
"] fn bar() {}
"##]],
"#]],
);
}

Expand Down Expand Up @@ -1342,10 +1344,10 @@ fn test_tt_composite2() {
macro_rules! m { ($($tt:tt)*) => { abs!(=> $($tt)*); } }
m! {#}
"#,
expect![[r##"
expect![[r#"
macro_rules! m { ($($tt:tt)*) => { abs!(=> $($tt)*); } }
abs!( = > #);
"##]],
"#]],
);
}

Expand Down
32 changes: 16 additions & 16 deletions crates/hir-def/src/macro_expansion_tests/mbe/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ STRUCT!{struct D3DVSHADERCAPS2_0 {Caps: u8,}}
STRUCT!{#[cfg_attr(target_arch = "x86", repr(packed))] struct D3DCONTENTPROTECTIONCAPS {Caps : u8 ,}}
"#,
expect![[r##"
expect![[r#"
macro_rules! STRUCT {
($(#[$attrs:meta])* struct $name:ident {
$($field:ident: $ftype:ty,)+
Expand Down Expand Up @@ -194,7 +194,7 @@ impl Clone for D3DCONTENTPROTECTIONCAPS {
}
}
}
"##]],
"#]],
);
}

Expand All @@ -214,7 +214,7 @@ macro_rules! int_base {
}
int_base!{Binary for isize as usize -> Binary}
"#,
expect![[r##"
expect![[r#"
macro_rules! int_base {
($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => {
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -230,7 +230,7 @@ macro_rules! int_base {
Binary.fmt_int(*self as usize, f)
}
}
"##]],
"#]],
);
}

Expand Down Expand Up @@ -318,7 +318,7 @@ impl_fn_for_zst ! {
}
"#,
expect![[r##"
expect![[r#"
macro_rules! impl_fn_for_zst {
{$( $( #[$attr: meta] )*
struct $Name: ident impl$( <$( $lifetime : lifetime ),+> )? Fn =
Expand Down Expand Up @@ -410,7 +410,7 @@ impl FnOnce<(char, )> for CharEscapeDefault {
}
}
"##]],
"#]],
);
}

Expand Down Expand Up @@ -511,7 +511,7 @@ cfg_if! {
@__apply cfg(all(not(any(not(any(target_os = "solaris", target_os = "illumos")))))),
}
"#,
expect![[r##"
expect![[r#"
macro_rules! cfg_if {
($(if #[cfg($($meta:meta),*)] { $($it:item)* } )else* else { $($it2:item)* })
=> {
Expand All @@ -534,7 +534,7 @@ __cfg_if_items! {
}
"##]],
"#]],
);
}

Expand Down Expand Up @@ -618,7 +618,7 @@ RIDL!{interface ID3D11Asynchronous(ID3D11AsynchronousVtbl): ID3D11DeviceChild(ID
fn GetDataSize(&mut self) -> UINT
}}
"#,
expect![[r##"
expect![[r#"
#[macro_export]
macro_rules! RIDL {
(interface $interface:ident ($vtbl:ident) : $pinterface:ident ($pvtbl:ident)
Expand All @@ -639,7 +639,7 @@ impl ID3D11Asynchronous {
((*self .lpVtbl).GetDataSize)(self )
}
}
"##]],
"#]],
);
}

Expand Down Expand Up @@ -676,7 +676,7 @@ quick_error ! (
);
"#,
expect![[r##"
expect![[r#"
macro_rules! quick_error {
(SORT [enum $name:ident $( #[$meta:meta] )*]
items [$($( #[$imeta:meta] )*
Expand All @@ -697,7 +697,7 @@ macro_rules! quick_error {
}
quick_error!(ENUMINITION[enum Wrapped#[derive(Debug)]]body[]queue[ = > One: UNIT[] = > Two: TUPLE[s: String]]);
"##]],
"#]],
)
}

Expand Down Expand Up @@ -746,7 +746,7 @@ delegate_impl ! {
[G, &'a mut G, deref] pub trait Data: GraphBase {@section type type NodeWeight;}
}
"#,
expect![[r##"
expect![[r#"
macro_rules! delegate_impl {
([$self_type:ident, $self_wrap:ty, $self_map:ident]
pub trait $name:ident $(: $sup:ident)* $(+ $more_sup:ident)* {
Expand Down Expand Up @@ -785,7 +785,7 @@ macro_rules! delegate_impl {
}
}
impl <> Data for &'amut G where G: Data {}
"##]],
"#]],
);
}

Expand Down Expand Up @@ -959,14 +959,14 @@ macro_rules! with_std {
with_std! {mod m;mod f;}
"#,
expect![[r##"
expect![[r#"
macro_rules! with_std {
($($i:item)*) => ($(#[cfg(feature = "std")]$i)*)
}
#[cfg(feature = "std")] mod m;
#[cfg(feature = "std")] mod f;
"##]],
"#]],
)
}

Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/macro_expansion_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains tests for macro expansion. Effectively, it covers `tt`,
//! `mbe`, `proc_macro_api` and `hir_expand` crates. This might seem like a
//! wrong architecture at the first glance, but is intentional.
//! This module contains integration tests for macro expansion with name resolution. Effectively, it
//! covers `tt`, `mbe`, `proc_macro_api` and `hir_expand` crates. This might seem like a wrong
//! architecture at the first glance, but is intentional.
//!
//! Physically, macro expansion process is intertwined with name resolution. You
//! can not expand *just* the syntax. So, to be able to write integration tests
Expand Down
12 changes: 6 additions & 6 deletions crates/hir-def/src/macro_expansion_tests/proc_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ fn attribute_macro_attr_censoring() {
#[attr1] #[proc_macros::identity] #[attr2]
struct S;
"#,
expect![[r##"
expect![[r#"
#[attr1] #[proc_macros::identity] #[attr2]
struct S;
#[attr1]
#[attr2] struct S;"##]],
#[attr2] struct S;"#]],
);
}

Expand All @@ -39,7 +39,7 @@ fn derive_censoring() {
#[attr2]
struct S;
"#,
expect![[r##"
expect![[r#"
#[attr1]
#[derive(Foo)]
#[derive(proc_macros::DeriveIdentity)]
Expand All @@ -49,7 +49,7 @@ struct S;
#[attr1]
#[derive(Bar)]
#[attr2] struct S;"##]],
#[attr2] struct S;"#]],
);
}

Expand All @@ -62,14 +62,14 @@ fn attribute_macro_syntax_completion_1() {
#[proc_macros::identity_when_valid]
fn foo() { bar.baz(); blub }
"#,
expect![[r##"
expect![[r#"
#[proc_macros::identity_when_valid]
fn foo() { bar.baz(); blub }
fn foo() {
bar.baz();
blub
}"##]],
}"#]],
);
}

Expand Down
1 change: 1 addition & 0 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub use crate::files::{AstId, ErasedAstId, FileRange, InFile, InMacroFile, InRea

pub use mbe::{DeclarativeMacro, ValueResult};
pub use span::{HirFileId, MacroCallId, MacroFileId};
pub use syntax_bridge::insert_whitespace_into_node;

pub mod tt {
pub use span::Span;
Expand Down
1 change: 1 addition & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub use {
},
hygiene::{marks_rev, SyntaxContextExt},
inert_attr_macro::AttributeTemplate,
insert_whitespace_into_node,
name::Name,
proc_macro::{ProcMacros, ProcMacrosBuilder},
tt, ExpandResult, HirFileId, HirFileIdExt, MacroFileId, MacroFileIdExt,
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod generated {
pub mod syntax_helpers {
pub mod format_string;
pub mod format_string_exprs;
pub mod insert_whitespace_into_node;
pub use hir::insert_whitespace_into_node;
pub mod node_ext;

pub use parser::LexedStr;
Expand Down
Loading

0 comments on commit 779d9ee

Please sign in to comment.