Skip to content

Commit a2cc3d6

Browse files
committed
Move {hir::lowering -> hir}::is_range_literal.
The function is never used inside lowering, but only ever in external crates. By moving it, we faciliate lowering as its own crate.
1 parent 26286c7 commit a2cc3d6

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

src/librustc/hir/lowering.rs

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,65 +3437,3 @@ fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body<'hir>>) -> Vec<hir::BodyId>
34373437
body_ids.sort_by_key(|b| bodies[b].value.span);
34383438
body_ids
34393439
}
3440-
3441-
/// Checks if the specified expression is a built-in range literal.
3442-
/// (See: `LoweringContext::lower_expr()`).
3443-
pub fn is_range_literal(sess: &Session, expr: &hir::Expr) -> bool {
3444-
use hir::{Path, QPath, ExprKind, TyKind};
3445-
3446-
// Returns whether the given path represents a (desugared) range,
3447-
// either in std or core, i.e. has either a `::std::ops::Range` or
3448-
// `::core::ops::Range` prefix.
3449-
fn is_range_path(path: &Path) -> bool {
3450-
let segs: Vec<_> = path.segments.iter().map(|seg| seg.ident.to_string()).collect();
3451-
let segs: Vec<_> = segs.iter().map(|seg| &**seg).collect();
3452-
3453-
// "{{root}}" is the equivalent of `::` prefix in `Path`.
3454-
if let ["{{root}}", std_core, "ops", range] = segs.as_slice() {
3455-
(*std_core == "std" || *std_core == "core") && range.starts_with("Range")
3456-
} else {
3457-
false
3458-
}
3459-
};
3460-
3461-
// Check whether a span corresponding to a range expression is a
3462-
// range literal, rather than an explicit struct or `new()` call.
3463-
fn is_lit(sess: &Session, span: &Span) -> bool {
3464-
let source_map = sess.source_map();
3465-
let end_point = source_map.end_point(*span);
3466-
3467-
if let Ok(end_string) = source_map.span_to_snippet(end_point) {
3468-
!(end_string.ends_with("}") || end_string.ends_with(")"))
3469-
} else {
3470-
false
3471-
}
3472-
};
3473-
3474-
match expr.kind {
3475-
// All built-in range literals but `..=` and `..` desugar to `Struct`s.
3476-
ExprKind::Struct(ref qpath, _, _) => {
3477-
if let QPath::Resolved(None, ref path) = **qpath {
3478-
return is_range_path(&path) && is_lit(sess, &expr.span);
3479-
}
3480-
}
3481-
3482-
// `..` desugars to its struct path.
3483-
ExprKind::Path(QPath::Resolved(None, ref path)) => {
3484-
return is_range_path(&path) && is_lit(sess, &expr.span);
3485-
}
3486-
3487-
// `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
3488-
ExprKind::Call(ref func, _) => {
3489-
if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.kind {
3490-
if let TyKind::Path(QPath::Resolved(None, ref path)) = ty.kind {
3491-
let new_call = segment.ident.name == sym::new;
3492-
return is_range_path(&path) && is_lit(sess, &expr.span) && new_call;
3493-
}
3494-
}
3495-
}
3496-
3497-
_ => {}
3498-
}
3499-
3500-
false
3501-
}

src/librustc/hir/mod.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,68 @@ impl fmt::Debug for Expr {
15951595
}
15961596
}
15971597

1598+
/// Checks if the specified expression is a built-in range literal.
1599+
/// (See: `LoweringContext::lower_expr()`).
1600+
pub fn is_range_literal(sess: &Session, expr: &hir::Expr) -> bool {
1601+
use hir::{Path, QPath, ExprKind, TyKind};
1602+
1603+
// Returns whether the given path represents a (desugared) range,
1604+
// either in std or core, i.e. has either a `::std::ops::Range` or
1605+
// `::core::ops::Range` prefix.
1606+
fn is_range_path(path: &Path) -> bool {
1607+
let segs: Vec<_> = path.segments.iter().map(|seg| seg.ident.to_string()).collect();
1608+
let segs: Vec<_> = segs.iter().map(|seg| &**seg).collect();
1609+
1610+
// "{{root}}" is the equivalent of `::` prefix in `Path`.
1611+
if let ["{{root}}", std_core, "ops", range] = segs.as_slice() {
1612+
(*std_core == "std" || *std_core == "core") && range.starts_with("Range")
1613+
} else {
1614+
false
1615+
}
1616+
};
1617+
1618+
// Check whether a span corresponding to a range expression is a
1619+
// range literal, rather than an explicit struct or `new()` call.
1620+
fn is_lit(sess: &Session, span: &Span) -> bool {
1621+
let source_map = sess.source_map();
1622+
let end_point = source_map.end_point(*span);
1623+
1624+
if let Ok(end_string) = source_map.span_to_snippet(end_point) {
1625+
!(end_string.ends_with("}") || end_string.ends_with(")"))
1626+
} else {
1627+
false
1628+
}
1629+
};
1630+
1631+
match expr.kind {
1632+
// All built-in range literals but `..=` and `..` desugar to `Struct`s.
1633+
ExprKind::Struct(ref qpath, _, _) => {
1634+
if let QPath::Resolved(None, ref path) = **qpath {
1635+
return is_range_path(&path) && is_lit(sess, &expr.span);
1636+
}
1637+
}
1638+
1639+
// `..` desugars to its struct path.
1640+
ExprKind::Path(QPath::Resolved(None, ref path)) => {
1641+
return is_range_path(&path) && is_lit(sess, &expr.span);
1642+
}
1643+
1644+
// `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
1645+
ExprKind::Call(ref func, _) => {
1646+
if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.kind {
1647+
if let TyKind::Path(QPath::Resolved(None, ref path)) = ty.kind {
1648+
let new_call = segment.ident.name == sym::new;
1649+
return is_range_path(&path) && is_lit(sess, &expr.span) && new_call;
1650+
}
1651+
}
1652+
}
1653+
1654+
_ => {}
1655+
}
1656+
1657+
false
1658+
}
1659+
15981660
#[derive(RustcEncodable, RustcDecodable, Debug, HashStable)]
15991661
pub enum ExprKind {
16001662
/// A `box x` expression.

0 commit comments

Comments
 (0)