Skip to content

Commit 8b80922

Browse files
committed
quote_*! macros take an ExtCtx
They previously required one called "ext_cx" to be in scope. Fixes part of #7727
1 parent 0a23828 commit 8b80922

File tree

10 files changed

+256
-186
lines changed

10 files changed

+256
-186
lines changed

src/librustc/front/test.rs

+110
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ fn mk_std(cx: &TestCtxt) -> ast::view_item {
292292
}
293293
}
294294

295+
#[cfg(stage0)]
295296
fn mk_test_module(cx: &TestCtxt) -> @ast::item {
296297

297298
// Link to extra
@@ -334,6 +335,48 @@ fn mk_test_module(cx: &TestCtxt) -> @ast::item {
334335

335336
return @item;
336337
}
338+
#[cfg(not(stage0))]
339+
fn mk_test_module(cx: &TestCtxt) -> @ast::item {
340+
341+
// Link to extra
342+
let view_items = ~[mk_std(cx)];
343+
344+
// A constant vector of test descriptors.
345+
let tests = mk_tests(cx);
346+
347+
// The synthesized main function which will call the console test runner
348+
// with our list of tests
349+
let mainfn = (quote_item!(cx.ext_cx,
350+
pub fn main() {
351+
#[main];
352+
extra::test::test_main_static(::std::os::args(), TESTS);
353+
}
354+
)).unwrap();
355+
356+
let testmod = ast::_mod {
357+
view_items: view_items,
358+
items: ~[mainfn, tests],
359+
};
360+
let item_ = ast::item_mod(testmod);
361+
362+
// This attribute tells resolve to let us call unexported functions
363+
let resolve_unexported_attr =
364+
attr::mk_attr(attr::mk_word_item(@"!resolve_unexported"));
365+
366+
let item = ast::item {
367+
ident: cx.sess.ident_of("__test"),
368+
attrs: ~[resolve_unexported_attr],
369+
id: cx.sess.next_node_id(),
370+
node: item_,
371+
vis: ast::public,
372+
span: dummy_sp(),
373+
};
374+
375+
debug!("Synthetic test module:\n%s\n",
376+
pprust::item_to_str(@item.clone(), cx.sess.intr()));
377+
378+
return @item;
379+
}
337380

338381
fn nospan<T>(t: T) -> codemap::spanned<T> {
339382
codemap::spanned { node: t, span: dummy_sp() }
@@ -355,6 +398,7 @@ fn path_node_global(ids: ~[ast::ident]) -> ast::Path {
355398
types: ~[] }
356399
}
357400

401+
#[cfg(stage0)]
358402
fn mk_tests(cx: &TestCtxt) -> @ast::item {
359403

360404
let ext_cx = cx.ext_cx;
@@ -368,6 +412,17 @@ fn mk_tests(cx: &TestCtxt) -> @ast::item {
368412
;
369413
)).unwrap()
370414
}
415+
#[cfg(not(stage0))]
416+
fn mk_tests(cx: &TestCtxt) -> @ast::item {
417+
// The vector of test_descs for this crate
418+
let test_descs = mk_test_descs(cx);
419+
420+
(quote_item!(cx.ext_cx,
421+
pub static TESTS : &'static [self::extra::test::TestDescAndFn] =
422+
$test_descs
423+
;
424+
)).unwrap()
425+
}
371426

372427
fn is_extra(cx: &TestCtxt) -> bool {
373428
let items = attr::find_linkage_metas(cx.crate.attrs);
@@ -398,6 +453,7 @@ fn mk_test_descs(cx: &TestCtxt) -> @ast::expr {
398453
}
399454
}
400455

456+
#[cfg(stage0)]
401457
fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> @ast::expr {
402458
let span = test.span;
403459
let path = test.path.clone();
@@ -453,3 +509,57 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> @ast::expr {
453509
);
454510
e
455511
}
512+
#[cfg(not(stage0))]
513+
fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> @ast::expr {
514+
let span = test.span;
515+
let path = test.path.clone();
516+
517+
debug!("encoding %s", ast_util::path_name_i(path));
518+
519+
let name_lit: ast::lit =
520+
nospan(ast::lit_str(ast_util::path_name_i(path).to_managed()));
521+
522+
let name_expr = @ast::expr {
523+
id: cx.sess.next_node_id(),
524+
node: ast::expr_lit(@name_lit),
525+
span: span
526+
};
527+
528+
let fn_path = path_node_global(path);
529+
530+
let fn_expr = @ast::expr {
531+
id: cx.sess.next_node_id(),
532+
node: ast::expr_path(fn_path),
533+
span: span,
534+
};
535+
536+
let t_expr = if test.bench {
537+
quote_expr!(cx.ext_cx, self::extra::test::StaticBenchFn($fn_expr) )
538+
} else {
539+
quote_expr!(cx.ext_cx, self::extra::test::StaticTestFn($fn_expr) )
540+
};
541+
542+
let ignore_expr = if test.ignore {
543+
quote_expr!(cx.ext_cx, true )
544+
} else {
545+
quote_expr!(cx.ext_cx, false )
546+
};
547+
548+
let fail_expr = if test.should_fail {
549+
quote_expr!(cx.ext_cx, true )
550+
} else {
551+
quote_expr!(cx.ext_cx, false )
552+
};
553+
554+
let e = quote_expr!(cx.ext_cx,
555+
self::extra::test::TestDescAndFn {
556+
desc: self::extra::test::TestDesc {
557+
name: self::extra::test::StaticTestName($name_expr),
558+
ignore: $ignore_expr,
559+
should_fail: $fail_expr
560+
},
561+
testfn: $t_expr,
562+
}
563+
);
564+
e
565+
}

src/librustc/middle/astencode.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1288,24 +1288,24 @@ fn roundtrip(in_item: Option<@ast::item>) {
12881288

12891289
#[test]
12901290
fn test_basic() {
1291-
let ext_cx = mk_ctxt();
1292-
roundtrip(quote_item!(
1291+
let cx = mk_ctxt();
1292+
roundtrip(quote_item!(cx,
12931293
fn foo() {}
12941294
));
12951295
}
12961296

12971297
#[test]
12981298
fn test_smalltalk() {
1299-
let ext_cx = mk_ctxt();
1300-
roundtrip(quote_item!(
1299+
let cx = mk_ctxt();
1300+
roundtrip(quote_item!(cx,
13011301
fn foo() -> int { 3 + 4 } // first smalltalk program ever executed.
13021302
));
13031303
}
13041304

13051305
#[test]
13061306
fn test_more() {
1307-
let ext_cx = mk_ctxt();
1308-
roundtrip(quote_item!(
1307+
let cx = mk_ctxt();
1308+
roundtrip(quote_item!(cx,
13091309
fn foo(x: uint, y: uint) -> uint {
13101310
let z = x + y;
13111311
return z;
@@ -1315,15 +1315,15 @@ fn test_more() {
13151315

13161316
#[test]
13171317
fn test_simplification() {
1318-
let ext_cx = mk_ctxt();
1319-
let item_in = ast::ii_item(quote_item!(
1318+
let cx = mk_ctxt();
1319+
let item_in = ast::ii_item(quote_item!(cx,
13201320
fn new_int_alist<B>() -> alist<int, B> {
13211321
fn eq_int(a: int, b: int) -> bool { a == b }
13221322
return alist {eq_fn: eq_int, data: ~[]};
13231323
}
13241324
).unwrap());
13251325
let item_out = simplify_ast(&item_in);
1326-
let item_exp = ast::ii_item(quote_item!(
1326+
let item_exp = ast::ii_item(quote_item!(cx,
13271327
fn new_int_alist<B>() -> alist<int, B> {
13281328
return alist {eq_fn: eq_int, data: ~[]};
13291329
}

src/libsyntax/ext/build.rs

+12
Original file line numberDiff line numberDiff line change
@@ -605,17 +605,29 @@ impl AstBuilder for @ExtCtxt {
605605

606606
self.expr(span, ast::expr_fn_block(fn_decl, blk))
607607
}
608+
#[cfg(stage0)]
608609
fn lambda0(&self, _span: span, blk: ast::Block) -> @ast::expr {
609610
let ext_cx = *self;
610611
let blk_e = self.expr(blk.span, ast::expr_block(blk.clone()));
611612
quote_expr!(|| $blk_e )
612613
}
614+
#[cfg(not(stage0))]
615+
fn lambda0(&self, _span: span, blk: ast::Block) -> @ast::expr {
616+
let blk_e = self.expr(blk.span, ast::expr_block(blk.clone()));
617+
quote_expr!(*self, || $blk_e )
618+
}
613619

620+
#[cfg(stage0)]
614621
fn lambda1(&self, _span: span, blk: ast::Block, ident: ast::ident) -> @ast::expr {
615622
let ext_cx = *self;
616623
let blk_e = self.expr(blk.span, ast::expr_block(blk.clone()));
617624
quote_expr!(|$ident| $blk_e )
618625
}
626+
#[cfg(not(stage0))]
627+
fn lambda1(&self, _span: span, blk: ast::Block, ident: ast::ident) -> @ast::expr {
628+
let blk_e = self.expr(blk.span, ast::expr_block(blk.clone()));
629+
quote_expr!(*self, |$ident| $blk_e )
630+
}
619631

620632
fn lambda_expr(&self, span: span, ids: ~[ast::ident], expr: @ast::expr) -> @ast::expr {
621633
self.lambda(span, ids, self.block_expr(expr))

src/libsyntax/ext/env.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use ext::build::AstBuilder;
2222

2323
use std::os;
2424

25+
#[cfg(stage0)]
2526
pub fn expand_option_env(ext_cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
2627
-> base::MacResult {
2728
let var = get_single_str_from_tts(ext_cx, sp, tts, "option_env!");
@@ -32,25 +33,36 @@ pub fn expand_option_env(ext_cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
3233
};
3334
MRExpr(e)
3435
}
36+
#[cfg(not(stage0))]
37+
pub fn expand_option_env(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
38+
-> base::MacResult {
39+
let var = get_single_str_from_tts(cx, sp, tts, "option_env!");
40+
41+
let e = match os::getenv(var) {
42+
None => quote_expr!(cx, ::std::option::None::<&'static str>),
43+
Some(s) => quote_expr!(cx, ::std::option::Some($s))
44+
};
45+
MRExpr(e)
46+
}
3547

36-
pub fn expand_env(ext_cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
48+
pub fn expand_env(cx: @ExtCtxt, sp: span, tts: &[ast::token_tree])
3749
-> base::MacResult {
38-
let exprs = get_exprs_from_tts(ext_cx, sp, tts);
50+
let exprs = get_exprs_from_tts(cx, sp, tts);
3951

4052
if exprs.len() == 0 {
41-
ext_cx.span_fatal(sp, "env! takes 1 or 2 arguments");
53+
cx.span_fatal(sp, "env! takes 1 or 2 arguments");
4254
}
4355

44-
let var = expr_to_str(ext_cx, exprs[0], "expected string literal");
56+
let var = expr_to_str(cx, exprs[0], "expected string literal");
4557
let msg = match exprs.len() {
4658
1 => fmt!("Environment variable %s not defined", var).to_managed(),
47-
2 => expr_to_str(ext_cx, exprs[1], "expected string literal"),
48-
_ => ext_cx.span_fatal(sp, "env! takes 1 or 2 arguments")
59+
2 => expr_to_str(cx, exprs[1], "expected string literal"),
60+
_ => cx.span_fatal(sp, "env! takes 1 or 2 arguments")
4961
};
5062

5163
let e = match os::getenv(var) {
52-
None => ext_cx.span_fatal(sp, msg),
53-
Some(s) => ext_cx.expr_str(sp, s.to_managed())
64+
None => cx.span_fatal(sp, msg),
65+
Some(s) => cx.expr_str(sp, s.to_managed())
5466
};
5567
MRExpr(e)
5668
}

0 commit comments

Comments
 (0)