-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Macros 1.1 #3064
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a50cc0
Macros 1.1
dtolnay 5cfc4fb
Test for rustc-macro
dtolnay 5ee95b1
Test a crate that is both a plugin and a proc macro
dtolnay 5daa4f6
Explicitly bind Some(false)
dtolnay acf34bb
Empty commit to trigger Travis build of rustc-macro
dtolnay 7ee6f57
Bump rustcversion to pick up rustc-macro support
dtolnay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }")); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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:
Are you suggesting this?
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.There was a problem hiding this comment.
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"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.