Skip to content

Commit f85b6e3

Browse files
jrudermangraydon
authored andcommitted
---
yaml --- r: 3901 b: refs/heads/master c: 156458b h: refs/heads/master i: 3899: 2a8719a v: v3
1 parent aaab56d commit f85b6e3

File tree

2 files changed

+87
-38
lines changed

2 files changed

+87
-38
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 9036758191b0df7f80c1aaabaf149f67c35dd451
2+
refs/heads/master: 156458b3f6e170aaf57ca5ceab68ecb76dab7b6e

trunk/src/fuzzer/fuzzer.rs

Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@ import std::vec;
1212
import std::ivec;
1313
import std::str;
1414
import std::uint;
15+
import std::option;
1516

1617
import rustc::syntax::ast;
1718
import rustc::syntax::fold;
1819
import rustc::syntax::walk;
1920
import rustc::syntax::codemap;
21+
import rustc::syntax::parse::parser;
2022
import rustc::syntax::print::pprust;
2123

24+
/*
25+
// Imports for "the rest of driver::compile_input"
2226
import driver = rustc::driver::rustc; // see https://github.com/graydon/rust/issues/624
2327
import rustc::back::link;
2428
import rustc::driver::rustc::time;
2529
import rustc::driver::session;
2630
27-
/*
28-
// Imports for "the rest of driver::compile_input"
2931
import rustc::metadata::creader;
3032
import rustc::metadata::cstore;
3133
import rustc::syntax::parse::parser;
@@ -43,21 +45,21 @@ import rustc::util::ppaux;
4345
import rustc::lib::llvm;
4446
*/
4547

48+
fn read_whole_file(&str filename) -> str {
49+
str::unsafe_from_bytes(io::file_reader(filename).read_whole_stream())
50+
}
51+
4652
fn file_contains(&str filename, &str needle) -> bool {
47-
auto r = io::file_reader(filename);
48-
auto contents = str::unsafe_from_bytes(r.read_whole_stream());
53+
auto contents = read_whole_file(filename);
4954
ret str::find(contents, needle) != -1;
5055
}
5156

57+
fn contains(&str haystack, &str needle) -> bool { str::find(haystack, needle) != -1 }
58+
5259
fn find_rust_files(&mutable str[] files, str path) {
5360
if (str::ends_with(path, ".rs")) {
5461
if (file_contains(path, "xfail-stage1")) {
5562
//log_err "Skipping " + path + " because it is marked as xfail-stage1";
56-
} else if (
57-
!str::ends_with(path, "constrained-type.rs") && // https://github.com/graydon/rust/issues/653
58-
str::find(path, "utf8") != -1 && // https://github.com/graydon/rust/issues/654
59-
true) {
60-
//log_err "Skipping " + path + " because of a known bug";
6163
} else {
6264
files += ~[path];
6365
}
@@ -68,10 +70,43 @@ fn find_rust_files(&mutable str[] files, str path) {
6870
}
6971
}
7072

73+
fn safe_to_steal(ast::expr_ e) -> bool {
74+
alt (e) {
75+
// pretty-printer precedence issues -- https://github.com/graydon/rust/issues/670
76+
case (ast::expr_unary(_, _)) { false }
77+
case (ast::expr_lit(?lit)) {
78+
alt(lit.node) {
79+
case(ast::lit_str(_, _)) { true }
80+
case(ast::lit_char(_)) { true }
81+
case(ast::lit_int(_)) { false }
82+
case(ast::lit_uint(_)) { false }
83+
case(ast::lit_mach_int(_, _)) { false }
84+
case(ast::lit_float(_)) { false }
85+
case(ast::lit_mach_float(_, _)) { false }
86+
case(ast::lit_nil) { true }
87+
case(ast::lit_bool(_)) { true }
88+
}
89+
}
90+
case (ast::expr_cast(_, _)) { false }
91+
case (ast::expr_send(_, _)) { false }
92+
case (ast::expr_recv(_, _)) { false }
93+
case (ast::expr_assert(_)) { false }
94+
case (ast::expr_binary(_, _, _)) { false }
95+
case (ast::expr_assign(_, _)) { false }
96+
case (ast::expr_assign_op(_, _, _)) { false }
97+
98+
// "if (ret) { }" doesn't make sense, at least from a typecheck point of view, but for some reason it's rejected by the *parser*
99+
case (ast::expr_ret(option::none)) { false }
100+
case (ast::expr_put(option::none)) { false }
101+
102+
case (_) { true }
103+
}
104+
}
105+
71106
fn steal_exprs(&ast::crate crate) -> ast::expr[] {
72107
let @mutable ast::expr[] exprs = @mutable ~[];
73-
// "Stash" cannot be type-parameterized because of https://github.com/graydon/rust/issues/375
74-
fn stash_expr(@mutable ast::expr[] es, &@ast::expr e) { *es += ~[*e]; }
108+
// "Stash" is not type-parameterized because of the need for safe_to_steal
109+
fn stash_expr(@mutable ast::expr[] es, &@ast::expr e) { if (safe_to_steal(e.node)) { *es += ~[*e]; } else { /* now my indices are wrong :( */ } }
75110
auto v = rec(visit_expr_pre = bind stash_expr(exprs, _) with walk::default_visitor());
76111
walk::walk_crate(v, crate);
77112
*exprs
@@ -108,7 +143,9 @@ iter under(uint n) -> uint { let uint i = 0u; while (i < n) { put i; i += 1u; }
108143

109144
fn devnull() -> io::writer { std::io::string_writer().get_writer() }
110145

111-
fn pp_variants(&ast::crate crate, &session::session sess, &str filename) {
146+
fn as_str(fn (io::writer) f) -> str { auto w = std::io::string_writer(); f(w.get_writer()); w.get_str() }
147+
148+
fn pp_variants(&ast::crate crate, &codemap::codemap cmap, &str filename) {
112149
auto exprs = steal_exprs(crate);
113150
auto exprsL = ivec::len(exprs);
114151
if (exprsL < 100u) {
@@ -117,44 +154,56 @@ fn pp_variants(&ast::crate crate, &session::session sess, &str filename) {
117154
for each (uint j in under(uint::min(exprsL, 5u))) {
118155
log_err "With... " + pprust::expr_to_str(@exprs.(j));
119156
auto crate2 = @replace_expr_in_crate(crate, i, exprs.(j).node);
120-
pprust::print_crate(sess.get_codemap(), crate2, filename, devnull(), pprust::no_ann());
157+
check_roundtrip(crate2, cmap, filename + ".4.rs");
121158
}
122159
}
123160
}
124161
}
125162

163+
fn check_roundtrip(@ast::crate crate2, &codemap::codemap cmap, &str fakefilename) {
164+
auto str3 = as_str(bind pprust::print_crate(cmap, crate2, "empty.rs", _, pprust::no_ann()));
165+
auto cm4 = codemap::new_codemap();
166+
if (true
167+
&& !contains(str3, "][]") // https://github.com/graydon/rust/issues/669
168+
&& !contains(str3, "][mutable]") // https://github.com/graydon/rust/issues/669
169+
&& !contains(str3, "][mutable ]") // https://github.com/graydon/rust/issues/669
170+
&& !contains(str3, "self") // crazy rules enforced by parser rather than typechecker?
171+
&& !contains(str3, "spawn") // more precedence issues
172+
&& !contains(str3, "bind") // more precedence issues?
173+
) {
174+
auto crate4 = parser::parse_crate_from_source_str(fakefilename, str3, ~[], cm4);
175+
// should compare crates at this point, but it's easier to compare strings
176+
auto str5 = as_str(bind pprust::print_crate(cmap, crate4, "empty.rs", _, pprust::no_ann()));
177+
if (!str::is_ascii(str3)) {
178+
log_err "Non-ASCII in " + fakefilename; // why does non-ASCII work correctly with "rustc --pretty normal" but not here???
179+
} else if (str3 != str5) {
180+
log_err "Mismatch: " + fakefilename;
181+
log_err "str3:\n" + str3;
182+
log_err "str5:\n" + str5;
183+
fail "Mismatch";
184+
}
185+
}
186+
}
187+
126188
fn main(vec[str] args) {
127189
auto files = ~[];
128190
auto root = "/Users/jruderman/code/rust/src/"; // XXX
129191
find_rust_files(files, root); // not using time here because that currently screws with passing-a-mutable-array
130192
log_err uint::str(ivec::len(files)) + " files";
131193

132-
auto binary = vec::shift[str](args);
133-
auto binary_dir = fs::dirname(binary);
134-
135-
let @session::options sopts =
136-
@rec(library=false,
137-
static=false,
138-
optimize=0u,
139-
debuginfo=false,
140-
verify=true,
141-
run_typestate=true,
142-
save_temps=false,
143-
stats=false,
144-
time_passes=false,
145-
time_llvm_passes=false,
146-
output_type=link::output_type_bitcode,
147-
library_search_paths=[binary_dir + "/lib"],
148-
sysroot=driver::get_default_sysroot(binary),
149-
cfg=~[],
150-
test=false);
151-
152194
for (str file in files) {
153195
log_err "=== " + file + " ===";
154-
let session::session sess = driver::build_session(sopts);
155-
let @ast::crate crate = time(true, "parsing " + file, bind driver::parse_input(sess, ~[], file));
156-
pprust::print_crate(sess.get_codemap(), crate, file, devnull(), pprust::no_ann());
157-
pp_variants(*crate, sess, file);
196+
auto cm = codemap::new_codemap();
197+
auto src = read_whole_file(file);
198+
auto crate = parser::parse_crate_from_source_str(file, src, ~[], cm);
199+
if (!contains(src, "#macro") // https://github.com/graydon/rust/issues/671
200+
&& !str::ends_with(file, "block-expr-precedence.rs") // https://github.com/graydon/rust/issues/674
201+
&& !str::ends_with(file, "syntax-extension-fmt.rs") // an issue where -2147483648 gains an extra negative sign each time through, which i can't reproduce using "rustc --pretty normal"???
202+
) {
203+
check_roundtrip(crate, cm, file + ".pp.rs");
204+
}
205+
//pprust::print_crate(cm, crate, file, devnull(), pprust::no_ann());
206+
//pp_variants(*crate, cm, file);
158207
}
159208
}
160209

0 commit comments

Comments
 (0)