From: Jeffrey Seyfried Date: Sat, 25 Mar 2017 02:37:55 +0000 (+0000) Subject: Add tests. X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=754fdad4a878340bb90a10f0072e6d4891d63c70;p=rust.git Add tests. --- diff --git a/src/test/compile-fail/feature-gate-decl_macro.rs b/src/test/compile-fail/feature-gate-decl_macro.rs new file mode 100644 index 00000000000..af7d5fec071 --- /dev/null +++ b/src/test/compile-fail/feature-gate-decl_macro.rs @@ -0,0 +1,14 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +macro m() {} //~ ERROR `macro` is experimental (see issue #39412) +//~| HELP add #![feature(decl_macro)] to the crate attributes to enable + +fn main() {} diff --git a/src/test/compile-fail/hygiene/globs.rs b/src/test/compile-fail/hygiene/globs.rs new file mode 100644 index 00000000000..7ba217061c6 --- /dev/null +++ b/src/test/compile-fail/hygiene/globs.rs @@ -0,0 +1,68 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +mod foo { + pub fn f() {} +} + +mod bar { + pub fn g() {} +} + +macro m($($t:tt)*) { + $($t)* + use foo::*; + f(); + g(); //~ ERROR cannot find function `g` in this scope +} + +fn main() { + m! { + use bar::*; + g(); + f(); //~ ERROR cannot find function `f` in this scope + } +} + +n!(f); +macro n($i:ident) { + mod foo { + pub fn $i() -> u32 { 0 } + pub fn f() {} + + mod test { + use super::*; + fn g() { + let _: u32 = $i(); + let _: () = f(); + } + } + + macro n($j:ident) { + mod test { + use super::*; + fn g() { + let _: u32 = $i(); + let _: () = f(); + $j(); + } + } + } + + n!(f); + mod test2 { + super::n! { + f //~ ERROR cannot find function `f` in this scope + } + } + } +} diff --git a/src/test/compile-fail/hygiene/nested_macro_privacy.rs b/src/test/compile-fail/hygiene/nested_macro_privacy.rs new file mode 100644 index 00000000000..6612359649c --- /dev/null +++ b/src/test/compile-fail/hygiene/nested_macro_privacy.rs @@ -0,0 +1,27 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +macro n($foo:ident, $S:ident, $i:ident, $m:ident) { + mod $foo { + #[derive(Default)] + pub struct $S { $i: u32 } + pub macro $m($e:expr) { $e.$i } + } +} + +n!(foo, S, i, m); + +fn main() { + use foo::{S, m}; + S::default().i; //~ ERROR field `i` of struct `foo::S` is private + m!(S::default()); // ok +} diff --git a/src/test/compile-fail/hygiene/no_implicit_prelude.rs b/src/test/compile-fail/hygiene/no_implicit_prelude.rs new file mode 100644 index 00000000000..c90c7b3093c --- /dev/null +++ b/src/test/compile-fail/hygiene/no_implicit_prelude.rs @@ -0,0 +1,25 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +mod foo { + pub macro m() { Vec::new(); ().clone() } + fn f() { ::bar::m!(); } +} + +#[no_implicit_prelude] +mod bar { + pub macro m() { + Vec::new(); //~ ERROR failed to resolve + ().clone() //~ ERROR no method named `clone` found + } + fn f() { ::foo::m!(); } +} diff --git a/src/test/compile-fail/hygiene/privacy.rs b/src/test/compile-fail/hygiene/privacy.rs new file mode 100644 index 00000000000..987cad187d4 --- /dev/null +++ b/src/test/compile-fail/hygiene/privacy.rs @@ -0,0 +1,28 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +mod foo { + fn f() {} + + pub macro m($e:expr) { + f(); + self::f(); + ::foo::f(); + $e + } +} + +fn main() { + foo::m!( + foo::f() //~ ERROR `f` is private + ); +} diff --git a/src/test/compile-fail/hygiene/trait_items.rs b/src/test/compile-fail/hygiene/trait_items.rs new file mode 100644 index 00000000000..3bd19cbc0ac --- /dev/null +++ b/src/test/compile-fail/hygiene/trait_items.rs @@ -0,0 +1,31 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +mod foo { + pub trait T { + fn f(&self) {} + } + impl T for () {} +} + +mod bar { + use foo::*; + pub macro m() { ().f() } + fn f() { ::baz::m!(); } +} + +mod baz { + pub macro m() { ().f() } //~ ERROR no method named `f` found for type `()` in the current scope + fn f() { ::bar::m!(); } +} + +fn main() {} diff --git a/src/test/run-pass/hygiene/arguments.rs b/src/test/run-pass/hygiene/arguments.rs new file mode 100644 index 00000000000..19fd08c14df --- /dev/null +++ b/src/test/run-pass/hygiene/arguments.rs @@ -0,0 +1,25 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +macro m($t:ty, $e:expr) { + mod foo { + #[allow(unused)] + struct S; + pub(super) fn f(_: $t) {} + } + foo::f($e); +} + +fn main() { + struct S; + m!(S, S); +} diff --git a/src/test/run-pass/hygiene/auxiliary/intercrate.rs b/src/test/run-pass/hygiene/auxiliary/intercrate.rs new file mode 100644 index 00000000000..aa67e5c5f4d --- /dev/null +++ b/src/test/run-pass/hygiene/auxiliary/intercrate.rs @@ -0,0 +1,21 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +pub mod foo { + pub use self::bar::m; + mod bar { + fn f() -> u32 { 1 } + pub macro m() { + f(); + } + } +} diff --git a/src/test/run-pass/hygiene/fields.rs b/src/test/run-pass/hygiene/fields.rs new file mode 100644 index 00000000000..6768753f16c --- /dev/null +++ b/src/test/run-pass/hygiene/fields.rs @@ -0,0 +1,38 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +mod foo { + struct S { x: u32 } + struct T(u32); + + pub macro m($S:ident, $x:ident) {{ + struct $S { + $x: u32, + x: i32, + } + + let s = S { x: 0 }; + let _ = s.x; + + let t = T(0); + let _ = t.0; + + let s = $S { $x: 0, x: 1 }; + assert_eq!((s.$x, s.x), (0, 1)); + s + }} +} + +fn main() { + let s = foo::m!(S, x); + assert_eq!(s.x, 0); +} diff --git a/src/test/run-pass/hygiene/impl_items.rs b/src/test/run-pass/hygiene/impl_items.rs new file mode 100644 index 00000000000..01b8e22e9ef --- /dev/null +++ b/src/test/run-pass/hygiene/impl_items.rs @@ -0,0 +1,42 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +mod foo { + struct S; + impl S { + fn f(&self) {} + } + + pub macro m() { + let _: () = S.f(); + } +} + +struct S; + +macro m($f:ident) { + impl S { + fn f(&self) -> u32 { 0 } + fn $f(&self) -> i32 { 0 } + } + fn f() { + let _: u32 = S.f(); + let _: i32 = S.$f(); + } +} + +m!(f); + +fn main() { + let _: i32 = S.f(); + foo::m!(); +} diff --git a/src/test/run-pass/hygiene/intercrate.rs b/src/test/run-pass/hygiene/intercrate.rs new file mode 100644 index 00000000000..6550b649230 --- /dev/null +++ b/src/test/run-pass/hygiene/intercrate.rs @@ -0,0 +1,19 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:intercrate.rs + +#![feature(decl_macro)] + +extern crate intercrate; + +fn main() { + assert_eq!(intercrate::foo::m!(), 1); +} diff --git a/src/test/run-pass/hygiene/items.rs b/src/test/run-pass/hygiene/items.rs new file mode 100644 index 00000000000..1fc51eec26c --- /dev/null +++ b/src/test/run-pass/hygiene/items.rs @@ -0,0 +1,34 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +pub macro m($foo:ident, $f:ident, $e:expr) { + mod foo { + pub fn f() -> u32 { 0 } + pub fn $f() -> u64 { 0 } + } + + mod $foo { + pub fn f() -> i32 { 0 } + pub fn $f() -> i64 { 0 } + } + + let _: u32 = foo::f(); + let _: u64 = foo::$f(); + let _: i32 = $foo::f(); + let _: i64 = $foo::$f(); + let _: i64 = $e; +} + +fn main() { + m!(foo, f, foo::f()); + let _: i64 = foo::f(); +} diff --git a/src/test/run-pass/hygiene/lexical.rs b/src/test/run-pass/hygiene/lexical.rs new file mode 100644 index 00000000000..40683917942 --- /dev/null +++ b/src/test/run-pass/hygiene/lexical.rs @@ -0,0 +1,31 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +mod bar { + mod baz { + pub fn f() {} + } + + pub macro m($f:ident) { + baz::f(); + let _: i32 = $f(); + { + fn $f() -> u32 { 0 } + let _: u32 = $f(); + } + } +} + +fn main() { + fn f() -> i32 { 0 } + bar::m!(f); +} diff --git a/src/test/run-pass/hygiene/trait_items.rs b/src/test/run-pass/hygiene/trait_items.rs new file mode 100644 index 00000000000..ed57d6de1af --- /dev/null +++ b/src/test/run-pass/hygiene/trait_items.rs @@ -0,0 +1,27 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +macro m($T:ident, $f:ident) { + pub trait $T { + fn f(&self) -> u32 { 0 } + fn $f(&self) -> i32 { 0 } + } + impl $T for () {} + + let _: u32 = ().f(); + let _: i32 = ().$f(); +} + +fn main() { + m!(T, f); + let _: i32 = ().f(); +} diff --git a/src/test/run-pass/hygiene/ty_params.rs b/src/test/run-pass/hygiene/ty_params.rs new file mode 100644 index 00000000000..f41c365d380 --- /dev/null +++ b/src/test/run-pass/hygiene/ty_params.rs @@ -0,0 +1,21 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(decl_macro)] + +macro m($T:ident) { + fn f(t: T, t2: $T) -> (T, $T) { + (t, t2) + } +} + +m!(T); + +fn main() {}