Skip to content

Commit 20b43b2

Browse files
committed
Rewrite the unit tests in ext/expand.rs as a compile-fail test.
1 parent a9821e1 commit 20b43b2

File tree

2 files changed

+46
-107
lines changed

2 files changed

+46
-107
lines changed

src/libsyntax/ext/expand.rs

-107
Original file line numberDiff line numberDiff line change
@@ -803,110 +803,3 @@ impl Folder for Marker {
803803
fn mark_tts(tts: &[TokenTree], m: Mark) -> Vec<TokenTree> {
804804
noop_fold_tts(tts, &mut Marker{mark:m, expn_id: None})
805805
}
806-
807-
808-
#[cfg(test)]
809-
mod tests {
810-
use super::{expand_crate, ExpansionConfig};
811-
use ast;
812-
use ext::base::{ExtCtxt, DummyResolver};
813-
use parse;
814-
use util::parser_testing::{string_to_parser};
815-
use visit;
816-
use visit::Visitor;
817-
818-
// a visitor that extracts the paths
819-
// from a given thingy and puts them in a mutable
820-
// array (passed in to the traversal)
821-
#[derive(Clone)]
822-
struct PathExprFinderContext {
823-
path_accumulator: Vec<ast::Path> ,
824-
}
825-
826-
impl Visitor for PathExprFinderContext {
827-
fn visit_expr(&mut self, expr: &ast::Expr) {
828-
if let ast::ExprKind::Path(None, ref p) = expr.node {
829-
self.path_accumulator.push(p.clone());
830-
}
831-
visit::walk_expr(self, expr);
832-
}
833-
}
834-
835-
// these following tests are quite fragile, in that they don't test what
836-
// *kind* of failure occurs.
837-
838-
fn test_ecfg() -> ExpansionConfig<'static> {
839-
ExpansionConfig::default("test".to_string())
840-
}
841-
842-
// make sure that macros can't escape fns
843-
#[should_panic]
844-
#[test] fn macros_cant_escape_fns_test () {
845-
let src = "fn bogus() {macro_rules! z (() => (3+4));}\
846-
fn inty() -> i32 { z!() }".to_string();
847-
let sess = parse::ParseSess::new();
848-
let crate_ast = parse::parse_crate_from_source_str(
849-
"<test>".to_string(),
850-
src,
851-
Vec::new(), &sess).unwrap();
852-
// should fail:
853-
let mut loader = DummyResolver;
854-
let mut ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut loader);
855-
expand_crate(&mut ecx, vec![], crate_ast);
856-
}
857-
858-
// make sure that macros can't escape modules
859-
#[should_panic]
860-
#[test] fn macros_cant_escape_mods_test () {
861-
let src = "mod foo {macro_rules! z (() => (3+4));}\
862-
fn inty() -> i32 { z!() }".to_string();
863-
let sess = parse::ParseSess::new();
864-
let crate_ast = parse::parse_crate_from_source_str(
865-
"<test>".to_string(),
866-
src,
867-
Vec::new(), &sess).unwrap();
868-
let mut loader = DummyResolver;
869-
let mut ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut loader);
870-
expand_crate(&mut ecx, vec![], crate_ast);
871-
}
872-
873-
// macro_use modules should allow macros to escape
874-
#[test] fn macros_can_escape_flattened_mods_test () {
875-
let src = "#[macro_use] mod foo {macro_rules! z (() => (3+4));}\
876-
fn inty() -> i32 { z!() }".to_string();
877-
let sess = parse::ParseSess::new();
878-
let crate_ast = parse::parse_crate_from_source_str(
879-
"<test>".to_string(),
880-
src,
881-
Vec::new(), &sess).unwrap();
882-
let mut loader = DummyResolver;
883-
let mut ecx = ExtCtxt::new(&sess, vec![], test_ecfg(), &mut loader);
884-
expand_crate(&mut ecx, vec![], crate_ast);
885-
}
886-
887-
fn expand_crate_str(crate_str: String) -> ast::Crate {
888-
let ps = parse::ParseSess::new();
889-
let crate_ast = panictry!(string_to_parser(&ps, crate_str).parse_crate_mod());
890-
// the cfg argument actually does matter, here...
891-
let mut loader = DummyResolver;
892-
let mut ecx = ExtCtxt::new(&ps, vec![], test_ecfg(), &mut loader);
893-
expand_crate(&mut ecx, vec![], crate_ast)
894-
}
895-
896-
#[test] fn macro_tokens_should_match(){
897-
expand_crate_str(
898-
"macro_rules! m((a)=>(13)) ;fn main(){m!(a);}".to_string());
899-
}
900-
901-
// should be able to use a bound identifier as a literal in a macro definition:
902-
#[test] fn self_macro_parsing(){
903-
expand_crate_str(
904-
"macro_rules! foo ((zz) => (287;));
905-
fn f(zz: i32) {foo!(zz);}".to_string()
906-
);
907-
}
908-
909-
// create a really evil test case where a $x appears inside a binding of $x
910-
// but *shouldn't* bind because it was inserted by a different macro....
911-
// can't write this test case until we have macro-generating macros.
912-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2016 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+
mod macros_cant_escape_fns {
12+
fn f() {
13+
macro_rules! m { () => { 3 + 4 } }
14+
}
15+
fn g() -> i32 { m!() } //~ ERROR macro undefined
16+
}
17+
18+
mod macros_cant_escape_mods {
19+
mod f {
20+
macro_rules! m { () => { 3 + 4 } }
21+
}
22+
fn g() -> i32 { m!() } //~ ERROR macro undefined
23+
}
24+
25+
mod macros_can_escape_flattened_mods_test {
26+
#[macro_use]
27+
mod f {
28+
macro_rules! m { () => { 3 + 4 } }
29+
}
30+
fn g() -> i32 { m!() }
31+
}
32+
33+
fn macro_tokens_should_match() {
34+
macro_rules! m { (a) => { 13 } }
35+
m!(a);
36+
}
37+
38+
// should be able to use a bound identifier as a literal in a macro definition:
39+
fn self_macro_parsing() {
40+
macro_rules! foo { (zz) => { 287; } }
41+
fn f(zz: i32) {
42+
foo!(zz);
43+
}
44+
}
45+
46+
fn main() {}

0 commit comments

Comments
 (0)