Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extract rustc_test_marker attr in test_case #113315

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions compiler/rustc_builtin_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/custom_test_frameworks/auxiliary/issue-100263-macro.rs
Original file line number Diff line number Diff line change
@@ -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()
}
12 changes: 12 additions & 0 deletions tests/ui/custom_test_frameworks/issue-100263.rs
Original file line number Diff line number Diff line change
@@ -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() {}
16 changes: 16 additions & 0 deletions tests/ui/custom_test_frameworks/issue-100263.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading