Skip to content

Test eof is reached when parsing format strings #8944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/libstd/fmt/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Piece<'self>> for Parser<'self> {
Expand All @@ -168,6 +169,11 @@ impl<'self> iterator::Iterator<Piece<'self>> 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)))
Expand All @@ -182,6 +188,7 @@ impl<'self> Parser<'self> {
Parser {
input: s,
cur: s.char_offset_iter(),
depth: 0,
}
}

Expand Down Expand Up @@ -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 `}`");
}
Expand Down Expand Up @@ -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 `}`");
}
Expand Down
3 changes: 3 additions & 0 deletions src/test/compile-fail/ifmt-bad-arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}