Skip to content

Commit 98bd4a9

Browse files
0c0w3brson
authored andcommitted
Improve unexpected error scanner for compile-fail tests (Closes #1476)
1 parent dc11e87 commit 98bd4a9

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

src/compiletest/runtest.rs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ fn check_expected_errors(expected_errors: ~[errors::expected_error],
289289
was_expected = true;
290290
}
291291

292-
if !was_expected && (str::contains(line, ~"error") ||
293-
str::contains(line, ~"warning")) {
294-
fatal_procres(fmt!("unexpected error pattern '%s'!", line),
292+
if !was_expected && is_compiler_error_or_warning(line) {
293+
fatal_procres(fmt!("unexpected compiler error or warning: '%s'",
294+
line),
295295
procres);
296296
}
297297
}
@@ -305,6 +305,81 @@ fn check_expected_errors(expected_errors: ~[errors::expected_error],
305305
}
306306
}
307307

308+
fn is_compiler_error_or_warning(line: ~str) -> bool {
309+
let mut i = 0u;
310+
return
311+
scan_until_char(line, ':', &mut i) &&
312+
scan_char(line, ':', &mut i) &&
313+
scan_integer(line, &mut i) &&
314+
scan_char(line, ':', &mut i) &&
315+
scan_integer(line, &mut i) &&
316+
scan_char(line, ':', &mut i) &&
317+
scan_char(line, ' ', &mut i) &&
318+
scan_integer(line, &mut i) &&
319+
scan_char(line, ':', &mut i) &&
320+
scan_integer(line, &mut i) &&
321+
scan_char(line, ' ', &mut i) &&
322+
(scan_string(line, ~"error", &mut i) ||
323+
scan_string(line, ~"warning", &mut i));
324+
}
325+
326+
fn scan_until_char(haystack: ~str, needle: char, idx: &mut uint) -> bool {
327+
if *idx >= haystack.len() {
328+
return false;
329+
}
330+
let opt = str::find_char_from(haystack, needle, *idx);
331+
if opt.is_none() {
332+
return false;
333+
}
334+
*idx = opt.get();
335+
return true;
336+
}
337+
338+
fn scan_char(haystack: ~str, needle: char, idx: &mut uint) -> bool {
339+
if *idx >= haystack.len() {
340+
return false;
341+
}
342+
let {ch, next} = str::char_range_at(haystack, *idx);
343+
if ch != needle {
344+
return false;
345+
}
346+
*idx = next;
347+
return true;
348+
}
349+
350+
fn scan_integer(haystack: ~str, idx: &mut uint) -> bool {
351+
let mut i = *idx;
352+
while i < haystack.len() {
353+
let {ch, next} = str::char_range_at(haystack, i);
354+
if ch < '0' || '9' < ch {
355+
break;
356+
}
357+
i = next;
358+
}
359+
if i == *idx {
360+
return false;
361+
}
362+
*idx = i;
363+
return true;
364+
}
365+
366+
fn scan_string(haystack: ~str, needle: ~str, idx: &mut uint) -> bool {
367+
let mut haystack_i = *idx;
368+
let mut needle_i = 0u;
369+
while needle_i < needle.len() {
370+
if haystack_i >= haystack.len() {
371+
return false;
372+
}
373+
let {ch, next} = str::char_range_at(haystack, haystack_i);
374+
haystack_i = next;
375+
if !scan_char(needle, ch, &mut needle_i) {
376+
return false;
377+
}
378+
}
379+
*idx = haystack_i;
380+
return true;
381+
}
382+
308383
type procargs = {prog: ~str, args: ~[~str]};
309384

310385
type procres = {status: int, stdout: ~str, stderr: ~str, cmdline: ~str};

src/test/compile-fail/issue-1476.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
log(error, x); //~ ERROR unresolved name: x
3+
}

0 commit comments

Comments
 (0)