Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not ICE when synthesizing spans falling inside unicode chars #63508

Merged
merged 3 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/libsyntax/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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()
Expand All @@ -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> {
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/suggestions/issue-61226.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
struct X {}
fn main() {
vec![X]; //…
//~^ ERROR expected value, found struct `X`
}
9 changes: 9 additions & 0 deletions src/test/ui/suggestions/issue-61226.stderr
Original file line number Diff line number Diff line change
@@ -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`.