Skip to content

Commit ddd4eb1

Browse files
committed
Add tests.
1 parent 0a6d444 commit ddd4eb1

15 files changed

+451
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro m() {} //~ ERROR `macro` is experimental (see issue #39412)
12+
//~| HELP add #![feature(decl_macro)] to the crate attributes to enable
13+
14+
fn main() {}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
mod foo {
14+
pub fn f() {}
15+
}
16+
17+
mod bar {
18+
pub fn g() {}
19+
}
20+
21+
macro m($($t:tt)*) {
22+
$($t)*
23+
use foo::*;
24+
f();
25+
g(); //~ ERROR cannot find function `g` in this scope
26+
}
27+
28+
fn main() {
29+
m! {
30+
use bar::*;
31+
g();
32+
f(); //~ ERROR cannot find function `f` in this scope
33+
}
34+
}
35+
36+
n!(f);
37+
macro n($i:ident) {
38+
mod foo {
39+
pub fn $i() -> u32 { 0 }
40+
pub fn f() {}
41+
42+
mod test {
43+
use super::*;
44+
fn g() {
45+
let _: u32 = $i();
46+
let _: () = f();
47+
}
48+
}
49+
50+
macro n($j:ident) {
51+
mod test {
52+
use super::*;
53+
fn g() {
54+
let _: u32 = $i();
55+
let _: () = f();
56+
$j();
57+
}
58+
}
59+
}
60+
61+
n!(f);
62+
mod test2 {
63+
super::n! {
64+
f //~ ERROR cannot find function `f` in this scope
65+
}
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
macro n($foo:ident, $S:ident, $i:ident, $m:ident) {
14+
mod $foo {
15+
#[derive(Default)]
16+
pub struct $S { $i: u32 }
17+
pub macro $m($e:expr) { $e.$i }
18+
}
19+
}
20+
21+
n!(foo, S, i, m);
22+
23+
fn main() {
24+
use foo::{S, m};
25+
S::default().i; //~ ERROR field `i` of struct `foo::S` is private
26+
m!(S::default()); // ok
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
mod foo {
14+
pub macro m() { Vec::new(); ().clone() }
15+
fn f() { ::bar::m!(); }
16+
}
17+
18+
#[no_implicit_prelude]
19+
mod bar {
20+
pub macro m() {
21+
Vec::new(); //~ ERROR failed to resolve
22+
().clone() //~ ERROR no method named `clone` found
23+
}
24+
fn f() { ::foo::m!(); }
25+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
mod foo {
14+
fn f() {}
15+
16+
pub macro m($e:expr) {
17+
f();
18+
self::f();
19+
::foo::f();
20+
$e
21+
}
22+
}
23+
24+
fn main() {
25+
foo::m!(
26+
foo::f() //~ ERROR `f` is private
27+
);
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
mod foo {
14+
pub trait T {
15+
fn f(&self) {}
16+
}
17+
impl T for () {}
18+
}
19+
20+
mod bar {
21+
use foo::*;
22+
pub macro m() { ().f() }
23+
fn f() { ::baz::m!(); }
24+
}
25+
26+
mod baz {
27+
pub macro m() { ().f() } //~ ERROR no method named `f` found for type `()` in the current scope
28+
fn f() { ::bar::m!(); }
29+
}
30+
31+
fn main() {}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
macro m($t:ty, $e:expr) {
14+
mod foo {
15+
#[allow(unused)]
16+
struct S;
17+
pub(super) fn f(_: $t) {}
18+
}
19+
foo::f($e);
20+
}
21+
22+
fn main() {
23+
struct S;
24+
m!(S, S);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
pub mod foo {
14+
pub use self::bar::m;
15+
mod bar {
16+
fn f() -> u32 { 1 }
17+
pub macro m() {
18+
f();
19+
}
20+
}
21+
}

src/test/run-pass/hygiene/fields.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
mod foo {
14+
struct S { x: u32 }
15+
struct T(u32);
16+
17+
pub macro m($S:ident, $x:ident) {{
18+
struct $S {
19+
$x: u32,
20+
x: i32,
21+
}
22+
23+
let s = S { x: 0 };
24+
let _ = s.x;
25+
26+
let t = T(0);
27+
let _ = t.0;
28+
29+
let s = $S { $x: 0, x: 1 };
30+
assert_eq!((s.$x, s.x), (0, 1));
31+
s
32+
}}
33+
}
34+
35+
fn main() {
36+
let s = foo::m!(S, x);
37+
assert_eq!(s.x, 0);
38+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
mod foo {
14+
struct S;
15+
impl S {
16+
fn f(&self) {}
17+
}
18+
19+
pub macro m() {
20+
let _: () = S.f();
21+
}
22+
}
23+
24+
struct S;
25+
26+
macro m($f:ident) {
27+
impl S {
28+
fn f(&self) -> u32 { 0 }
29+
fn $f(&self) -> i32 { 0 }
30+
}
31+
fn f() {
32+
let _: u32 = S.f();
33+
let _: i32 = S.$f();
34+
}
35+
}
36+
37+
m!(f);
38+
39+
fn main() {
40+
let _: i32 = S.f();
41+
foo::m!();
42+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:intercrate.rs
12+
13+
#![feature(decl_macro)]
14+
15+
extern crate intercrate;
16+
17+
fn main() {
18+
assert_eq!(intercrate::foo::m!(), 1);
19+
}

src/test/run-pass/hygiene/items.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(decl_macro)]
12+
13+
pub macro m($foo:ident, $f:ident, $e:expr) {
14+
mod foo {
15+
pub fn f() -> u32 { 0 }
16+
pub fn $f() -> u64 { 0 }
17+
}
18+
19+
mod $foo {
20+
pub fn f() -> i32 { 0 }
21+
pub fn $f() -> i64 { 0 }
22+
}
23+
24+
let _: u32 = foo::f();
25+
let _: u64 = foo::$f();
26+
let _: i32 = $foo::f();
27+
let _: i64 = $foo::$f();
28+
let _: i64 = $e;
29+
}
30+
31+
fn main() {
32+
m!(foo, f, foo::f());
33+
let _: i64 = foo::f();
34+
}

0 commit comments

Comments
 (0)