2018-12-06 19:17:36 +00:00
|
|
|
use crate::support::is_nightly;
|
|
|
|
use crate::support::project;
|
2016-09-01 17:29:24 +00:00
|
|
|
|
2017-08-21 11:46:31 +00:00
|
|
|
#[test]
|
|
|
|
fn probe_cfg_before_crate_type_discovery() {
|
2018-07-20 17:41:44 +00:00
|
|
|
let p = project()
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2017-08-21 11:46:31 +00:00
|
|
|
[package]
|
2018-07-20 17:41:44 +00:00
|
|
|
name = "foo"
|
2017-08-21 11:46:31 +00:00
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[target.'cfg(not(stage300))'.dependencies.noop]
|
|
|
|
path = "../noop"
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).file(
|
2018-03-14 15:17:44 +00:00
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
2017-08-21 11:46:31 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate noop;
|
|
|
|
|
|
|
|
#[derive(Noop)]
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
fn main() {}
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).build();
|
|
|
|
let _noop = project()
|
|
|
|
.at("noop")
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2017-08-21 11:46:31 +00:00
|
|
|
[package]
|
|
|
|
name = "noop"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[lib]
|
|
|
|
proc-macro = true
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).file(
|
2018-03-14 15:17:44 +00:00
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2017-08-21 11:46:31 +00:00
|
|
|
extern crate proc_macro;
|
|
|
|
use proc_macro::TokenStream;
|
|
|
|
|
|
|
|
#[proc_macro_derive(Noop)]
|
|
|
|
pub fn noop(_input: TokenStream) -> TokenStream {
|
|
|
|
"".parse().unwrap()
|
|
|
|
}
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).build();
|
2017-08-21 11:46:31 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build").run();
|
2017-08-21 11:46:31 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 17:29:24 +00:00
|
|
|
#[test]
|
|
|
|
fn noop() {
|
2018-07-20 17:41:44 +00:00
|
|
|
let p = project()
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2016-09-01 17:29:24 +00:00
|
|
|
[package]
|
2018-07-20 17:41:44 +00:00
|
|
|
name = "foo"
|
2016-09-01 17:29:24 +00:00
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[dependencies.noop]
|
|
|
|
path = "../noop"
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).file(
|
2018-03-14 15:17:44 +00:00
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
2016-09-01 17:29:24 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate noop;
|
|
|
|
|
|
|
|
#[derive(Noop)]
|
|
|
|
struct X;
|
|
|
|
|
|
|
|
fn main() {}
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).build();
|
|
|
|
let _noop = project()
|
|
|
|
.at("noop")
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2016-09-01 17:29:24 +00:00
|
|
|
[package]
|
|
|
|
name = "noop"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[lib]
|
2016-10-04 16:58:28 +00:00
|
|
|
proc-macro = true
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).file(
|
2018-03-14 15:17:44 +00:00
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2016-10-04 16:58:28 +00:00
|
|
|
extern crate proc_macro;
|
|
|
|
use proc_macro::TokenStream;
|
2016-09-01 17:29:24 +00:00
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
#[proc_macro_derive(Noop)]
|
2016-11-17 20:21:13 +00:00
|
|
|
pub fn noop(_input: TokenStream) -> TokenStream {
|
|
|
|
"".parse().unwrap()
|
2016-09-01 17:29:24 +00:00
|
|
|
}
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).build();
|
2016-09-01 17:29:24 +00:00
|
|
|
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build").run();
|
|
|
|
p.cargo("build").run();
|
2016-09-01 17:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn impl_and_derive() {
|
2018-07-20 17:41:44 +00:00
|
|
|
let p = project()
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2016-09-01 17:29:24 +00:00
|
|
|
[package]
|
2018-07-20 17:41:44 +00:00
|
|
|
name = "foo"
|
2016-09-01 17:29:24 +00:00
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[dependencies.transmogrify]
|
|
|
|
path = "../transmogrify"
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).file(
|
2018-03-14 15:17:44 +00:00
|
|
|
"src/main.rs",
|
|
|
|
r#"
|
2016-09-01 17:29:24 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate transmogrify;
|
|
|
|
|
|
|
|
trait ImplByTransmogrify {
|
|
|
|
fn impl_by_transmogrify(&self) -> bool;
|
|
|
|
}
|
|
|
|
|
2016-11-17 20:21:13 +00:00
|
|
|
#[derive(Transmogrify, Debug)]
|
|
|
|
struct X { success: bool }
|
2016-09-01 17:29:24 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x = X::new();
|
|
|
|
assert!(x.impl_by_transmogrify());
|
|
|
|
println!("{:?}", x);
|
|
|
|
}
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).build();
|
|
|
|
let _transmogrify = project()
|
|
|
|
.at("transmogrify")
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2016-09-01 17:29:24 +00:00
|
|
|
[package]
|
|
|
|
name = "transmogrify"
|
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[lib]
|
2016-10-04 16:58:28 +00:00
|
|
|
proc-macro = true
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).file(
|
2018-03-14 15:17:44 +00:00
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2016-10-04 16:58:28 +00:00
|
|
|
extern crate proc_macro;
|
|
|
|
use proc_macro::TokenStream;
|
2016-09-01 17:29:24 +00:00
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
#[proc_macro_derive(Transmogrify)]
|
2016-09-01 17:29:24 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn transmogrify(input: TokenStream) -> TokenStream {
|
|
|
|
"
|
|
|
|
impl X {
|
|
|
|
fn new() -> Self {
|
|
|
|
X { success: true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ImplByTransmogrify for X {
|
|
|
|
fn impl_by_transmogrify(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
".parse().unwrap()
|
|
|
|
}
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).build();
|
|
|
|
|
|
|
|
p.cargo("build").run();
|
|
|
|
p.cargo("run").with_stdout("X { success: true }").run();
|
2016-09-01 17:29:24 +00:00
|
|
|
}
|
2016-09-01 17:42:23 +00:00
|
|
|
|
|
|
|
#[test]
|
2016-10-04 16:58:28 +00:00
|
|
|
fn plugin_and_proc_macro() {
|
2016-09-01 17:42:23 +00:00
|
|
|
if !is_nightly() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-20 17:41:44 +00:00
|
|
|
let p = project()
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2016-09-01 17:42:23 +00:00
|
|
|
[package]
|
2018-07-20 17:41:44 +00:00
|
|
|
name = "foo"
|
2016-09-01 17:42:23 +00:00
|
|
|
version = "0.0.1"
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
[lib]
|
|
|
|
plugin = true
|
2016-10-04 16:58:28 +00:00
|
|
|
proc-macro = true
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).file(
|
2018-03-14 15:17:44 +00:00
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2016-09-01 17:42:23 +00:00
|
|
|
#![feature(plugin_registrar, rustc_private)]
|
2016-10-04 16:58:28 +00:00
|
|
|
#![feature(proc_macro, proc_macro_lib)]
|
2016-09-01 17:42:23 +00:00
|
|
|
|
|
|
|
extern crate rustc_plugin;
|
|
|
|
use rustc_plugin::Registry;
|
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
extern crate proc_macro;
|
|
|
|
use proc_macro::TokenStream;
|
2016-09-01 17:42:23 +00:00
|
|
|
|
|
|
|
#[plugin_registrar]
|
|
|
|
pub fn plugin_registrar(reg: &mut Registry) {}
|
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
#[proc_macro_derive(Questionable)]
|
2016-09-01 17:42:23 +00:00
|
|
|
pub fn questionable(input: TokenStream) -> TokenStream {
|
|
|
|
input
|
|
|
|
}
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).build();
|
2016-09-01 17:42:23 +00:00
|
|
|
|
2016-10-04 16:58:28 +00:00
|
|
|
let msg = " lib.plugin and lib.proc-macro cannot both be true";
|
2018-08-28 09:20:03 +00:00
|
|
|
p.cargo("build")
|
|
|
|
.with_status(101)
|
|
|
|
.with_stderr_contains(msg)
|
|
|
|
.run();
|
2016-09-01 17:42:23 +00:00
|
|
|
}
|
2017-01-17 23:48:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proc_macro_doctest() {
|
2018-07-20 11:47:47 +00:00
|
|
|
let foo = project()
|
2018-03-14 15:17:44 +00:00
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
2017-01-17 23:48:35 +00:00
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.1.0"
|
|
|
|
authors = []
|
|
|
|
[lib]
|
|
|
|
proc-macro = true
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).file(
|
2018-03-14 15:17:44 +00:00
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
2017-01-17 23:48:35 +00:00
|
|
|
#![crate_type = "proc-macro"]
|
|
|
|
|
|
|
|
extern crate proc_macro;
|
|
|
|
|
|
|
|
use proc_macro::TokenStream;
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
/// assert!(true);
|
|
|
|
/// ```
|
|
|
|
#[proc_macro_derive(Bar)]
|
|
|
|
pub fn derive(_input: TokenStream) -> TokenStream {
|
|
|
|
"".parse().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn a() {
|
|
|
|
assert!(true);
|
|
|
|
}
|
2018-03-14 15:17:44 +00:00
|
|
|
"#,
|
2018-08-28 09:20:03 +00:00
|
|
|
).build();
|
|
|
|
|
|
|
|
foo.cargo("test")
|
|
|
|
.with_stdout_contains("test a ... ok")
|
|
|
|
.with_stdout_contains_n("test [..] ... ok", 2)
|
|
|
|
.run();
|
2017-01-17 23:48:35 +00:00
|
|
|
}
|
2018-11-03 19:36:41 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proc_macro_crate_type() {
|
|
|
|
// Verify that `crate-type = ["proc-macro"]` is the same as `proc-macro = true`
|
|
|
|
// and that everything, including rustdoc, works correctly.
|
|
|
|
let foo = project()
|
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.1.0"
|
|
|
|
[dependencies]
|
|
|
|
pm = { path = "pm" }
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"src/lib.rs",
|
|
|
|
r#"
|
|
|
|
//! ```
|
|
|
|
//! use foo::THING;
|
|
|
|
//! assert_eq!(THING, 123);
|
|
|
|
//! ```
|
|
|
|
#[macro_use]
|
|
|
|
extern crate pm;
|
|
|
|
#[derive(MkItem)]
|
|
|
|
pub struct S;
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::THING;
|
|
|
|
#[test]
|
|
|
|
fn it_works() {
|
|
|
|
assert_eq!(THING, 123);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"pm/Cargo.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "pm"
|
|
|
|
version = "0.1.0"
|
|
|
|
[lib]
|
|
|
|
crate-type = ["proc-macro"]
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file(
|
|
|
|
"pm/src/lib.rs",
|
|
|
|
r#"
|
|
|
|
extern crate proc_macro;
|
|
|
|
use proc_macro::TokenStream;
|
|
|
|
|
|
|
|
#[proc_macro_derive(MkItem)]
|
|
|
|
pub fn mk_item(_input: TokenStream) -> TokenStream {
|
|
|
|
"pub const THING: i32 = 123;".parse().unwrap()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
foo.cargo("test")
|
|
|
|
.with_stdout_contains("test tests::it_works ... ok")
|
|
|
|
.with_stdout_contains_n("test [..] ... ok", 2)
|
|
|
|
.run();
|
|
|
|
}
|
2018-11-06 18:32:56 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proc_macro_crate_type_warning() {
|
|
|
|
let foo = project()
|
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.1.0"
|
|
|
|
[lib]
|
|
|
|
crate-type = ["proc-macro"]
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file("src/lib.rs", "")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
foo.cargo("build")
|
|
|
|
.with_stderr_contains(
|
|
|
|
"[WARNING] library `foo` should only specify `proc-macro = true` instead of setting `crate-type`")
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proc_macro_crate_type_warning_plugin() {
|
|
|
|
let foo = project()
|
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.1.0"
|
|
|
|
[lib]
|
|
|
|
crate-type = ["proc-macro"]
|
|
|
|
plugin = true
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file("src/lib.rs", "")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
foo.cargo("build")
|
|
|
|
.with_stderr_contains(
|
|
|
|
"[WARNING] proc-macro library `foo` should not specify `plugin = true`")
|
|
|
|
.with_stderr_contains(
|
|
|
|
"[WARNING] library `foo` should only specify `proc-macro = true` instead of setting `crate-type`")
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn proc_macro_crate_type_multiple() {
|
|
|
|
let foo = project()
|
|
|
|
.file(
|
|
|
|
"Cargo.toml",
|
|
|
|
r#"
|
|
|
|
[package]
|
|
|
|
name = "foo"
|
|
|
|
version = "0.1.0"
|
|
|
|
[lib]
|
|
|
|
crate-type = ["proc-macro", "rlib"]
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.file("src/lib.rs", "")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
foo.cargo("build")
|
|
|
|
.with_stderr(
|
|
|
|
"\
|
|
|
|
[ERROR] failed to parse manifest at `[..]/foo/Cargo.toml`
|
|
|
|
|
|
|
|
Caused by:
|
|
|
|
cannot mix `proc-macro` crate type with others
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.with_status(101)
|
|
|
|
.run();
|
|
|
|
}
|