Skip to content

Commit 48f5f01

Browse files
committed
move logic to a function
1 parent efc8f65 commit 48f5f01

File tree

1 file changed

+79
-81
lines changed

1 file changed

+79
-81
lines changed

compiler/rustc_mir/src/borrow_check/diagnostics/mutability_errors.rs

+79-81
Original file line numberDiff line numberDiff line change
@@ -424,87 +424,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
424424

425425
match label {
426426
Some((true, err_help_span, suggested_code)) => {
427-
/// User cannot make signature of a trait mutable
428-
/// without changing the trait. So we find if this
429-
/// error belongs to a trait and if so we move
430-
/// suggestion to the trait or disable it if it is
431-
/// out of scope of this crate
432-
let (is_trait_sig, local_trait) = {
433-
if self.body.local_kind(local) != LocalKind::Arg {
434-
(false, None)
435-
} else {
436-
let hir_map = self.infcx.tcx.hir();
437-
let my_hir = hir_map.local_def_id_to_hir_id(
438-
self.body.source.def_id().as_local().unwrap(),
439-
);
440-
match hir_map.find(hir_map.get_parent_node(my_hir)) {
441-
Some(Node::Item(hir::Item {
442-
kind:
443-
hir::ItemKind::Impl(hir::Impl {
444-
of_trait:
445-
Some(hir::TraitRef {
446-
path:
447-
hir::Path {
448-
res:
449-
hir::def::Res::Def(_, td),
450-
..
451-
},
452-
..
453-
}),
454-
..
455-
}),
456-
..
457-
})) => {
458-
(true, td.as_local().and_then(|tld| {
459-
let h = hir_map.local_def_id_to_hir_id(tld);
460-
match hir_map.find(h) {
461-
Some(Node::Item(hir::Item {
462-
kind: hir::ItemKind::Trait(
463-
_, _, _, _,
464-
items
465-
),
466-
..
467-
})) => {
468-
let mut f_in_trait_opt = None;
469-
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
470-
let hi = fi.hir_id();
471-
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
472-
continue;
473-
}
474-
if hir_map.name(hi) != hir_map.name(my_hir) {
475-
continue;
476-
}
477-
f_in_trait_opt = Some(hi);
478-
break;
479-
}
480-
f_in_trait_opt.and_then(|f_in_trait| {
481-
match hir_map.find(f_in_trait) {
482-
Some(Node::TraitItem(hir::TraitItem {
483-
kind: hir::TraitItemKind::Fn(hir::FnSig {
484-
decl: hir::FnDecl {
485-
inputs,
486-
..
487-
},
488-
..
489-
}, _),
490-
..
491-
})) => {
492-
let hir::Ty { span, .. } = inputs[local.index() - 1];
493-
Some(span)
494-
},
495-
_ => None,
496-
}
497-
})
498-
//Some(hir_map.span(h))
499-
}
500-
_ => None
501-
}
502-
}))
503-
}
504-
_ => (false, None),
505-
}
506-
}
507-
};
427+
let (is_trait_sig, local_trait) = self.is_error_in_trait(local);
508428
if !is_trait_sig {
509429
err.span_suggestion(
510430
err_help_span,
@@ -595,6 +515,84 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
595515

596516
err.buffer(&mut self.errors_buffer);
597517
}
518+
519+
/// User cannot make signature of a trait mutable without changing the
520+
/// trait. So we find if this error belongs to a trait and if so we move
521+
/// suggestion to the trait or disable it if it is out of scope of this crate
522+
fn is_error_in_trait(&self, local: Local) -> (bool, Option<Span>) {
523+
if self.body.local_kind(local) != LocalKind::Arg {
524+
return (false, None);
525+
}
526+
let hir_map = self.infcx.tcx.hir();
527+
let my_hir = hir_map.local_def_id_to_hir_id(
528+
self.body.source.def_id().as_local().unwrap(),
529+
);
530+
match hir_map.find(hir_map.get_parent_node(my_hir)) {
531+
Some(Node::Item(hir::Item {
532+
kind:
533+
hir::ItemKind::Impl(hir::Impl {
534+
of_trait:
535+
Some(hir::TraitRef {
536+
path:
537+
hir::Path {
538+
res:
539+
hir::def::Res::Def(_, td),
540+
..
541+
},
542+
..
543+
}),
544+
..
545+
}),
546+
..
547+
})) => {
548+
(true, td.as_local().and_then(|tld| {
549+
let h = hir_map.local_def_id_to_hir_id(tld);
550+
match hir_map.find(h) {
551+
Some(Node::Item(hir::Item {
552+
kind: hir::ItemKind::Trait(
553+
_, _, _, _,
554+
items
555+
),
556+
..
557+
})) => {
558+
let mut f_in_trait_opt = None;
559+
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
560+
let hi = fi.hir_id();
561+
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
562+
continue;
563+
}
564+
if hir_map.name(hi) != hir_map.name(my_hir) {
565+
continue;
566+
}
567+
f_in_trait_opt = Some(hi);
568+
break;
569+
}
570+
f_in_trait_opt.and_then(|f_in_trait| {
571+
match hir_map.find(f_in_trait) {
572+
Some(Node::TraitItem(hir::TraitItem {
573+
kind: hir::TraitItemKind::Fn(hir::FnSig {
574+
decl: hir::FnDecl {
575+
inputs,
576+
..
577+
},
578+
..
579+
}, _),
580+
..
581+
})) => {
582+
let hir::Ty { span, .. } = inputs[local.index() - 1];
583+
Some(span)
584+
},
585+
_ => None,
586+
}
587+
})
588+
}
589+
_ => None
590+
}
591+
}))
592+
}
593+
_ => (false, None),
594+
}
595+
}
598596

599597
// point to span of upvar making closure call require mutable borrow
600598
fn show_mutating_upvar(

0 commit comments

Comments
 (0)