Add tests.

This commit is contained in:
Jeffrey Seyfried 2017-03-25 02:37:55 +00:00
parent 3eb235b45e
commit 754fdad4a8
15 changed files with 451 additions and 0 deletions

View File

@ -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 <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.
macro m() {} //~ ERROR `macro` is experimental (see issue #39412)
//~| HELP add #![feature(decl_macro)] to the crate attributes to enable
fn main() {}

View File

@ -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 <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.
#![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
}
}
}
}

View File

@ -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 <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.
#![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
}

View File

@ -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 <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.
#![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!(); }
}

View File

@ -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 <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.
#![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
);
}

View File

@ -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 <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.
#![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() {}

View File

@ -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 <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.
#![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);
}

View File

@ -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 <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.
#![feature(decl_macro)]
pub mod foo {
pub use self::bar::m;
mod bar {
fn f() -> u32 { 1 }
pub macro m() {
f();
}
}
}

View File

@ -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 <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.
#![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);
}

View File

@ -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 <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.
#![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!();
}

View File

@ -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 <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:intercrate.rs
#![feature(decl_macro)]
extern crate intercrate;
fn main() {
assert_eq!(intercrate::foo::m!(), 1);
}

View File

@ -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 <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.
#![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();
}

View File

@ -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 <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.
#![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);
}

View File

@ -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 <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.
#![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();
}

View File

@ -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 <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.
#![feature(decl_macro)]
macro m($T:ident) {
fn f<T, $T>(t: T, t2: $T) -> (T, $T) {
(t, t2)
}
}
m!(T);
fn main() {}