Skip to content

Commit 4d3d06a

Browse files
authored
Rollup merge of #63859 - matthewjasper:check-snippet-result, r=Centril
Don't unwrap the result of `span_to_snippet` Closes #63800
2 parents b0d374a + 365ff62 commit 4d3d06a

File tree

5 files changed

+90
-43
lines changed

5 files changed

+90
-43
lines changed

src/librustc_mir/borrow_check/move_errors.rs

+43-41
Original file line numberDiff line numberDiff line change
@@ -415,20 +415,21 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
415415
"{:?}",
416416
move_place.ty(self.body, self.infcx.tcx).ty,
417417
);
418-
let snippet = self.infcx.tcx.sess.source_map().span_to_snippet(span).unwrap();
419-
let is_option = move_ty.starts_with("std::option::Option");
420-
let is_result = move_ty.starts_with("std::result::Result");
421-
if is_option || is_result {
422-
err.span_suggestion(
423-
span,
424-
&format!("consider borrowing the `{}`'s content", if is_option {
425-
"Option"
426-
} else {
427-
"Result"
428-
}),
429-
format!("{}.as_ref()", snippet),
430-
Applicability::MaybeIncorrect,
431-
);
418+
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
419+
let is_option = move_ty.starts_with("std::option::Option");
420+
let is_result = move_ty.starts_with("std::result::Result");
421+
if is_option || is_result {
422+
err.span_suggestion(
423+
span,
424+
&format!("consider borrowing the `{}`'s content", if is_option {
425+
"Option"
426+
} else {
427+
"Result"
428+
}),
429+
format!("{}.as_ref()", snippet),
430+
Applicability::MaybeIncorrect,
431+
);
432+
}
432433
}
433434
err
434435
}
@@ -439,19 +440,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
439440
err: &mut DiagnosticBuilder<'a>,
440441
span: Span,
441442
) {
442-
let snippet = self.infcx.tcx.sess.source_map().span_to_snippet(span).unwrap();
443443
match error {
444444
GroupedMoveError::MovesFromPlace {
445445
mut binds_to,
446446
move_from,
447447
..
448448
} => {
449-
err.span_suggestion(
450-
span,
451-
"consider borrowing here",
452-
format!("&{}", snippet),
453-
Applicability::Unspecified,
454-
);
449+
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
450+
err.span_suggestion(
451+
span,
452+
"consider borrowing here",
453+
format!("&{}", snippet),
454+
Applicability::Unspecified,
455+
);
456+
}
455457

456458
if binds_to.is_empty() {
457459
let place_ty = move_from.ty(self.body, self.infcx.tcx).ty;
@@ -517,27 +519,27 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
517519
..
518520
}))
519521
) = bind_to.is_user_variable {
520-
let pat_snippet = self.infcx.tcx.sess.source_map()
521-
.span_to_snippet(pat_span)
522-
.unwrap();
523-
if pat_snippet.starts_with('&') {
524-
let pat_snippet = pat_snippet[1..].trim_start();
525-
let suggestion;
526-
let to_remove;
527-
if pat_snippet.starts_with("mut")
528-
&& pat_snippet["mut".len()..].starts_with(Pattern_White_Space)
529-
{
530-
suggestion = pat_snippet["mut".len()..].trim_start();
531-
to_remove = "&mut";
532-
} else {
533-
suggestion = pat_snippet;
534-
to_remove = "&";
522+
if let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span)
523+
{
524+
if pat_snippet.starts_with('&') {
525+
let pat_snippet = pat_snippet[1..].trim_start();
526+
let suggestion;
527+
let to_remove;
528+
if pat_snippet.starts_with("mut")
529+
&& pat_snippet["mut".len()..].starts_with(Pattern_White_Space)
530+
{
531+
suggestion = pat_snippet["mut".len()..].trim_start();
532+
to_remove = "&mut";
533+
} else {
534+
suggestion = pat_snippet;
535+
to_remove = "&";
536+
}
537+
suggestions.push((
538+
pat_span,
539+
to_remove,
540+
suggestion.to_owned(),
541+
));
535542
}
536-
suggestions.push((
537-
pat_span,
538-
to_remove,
539-
suggestion.to_owned(),
540-
));
541543
}
542544
}
543545
}

src/librustc_mir/borrow_check/mutability_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,8 @@ fn annotate_struct_field(
711711
}
712712

713713
/// If possible, suggest replacing `ref` with `ref mut`.
714-
fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<(String)> {
715-
let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).unwrap();
714+
fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<String> {
715+
let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).ok()?;
716716
if hi_src.starts_with("ref")
717717
&& hi_src["ref".len()..].starts_with(Pattern_White_Space)
718718
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// ignore-test
2+
3+
macro_rules! aaa {
4+
($c:ident) => {{
5+
let a = $c;
6+
}}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Test that we don't ICE after trying to construct a cross-file snippet #63800.
2+
3+
// compile-flags: --test
4+
5+
#[macro_use]
6+
#[path = "move-error-snippets-ext.rs"]
7+
mod move_error_snippets_ext;
8+
9+
struct A;
10+
11+
macro_rules! sss {
12+
() => {
13+
#[test]
14+
fn fff() {
15+
static D: A = A;
16+
aaa!(D); //~ ERROR cannot move
17+
}
18+
};
19+
}
20+
21+
sss!();
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0507]: cannot move out of static item `D`
2+
--> $DIR/move-error-snippets.rs:16:18
3+
|
4+
LL | | #[macro_use]
5+
| |__________________^ move occurs because `D` has type `A`, which does not implement the `Copy` trait
6+
...
7+
LL | aaa!(D);
8+
| __________________^
9+
...
10+
LL | sss!();
11+
| ------- in this macro invocation
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)