Skip to content

Commit 5032bc2

Browse files
committed
HIR: add inclusive ranges, desugar all ranges (remove ExprRange)
1 parent 3c50165 commit 5032bc2

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

src/librustc_front/hir.rs

-2
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,6 @@ pub enum Expr_ {
772772
ExprTupField(P<Expr>, Spanned<usize>),
773773
/// An indexing operation (`foo[2]`)
774774
ExprIndex(P<Expr>, P<Expr>),
775-
/// A range (`1..2`, `1..`, or `..2`)
776-
ExprRange(Option<P<Expr>>, Option<P<Expr>>),
777775

778776
/// Variable reference, possibly containing `::` and/or type
779777
/// parameters, e.g. foo::bar::<baz>.

src/librustc_front/lowering.rs

+73-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use hir;
6565

6666
use std::collections::BTreeMap;
6767
use std::collections::HashMap;
68+
use std::iter;
6869
use syntax::ast::*;
6970
use syntax::attr::{ThinAttributes, ThinAttributesExt};
7071
use syntax::ext::mtwt;
@@ -1207,9 +1208,58 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
12071208
ExprIndex(ref el, ref er) => {
12081209
hir::ExprIndex(lower_expr(lctx, el), lower_expr(lctx, er))
12091210
}
1210-
ExprRange(ref e1, ref e2) => {
1211-
hir::ExprRange(e1.as_ref().map(|x| lower_expr(lctx, x)),
1212-
e2.as_ref().map(|x| lower_expr(lctx, x)))
1211+
ExprRange(ref e1, ref e2, lims) => {
1212+
fn make_struct(lctx: &LoweringContext, span: Span, path: &[&str], fields: Vec<(&str, &P<Expr>)>) -> P<hir::Expr> {
1213+
let strs = std_path(lctx, &iter::once(&"ops").chain(path).map(|&x| x).collect::<Vec<_>>());
1214+
1215+
let structpath = path_global(span, strs);
1216+
1217+
if fields.len() == 0 {
1218+
expr_path(lctx,
1219+
structpath,
1220+
None)
1221+
} else {
1222+
expr_struct(lctx,
1223+
span,
1224+
structpath,
1225+
fields.into_iter().map(|(s, e)| {
1226+
field(token::intern(s), lower_expr(lctx, &**e), span)
1227+
}).collect(),
1228+
None,
1229+
None)
1230+
}
1231+
}
1232+
1233+
return cache_ids(lctx, e.id, |lctx| {
1234+
match (e1, e2, lims) {
1235+
(&None, &None, RangeLimits::HalfOpen) => make_struct(lctx,
1236+
e.span,
1237+
&["RangeFull"],
1238+
vec![]),
1239+
(&None, &None, RangeLimits::Closed) => panic!("No such thing as RangeFullInclusive"),
1240+
(&None, &Some(ref e2), RangeLimits::HalfOpen) => make_struct(lctx,
1241+
e.span,
1242+
&["RangeTo"],
1243+
vec![("end", e2)]),
1244+
(&None, &Some(ref e2), RangeLimits::Closed) => make_struct(lctx,
1245+
e.span,
1246+
&["RangeToInclusive"],
1247+
vec![("end", e2)]),
1248+
(&Some(ref e1), &None, RangeLimits::HalfOpen) => make_struct(lctx,
1249+
e.span,
1250+
&["RangeFrom"],
1251+
vec![("start", e1)]),
1252+
(&Some(_), &None, RangeLimits::Closed) => panic!("No such thing as RangeFromInclusive"),
1253+
(&Some(ref e1), &Some(ref e2), RangeLimits::HalfOpen) => make_struct(lctx,
1254+
e.span,
1255+
&["Range"],
1256+
vec![("start", e1), ("end", e2)]),
1257+
(&Some(ref e1), &Some(ref e2), RangeLimits::Closed) => make_struct(lctx,
1258+
e.span,
1259+
&["RangeInclusive", "NonEmpty"],
1260+
vec![("start", e1), ("end", e2)]),
1261+
}
1262+
});
12131263
}
12141264
ExprPath(ref qself, ref path) => {
12151265
let hir_qself = qself.as_ref().map(|&QSelf { ref ty, position }| {
@@ -1626,6 +1676,17 @@ fn arm(pats: hir::HirVec<P<hir::Pat>>, expr: P<hir::Expr>) -> hir::Arm {
16261676
}
16271677
}
16281678

1679+
fn field(name: Name, expr: P<hir::Expr>, span: Span) -> hir::Field {
1680+
hir::Field {
1681+
name: Spanned {
1682+
node: name,
1683+
span: span,
1684+
},
1685+
span: span,
1686+
expr: expr,
1687+
}
1688+
}
1689+
16291690
fn expr_break(lctx: &LoweringContext, span: Span,
16301691
attrs: ThinAttributes) -> P<hir::Expr> {
16311692
expr(lctx, span, hir::ExprBreak(None), attrs)
@@ -1675,6 +1736,15 @@ fn expr_tuple(lctx: &LoweringContext, sp: Span, exprs: hir::HirVec<P<hir::Expr>>
16751736
expr(lctx, sp, hir::ExprTup(exprs), attrs)
16761737
}
16771738

1739+
fn expr_struct(lctx: &LoweringContext,
1740+
sp: Span,
1741+
path: hir::Path,
1742+
fields: hir::HirVec<hir::Field>,
1743+
e: Option<P<hir::Expr>>,
1744+
attrs: ThinAttributes) -> P<hir::Expr> {
1745+
expr(lctx, sp, hir::ExprStruct(path, fields, e), attrs)
1746+
}
1747+
16781748
fn expr(lctx: &LoweringContext, span: Span, node: hir::Expr_,
16791749
attrs: ThinAttributes) -> P<hir::Expr> {
16801750
P(hir::Expr {

0 commit comments

Comments
 (0)