about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-08-14 04:18:51 +0200
committerGitHub <noreply@github.com>2019-08-14 04:18:51 +0200
commit43f4990fc468ef922dbe2ab16b396fd51054a708 (patch)
tree31ccbdb082c129ad542086f0752ddcd4a1a34965
parent5741e2941797039abdfb91138abfa33e8631a578 (diff)
parent84e202e6b36250dfc319aa5a869ad1df29b4b55a (diff)
Rollup merge of #63508 - estebank:compromice, r=petrochenkov
Do not ICE when synthesizing spans falling inside unicode chars

Fix https://github.com/rust-lang/rust/issues/61226.
-rw-r--r--src/libsyntax/source_map.rs15
-rw-r--r--src/test/ui/suggestions/issue-61226.rs5
-rw-r--r--src/test/ui/suggestions/issue-61226.stderr9
3 files changed, 23 insertions, 6 deletions
diff --git a/src/libsyntax/source_map.rs b/src/libsyntax/source_map.rs
index 4e29c77c89e..74cab00d3c1 100644
--- a/src/libsyntax/source_map.rs
+++ b/src/libsyntax/source_map.rs
@@ -519,7 +519,7 @@ impl SourceMap {
     /// extract function takes three arguments: a string slice containing the source, an index in
     /// the slice for the beginning of the span and an index in the slice for the end of the span.
     fn span_to_source<F>(&self, sp: Span, extract_source: F) -> Result<String, SpanSnippetError>
-        where F: Fn(&str, usize, usize) -> String
+        where F: Fn(&str, usize, usize) -> Result<String, SpanSnippetError>
     {
         if sp.lo() > sp.hi() {
             return Err(SpanSnippetError::IllFormedSpan(sp));
@@ -554,9 +554,9 @@ impl SourceMap {
             }
 
             if let Some(ref src) = local_begin.sf.src {
-                return Ok(extract_source(src, start_index, end_index));
+                return extract_source(src, start_index, end_index);
             } else if let Some(src) = local_begin.sf.external_src.borrow().get_source() {
-                return Ok(extract_source(src, start_index, end_index));
+                return extract_source(src, start_index, end_index);
             } else {
                 return Err(SpanSnippetError::SourceNotAvailable {
                     filename: local_begin.sf.name.clone()
@@ -567,8 +567,9 @@ impl SourceMap {
 
     /// Returns the source snippet as `String` corresponding to the given `Span`
     pub fn span_to_snippet(&self, sp: Span) -> Result<String, SpanSnippetError> {
-        self.span_to_source(sp, |src, start_index, end_index| src[start_index..end_index]
-                                                                .to_string())
+        self.span_to_source(sp, |src, start_index, end_index| src.get(start_index..end_index)
+            .map(|s| s.to_string())
+            .ok_or_else(|| SpanSnippetError::IllFormedSpan(sp)))
     }
 
     pub fn span_to_margin(&self, sp: Span) -> Option<usize> {
@@ -582,7 +583,9 @@ impl SourceMap {
 
     /// Returns the source snippet as `String` before the given `Span`
     pub fn span_to_prev_source(&self, sp: Span) -> Result<String, SpanSnippetError> {
-        self.span_to_source(sp, |src, start_index, _| src[..start_index].to_string())
+        self.span_to_source(sp, |src, start_index, _| src.get(..start_index)
+            .map(|s| s.to_string())
+            .ok_or_else(|| SpanSnippetError::IllFormedSpan(sp)))
     }
 
     /// Extend the given `Span` to just after the previous occurrence of `c`. Return the same span
diff --git a/src/test/ui/suggestions/issue-61226.rs b/src/test/ui/suggestions/issue-61226.rs
new file mode 100644
index 00000000000..e83b0b4d630
--- /dev/null
+++ b/src/test/ui/suggestions/issue-61226.rs
@@ -0,0 +1,5 @@
+struct X {}
+fn main() {
+    vec![X]; //…
+    //~^ ERROR expected value, found struct `X`
+}
diff --git a/src/test/ui/suggestions/issue-61226.stderr b/src/test/ui/suggestions/issue-61226.stderr
new file mode 100644
index 00000000000..6d7d98ac6a1
--- /dev/null
+++ b/src/test/ui/suggestions/issue-61226.stderr
@@ -0,0 +1,9 @@
+error[E0423]: expected value, found struct `X`
+  --> $DIR/issue-61226.rs:3:10
+   |
+LL |     vec![X]; //…
+   |          ^ did you mean `X { /* fields */ }`?
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0423`.