Skip to content

Commit 163d1cd

Browse files
committed
---
yaml --- r: 124854 b: refs/heads/master c: 32f4d99 h: refs/heads/master v: v3
1 parent 19047c5 commit 163d1cd

File tree

7 files changed

+290
-312
lines changed

7 files changed

+290
-312
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: febfb752d2775bb1e875634e45058e27a36dd714
2+
refs/heads/master: 32f4d996ea2bea69fe363dca4e6e09b91bbd83f3
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 9fc8394d3bce22ab483f98842434c84c396212ae
55
refs/heads/try: ac70b438a8382389c9c9b59c66b14774bff46e10

trunk/src/libcore/iter.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1962,7 +1962,19 @@ pub struct Range<A> {
19621962
one: A
19631963
}
19641964

1965-
/// Return an iterator over the range [start, stop)
1965+
/// Returns an iterator over the given range [start, stop) (that is, starting
1966+
/// at start (inclusive), and ending at stop (exclusive)).
1967+
///
1968+
/// # Example
1969+
///
1970+
/// ```rust
1971+
/// let array = [0, 1, 2, 3, 4];
1972+
///
1973+
/// for i in range(0, 5u) {
1974+
/// println!("{}", i);
1975+
/// assert_eq!(i, array[i]);
1976+
/// }
1977+
/// ```
19661978
#[inline]
19671979
pub fn range<A: Add<A, A> + PartialOrd + Clone + One>(start: A, stop: A) -> Range<A> {
19681980
Range{state: start, stop: stop, one: One::one()}

trunk/src/libregex_macros/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use syntax::ext::build::AstBuilder;
3434
use syntax::ext::base::{ExtCtxt, MacResult, MacExpr, DummyResult};
3535
use syntax::parse::token;
3636
use syntax::print::pprust;
37+
use syntax::fold::Folder;
3738

3839
use rustc::plugin::Registry;
3940

@@ -615,7 +616,7 @@ fn exec<'t>(which: ::regex::native::MatchKind, input: &'t str,
615616
/// Otherwise, logs an error with cx.span_err and returns None.
616617
fn parse(cx: &mut ExtCtxt, tts: &[ast::TokenTree]) -> Option<String> {
617618
let mut parser = cx.new_parser_from_tts(tts);
618-
let entry = cx.expand_expr(parser.parse_expr());
619+
let entry = cx.expander().fold_expr(parser.parse_expr());
619620
let regex = match entry.node {
620621
ast::ExprLit(lit) => {
621622
match lit.node {

trunk/src/librustc/middle/trans/_match.rs

Lines changed: 22 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ use middle::resolve::DefMap;
202202
use middle::trans::adt;
203203
use middle::trans::base::*;
204204
use middle::trans::build::*;
205+
use middle::trans::build;
205206
use middle::trans::callee;
206207
use middle::trans::cleanup;
207208
use middle::trans::cleanup::CleanupMethods;
208209
use middle::trans::common::*;
209210
use middle::trans::consts;
210-
use middle::trans::controlflow;
211211
use middle::trans::datum::*;
212212
use middle::trans::expr::Dest;
213213
use middle::trans::expr;
@@ -220,14 +220,12 @@ use util::ppaux::{Repr, vec_map_to_string};
220220

221221
use std;
222222
use std::collections::HashMap;
223-
use std::cell::Cell;
224223
use std::rc::Rc;
225224
use std::gc::{Gc};
226225
use syntax::ast;
227226
use syntax::ast::Ident;
228227
use syntax::codemap::Span;
229228
use syntax::fold::Folder;
230-
use syntax::parse::token::InternedString;
231229

232230
#[deriving(PartialEq)]
233231
pub enum VecLenOpt {
@@ -301,20 +299,6 @@ fn trans_opt<'a>(mut bcx: &'a Block<'a>, o: &Opt) -> opt_result<'a> {
301299
}
302300
}
303301

304-
fn variant_opt(bcx: &Block, pat_id: ast::NodeId) -> Opt {
305-
let ccx = bcx.ccx();
306-
let def = ccx.tcx.def_map.borrow().get_copy(&pat_id);
307-
match def {
308-
def::DefVariant(enum_id, var_id, _) => {
309-
let variant = ty::enum_variant_with_id(ccx.tcx(), enum_id, var_id);
310-
var(variant.disr_val, adt::represent_node(bcx, pat_id), var_id)
311-
}
312-
_ => {
313-
ccx.sess().bug("non-variant or struct in variant_opt()");
314-
}
315-
}
316-
}
317-
318302
#[deriving(Clone)]
319303
pub enum TransBindingMode {
320304
TrByCopy(/* llbinding */ ValueRef),
@@ -630,26 +614,15 @@ fn get_options(bcx: &Block, m: &[Match], col: uint) -> Vec<Opt> {
630614
ast::PatLit(l) => {
631615
add_to_set(ccx.tcx(), &mut found, lit(l));
632616
}
633-
ast::PatIdent(..) => {
617+
ast::PatIdent(..) | ast::PatEnum(..) | ast::PatStruct(..) => {
634618
// This is either an enum variant or a variable binding.
635619
let opt_def = ccx.tcx.def_map.borrow().find_copy(&cur.id);
636620
match opt_def {
637-
Some(def::DefVariant(..)) => {
638-
add_to_set(ccx.tcx(), &mut found,
639-
variant_opt(bcx, cur.id));
640-
}
641-
_ => {}
642-
}
643-
}
644-
ast::PatEnum(..) | ast::PatStruct(..) => {
645-
// This could be one of: a tuple-like enum variant, a
646-
// struct-like enum variant, or a struct.
647-
let opt_def = ccx.tcx.def_map.borrow().find_copy(&cur.id);
648-
match opt_def {
649-
Some(def::DefFn(..)) |
650-
Some(def::DefVariant(..)) => {
621+
Some(def::DefVariant(enum_id, var_id, _)) => {
622+
let variant = ty::enum_variant_with_id(ccx.tcx(), enum_id, var_id);
651623
add_to_set(ccx.tcx(), &mut found,
652-
variant_opt(bcx, cur.id));
624+
var(variant.disr_val,
625+
adt::represent_node(bcx, cur.id), var_id));
653626
}
654627
_ => {}
655628
}
@@ -795,56 +768,33 @@ fn any_irrefutable_adt_pat(bcx: &Block, m: &[Match], col: uint) -> bool {
795768
})
796769
}
797770

798-
struct DynamicFailureHandler<'a> {
799-
bcx: &'a Block<'a>,
800-
sp: Span,
801-
msg: InternedString,
802-
finished: Cell<Option<BasicBlockRef>>,
803-
}
804-
805-
impl<'a> DynamicFailureHandler<'a> {
806-
fn handle_fail(&self) -> BasicBlockRef {
807-
match self.finished.get() {
808-
Some(bb) => return bb,
809-
_ => (),
810-
}
811-
812-
let fcx = self.bcx.fcx;
813-
let fail_cx = fcx.new_block(false, "case_fallthrough", None);
814-
controlflow::trans_fail(fail_cx, self.sp, self.msg.clone());
815-
self.finished.set(Some(fail_cx.llbb));
816-
fail_cx.llbb
817-
}
818-
}
819-
820771
/// What to do when the pattern match fails.
821772
enum FailureHandler<'a> {
822773
Infallible,
823774
JumpToBasicBlock(BasicBlockRef),
824-
DynamicFailureHandlerClass(Box<DynamicFailureHandler<'a>>),
775+
Unreachable
825776
}
826777

827778
impl<'a> FailureHandler<'a> {
828779
fn is_infallible(&self) -> bool {
829780
match *self {
830781
Infallible => true,
831-
_ => false,
782+
_ => false
832783
}
833784
}
834785

835786
fn is_fallible(&self) -> bool {
836787
!self.is_infallible()
837788
}
838789

839-
fn handle_fail(&self) -> BasicBlockRef {
790+
fn handle_fail(&self, bcx: &Block) {
840791
match *self {
841-
Infallible => {
842-
fail!("attempted to fail in infallible failure handler!")
843-
}
844-
JumpToBasicBlock(basic_block) => basic_block,
845-
DynamicFailureHandlerClass(ref dynamic_failure_handler) => {
846-
dynamic_failure_handler.handle_fail()
847-
}
792+
Infallible =>
793+
fail!("attempted to fail in infallible failure handler!"),
794+
JumpToBasicBlock(basic_block) =>
795+
Br(bcx, basic_block),
796+
Unreachable =>
797+
build::Unreachable(bcx)
848798
}
849799
}
850800
}
@@ -1005,7 +955,7 @@ fn compile_guard<'a, 'b>(
1005955
// condition explicitly rather than (possibly) falling back to
1006956
// the default arm.
1007957
&JumpToBasicBlock(_) if m.len() == 1 && has_genuine_default => {
1008-
Br(bcx, chk.handle_fail());
958+
chk.handle_fail(bcx);
1009959
}
1010960
_ => {
1011961
compile_submatch(bcx, m, vals, chk, has_genuine_default);
@@ -1030,7 +980,7 @@ fn compile_submatch<'a, 'b>(
1030980
let mut bcx = bcx;
1031981
if m.len() == 0u {
1032982
if chk.is_fallible() {
1033-
Br(bcx, chk.handle_fail());
983+
chk.handle_fail(bcx);
1034984
}
1035985
return;
1036986
}
@@ -1301,7 +1251,7 @@ fn compile_submatch_continue<'a, 'b>(
13011251
// condition explicitly rather than (eventually) falling back to
13021252
// the last default arm.
13031253
&JumpToBasicBlock(_) if defaults.len() == 1 && has_genuine_default => {
1304-
Br(else_cx, chk.handle_fail());
1254+
chk.handle_fail(else_cx);
13051255
}
13061256
_ => {
13071257
compile_submatch(else_cx,
@@ -1395,21 +1345,10 @@ fn trans_match_inner<'a>(scope_cx: &'a Block<'a>,
13951345
}
13961346

13971347
let t = node_id_type(bcx, discr_expr.id);
1398-
let chk = {
1399-
if ty::type_is_empty(tcx, t) {
1400-
// Special case for empty types
1401-
let fail_cx = Cell::new(None);
1402-
let fail_handler = box DynamicFailureHandler {
1403-
bcx: scope_cx,
1404-
sp: discr_expr.span,
1405-
msg: InternedString::new("scrutinizing value that can't \
1406-
exist"),
1407-
finished: fail_cx,
1408-
};
1409-
DynamicFailureHandlerClass(fail_handler)
1410-
} else {
1411-
Infallible
1412-
}
1348+
let chk = if ty::type_is_empty(tcx, t) {
1349+
Unreachable
1350+
} else {
1351+
Infallible
14131352
};
14141353

14151354
let arm_datas: Vec<ArmData> = arms.iter().map(|arm| ArmData {

trunk/src/libsyntax/ext/base.rs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ use parse::token;
2020
use parse::token::{InternedString, intern, str_to_ident};
2121
use util::small_vector::SmallVector;
2222
use ext::mtwt;
23+
use fold::Folder;
2324

2425
use std::collections::HashMap;
2526
use std::gc::{Gc, GC};
27+
use std::rc::Rc;
2628

2729
// new-style macro! tt code:
2830
//
@@ -104,9 +106,9 @@ pub type IdentMacroExpanderFn =
104106
/// just into the compiler's internal macro table, for `make_def`).
105107
pub trait MacResult {
106108
/// Define a new macro.
107-
// this particular flavor should go away; the idea that a macro might
108-
// expand into either a macro definition or an expression, depending
109-
// on what the context wants, is kind of silly.
109+
// this should go away; the idea that a macro might expand into
110+
// either a macro definition or an expression, depending on what
111+
// the context wants, is kind of silly.
110112
fn make_def(&self) -> Option<MacroDef> {
111113
None
112114
}
@@ -314,7 +316,7 @@ impl BlockInfo {
314316

315317
/// The base map of methods for expanding syntax extension
316318
/// AST nodes into full ASTs
317-
pub fn syntax_expander_table() -> SyntaxEnv {
319+
fn initial_syntax_expander_table() -> SyntaxEnv {
318320
// utility function to simplify creating NormalTT syntax extensions
319321
fn builtin_normal_expander(f: MacroExpanderFn) -> SyntaxExtension {
320322
NormalTT(box BasicMacroExpander {
@@ -431,7 +433,9 @@ pub struct ExtCtxt<'a> {
431433

432434
pub mod_path: Vec<ast::Ident> ,
433435
pub trace_mac: bool,
434-
pub exported_macros: Vec<Gc<ast::Item>>
436+
pub exported_macros: Vec<Gc<ast::Item>>,
437+
438+
pub syntax_env: SyntaxEnv,
435439
}
436440

437441
impl<'a> ExtCtxt<'a> {
@@ -445,22 +449,18 @@ impl<'a> ExtCtxt<'a> {
445449
ecfg: ecfg,
446450
trace_mac: false,
447451
exported_macros: Vec::new(),
452+
syntax_env: initial_syntax_expander_table(),
448453
}
449454
}
450455

451-
pub fn expand_expr(&mut self, mut e: Gc<ast::Expr>) -> Gc<ast::Expr> {
452-
loop {
453-
match e.node {
454-
ast::ExprMac(..) => {
455-
let mut expander = expand::MacroExpander {
456-
extsbox: syntax_expander_table(),
457-
cx: self,
458-
};
459-
e = expand::expand_expr(e, &mut expander);
460-
}
461-
_ => return e
462-
}
463-
}
456+
#[deprecated = "Replaced with `expander().fold_expr()`"]
457+
pub fn expand_expr(&mut self, e: Gc<ast::Expr>) -> Gc<ast::Expr> {
458+
self.expander().fold_expr(e)
459+
}
460+
461+
/// Returns a `Folder` for deeply expanding all macros in a AST node.
462+
pub fn expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> {
463+
expand::MacroExpander { cx: self }
464464
}
465465

466466
pub fn new_parser_from_tts(&self, tts: &[ast::TokenTree])
@@ -570,7 +570,7 @@ impl<'a> ExtCtxt<'a> {
570570
pub fn expr_to_string(cx: &mut ExtCtxt, expr: Gc<ast::Expr>, err_msg: &str)
571571
-> Option<(InternedString, ast::StrStyle)> {
572572
// we want to be able to handle e.g. concat("foo", "bar")
573-
let expr = cx.expand_expr(expr);
573+
let expr = cx.expander().fold_expr(expr);
574574
match expr.node {
575575
ast::ExprLit(l) => match l.node {
576576
ast::LitStr(ref s, style) => return Some(((*s).clone(), style)),
@@ -627,7 +627,7 @@ pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
627627
let mut p = cx.new_parser_from_tts(tts);
628628
let mut es = Vec::new();
629629
while p.token != token::EOF {
630-
es.push(cx.expand_expr(p.parse_expr()));
630+
es.push(cx.expander().fold_expr(p.parse_expr()));
631631
if p.eat(&token::COMMA) {
632632
continue;
633633
}
@@ -642,10 +642,13 @@ pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
642642
/// In order to have some notion of scoping for macros,
643643
/// we want to implement the notion of a transformation
644644
/// environment.
645-
645+
///
646646
/// This environment maps Names to SyntaxExtensions.
647+
pub struct SyntaxEnv {
648+
chain: Vec<MapChainFrame> ,
649+
}
647650

648-
//impl question: how to implement it? Initially, the
651+
// impl question: how to implement it? Initially, the
649652
// env will contain only macros, so it might be painful
650653
// to add an empty frame for every context. Let's just
651654
// get it working, first....
@@ -657,15 +660,11 @@ pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
657660

658661
struct MapChainFrame {
659662
info: BlockInfo,
660-
map: HashMap<Name, SyntaxExtension>,
661-
}
662-
663-
pub struct SyntaxEnv {
664-
chain: Vec<MapChainFrame> ,
663+
map: HashMap<Name, Rc<SyntaxExtension>>,
665664
}
666665

667666
impl SyntaxEnv {
668-
pub fn new() -> SyntaxEnv {
667+
fn new() -> SyntaxEnv {
669668
let mut map = SyntaxEnv { chain: Vec::new() };
670669
map.push_frame();
671670
map
@@ -692,18 +691,18 @@ impl SyntaxEnv {
692691
unreachable!()
693692
}
694693

695-
pub fn find<'a>(&'a self, k: &Name) -> Option<&'a SyntaxExtension> {
694+
pub fn find(&self, k: &Name) -> Option<Rc<SyntaxExtension>> {
696695
for frame in self.chain.iter().rev() {
697696
match frame.map.find(k) {
698-
Some(v) => return Some(v),
697+
Some(v) => return Some(v.clone()),
699698
None => {}
700699
}
701700
}
702701
None
703702
}
704703

705704
pub fn insert(&mut self, k: Name, v: SyntaxExtension) {
706-
self.find_escape_frame().map.insert(k, v);
705+
self.find_escape_frame().map.insert(k, Rc::new(v));
707706
}
708707

709708
pub fn info<'a>(&'a mut self) -> &'a mut BlockInfo {

0 commit comments

Comments
 (0)