Skip to content

Commit

Permalink
Rollup merge of #63859 - matthewjasper:check-snippet-result, r=Centril
Browse files Browse the repository at this point in the history
Don't unwrap the result of `span_to_snippet`

Closes #63800
  • Loading branch information
Centril authored Aug 24, 2019
2 parents 81ec558 + 365ff62 commit 8ade359
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 43 deletions.
84 changes: 43 additions & 41 deletions src/librustc_mir/borrow_check/move_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,20 +415,21 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
"{:?}",
move_place.ty(self.body, self.infcx.tcx).ty,
);
let snippet = self.infcx.tcx.sess.source_map().span_to_snippet(span).unwrap();
let is_option = move_ty.starts_with("std::option::Option");
let is_result = move_ty.starts_with("std::result::Result");
if is_option || is_result {
err.span_suggestion(
span,
&format!("consider borrowing the `{}`'s content", if is_option {
"Option"
} else {
"Result"
}),
format!("{}.as_ref()", snippet),
Applicability::MaybeIncorrect,
);
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
let is_option = move_ty.starts_with("std::option::Option");
let is_result = move_ty.starts_with("std::result::Result");
if is_option || is_result {
err.span_suggestion(
span,
&format!("consider borrowing the `{}`'s content", if is_option {
"Option"
} else {
"Result"
}),
format!("{}.as_ref()", snippet),
Applicability::MaybeIncorrect,
);
}
}
err
}
Expand All @@ -439,19 +440,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
err: &mut DiagnosticBuilder<'a>,
span: Span,
) {
let snippet = self.infcx.tcx.sess.source_map().span_to_snippet(span).unwrap();
match error {
GroupedMoveError::MovesFromPlace {
mut binds_to,
move_from,
..
} => {
err.span_suggestion(
span,
"consider borrowing here",
format!("&{}", snippet),
Applicability::Unspecified,
);
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
err.span_suggestion(
span,
"consider borrowing here",
format!("&{}", snippet),
Applicability::Unspecified,
);
}

if binds_to.is_empty() {
let place_ty = move_from.ty(self.body, self.infcx.tcx).ty;
Expand Down Expand Up @@ -517,27 +519,27 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
..
}))
) = bind_to.is_user_variable {
let pat_snippet = self.infcx.tcx.sess.source_map()
.span_to_snippet(pat_span)
.unwrap();
if pat_snippet.starts_with('&') {
let pat_snippet = pat_snippet[1..].trim_start();
let suggestion;
let to_remove;
if pat_snippet.starts_with("mut")
&& pat_snippet["mut".len()..].starts_with(Pattern_White_Space)
{
suggestion = pat_snippet["mut".len()..].trim_start();
to_remove = "&mut";
} else {
suggestion = pat_snippet;
to_remove = "&";
if let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span)
{
if pat_snippet.starts_with('&') {
let pat_snippet = pat_snippet[1..].trim_start();
let suggestion;
let to_remove;
if pat_snippet.starts_with("mut")
&& pat_snippet["mut".len()..].starts_with(Pattern_White_Space)
{
suggestion = pat_snippet["mut".len()..].trim_start();
to_remove = "&mut";
} else {
suggestion = pat_snippet;
to_remove = "&";
}
suggestions.push((
pat_span,
to_remove,
suggestion.to_owned(),
));
}
suggestions.push((
pat_span,
to_remove,
suggestion.to_owned(),
));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/borrow_check/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,8 @@ fn annotate_struct_field(
}

/// If possible, suggest replacing `ref` with `ref mut`.
fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<(String)> {
let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).unwrap();
fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<String> {
let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).ok()?;
if hi_src.starts_with("ref")
&& hi_src["ref".len()..].starts_with(Pattern_White_Space)
{
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/borrowck/move-error-snippets-ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// ignore-test

macro_rules! aaa {
($c:ident) => {{
let a = $c;
}}
}
23 changes: 23 additions & 0 deletions src/test/ui/borrowck/move-error-snippets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Test that we don't ICE after trying to construct a cross-file snippet #63800.

// compile-flags: --test

#[macro_use]
#[path = "move-error-snippets-ext.rs"]
mod move_error_snippets_ext;

struct A;

macro_rules! sss {
() => {
#[test]
fn fff() {
static D: A = A;
aaa!(D); //~ ERROR cannot move
}
};
}

sss!();

fn main() {}
15 changes: 15 additions & 0 deletions src/test/ui/borrowck/move-error-snippets.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0507]: cannot move out of static item `D`
--> $DIR/move-error-snippets.rs:16:18
|
LL | | #[macro_use]
| |__________________^ move occurs because `D` has type `A`, which does not implement the `Copy` trait
...
LL | aaa!(D);
| __________________^
...
LL | sss!();
| ------- in this macro invocation

error: aborting due to previous error

For more information about this error, try `rustc --explain E0507`.

0 comments on commit 8ade359

Please sign in to comment.