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 2 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
18 changes: 17 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,14 @@ impl TomlTarget {
None => Err(human("bench target bench.name is required".to_string()))
}
}

fn validate_crate_type(&self) -> CargoResult<()> {
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 +1051,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,
_ => false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this explicitly bind the Some(false) here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well out of the 9 cases, 1 is handled by the first line and 5 are handled by the second line so there are 3 left:

(None, Some(false))
(Some(false), None)
(Some(false), Some(false))

Are you suggesting this?

(Some(false), _) | (_, Some(false)) => false

I think the current code is clearer. It says "if nothing is set, inherit; if anything is true, then true otherwise false." The explicitly bound Some(false) looks like it means "if anything is false, then false" but that is misleading because (Some(true), Some(false)) is true.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, the latter. Just writing out what the case is catching vs "something we accidentally forgot to cover above"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

});
}

fn lib_target(dst: &mut Vec<Target>,
Expand All @@ -1053,6 +1068,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
146 changes: 146 additions & 0 deletions tests/rustc-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
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 }"));
}