Skip to content

Commit 43f4990

Browse files
authored
Rollup merge of #63508 - estebank:compromice, r=petrochenkov
Do not ICE when synthesizing spans falling inside unicode chars Fix #61226.
2 parents 5741e29 + 84e202e commit 43f4990

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/libsyntax/source_map.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl SourceMap {
519519
/// extract function takes three arguments: a string slice containing the source, an index in
520520
/// the slice for the beginning of the span and an index in the slice for the end of the span.
521521
fn span_to_source<F>(&self, sp: Span, extract_source: F) -> Result<String, SpanSnippetError>
522-
where F: Fn(&str, usize, usize) -> String
522+
where F: Fn(&str, usize, usize) -> Result<String, SpanSnippetError>
523523
{
524524
if sp.lo() > sp.hi() {
525525
return Err(SpanSnippetError::IllFormedSpan(sp));
@@ -554,9 +554,9 @@ impl SourceMap {
554554
}
555555

556556
if let Some(ref src) = local_begin.sf.src {
557-
return Ok(extract_source(src, start_index, end_index));
557+
return extract_source(src, start_index, end_index);
558558
} else if let Some(src) = local_begin.sf.external_src.borrow().get_source() {
559-
return Ok(extract_source(src, start_index, end_index));
559+
return extract_source(src, start_index, end_index);
560560
} else {
561561
return Err(SpanSnippetError::SourceNotAvailable {
562562
filename: local_begin.sf.name.clone()
@@ -567,8 +567,9 @@ impl SourceMap {
567567

568568
/// Returns the source snippet as `String` corresponding to the given `Span`
569569
pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
570-
self.span_to_source(sp, |src, start_index, end_index| src[start_index..end_index]
571-
.to_string())
570+
self.span_to_source(sp, |src, start_index, end_index| src.get(start_index..end_index)
571+
.map(|s| s.to_string())
572+
.ok_or_else(|| SpanSnippetError::IllFormedSpan(sp)))
572573
}
573574

574575
pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
@@ -582,7 +583,9 @@ impl SourceMap {
582583

583584
/// Returns the source snippet as `String` before the given `Span`
584585
pub fn span_to_prev_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
585-
self.span_to_source(sp, |src, start_index, _| src[..start_index].to_string())
586+
self.span_to_source(sp, |src, start_index, _| src.get(..start_index)
587+
.map(|s| s.to_string())
588+
.ok_or_else(|| SpanSnippetError::IllFormedSpan(sp)))
586589
}
587590

588591
/// Extend the given `Span` to just after the previous occurrence of `c`. Return the same span
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
struct X {}
2+
fn main() {
3+
vec![X]; //…
4+
//~^ ERROR expected value, found struct `X`
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0423]: expected value, found struct `X`
2+
--> $DIR/issue-61226.rs:3:10
3+
|
4+
LL | vec![X]; //…
5+
| ^ did you mean `X { /* fields */ }`?
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)