Skip to content

Commit 0f27161

Browse files
authored
Rollup merge of #110255 - clubby789:proc-macro-test-help, r=jackh726
Suggest using integration tests for test crate using own proc-macro cc #110247
2 parents f65615f + f7581d8 commit 0f27161

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

compiler/rustc_resolve/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,6 @@ resolve_remove_surrounding_derive =
223223
resolve_add_as_non_derive =
224224
add as non-Derive macro
225225
`#[{$macro_path}]`
226+
227+
resolve_proc_macro_same_crate = can't use a procedural macro from the same crate that defines it
228+
.help = you can define integration tests in a directory named `tests`

compiler/rustc_resolve/src/errors.rs

+9
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,12 @@ pub(crate) struct RemoveSurroundingDerive {
508508
pub(crate) struct AddAsNonDerive<'a> {
509509
pub(crate) macro_path: &'a str,
510510
}
511+
512+
#[derive(Diagnostic)]
513+
#[diag(resolve_proc_macro_same_crate)]
514+
pub(crate) struct ProcMacroSameCrate {
515+
#[primary_span]
516+
pub(crate) span: Span,
517+
#[help]
518+
pub(crate) is_test: bool,
519+
}

compiler/rustc_resolve/src/macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A bunch of methods and structures more or less related to resolving macros and
22
//! interface provided by `Resolver` to macro expander.
33
4-
use crate::errors::{AddAsNonDerive, MacroExpectedFound, RemoveSurroundingDerive};
4+
use crate::errors::{self, AddAsNonDerive, MacroExpectedFound, RemoveSurroundingDerive};
55
use crate::Namespace::*;
66
use crate::{BuiltinMacroState, Determinacy};
77
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
@@ -513,10 +513,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
513513
if let Some(def_id) = def_id.as_local() {
514514
self.unused_macros.remove(&def_id);
515515
if self.proc_macro_stubs.contains(&def_id) {
516-
self.tcx.sess.span_err(
517-
path.span,
518-
"can't use a procedural macro from the same crate that defines it",
519-
);
516+
self.tcx.sess.emit_err(errors::ProcMacroSameCrate {
517+
span: path.span,
518+
is_test: self.tcx.sess.is_test_crate(),
519+
});
520520
}
521521
}
522522
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-flags: --test
2+
#![crate_type = "proc-macro"]
3+
4+
extern crate proc_macro;
5+
use proc_macro::TokenStream;
6+
7+
#[proc_macro]
8+
pub fn mac(input: TokenStream) -> TokenStream { loop {} }
9+
10+
#[cfg(test)]
11+
mod test {
12+
#[test]
13+
fn t() { crate::mac!(A) }
14+
//~^ ERROR can't use a procedural macro from the same crate that defines it
15+
//~| HELP you can define integration tests in a directory named `tests`
16+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: can't use a procedural macro from the same crate that defines it
2+
--> $DIR/test-same-crate.rs:13:14
3+
|
4+
LL | fn t() { crate::mac!(A) }
5+
| ^^^^^^^^^^
6+
|
7+
= help: you can define integration tests in a directory named `tests`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)