Skip to content

Commit d6d7134

Browse files
kevinanikomatsakis
authored andcommitted
When parsing a source string, fail when the entire string is not parsed.
(For now only fail when parse_from_source_str is used to avoid possible compatibility problems; parse_expr_from_source_str still does not check.)
1 parent a803a14 commit d6d7134

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/comp/syntax/parse/parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,6 +2633,9 @@ fn parse_from_source_str<T>(f: fn (p: parser) -> T,
26332633
{
26342634
let p = new_parser_from_source_str(sess, cfg, name, ss, source);
26352635
let r = f(p);
2636+
if !p.reader.is_eof() {
2637+
p.reader.fatal("expected end-of-string");
2638+
}
26362639
sess.chpos = p.reader.chpos;
26372640
sess.byte_pos = sess.byte_pos + p.reader.pos;
26382641
ret r;
File renamed without changes.

src/test/compile-fail/qquote-2.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// xfail-pretty
2+
3+
use std;
4+
use rustc;
5+
6+
import rustc::*;
7+
import std::io::*;
8+
9+
import rustc::driver::diagnostic;
10+
import rustc::syntax::ast;
11+
import rustc::syntax::codemap;
12+
import rustc::syntax::parse::parser;
13+
import rustc::syntax::print::*;
14+
15+
fn new_parse_sess() -> parser::parse_sess {
16+
fail;
17+
}
18+
19+
iface fake_ext_ctxt {
20+
fn session() -> fake_session;
21+
}
22+
23+
type fake_options = {cfg: ast::crate_cfg};
24+
25+
type fake_session = {opts: @fake_options,
26+
parse_sess: parser::parse_sess};
27+
28+
impl of fake_ext_ctxt for fake_session {
29+
fn session() -> fake_session {self}
30+
}
31+
32+
fn mk_ctxt() -> fake_ext_ctxt {
33+
let opts : fake_options = {cfg: []};
34+
{opts: @opts, parse_sess: new_parse_sess()} as fake_ext_ctxt
35+
}
36+
37+
38+
fn main() {
39+
let ext_cx = mk_ctxt();
40+
41+
let stmt = #ast(stmt){let x int = 20;}; //! ERROR expected end-of-string
42+
check_pp(*stmt, pprust::print_stmt, "");
43+
}
44+
45+
fn check_pp<T>(expr: T, f: fn(pprust::ps, T), expect: str) {
46+
fail;
47+
}
48+

0 commit comments

Comments
 (0)