From bcdcaea5728db8003b1ad781490d782465658589 Mon Sep 17 00:00:00 2001 From: John Clements Date: Mon, 11 Nov 2013 17:35:25 -0800 Subject: revive old commented-out test cases as ignored test cases for hygiene --- src/libsyntax/ast.rs | 1 + src/libsyntax/ext/expand.rs | 64 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 6 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 418d18d8d34..d24c2be5a74 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -835,6 +835,7 @@ impl Arg { } } +// represents the header (not the body) of a function declaration #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash)] pub struct FnDecl { pub inputs: Vec, diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index fb2de2e271a..eca04148513 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -658,9 +658,9 @@ fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander) } } -// a visitor that extracts the pat_ident paths +// a visitor that extracts the pat_ident (binding) paths // from a given thingy and puts them in a mutable -// array (passed in to the traversal) +// array (passed in to the traversal). #[deriving(Clone)] pub struct NewNameFinderContext { ident_accumulator: Vec , @@ -1026,7 +1026,7 @@ mod test { use parse; use parse::token; use util::parser_testing::{string_to_parser}; - use util::parser_testing::{string_to_pat, strs_to_idents}; + use util::parser_testing::{string_to_pat, string_to_crate, strs_to_idents}; use visit; use visit::Visitor; @@ -1059,7 +1059,7 @@ mod test { } // return a visitor that extracts the paths - // from a given pattern and puts them in a mutable + // from a given thingy and puts them in a mutable // array (passed in to the traversal) pub fn new_path_finder(paths: Vec ) -> NewPathExprFinderContext { NewPathExprFinderContext { @@ -1188,7 +1188,7 @@ mod test { // The comparisons are done post-mtwt-resolve, so we're comparing renamed // names; differences in marks don't matter any more. // - // oog... I also want tests that check "binding-identifier-=?". That is, + // oog... I also want tests that check "bound-identifier-=?". That is, // not just "do these have the same name", but "do they have the same // name *and* the same marks"? Understanding this is really pretty painful. // in principle, you might want to control this boolean on a per-varref basis, @@ -1217,12 +1217,55 @@ mod test { ("macro_rules! letty(($x:ident) => (let $x = 15;)) macro_rules! user(($x:ident) => ({letty!($x); $x})) fn main() -> int {user!(z)}", - vec!(vec!(0)), false)); + vec!(vec!(0)), false) + ); for (idx,s) in tests.iter().enumerate() { run_renaming_test(s,idx); } } + // no longer a fixme #8062: this test exposes a *potential* bug; our system does + // not behave exactly like MTWT, but a conversation with Matthew Flatt + // suggests that this can only occur in the presence of local-expand, which + // we have no plans to support. ... unless it's needed for item hygiene.... + #[ignore] + #[test] fn issue_8062(){ + run_renaming_test( + &("fn main() {let hrcoo = 19; macro_rules! getx(()=>(hrcoo)); getx!();}", + vec!(vec!(0)), true), 0) + } + + // FIXME #6994: + // the z flows into and out of two macros (g & f) along one path, and one + // (just g) along the other, so the result of the whole thing should + // be "let z_123 = 3; z_123" + #[ignore] + #[test] fn issue_6994(){ + run_renaming_test( + &("macro_rules! g (($x:ident) => + ({macro_rules! f(($y:ident)=>({let $y=3;$x}));f!($x)})) + fn a(){g!(z)}", + vec!(vec!(0)),false), + 0) + } + + // create a really evil test case where a $x appears inside a binding of $x + // but *shouldnt* bind because it was inserted by a different macro.... + // can't write this test case until we have macro-generating macros. + + // FIXME #9383 : lambda var hygiene + // interesting... can't even write this test, yet, because the name-finder + // only finds pattern vars. Time to upgrade test framework. + /*#[test] + fn issue_9383(){ + run_renaming_test( + &("macro_rules! bad_macro (($ex:expr) => ({(|_x| { $ex }) (9) })) + fn takes_x(_x : int) { assert_eq!(bad_macro!(_x),8); } + fn main() { takes_x(8); }", + vec!(vec!()),false), + 0) + }*/ + // run one of the renaming tests fn run_renaming_test(t: &RenamingTest, test_idx: uint) { let invalid_name = token::special_idents::invalid.name; @@ -1358,4 +1401,13 @@ foo_module!() strs_to_idents(vec!("a","c","b","d"))); } + #[test] + fn pat_idents_2(){ + let the_crate = string_to_crate("fn main (a : int) -> int {|b| {a + b} }".to_string()); + let mut pat_idents = new_name_finder(Vec::new()); + pat_idents.visit_mod(&the_crate.module, the_crate.span, ast::CRATE_NODE_ID, ()); + assert_eq!(pat_idents.ident_accumulator, + strs_to_idents(vec!("a","b"))); + } + } -- cgit 1.4.1-3-g733a5 From 9215d7e5b764c173ef8b2fecda913d39291e378b Mon Sep 17 00:00:00 2001 From: John Clements Date: Mon, 23 Jun 2014 18:45:51 -0700 Subject: comments only --- src/libsyntax/ext/expand.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index eca04148513..a33e6e5e923 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1070,7 +1070,7 @@ mod test { // these following tests are quite fragile, in that they don't test what // *kind* of failure occurs. - // make sure that macros can leave scope + // make sure that macros can't escape fns #[should_fail] #[test] fn macros_cant_escape_fns_test () { let src = "fn bogus() {macro_rules! z (() => (3+4))}\ @@ -1088,7 +1088,7 @@ mod test { expand_crate(&sess,cfg,vec!(),vec!(),crate_ast); } - // make sure that macros can leave scope for modules + // make sure that macros can't escape modules #[should_fail] #[test] fn macros_cant_escape_mods_test () { let src = "mod foo {macro_rules! z (() => (3+4))}\ @@ -1105,7 +1105,7 @@ mod test { expand_crate(&sess,cfg,vec!(),vec!(),crate_ast); } - // macro_escape modules shouldn't cause macros to leave scope + // macro_escape modules should allow macros to escape #[test] fn macros_can_escape_flattened_mods_test () { let src = "#[macro_escape] mod foo {macro_rules! z (() => (3+4))}\ fn inty() -> int { z!() }".to_string(); @@ -1114,7 +1114,6 @@ mod test { "".to_string(), src, Vec::new(), &sess); - // should fail: let cfg = ::syntax::ext::expand::ExpansionConfig { deriving_hash_type_parameter: false, crate_id: from_str("test").unwrap(), @@ -1185,6 +1184,12 @@ mod test { // binding should match the second two varrefs, and the second binding // should match the first varref. // + // Put differently; this is a sparse representation of a boolean matrix + // indicating which bindings capture which identifiers. + // + // Note also that this matrix is dependent on the implicit ordering of + // the bindings and the varrefs discovered by the name-finder and the path-finder. + // // The comparisons are done post-mtwt-resolve, so we're comparing renamed // names; differences in marks don't matter any more. // -- cgit 1.4.1-3-g733a5 From 19e1d834ffad4c5023a44f894d570054913e31e8 Mon Sep 17 00:00:00 2001 From: John Clements Date: Tue, 24 Jun 2014 16:33:44 -0700 Subject: enrich and rename crate_idents test case --- src/libsyntax/ext/expand.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index a33e6e5e923..bf68538d656 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1406,13 +1406,19 @@ foo_module!() strs_to_idents(vec!("a","c","b","d"))); } + // test the list of identifier patterns gathered by the visitor. Note that + // 'None' is listed as an identifier pattern because we don't yet know that + // it's the name of a 0-ary variant, and that 'i' appears twice in succession. #[test] - fn pat_idents_2(){ - let the_crate = string_to_crate("fn main (a : int) -> int {|b| {a + b} }".to_string()); - let mut pat_idents = new_name_finder(Vec::new()); - pat_idents.visit_mod(&the_crate.module, the_crate.span, ast::CRATE_NODE_ID, ()); - assert_eq!(pat_idents.ident_accumulator, - strs_to_idents(vec!("a","b"))); + fn crate_idents(){ + let the_crate = string_to_crate("fn main (a : int) -> int {|b| { + match 34 {None => 3, Some(i) | i => j, Foo{k:z,l:y} => \"banana\"}} }".to_string()); + let mut idents = new_name_finder(Vec::new()); + //visit::walk_crate(&mut idents, &the_crate, ()); + idents.visit_mod(&the_crate.module, the_crate.span, ast::CRATE_NODE_ID, ()); + assert_eq!(idents.ident_accumulator, + strs_to_idents(vec!("a","b","None","i","i","z","y"))); } + } -- cgit 1.4.1-3-g733a5 From bc837fdadf185a7aa593008e13364fde2b8c7fc9 Mon Sep 17 00:00:00 2001 From: John Clements Date: Tue, 24 Jun 2014 16:34:03 -0700 Subject: add unit test for match var hygiene --- src/libsyntax/ext/expand.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index bf68538d656..c2e13a53b1e 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1254,6 +1254,18 @@ mod test { 0) } + // FIXME #9384, match variable hygiene. Should expand into + // fn z() {match 8 {x_1 => {match 9 {x_2 | x_2 => x_2 + x_1}}}} + #[test] fn issue_9384(){ + run_renaming_test( + &("macro_rules! bad_macro (($ex:expr) => ({match 9 {x | x => x + $ex}})) + fn z() {match 8 {x => bad_macro!(_x)}}", + // NB: the third "binding" is the repeat of the second one. + vec!(vec!(1),vec!(0),vec!(0)), + true), + 0) + } + // create a really evil test case where a $x appears inside a binding of $x // but *shouldnt* bind because it was inserted by a different macro.... // can't write this test case until we have macro-generating macros. -- cgit 1.4.1-3-g733a5 From 84027937745835e1c9328013ba9b8faebf4352af Mon Sep 17 00:00:00 2001 From: John Clements Date: Tue, 24 Jun 2014 16:50:06 -0700 Subject: remove unneeded visit_ty methods --- src/libsyntax/ext/expand.rs | 9 --------- 1 file changed, 9 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index c2e13a53b1e..3aec353f6e6 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -698,10 +698,6 @@ impl Visitor<()> for NewNameFinderContext { } } - fn visit_ty(&mut self, typ: &ast::Ty, _: ()) { - visit::walk_ty(self, typ, ()) - } - } // return a visitor that extracts the pat_ident paths @@ -1051,11 +1047,6 @@ mod test { _ => visit::walk_expr(self,expr,()) } } - - fn visit_ty(&mut self, typ: &ast::Ty, _: ()) { - visit::walk_ty(self, typ, ()) - } - } // return a visitor that extracts the paths -- cgit 1.4.1-3-g733a5 From 26b5347310f8e07dba695f857821d08b7deca208 Mon Sep 17 00:00:00 2001 From: John Clements Date: Tue, 24 Jun 2014 17:03:49 -0700 Subject: remove unneccessary pubs, shorten names --- src/libsyntax/ext/expand.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 3aec353f6e6..2b325d1c787 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -662,11 +662,11 @@ fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander) // from a given thingy and puts them in a mutable // array (passed in to the traversal). #[deriving(Clone)] -pub struct NewNameFinderContext { +struct NameFinderContext { ident_accumulator: Vec , } -impl Visitor<()> for NewNameFinderContext { +impl Visitor<()> for NameFinderContext { fn visit_pat(&mut self, pattern: &ast::Pat, _: ()) { match *pattern { // we found a pat_ident! @@ -703,8 +703,8 @@ impl Visitor<()> for NewNameFinderContext { // return a visitor that extracts the pat_ident paths // from a given thingy and puts them in a mutable // array (passed in to the traversal) -pub fn new_name_finder(idents: Vec ) -> NewNameFinderContext { - NewNameFinderContext { +fn new_name_finder(idents: Vec ) -> NameFinderContext { + NameFinderContext { ident_accumulator: idents, } } @@ -1012,7 +1012,7 @@ fn original_span(cx: &ExtCtxt) -> Gc { #[cfg(test)] mod test { - use super::*; + use super::{new_name_finder, expand_crate, contains_macro_escape}; use ast; use ast::{Attribute_, AttrOuter, MetaWord}; use attr; @@ -1032,11 +1032,11 @@ mod test { // from a given thingy and puts them in a mutable // array (passed in to the traversal) #[deriving(Clone)] - struct NewPathExprFinderContext { + struct PathExprFinderContext { path_accumulator: Vec , } - impl Visitor<()> for NewPathExprFinderContext { + impl Visitor<()> for PathExprFinderContext { fn visit_expr(&mut self, expr: &ast::Expr, _: ()) { match *expr { @@ -1052,8 +1052,8 @@ mod test { // return a visitor that extracts the paths // from a given thingy and puts them in a mutable // array (passed in to the traversal) - pub fn new_path_finder(paths: Vec ) -> NewPathExprFinderContext { - NewPathExprFinderContext { + fn new_path_finder(paths: Vec ) -> PathExprFinderContext { + PathExprFinderContext { path_accumulator: paths } } -- cgit 1.4.1-3-g733a5 From d2adb7cf81f565bdacd45190aac5e53cc2e04ea6 Mon Sep 17 00:00:00 2001 From: John Clements Date: Tue, 24 Jun 2014 17:30:53 -0700 Subject: remove misleading and unnecessary underscores --- src/libsyntax/ext/expand.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 2b325d1c787..5cdbbaf3e9a 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -156,7 +156,7 @@ pub fn expand_expr(e: Gc, fld: &mut MacroExpander) -> Gc { // (The use of the `let` is to give better error messages // when the pattern is refutable.) - let local_ident = token::gensym_ident("__i"); // FIXME #13573 + let local_ident = token::gensym_ident("i"); let next_ident = fld.cx.ident_of("next"); let none_ident = fld.cx.ident_of("None"); @@ -171,7 +171,8 @@ pub fn expand_expr(e: Gc, fld: &mut MacroExpander) -> Gc { }; // let = value; - let value_ident = token::gensym_ident("__value"); + // use underscore to suppress lint error: + let value_ident = token::gensym_ident("_value"); // this is careful to use src_pat.span so that error // messages point exact at that. let local = box(GC) ast::Local { -- cgit 1.4.1-3-g733a5 From 1e329bf050088281729a2ac8e83bd52f1776363e Mon Sep 17 00:00:00 2001 From: John Clements Date: Tue, 24 Jun 2014 17:58:56 -0700 Subject: don't expand subexprs of for loop, just re-expand whole thing. Fixes #15167 --- src/libsyntax/ext/expand.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 5cdbbaf3e9a..321c56d4bbf 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -132,8 +132,6 @@ pub fn expand_expr(e: Gc, fld: &mut MacroExpander) -> Gc { ast::ExprForLoop(src_pat, src_expr, src_loop_block, opt_ident) => { // Expand any interior macros etc. // NB: we don't fold pats yet. Curious. - let src_expr = fld.fold_expr(src_expr).clone(); - let (src_loop_block, opt_ident) = expand_loop_block(src_loop_block, opt_ident, fld); let span = e.span; @@ -143,7 +141,7 @@ pub fn expand_expr(e: Gc, fld: &mut MacroExpander) -> Gc { // i => { // [':] loop { // match i.next() { - // None => break, + // None => break ['], // Some(mut value) => { // let = value; // @@ -163,7 +161,7 @@ pub fn expand_expr(e: Gc, fld: &mut MacroExpander) -> Gc { let local_path = fld.cx.path_ident(span, local_ident); let some_path = fld.cx.path_ident(span, fld.cx.ident_of("Some")); - // `None => break ['];` + // `None => break ['],` let none_arm = { let break_expr = fld.cx.expr(span, ast::ExprBreak(opt_ident)); let none_pat = fld.cx.pat_ident(span, none_ident); @@ -222,7 +220,9 @@ pub fn expand_expr(e: Gc, fld: &mut MacroExpander) -> Gc { let discrim = fld.cx.expr_mut_addr_of(span, src_expr); let i_pattern = fld.cx.pat_ident(span, local_ident); let arm = fld.cx.arm(span, vec!(i_pattern), loop_expr); - fld.cx.expr_match(span, discrim, vec!(arm)) + // why these clone()'s everywhere? I guess I'll follow the pattern.... + let match_expr = fld.cx.expr_match(span, discrim, vec!(arm)); + fld.fold_expr(match_expr).clone() } ast::ExprLoop(loop_block, opt_ident) => { @@ -1248,6 +1248,7 @@ mod test { // FIXME #9384, match variable hygiene. Should expand into // fn z() {match 8 {x_1 => {match 9 {x_2 | x_2 => x_2 + x_1}}}} + #[ignore] #[test] fn issue_9384(){ run_renaming_test( &("macro_rules! bad_macro (($ex:expr) => ({match 9 {x | x => x + $ex}})) -- cgit 1.4.1-3-g733a5