Skip to content

Commit 74d0866

Browse files
committedMar 7, 2022
Remove redundant code from copy-suggestions
1 parent ecb867e commit 74d0866

File tree

1 file changed

+55
-80
lines changed

1 file changed

+55
-80
lines changed
 

‎compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+55-80
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use rustc_middle::mir::{
1212
FakeReadCause, LocalDecl, LocalInfo, LocalKind, Location, Operand, Place, PlaceRef,
1313
ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind, VarBindingForm,
1414
};
15-
use rustc_middle::ty::{
16-
self, suggest_constraining_type_param, suggest_constraining_type_params, PredicateKind, Ty,
17-
};
15+
use rustc_middle::ty::{self, suggest_constraining_type_params, PredicateKind, Ty};
1816
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
1917
use rustc_span::symbol::sym;
2018
use rustc_span::{BytePos, MultiSpan, Span, DUMMY_SP};
@@ -410,86 +408,63 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
410408
Some(ref name) => format!("`{}`", name),
411409
None => "value".to_owned(),
412410
};
413-
if let ty::Param(param_ty) = ty.kind() {
414-
let tcx = self.infcx.tcx;
415-
let generics = tcx.generics_of(self.mir_def_id());
416-
let param = generics.type_param(&param_ty, tcx);
417-
if let Some(generics) = tcx
418-
.typeck_root_def_id(self.mir_def_id().to_def_id())
419-
.as_local()
420-
.and_then(|def_id| tcx.hir().get_generics(def_id))
421-
{
422-
suggest_constraining_type_param(
423-
tcx,
424-
generics,
425-
&mut err,
426-
param.name.as_str(),
427-
"Copy",
428-
None,
411+
412+
// Try to find predicates on *generic params* that would allow copying `ty`
413+
let tcx = self.infcx.tcx;
414+
let generics = tcx.generics_of(self.mir_def_id());
415+
if let Some(hir_generics) = tcx
416+
.typeck_root_def_id(self.mir_def_id().to_def_id())
417+
.as_local()
418+
.and_then(|def_id| tcx.hir().get_generics(def_id))
419+
{
420+
let predicates: Result<Vec<_>, _> = tcx.infer_ctxt().enter(|infcx| {
421+
let mut fulfill_cx =
422+
<dyn rustc_infer::traits::TraitEngine<'_>>::new(infcx.tcx);
423+
424+
let copy_did = infcx.tcx.lang_items().copy_trait().unwrap();
425+
let cause = ObligationCause::new(
426+
span,
427+
self.mir_hir_id(),
428+
rustc_infer::traits::ObligationCauseCode::MiscObligation,
429429
);
430-
}
431-
} else {
432-
// Try to find predicates on *generic params* that would allow copying `ty`
433-
434-
let tcx = self.infcx.tcx;
435-
let generics = tcx.generics_of(self.mir_def_id());
436-
if let Some(hir_generics) = tcx
437-
.typeck_root_def_id(self.mir_def_id().to_def_id())
438-
.as_local()
439-
.and_then(|def_id| tcx.hir().get_generics(def_id))
440-
{
441-
let predicates: Result<Vec<_>, _> = tcx.infer_ctxt().enter(|infcx| {
442-
let mut fulfill_cx =
443-
<dyn rustc_infer::traits::TraitEngine<'_>>::new(infcx.tcx);
444-
445-
let copy_did = infcx.tcx.lang_items().copy_trait().unwrap();
446-
let cause = ObligationCause::new(
447-
span,
448-
self.mir_hir_id(),
449-
rustc_infer::traits::ObligationCauseCode::MiscObligation,
450-
);
451-
fulfill_cx.register_bound(
452-
&infcx,
453-
self.param_env,
454-
// Erase any region vids from the type, which may not be resolved
455-
infcx.tcx.erase_regions(ty),
456-
copy_did,
457-
cause,
458-
);
459-
// Select all, including ambiguous predicates
460-
let errors = fulfill_cx.select_all_or_error(&infcx);
461-
462-
// Only emit suggestion if all required predicates are on generic
463-
errors
464-
.into_iter()
465-
.map(|err| match err.obligation.predicate.kind().skip_binder() {
466-
PredicateKind::Trait(predicate) => {
467-
match predicate.self_ty().kind() {
468-
ty::Param(param_ty) => Ok((
469-
generics.type_param(param_ty, tcx),
470-
predicate
471-
.trait_ref
472-
.print_only_trait_path()
473-
.to_string(),
474-
)),
475-
_ => Err(()),
476-
}
430+
fulfill_cx.register_bound(
431+
&infcx,
432+
self.param_env,
433+
// Erase any region vids from the type, which may not be resolved
434+
infcx.tcx.erase_regions(ty),
435+
copy_did,
436+
cause,
437+
);
438+
// Select all, including ambiguous predicates
439+
let errors = fulfill_cx.select_all_or_error(&infcx);
440+
441+
// Only emit suggestion if all required predicates are on generic
442+
errors
443+
.into_iter()
444+
.map(|err| match err.obligation.predicate.kind().skip_binder() {
445+
PredicateKind::Trait(predicate) => {
446+
match predicate.self_ty().kind() {
447+
ty::Param(param_ty) => Ok((
448+
generics.type_param(param_ty, tcx),
449+
predicate.trait_ref.print_only_trait_path().to_string(),
450+
)),
451+
_ => Err(()),
477452
}
478-
_ => Err(()),
479-
})
480-
.collect()
481-
});
453+
}
454+
_ => Err(()),
455+
})
456+
.collect()
457+
});
482458

483-
if let Ok(predicates) = predicates {
484-
suggest_constraining_type_params(
485-
tcx,
486-
hir_generics,
487-
&mut err,
488-
predicates.iter().map(|(param, constraint)| {
489-
(param.name.as_str(), &**constraint, None)
490-
}),
491-
);
492-
}
459+
if let Ok(predicates) = predicates {
460+
suggest_constraining_type_params(
461+
tcx,
462+
hir_generics,
463+
&mut err,
464+
predicates.iter().map(|(param, constraint)| {
465+
(param.name.as_str(), &**constraint, None)
466+
}),
467+
);
493468
}
494469
}
495470

0 commit comments

Comments
 (0)