Skip to content

Commit 1dd1880

Browse files
committed
syntax: convert the lexer to use Option<char> over transmute(-1).
The transmute was unsound. There are many instances of .unwrap_or('\x00') for "ignoring" EOF which either do not make the situation worse than it was (well, actually make it better, since it's easy to grep for places that don't handle EOF) or can never ever be read. Fixes #8971.
1 parent e7908c0 commit 1dd1880

File tree

2 files changed

+157
-116
lines changed

2 files changed

+157
-116
lines changed

src/libsyntax/parse/comments.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use ast;
1212
use codemap::{BytePos, CharPos, CodeMap, Pos};
1313
use diagnostic;
1414
use parse::lexer::{is_whitespace, with_str_from, Reader};
15-
use parse::lexer::{StringReader, bump, is_eof, nextch, TokenAndSpan};
15+
use parse::lexer::{StringReader, bump, is_eof, nextch_is, TokenAndSpan};
1616
use parse::lexer::{is_line_non_doc_comment, is_block_non_doc_comment};
1717
use parse::lexer;
1818
use parse::token;
@@ -136,11 +136,11 @@ pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
136136

137137
fn read_to_eol(rdr: &StringReader) -> ~str {
138138
let mut val = ~"";
139-
while rdr.curr.get() != '\n' && !is_eof(rdr) {
140-
val.push_char(rdr.curr.get());
139+
while !rdr.curr_is('\n') && !is_eof(rdr) {
140+
val.push_char(rdr.curr.get().unwrap());
141141
bump(rdr);
142142
}
143-
if rdr.curr.get() == '\n' { bump(rdr); }
143+
if rdr.curr_is('\n') { bump(rdr); }
144144
return val;
145145
}
146146

@@ -152,7 +152,7 @@ fn read_one_line_comment(rdr: &StringReader) -> ~str {
152152
}
153153

154154
fn consume_non_eol_whitespace(rdr: &StringReader) {
155-
while is_whitespace(rdr.curr.get()) && rdr.curr.get() != '\n' &&
155+
while is_whitespace(rdr.curr.get()) && !rdr.curr_is('\n') &&
156156
!is_eof(rdr) {
157157
bump(rdr);
158158
}
@@ -171,7 +171,7 @@ fn push_blank_line_comment(rdr: &StringReader, comments: &mut ~[Comment]) {
171171
fn consume_whitespace_counting_blank_lines(rdr: &StringReader,
172172
comments: &mut ~[Comment]) {
173173
while is_whitespace(rdr.curr.get()) && !is_eof(rdr) {
174-
if rdr.col.get() == CharPos(0u) && rdr.curr.get() == '\n' {
174+
if rdr.col.get() == CharPos(0u) && rdr.curr_is('\n') {
175175
push_blank_line_comment(rdr, &mut *comments);
176176
}
177177
bump(rdr);
@@ -196,7 +196,7 @@ fn read_line_comments(rdr: &StringReader, code_to_the_left: bool,
196196
debug!(">>> line comments");
197197
let p = rdr.last_pos.get();
198198
let mut lines: ~[~str] = ~[];
199-
while rdr.curr.get() == '/' && nextch(rdr) == '/' {
199+
while rdr.curr_is('/') && nextch_is(rdr, '/') {
200200
let line = read_one_line_comment(rdr);
201201
debug!("{}", line);
202202
if is_doc_comment(line) { // doc-comments are not put in comments
@@ -261,9 +261,9 @@ fn read_block_comment(rdr: &StringReader,
261261
let mut curr_line = ~"/*";
262262
263263
// doc-comments are not really comments, they are attributes
264-
if rdr.curr.get() == '*' || rdr.curr.get() == '!' {
265-
while !(rdr.curr.get() == '*' && nextch(rdr) == '/') && !is_eof(rdr) {
266-
curr_line.push_char(rdr.curr.get());
264+
if rdr.curr_is('*') || rdr.curr_is('!') {
265+
while !(rdr.curr_is('*') && nextch_is(rdr, '/')) && !is_eof(rdr) {
266+
curr_line.push_char(rdr.curr.get().unwrap());
267267
bump(rdr);
268268
}
269269
if !is_eof(rdr) {
@@ -281,20 +281,20 @@ fn read_block_comment(rdr: &StringReader,
281281
if is_eof(rdr) {
282282
rdr.fatal(~"unterminated block comment");
283283
}
284-
if rdr.curr.get() == '\n' {
284+
if rdr.curr_is('\n') {
285285
trim_whitespace_prefix_and_push_line(&mut lines, curr_line,
286286
col);
287287
curr_line = ~"";
288288
bump(rdr);
289289
} else {
290-
curr_line.push_char(rdr.curr.get());
291-
if rdr.curr.get() == '/' && nextch(rdr) == '*' {
290+
curr_line.push_char(rdr.curr.get().unwrap());
291+
if rdr.curr_is('/') && nextch_is(rdr, '*') {
292292
bump(rdr);
293293
bump(rdr);
294294
curr_line.push_char('*');
295295
level += 1;
296296
} else {
297-
if rdr.curr.get() == '*' && nextch(rdr) == '/' {
297+
if rdr.curr_is('*') && nextch_is(rdr, '/') {
298298
bump(rdr);
299299
bump(rdr);
300300
curr_line.push_char('/');
@@ -310,28 +310,28 @@ fn read_block_comment(rdr: &StringReader,
310310

311311
let mut style = if code_to_the_left { Trailing } else { Isolated };
312312
consume_non_eol_whitespace(rdr);
313-
if !is_eof(rdr) && rdr.curr.get() != '\n' && lines.len() == 1u {
313+
if !is_eof(rdr) && !rdr.curr_is('\n') && lines.len() == 1u {
314314
style = Mixed;
315315
}
316316
debug!("<<< block comment");
317317
comments.push(Comment {style: style, lines: lines, pos: p});
318318
}
319319

320320
fn peeking_at_comment(rdr: &StringReader) -> bool {
321-
return ((rdr.curr.get() == '/' && nextch(rdr) == '/') ||
322-
(rdr.curr.get() == '/' && nextch(rdr) == '*')) ||
323-
(rdr.curr.get() == '#' && nextch(rdr) == '!');
321+
return (rdr.curr_is('/') && nextch_is(rdr, '/')) ||
322+
(rdr.curr_is('/') && nextch_is(rdr, '*')) ||
323+
(rdr.curr_is('#') && nextch_is(rdr, '!'));
324324
}
325325

326326
fn consume_comment(rdr: &StringReader,
327327
code_to_the_left: bool,
328328
comments: &mut ~[Comment]) {
329329
debug!(">>> consume comment");
330-
if rdr.curr.get() == '/' && nextch(rdr) == '/' {
330+
if rdr.curr_is('/') && nextch_is(rdr, '/') {
331331
read_line_comments(rdr, code_to_the_left, comments);
332-
} else if rdr.curr.get() == '/' && nextch(rdr) == '*' {
332+
} else if rdr.curr_is('/') && nextch_is(rdr, '*') {
333333
read_block_comment(rdr, code_to_the_left, comments);
334-
} else if rdr.curr.get() == '#' && nextch(rdr) == '!' {
334+
} else if rdr.curr_is('#') && nextch_is(rdr, '!') {
335335
read_shebang_comment(rdr, code_to_the_left, comments);
336336
} else { fail!(); }
337337
debug!("<<< consume comment");
@@ -363,7 +363,7 @@ pub fn gather_comments_and_literals(span_diagnostic:
363363
loop {
364364
let mut code_to_the_left = !first_read;
365365
consume_non_eol_whitespace(&rdr);
366-
if rdr.curr.get() == '\n' {
366+
if rdr.curr_is('\n') {
367367
code_to_the_left = false;
368368
consume_whitespace_counting_blank_lines(&rdr, &mut comments);
369369
}

0 commit comments

Comments
 (0)