diff --git a/compiler/rustc_builtin_macros/src/test.rs b/compiler/rustc_builtin_macros/src/test.rs index 6bc4f6fc1fcfc..d007c1ba18351 100644 --- a/compiler/rustc_builtin_macros/src/test.rs +++ b/compiler/rustc_builtin_macros/src/test.rs @@ -32,7 +32,6 @@ pub fn expand_test_case( return vec![]; } - let sp = ecx.with_def_site_ctxt(attr_sp); let (mut item, is_stmt) = match anno_item { Annotatable::Item(item) => (item, false), Annotatable::Stmt(stmt) if let ast::StmtKind::Item(_) = stmt.kind => if let ast::StmtKind::Item(i) = stmt.into_inner().kind { @@ -45,29 +44,41 @@ pub fn expand_test_case( return vec![]; } }; + let attr_sp = ecx.with_def_site_ctxt(attr_sp); + let sp = ecx.with_def_site_ctxt(item.span); + + let test_path_symbol = Symbol::intern(&item_path( + // skip the name of the root module + &ecx.current_expansion.module.mod_path[1..], + &item.ident, + )); + let test_item = ecx.item( + sp, + item.ident, + thin_vec![ecx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp)], + item.kind.clone(), + ); + item = item.map(|mut item| { - let test_path_symbol = Symbol::intern(&item_path( - // skip the name of the root module - &ecx.current_expansion.module.mod_path[1..], - &item.ident, - )); item.vis = ast::Visibility { span: item.vis.span, kind: ast::VisibilityKind::Public, tokens: None, }; - item.ident.span = item.ident.span.with_ctxt(sp.ctxt()); - item.attrs.push(ecx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, sp)); + item.ident.span = item.ident.span.with_ctxt(attr_sp.ctxt()); item }); let ret = if is_stmt { - Annotatable::Stmt(P(ecx.stmt_item(item.span, item))) + vec![ + Annotatable::Stmt(P(ecx.stmt_item(test_item.span, test_item))), + Annotatable::Stmt(P(ecx.stmt_item(item.span, item))), + ] } else { - Annotatable::Item(item) + vec![Annotatable::Item(test_item), Annotatable::Item(item)] }; - vec![ret] + ret } pub fn expand_test( diff --git a/tests/ui/custom_test_frameworks/auxiliary/issue-100263-macro.rs b/tests/ui/custom_test_frameworks/auxiliary/issue-100263-macro.rs new file mode 100644 index 0000000000000..2d4abbe8f1124 --- /dev/null +++ b/tests/ui/custom_test_frameworks/auxiliary/issue-100263-macro.rs @@ -0,0 +1,12 @@ +// force-host +// no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; +use proc_macro::TokenStream; + +#[proc_macro_attribute] +pub fn test(_args: TokenStream, _item: TokenStream) -> TokenStream { + TokenStream::default() +} diff --git a/tests/ui/custom_test_frameworks/issue-100263.rs b/tests/ui/custom_test_frameworks/issue-100263.rs new file mode 100644 index 0000000000000..63a7e53907d3c --- /dev/null +++ b/tests/ui/custom_test_frameworks/issue-100263.rs @@ -0,0 +1,12 @@ +// compile-flags: --test +// aux-build: issue-100263-macro.rs +#![feature(custom_test_frameworks)] + +extern crate issue_100263_macro; + +#[test_case] +#[issue_100263_macro::test] +fn foo() {} +//~^ ERROR mismatched types + +fn main() {} diff --git a/tests/ui/custom_test_frameworks/issue-100263.stderr b/tests/ui/custom_test_frameworks/issue-100263.stderr new file mode 100644 index 0000000000000..67d9d9716a308 --- /dev/null +++ b/tests/ui/custom_test_frameworks/issue-100263.stderr @@ -0,0 +1,16 @@ +error[E0308]: mismatched types + --> $DIR/issue-100263.rs:9:1 + | +LL | #[test_case] + | ------------ in this procedural macro expansion +LL | #[issue_100263_macro::test] +LL | fn foo() {} + | ^^^^^^^^^^^ expected `&TestDescAndFn`, found `&fn() {foo}` + | + = note: expected reference `&TestDescAndFn` + found reference `&fn() {foo}` + = note: this error originates in the attribute macro `test_case` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`.