Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Represent lifetimes as two joint tokens in proc macros
- Loading branch information
1 parent
5b820a6
commit c106125
Showing
12 changed files
with
158 additions
and
21 deletions.
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
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
36 changes: 36 additions & 0 deletions
36
src/test/run-pass-fulldeps/proc-macro/auxiliary/lifetimes.rs
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,36 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// no-prefer-dynamic | ||
|
||
#![feature(proc_macro)] | ||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::*; | ||
|
||
#[proc_macro] | ||
pub fn lifetimes_bang(input: TokenStream) -> TokenStream { | ||
// Roundtrip through token trees | ||
input.into_iter().collect() | ||
} | ||
|
||
#[proc_macro_attribute] | ||
pub fn lifetimes_attr(_: TokenStream, input: TokenStream) -> TokenStream { | ||
// Roundtrip through AST | ||
input | ||
} | ||
|
||
#[proc_macro_derive(Lifetimes)] | ||
pub fn lifetimes_derive(input: TokenStream) -> TokenStream { | ||
// Roundtrip through a string | ||
format!("mod m {{ {} }}", input).parse().unwrap() | ||
} |
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,36 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// aux-build:lifetimes.rs | ||
// ignore-stage1 | ||
|
||
#![feature(proc_macro)] | ||
|
||
extern crate lifetimes; | ||
use lifetimes::*; | ||
|
||
lifetimes_bang! { | ||
fn bang<'a>() -> &'a u8 { &0 } | ||
} | ||
|
||
#[lifetimes_attr] | ||
fn attr<'a>() -> &'a u8 { &1 } | ||
|
||
#[derive(Lifetimes)] | ||
pub struct Lifetimes<'a> { | ||
pub field: &'a u8, | ||
} | ||
|
||
fn main() { | ||
assert_eq!(bang::<'static>(), &0); | ||
assert_eq!(attr::<'static>(), &1); | ||
let l1 = Lifetimes { field: &0 }; | ||
let l2 = m::Lifetimes { field: &1 }; | ||
} |
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,30 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// no-prefer-dynamic | ||
|
||
#![feature(proc_macro)] | ||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::*; | ||
|
||
#[proc_macro] | ||
pub fn single_quote_alone(_: TokenStream) -> TokenStream { | ||
// `&'a u8`, but the `'` token is not joint | ||
let trees: Vec<TokenTree> = vec![ | ||
Punct::new('&', Spacing::Alone).into(), | ||
Punct::new('\'', Spacing::Alone).into(), | ||
Ident::new("a", Span::call_site()).into(), | ||
Ident::new("u8", Span::call_site()).into(), | ||
]; | ||
trees.into_iter().collect() | ||
} |
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,19 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// aux-build:lifetimes.rs | ||
|
||
#![feature(proc_macro, proc_macro_non_items)] | ||
|
||
extern crate lifetimes; | ||
|
||
use lifetimes::*; | ||
|
||
type A = single_quote_alone!(); //~ ERROR expected type, found `'` |
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,8 @@ | ||
error: expected type, found `'` | ||
--> $DIR/lifetimes.rs:19:10 | ||
| | ||
LL | type A = single_quote_alone!(); //~ ERROR expected type, found `'` | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|