Skip to content

Commit e36c3ec

Browse files
powerboat9P-E-P
authored andcommitted
Improve parsing of raw string literals
gcc/rust/ChangeLog: * lex/rust-lex.cc (Lexer::parse_raw_string): Bring handling of edge cases to par with parse_raw_byte_string. gcc/testsuite/ChangeLog: * rust/compile/raw-string-loc.rs: New test. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
1 parent 01092b8 commit e36c3ec

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

gcc/rust/lex/rust-lex.cc

+18-3
Original file line numberDiff line numberDiff line change
@@ -2152,6 +2152,9 @@ Lexer::parse_raw_string (location_t loc, int initial_hash_count)
21522152
str.reserve (16); // some sensible default
21532153

21542154
int length = 1 + initial_hash_count;
2155+
current_column += length;
2156+
2157+
const location_t string_begin_locus = get_current_location ();
21552158

21562159
if (initial_hash_count > 0)
21572160
skip_input (initial_hash_count - 1);
@@ -2162,10 +2165,11 @@ Lexer::parse_raw_string (location_t loc, int initial_hash_count)
21622165
rust_error_at (get_current_location (), "raw string has no opening %<\"%>");
21632166

21642167
length++;
2168+
current_column++;
21652169
skip_input ();
21662170
current_char = peek_input ();
21672171

2168-
while (!current_char.is_eof ())
2172+
while (true)
21692173
{
21702174
if (current_char.value == '"')
21712175
{
@@ -2186,19 +2190,30 @@ Lexer::parse_raw_string (location_t loc, int initial_hash_count)
21862190
skip_input (initial_hash_count);
21872191
current_char = peek_input ();
21882192
length += initial_hash_count + 1;
2193+
current_column += initial_hash_count + 1;
21892194
break;
21902195
}
21912196
}
2197+
else if (current_char.is_eof ())
2198+
{
2199+
rust_error_at (string_begin_locus, "unended raw string literal");
2200+
return Token::make (END_OF_FILE, get_current_location ());
2201+
}
21922202

21932203
length++;
2204+
current_column++;
2205+
if (current_char == '\n')
2206+
{
2207+
current_line++;
2208+
current_column = 1;
2209+
start_line (current_line, max_column_hint);
2210+
}
21942211

21952212
str += current_char.as_string ();
21962213
skip_input ();
21972214
current_char = peek_input ();
21982215
}
21992216

2200-
current_column += length;
2201-
22022217
loc += length - 1;
22032218

22042219
str.shrink_to_fit ();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const X: &'static str = r#"12
2+
12"#;
3+
4+
BREAK
5+
// { dg-error "unrecognised token" "" { target *-*-* } .-1 }
6+
// { dg-excess-errors "error 'failed to parse item' does not have location" }

0 commit comments

Comments
 (0)