Skip to content

Commit

Permalink
Suggest using integration tests for proc-macros
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Apr 17, 2023
1 parent 0d7ed3b commit f7581d8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,6 @@ resolve_remove_surrounding_derive =
resolve_add_as_non_derive =
add as non-Derive macro
`#[{$macro_path}]`
resolve_proc_macro_same_crate = can't use a procedural macro from the same crate that defines it
.help = you can define integration tests in a directory named `tests`
9 changes: 9 additions & 0 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,12 @@ pub(crate) struct RemoveSurroundingDerive {
pub(crate) struct AddAsNonDerive<'a> {
pub(crate) macro_path: &'a str,
}

#[derive(Diagnostic)]
#[diag(resolve_proc_macro_same_crate)]
pub(crate) struct ProcMacroSameCrate {
#[primary_span]
pub(crate) span: Span,
#[help]
pub(crate) is_test: bool,
}
10 changes: 5 additions & 5 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A bunch of methods and structures more or less related to resolving macros and
//! interface provided by `Resolver` to macro expander.
use crate::errors::{AddAsNonDerive, MacroExpectedFound, RemoveSurroundingDerive};
use crate::errors::{self, AddAsNonDerive, MacroExpectedFound, RemoveSurroundingDerive};
use crate::Namespace::*;
use crate::{BuiltinMacroState, Determinacy};
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
Expand Down Expand Up @@ -513,10 +513,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
if let Some(def_id) = def_id.as_local() {
self.unused_macros.remove(&def_id);
if self.proc_macro_stubs.contains(&def_id) {
self.tcx.sess.span_err(
path.span,
"can't use a procedural macro from the same crate that defines it",
);
self.tcx.sess.emit_err(errors::ProcMacroSameCrate {
span: path.span,
is_test: self.tcx.sess.is_test_crate(),
});
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/proc-macro/test-same-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// compile-flags: --test
#![crate_type = "proc-macro"]

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro]
pub fn mac(input: TokenStream) -> TokenStream { loop {} }

#[cfg(test)]
mod test {
#[test]
fn t() { crate::mac!(A) }
//~^ ERROR can't use a procedural macro from the same crate that defines it
//~| HELP you can define integration tests in a directory named `tests`
}
10 changes: 10 additions & 0 deletions tests/ui/proc-macro/test-same-crate.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: can't use a procedural macro from the same crate that defines it
--> $DIR/test-same-crate.rs:13:14
|
LL | fn t() { crate::mac!(A) }
| ^^^^^^^^^^
|
= help: you can define integration tests in a directory named `tests`

error: aborting due to previous error

0 comments on commit f7581d8

Please sign in to comment.