diff --git a/src/libstd/fmt/parse.rs b/src/libstd/fmt/parse.rs index 4bfa6b5afce..245318c4699 100644 --- a/src/libstd/fmt/parse.rs +++ b/src/libstd/fmt/parse.rs @@ -149,6 +149,7 @@ pub struct SelectArm<'self> { pub struct Parser<'self> { priv input: &'self str, priv cur: str::CharOffsetIterator<'self>, + priv depth: uint, } impl<'self> iterator::Iterator> for Parser<'self> { @@ -168,6 +169,11 @@ impl<'self> iterator::Iterator> for Parser<'self> { self.escape(); // ensure it's a valid escape sequence Some(String(self.string(pos + 1))) // skip the '\' character } + Some((_, '}')) if self.depth == 0 => { + self.cur.next(); + self.err(~"unmatched `}` found"); + None + } Some((_, '}')) | None => { None } Some((pos, _)) => { Some(String(self.string(pos))) @@ -182,6 +188,7 @@ impl<'self> Parser<'self> { Parser { input: s, cur: s.char_offset_iter(), + depth: 0, } } @@ -393,7 +400,9 @@ impl<'self> Parser<'self> { if !self.wsconsume('{') { self.err(~"selector must be followed by `{`"); } + self.depth += 1; let pieces = self.collect(); + self.depth -= 1; if !self.wsconsume('}') { self.err(~"selector case must be terminated by `}`"); } @@ -494,7 +503,9 @@ impl<'self> Parser<'self> { if !self.wsconsume('{') { self.err(~"selector must be followed by `{`"); } + self.depth += 1; let pieces = self.collect(); + self.depth -= 1; if !self.wsconsume('}') { self.err(~"selector case must be terminated by `}`"); } diff --git a/src/test/compile-fail/ifmt-bad-arg.rs b/src/test/compile-fail/ifmt-bad-arg.rs index 273394794a9..5da73a495f6 100644 --- a/src/test/compile-fail/ifmt-bad-arg.rs +++ b/src/test/compile-fail/ifmt-bad-arg.rs @@ -71,4 +71,7 @@ fn main() { format!("{0, select, other{{}}}", "a"); //~ ERROR: cannot use implicit format!("{0, plural, other{{}}}", 1); //~ ERROR: cannot use implicit format!("{0, plural, other{{1:.*d}}}", 1, 2); //~ ERROR: cannot use implicit + + format!("foo } bar"); //~ ERROR: unmatched `}` found + format!("foo }"); //~ ERROR: unmatched `}` found }