Skip to content

Commit 56b39fb

Browse files
committed
Add 'span_to_snippet' shortcut.
1 parent 1b11860 commit 56b39fb

File tree

2 files changed

+13
-16
lines changed

2 files changed

+13
-16
lines changed

src/libsyntax/parse/diagnostics.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::ThinVec;
1414
use crate::util::parser::AssocOp;
1515
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
1616
use rustc_data_structures::fx::FxHashSet;
17-
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
17+
use syntax_pos::{Span, DUMMY_SP, MultiSpan, SpanSnippetError};
1818
use log::{debug, trace};
1919
use std::mem;
2020

@@ -199,6 +199,10 @@ impl<'a> Parser<'a> {
199199
&self.sess.span_diagnostic
200200
}
201201

202+
crate fn span_to_snippet(&self, span: Span) -> Result<String, SpanSnippetError> {
203+
self.sess.source_map().span_to_snippet(span)
204+
}
205+
202206
crate fn expected_ident_found(&self) -> DiagnosticBuilder<'a> {
203207
let mut err = self.struct_span_err(
204208
self.token.span,
@@ -719,8 +723,6 @@ impl<'a> Parser<'a> {
719723
path.span = ty_span.to(self.prev_span);
720724

721725
let ty_str = self
722-
.sess
723-
.source_map()
724726
.span_to_snippet(ty_span)
725727
.unwrap_or_else(|_| pprust::ty_to_string(&ty));
726728
self.diagnostic()
@@ -891,7 +893,7 @@ impl<'a> Parser<'a> {
891893
err.span_label(await_sp, "while parsing this incorrect await expression");
892894
err
893895
})?;
894-
let expr_str = self.sess.source_map().span_to_snippet(expr.span)
896+
let expr_str = self.span_to_snippet(expr.span)
895897
.unwrap_or_else(|_| pprust::expr_to_string(&expr));
896898
let suggestion = format!("{}.await{}", expr_str, if is_question { "?" } else { "" });
897899
let sp = lo.to(expr.span);
@@ -940,8 +942,6 @@ impl<'a> Parser<'a> {
940942
self.bump();
941943

942944
let pat_str = self
943-
.sess
944-
.source_map()
945945
// Remove the `(` from the span of the pattern:
946946
.span_to_snippet(pat.span.trim_start(begin_par_sp).unwrap())
947947
.unwrap_or_else(|_| pprust::pat_to_string(&pat));

src/libsyntax/parse/parser.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -3071,10 +3071,7 @@ impl<'a> Parser<'a> {
30713071
id: ast::DUMMY_NODE_ID
30723072
}));
30733073

3074-
let expr_str = self
3075-
.sess
3076-
.source_map()
3077-
.span_to_snippet(expr.span)
3074+
let expr_str = self.span_to_snippet(expr.span)
30783075
.unwrap_or_else(|_| pprust::expr_to_string(&expr));
30793076

30803077
self.struct_span_err(self.token.span, &msg)
@@ -3796,7 +3793,7 @@ impl<'a> Parser<'a> {
37963793
let seq_span = pat.span.to(self.prev_span);
37973794
let mut err = self.struct_span_err(comma_span,
37983795
"unexpected `,` in pattern");
3799-
if let Ok(seq_snippet) = self.sess.source_map().span_to_snippet(seq_span) {
3796+
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
38003797
err.span_suggestion(
38013798
seq_span,
38023799
"try adding parentheses to match on a tuple..",
@@ -4145,7 +4142,7 @@ impl<'a> Parser<'a> {
41454142
let parser_snapshot_after_type = self.clone();
41464143
mem::replace(self, parser_snapshot_before_type);
41474144

4148-
let snippet = self.sess.source_map().span_to_snippet(pat.span).unwrap();
4145+
let snippet = self.span_to_snippet(pat.span).unwrap();
41494146
err.span_label(pat.span, format!("while parsing the type for `{}`", snippet));
41504147
(Some((parser_snapshot_after_type, colon_sp, err)), None)
41514148
}
@@ -4565,7 +4562,7 @@ impl<'a> Parser<'a> {
45654562
if self.eat(&token::Semi) {
45664563
stmt_span = stmt_span.with_hi(self.prev_span.hi());
45674564
}
4568-
if let Ok(snippet) = self.sess.source_map().span_to_snippet(stmt_span) {
4565+
if let Ok(snippet) = self.span_to_snippet(stmt_span) {
45694566
e.span_suggestion(
45704567
stmt_span,
45714568
"try placing this code inside a block",
@@ -4738,7 +4735,7 @@ impl<'a> Parser<'a> {
47384735
lo.to(self.prev_span),
47394736
"parenthesized lifetime bounds are not supported"
47404737
);
4741-
if let Ok(snippet) = self.sess.source_map().span_to_snippet(inner_span) {
4738+
if let Ok(snippet) = self.span_to_snippet(inner_span) {
47424739
err.span_suggestion_short(
47434740
lo.to(self.prev_span),
47444741
"remove the parentheses",
@@ -4796,7 +4793,7 @@ impl<'a> Parser<'a> {
47964793
let mut new_bound_list = String::new();
47974794
if !bounds.is_empty() {
47984795
let mut snippets = bounds.iter().map(|bound| bound.span())
4799-
.map(|span| self.sess.source_map().span_to_snippet(span));
4796+
.map(|span| self.span_to_snippet(span));
48004797
while let Some(Ok(snippet)) = snippets.next() {
48014798
new_bound_list.push_str(" + ");
48024799
new_bound_list.push_str(&snippet);
@@ -7415,7 +7412,7 @@ impl<'a> Parser<'a> {
74157412
sp, &suggestion, format!(" {} ", kw), Applicability::MachineApplicable
74167413
);
74177414
} else {
7418-
if let Ok(snippet) = self.sess.source_map().span_to_snippet(ident_sp) {
7415+
if let Ok(snippet) = self.span_to_snippet(ident_sp) {
74197416
err.span_suggestion(
74207417
full_sp,
74217418
"if you meant to call a macro, try",

0 commit comments

Comments
 (0)