Skip to content

Commit db0693a

Browse files
committed
libsyntax: Tighten up expressions in patterns to only allow identifiers or literals (possibly with a minus).
This had very minimal fallout.
1 parent 5d3559e commit db0693a

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

src/libcore/num/strconv.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,11 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Copy+Div<T,T>+
486486
}
487487
}
488488

489-
let (start, accum_positive) = match buf[0] {
490-
'-' as u8 if !negative => return None,
491-
'-' as u8 => (1u, false),
492-
'+' as u8 => (1u, true),
493-
_ => (0u, true)
489+
let (start, accum_positive) = match buf[0] as char {
490+
'-' if !negative => return None,
491+
'-' => (1u, false),
492+
'+' => (1u, true),
493+
_ => (0u, true)
494494
};
495495

496496
// Initialize accumulator with signed zero for floating point parsing to

src/libcore/unstable/extfmt.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ pub mod ct {
257257
let mut flags = ~[];
258258

259259
while i < lim {
260-
let f = match s[i] {
261-
'-' as u8 => FlagLeftJustify,
262-
'0' as u8 => FlagLeftZeroPad,
263-
' ' as u8 => FlagSpaceForSign,
264-
'+' as u8 => FlagSignAlways,
265-
'#' as u8 => FlagAlternate,
260+
let f = match s[i] as char {
261+
'-' => FlagLeftJustify,
262+
'0' => FlagLeftZeroPad,
263+
' ' => FlagSpaceForSign,
264+
'+' => FlagSignAlways,
265+
'#' => FlagAlternate,
266266
_ => break
267267
};
268268

@@ -313,18 +313,18 @@ pub mod ct {
313313

314314
// FIXME (#2249): Do we really want two signed types here?
315315
// How important is it to be printf compatible?
316-
let t = match s[i] {
317-
'b' as u8 => TyBool,
318-
's' as u8 => TyStr,
319-
'c' as u8 => TyChar,
320-
'd' as u8 | 'i' as u8 => TyInt(Signed),
321-
'u' as u8 => TyInt(Unsigned),
322-
'x' as u8 => TyHex(CaseLower),
323-
'X' as u8 => TyHex(CaseUpper),
324-
't' as u8 => TyBits,
325-
'o' as u8 => TyOctal,
326-
'f' as u8 => TyFloat,
327-
'?' as u8 => TyPoly,
316+
let t = match s[i] as char {
317+
'b' => TyBool,
318+
's' => TyStr,
319+
'c' => TyChar,
320+
'd' | 'i' => TyInt(Signed),
321+
'u' => TyInt(Unsigned),
322+
'x' => TyHex(CaseLower),
323+
'X' => TyHex(CaseUpper),
324+
't' => TyBits,
325+
'o' => TyOctal,
326+
'f' => TyFloat,
327+
'?' => TyPoly,
328328
_ => err(~"unknown type in conversion: " + s.substr(i, 1))
329329
};
330330

src/libsyntax/parse/parser.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,24 @@ pub impl Parser {
915915
codemap::spanned { node: lit, span: mk_sp(lo, self.last_span.hi) }
916916
}
917917

918+
// matches '-' lit | lit
919+
fn parse_literal_maybe_minus(&self) -> @expr {
920+
let minus_lo = self.span.lo;
921+
let minus_present = self.eat(&token::BINOP(token::MINUS));
922+
923+
let lo = self.span.lo;
924+
let literal = @self.parse_lit();
925+
let hi = self.span.hi;
926+
let expr = self.mk_expr(lo, hi, expr_lit(literal));
927+
928+
if minus_present {
929+
let minus_hi = self.span.hi;
930+
self.mk_expr(minus_lo, minus_hi, expr_unary(neg, expr))
931+
} else {
932+
expr
933+
}
934+
}
935+
918936
// parse a path into a vector of idents, whether the path starts
919937
// with ::, and a span.
920938
fn parse_path(&self) -> (~[ast::ident],bool,span) {
@@ -2360,10 +2378,19 @@ pub impl Parser {
23602378
|| self.is_keyword(&~"true")
23612379
|| self.is_keyword(&~"false")
23622380
{
2363-
// parse an expression pattern or exp .. exp
2364-
let val = self.parse_expr_res(RESTRICT_NO_BAR_OP);
2381+
// Parse an expression pattern or exp .. exp.
2382+
//
2383+
// These expressions are limited to literals (possibly
2384+
// preceded by unary-minus) or identifiers.
2385+
let val = self.parse_literal_maybe_minus();
23652386
if self.eat(&token::DOTDOT) {
2366-
let end = self.parse_expr_res(RESTRICT_NO_BAR_OP);
2387+
let end = if is_ident_or_path(&tok) {
2388+
let path = self.parse_path_with_tps(true);
2389+
let hi = self.span.hi;
2390+
self.mk_expr(lo, hi, expr_path(path))
2391+
} else {
2392+
self.parse_literal_maybe_minus()
2393+
};
23672394
pat = pat_range(val, end);
23682395
} else {
23692396
pat = pat_lit(val);

0 commit comments

Comments
 (0)