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

Macros 1.1 #3064

Merged
merged 6 commits into from
Sep 6, 2016
Merged
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
6 changes: 5 additions & 1 deletion src/cargo/core/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub enum LibKind {
Lib,
Rlib,
Dylib,
RustcMacro,
Other(String),
}

Expand All @@ -70,6 +71,7 @@ impl LibKind {
"lib" => LibKind::Lib,
"rlib" => LibKind::Rlib,
"dylib" => LibKind::Dylib,
"rustc-macro" => LibKind::RustcMacro,
s => LibKind::Other(s.to_string()),
}
}
Expand All @@ -80,6 +82,7 @@ impl LibKind {
LibKind::Lib => "lib",
LibKind::Rlib => "rlib",
LibKind::Dylib => "dylib",
LibKind::RustcMacro => "rustc-macro",
LibKind::Other(ref s) => s,
}
}
Expand All @@ -88,7 +91,8 @@ impl LibKind {
match *self {
LibKind::Lib |
LibKind::Rlib |
LibKind::Dylib => true,
LibKind::Dylib |
LibKind::RustcMacro => true,
LibKind::Other(..) => false,
}
}
Expand Down
27 changes: 26 additions & 1 deletion src/cargo/util/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ impl TomlManifest {
let lib = match self.lib {
Some(ref lib) => {
try!(lib.validate_library_name());
try!(lib.validate_crate_type());
Some(
TomlTarget {
name: lib.name.clone().or(Some(project.name.clone())),
Expand Down Expand Up @@ -900,6 +901,7 @@ struct TomlTarget {
bench: Option<bool>,
doc: Option<bool>,
plugin: Option<bool>,
rustc_macro: Option<bool>,
harness: Option<bool>,
}

Expand Down Expand Up @@ -928,6 +930,7 @@ impl TomlTarget {
bench: None,
doc: None,
plugin: None,
rustc_macro: None,
harness: None,
}
}
Expand Down Expand Up @@ -1006,6 +1009,23 @@ impl TomlTarget {
None => Err(human("bench target bench.name is required".to_string()))
}
}

fn validate_crate_type(&self) -> CargoResult<()> {
// Per the Macros 1.1 RFC:
//
// > Initially if a crate is compiled with the rustc-macro crate type
// > (and possibly others) it will forbid exporting any items in the
// > crate other than those functions tagged #[rustc_macro_derive] and
// > those functions must also be placed at the crate root.
//
// A plugin requires exporting plugin_registrar so a crate cannot be
// both at once.
if self.plugin == Some(true) && self.rustc_macro == Some(true) {
Err(human("lib.plugin and lib.rustc-macro cannot both be true".to_string()))
} else {
Ok(())
}
}
}

impl PathValue {
Expand Down Expand Up @@ -1040,7 +1060,11 @@ fn normalize(lib: &Option<TomlLibTarget>,
.set_doctest(toml.doctest.unwrap_or(t2.doctested()))
.set_benched(toml.bench.unwrap_or(t2.benched()))
.set_harness(toml.harness.unwrap_or(t2.harness()))
.set_for_host(toml.plugin.unwrap_or(t2.for_host()));
.set_for_host(match (toml.plugin, toml.rustc_macro) {
(None, None) => t2.for_host(),
(Some(true), _) | (_, Some(true)) => true,
(Some(false), _) | (_, Some(false)) => false,
});
}

fn lib_target(dst: &mut Vec<Target>,
Expand All @@ -1053,6 +1077,7 @@ fn normalize(lib: &Option<TomlLibTarget>,
Some(kinds) => kinds.iter().map(|s| LibKind::from_str(s)).collect(),
None => {
vec![ if l.plugin == Some(true) {LibKind::Dylib}
else if l.rustc_macro == Some(true) {LibKind::RustcMacro}
else {LibKind::Lib} ]
}
};
Expand Down
11 changes: 8 additions & 3 deletions src/doc/manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ doc = true
# for Cargo to correctly compile it and make it available for all dependencies.
plugin = false

# If the target is meant to be a "macros 1.1" procedural macro, this field must
# be set to true.
rustc-macro = false

# If set to false, `cargo test` will omit the `--test` flag to rustc, which
# stops it from generating a test harness. This is useful when the binary being
# built manages the test runner itself.
Expand All @@ -527,9 +531,10 @@ name = "..."
crate-type = ["dylib"] # could be `staticlib` as well
```

The available options are `dylib`, `rlib`, `staticlib`, and `cdylib`. You
should only use this option in a project. Cargo will always compile packages
(dependencies) based on the requirements of the project that includes them.
The available options are `dylib`, `rlib`, `staticlib`, `cdylib`, and
`rustc-macro`. You should only use this option in a project. Cargo will always
compile packages (dependencies) based on the requirements of the project that
includes them.

You can read more about the different crate types in the
[Rust Reference Manual](https://doc.rust-lang.org/reference.html#linkage)
Expand Down
2 changes: 1 addition & 1 deletion src/rustversion.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2016-07-12
2016-09-05
187 changes: 187 additions & 0 deletions tests/rustc-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
extern crate cargotest;
extern crate hamcrest;

use cargotest::is_nightly;
use cargotest::support::{project, execs};
use hamcrest::assert_that;

#[test]
fn noop() {
if !is_nightly() {
return;
}

let client = project("client")
.file("Cargo.toml", r#"
[package]
name = "client"
version = "0.0.1"
authors = []

[dependencies.noop]
path = "../noop"
"#)
.file("src/main.rs", r#"
#![feature(rustc_macro)]

#[macro_use]
extern crate noop;

#[derive(Noop)]
struct X;

fn main() {}
"#);
let noop = project("noop")
.file("Cargo.toml", r#"
[package]
name = "noop"
version = "0.0.1"
authors = []

[lib]
rustc-macro = true
"#)
.file("src/lib.rs", r#"
#![feature(rustc_macro, rustc_macro_lib)]

extern crate rustc_macro;
use rustc_macro::TokenStream;

#[rustc_macro_derive(Noop)]
pub fn noop(input: TokenStream) -> TokenStream {
input
}
"#);
noop.build();

assert_that(client.cargo_process("build"),
execs().with_status(0));
assert_that(client.cargo("build"),
execs().with_status(0));
}

#[test]
fn impl_and_derive() {
if !is_nightly() {
return;
}

let client = project("client")
.file("Cargo.toml", r#"
[package]
name = "client"
version = "0.0.1"
authors = []

[dependencies.transmogrify]
path = "../transmogrify"
"#)
.file("src/main.rs", r#"
#![feature(rustc_macro)]

#[macro_use]
extern crate transmogrify;

trait ImplByTransmogrify {
fn impl_by_transmogrify(&self) -> bool;
}

#[derive(Transmogrify)]
struct X;

fn main() {
let x = X::new();
assert!(x.impl_by_transmogrify());
println!("{:?}", x);
}
"#);
let transmogrify = project("transmogrify")
.file("Cargo.toml", r#"
[package]
name = "transmogrify"
version = "0.0.1"
authors = []

[lib]
rustc-macro = true
"#)
.file("src/lib.rs", r#"
#![feature(rustc_macro, rustc_macro_lib)]

extern crate rustc_macro;
use rustc_macro::TokenStream;

#[rustc_macro_derive(Transmogrify)]
#[doc(hidden)]
pub fn transmogrify(input: TokenStream) -> TokenStream {
assert_eq!(input.to_string(), "struct X;\n");

"
impl X {
fn new() -> Self {
X { success: true }
}
}

impl ImplByTransmogrify for X {
fn impl_by_transmogrify(&self) -> bool {
true
}
}

#[derive(Debug)]
struct X {
success: bool,
}
".parse().unwrap()
}
"#);
transmogrify.build();

assert_that(client.cargo_process("build"),
execs().with_status(0));
assert_that(client.cargo("run"),
execs().with_status(0).with_stdout("X { success: true }"));
}

#[test]
fn plugin_and_rustc_macro() {
if !is_nightly() {
return;
}

let questionable = project("questionable")
.file("Cargo.toml", r#"
[package]
name = "questionable"
version = "0.0.1"
authors = []

[lib]
plugin = true
rustc-macro = true
"#)
.file("src/lib.rs", r#"
#![feature(plugin_registrar, rustc_private)]
#![feature(rustc_macro, rustc_macro_lib)]

extern crate rustc_plugin;
use rustc_plugin::Registry;

extern crate rustc_macro;
use rustc_macro::TokenStream;

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {}

#[rustc_macro_derive(Questionable)]
pub fn questionable(input: TokenStream) -> TokenStream {
input
}
"#);

let msg = " lib.plugin and lib.rustc-macro cannot both be true";
assert_that(questionable.cargo_process("build"),
execs().with_status(101).with_stderr_contains(msg));
}