rust/tests/source/macros.rs

335 lines
11 KiB
Rust
Raw Normal View History

// rustfmt-normalize_comments: true
2016-01-22 20:40:26 +01:00
itemmacro!(this, is.now() .formatted(yay));
2015-09-14 22:53:30 +02:00
2016-01-24 20:11:18 +01:00
itemmacro!(really, long.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb() .is.formatted());
itemmacro!{this, is.bracket().formatted()}
peg_file! modname ("mygrammarfile.rustpeg");
2015-09-14 22:53:30 +02:00
fn main() {
foo! ( );
bar!( a , b , c );
bar!( a , b , c , );
2015-09-14 22:53:30 +02:00
baz!(1+2+3, quux. kaas());
quux!(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB);
kaas!(/* comments */ a /* post macro */, b /* another */);
trailingcomma!( a , b , c , );
2017-10-27 09:35:40 +02:00
// Preserve trailing comma only when necessary.
ok!(file.seek(
SeekFrom::Start(
table.map(|table| fixture.offset(table)).unwrap_or(0),
)
));
2015-09-14 22:53:30 +02:00
noexpr!( i am not an expression, OK? );
vec! [ a , b , c];
vec! [AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA, AAAAAA,
BBBBB, 5, 100-30, 1.33, b, b, b];
vec! [a /* comment */];
// Trailing spaces after a comma
vec![
a,
];
2017-05-19 12:25:53 +02:00
vec![a; b];
vec!(a; b);
vec!{a; b};
vec![a, b; c];
vec![a; b, c];
2017-05-22 01:50:46 +02:00
vec![a; (|x| { let y = x + 1; let z = y + 1; z })(2)];
vec![a; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx];
vec![a; unsafe {
x + 1
}];
unknown_bracket_macro__comma_should_not_be_stripped![
a,
];
2015-09-14 22:53:30 +02:00
foo(makro!(1, 3));
hamkaas!{ () };
macrowithbraces! {dont, format, me}
x!(fn);
some_macro!(
);
some_macro![
];
some_macro!{
// comment
};
some_macro!{
// comment
};
some_macro!(
// comment
not function like
);
// #1712
let image = gray_image!(
00, 01, 02;
10, 11, 12;
20, 21, 22);
// #1092
chain!(input, a:take!(max_size), || []);
}
impl X {
empty_invoc!{}
2015-09-14 22:53:30 +02:00
}
fn issue_1279() {
println!("dsfs"); // a comment
}
fn issue_1555() {
let hello = &format!("HTTP/1.1 200 OK\r\nServer: {}\r\n\r\n{}",
"65454654654654654654654655464",
"4");
}
2017-05-28 04:41:16 +02:00
fn issue1178() {
macro_rules! foo {
(#[$attr:meta] $name:ident) => {}
}
foo!(#[doc = "bar"] baz);
}
fn issue1739() {
sql_function!(add_rss_item,
add_rss_item_t,
(a: types::Integer,
b: types::Timestamptz,
c: types::Text,
d: types::Text,
e: types::Text));
w.slice_mut(s![.., init_size[1] - extreeeeeeeeeeeeeeeeeeeeeeeem..init_size[1], ..])
.par_map_inplace(|el| *el = 0.);
}
fn issue_1885() {
let threads = people.into_iter().map(|name| {
chan_select! {
rx.recv() => {}
}
}).collect::<Vec<_>>();
}
fn issue_1917() {
mod x {
quickcheck! {
fn test(a: String, s: String, b: String) -> TestResult {
if a.find(&s).is_none() {
TestResult::from_bool(true)
} else {
TestResult::discard()
}
}
}
}
}
2017-08-27 06:44:49 +02:00
fn issue_1921() {
// Macro with tabs.
lazy_static! {
static ref ONE: u32 = 1;
static ref TWO: u32 = 2;
static ref THREE: u32 = 3;
static ref FOUR: u32 = {
let mut acc = 1;
acc += 1;
acc += 2;
acc
}
}
}
// #1577
fn issue1577() {
let json = json!({
"foo": "bar",
});
}
gfx_pipeline!(pipe {
vbuf: gfx::VertexBuffer<Vertex> = (),
out: gfx::RenderTarget<ColorFormat> = "Target0",
});
2017-08-30 05:00:10 +02:00
// #1919
#[test]
fn __bindgen_test_layout_HandleWithDtor_open0_int_close0_instantiation() {
assert_eq!(
::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>(),
8usize,
concat!(
"Size of template specialization: ",
stringify ! ( HandleWithDtor < :: std :: os :: raw :: c_int > )
)
);
assert_eq ! ( :: std :: mem :: align_of :: < HandleWithDtor < :: std :: os :: raw :: c_int > > ( ) , 8usize , concat ! ( "Alignment of template specialization: " , stringify ! ( HandleWithDtor < :: std :: os :: raw :: c_int > ) ) );
}
2017-10-05 12:44:45 +02:00
// #878
macro_rules! try_opt {
($expr:expr) => (match $expr {
Some(val) => val,
None => { return None; }
})
}
2017-11-30 14:12:55 +01:00
// #2214
// macro call whose argument is an array with trailing comma.
fn issue2214() {
make_test!(str_searcher_ascii_haystack, "bb", "abbcbbd", [
Reject(0, 1),
Match (1, 3),
Reject(3, 4),
Match (4, 6),
Reject(6, 7),
]);
}
fn special_case_macros() {
let p = eprint!();
let q = eprint!("{}", 1);
let r = eprint!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
let s = eprint!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
let q = eprintln!("{}", 1);
let r = eprintln!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
let s = eprintln!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
let q = format!("{}", 1);
let r = format!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
let s = format!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
let q = format_args!("{}", 1);
let r = format_args!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
let s = format_args!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
let q = print!("{}", 1);
let r = print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
let s = print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
let q = println!("{}", 1);
let r = println!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
let s = println!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
let q = unreachable!("{}", 1);
let r = unreachable!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
let s = unreachable!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
debug!("{}", 1);
debug!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
debug!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
error!("{}", 1);
error!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
error!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
info!("{}", 1);
info!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
info!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
panic!("{}", 1);
panic!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
panic!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
warn!("{}", 1);
warn!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
warn!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
assert!();
assert!(result == 42);
assert!(result == 42, "Ahoy there, {}!", target);
assert!(result == 42, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
assert!(result == 42, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
assert_eq!();
assert_eq!(left);
assert_eq!(left, right);
assert_eq!(left, right, "Ahoy there, {}!", target);
assert_eq!(left, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
assert_eq!(first_realllllllllllly_long_variable_that_doesnt_fit_one_one_line, second_reallllllllllly_long_variable_that_doesnt_fit_one_one_line, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
assert_eq!(left + 42, right, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
assert_eq!(left, right, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
write!(&mut s, "Ahoy there, {}!", target);
write!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
write!(&mut s, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
writeln!(&mut s, "Ahoy there, {}!", target);
writeln!(&mut s, "Arr! While plunderin' the hold, we got '{}' when given '{}' (we expected '{}')", result, input, expected);
writeln!(&mut s, "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
}
2017-12-05 00:41:10 +01:00
// #1209
impl Foo {
/// foo
pub fn foo(&self) -> Bar<foo!( )> {}
}
// #819
fn macro_in_pattern_position () {
let x = match y {
foo!( ) => (),
bar!( a, b,
c) => (),
bar!(a
, b
, c
,) => (),
baz!( 1 + 2 + 3, quux.kaas( )
) => (),
quux!(AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB) => (),
};
}
2018-01-01 07:51:30 +01:00
macro foo() {
}
pub macro bar($x:ident+$y:expr; ) {
fn foo($x: Foo) {
long_function(a_long_argument_to_a_long_function_is_what_this_is(AAAAAAAAAAAAAAAAAAAAAAAAAAAA),
$x.bar($y));
}
}
macro foo() {
// a comment
fn foo() {
// another comment
bar();
}
}