Skip to content

Commit 6e794dc

Browse files
Address review comments
1 parent 992ba80 commit 6e794dc

File tree

1 file changed

+29
-35
lines changed
  • compiler/rustc_infer/src/infer/error_reporting

1 file changed

+29
-35
lines changed

compiler/rustc_infer/src/infer/error_reporting/note.rs

+29-35
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
324324
&format!("`{}: {}`", sup, sub),
325325
);
326326
// We should only suggest rewriting the `where` clause if the predicate is within that `where` clause
327-
if self
328-
.tcx
329-
.hir()
330-
.get_generics(impl_item_def_id)
331-
.unwrap()
332-
.where_clause_span
333-
.contains(span)
327+
if let Some(generics) = self.tcx.hir().get_generics(impl_item_def_id)
328+
&& generics.where_clause_span.contains(span)
334329
{
335330
self.suggest_copy_trait_method_bounds(
336331
trait_item_def_id,
@@ -390,52 +385,51 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
390385
// but right now it's not really very smart when it comes to implicit `Sized`
391386
// predicates and bounds on the trait itself.
392387

393-
let impl_def_id =
394-
self.tcx.associated_item(impl_item_def_id).impl_container(self.tcx).unwrap();
395-
let trait_substs = self
388+
let Some(impl_def_id) =
389+
self.tcx.associated_item(impl_item_def_id).impl_container(self.tcx) else { return; };
390+
let Some(trait_ref) = self
396391
.tcx
397392
.impl_trait_ref(impl_def_id)
398-
.unwrap()
393+
else { return; };
394+
let trait_substs = trait_ref
399395
// Replace the explicit self type with `Self` for better suggestion rendering
400396
.with_self_ty(self.tcx, self.tcx.mk_ty_param(0, kw::SelfUpper))
401397
.substs;
402398
let trait_item_substs =
403399
ty::InternalSubsts::identity_for_item(self.tcx, impl_item_def_id.to_def_id())
404400
.rebase_onto(self.tcx, impl_def_id, trait_substs);
405401

406-
let mut is_suggestable = true;
407-
let trait_predicates = self
402+
let Ok(trait_predicates) = self
408403
.tcx
409404
.bound_explicit_predicates_of(trait_item_def_id)
410405
.map_bound(|p| p.predicates)
411406
.subst_iter_copied(self.tcx, trait_item_substs)
412407
.map(|(pred, _)| {
413-
if !pred.is_suggestable(self.tcx, false) {
414-
is_suggestable = false;
408+
if pred.is_suggestable(self.tcx, false) {
409+
Ok(pred.to_string())
410+
} else {
411+
Err(())
415412
}
416-
pred.to_string()
417413
})
418-
.collect::<Vec<_>>();
414+
.collect::<Result<Vec<_>, ()>>() else { return; };
419415

420-
let generics = self.tcx.hir().get_generics(impl_item_def_id).unwrap();
416+
let Some(generics) = self.tcx.hir().get_generics(impl_item_def_id) else { return; };
421417

422-
if is_suggestable {
423-
if trait_predicates.is_empty() {
424-
err.span_suggestion_verbose(
425-
generics.where_clause_span,
426-
"remove the `where` clause",
427-
String::new(),
428-
Applicability::MachineApplicable,
429-
);
430-
} else {
431-
let space = if generics.where_clause_span.is_empty() { " " } else { "" };
432-
err.span_suggestion_verbose(
433-
generics.where_clause_span,
434-
"copy the `where` clause predicates from the trait",
435-
format!("{space}where {}", trait_predicates.join(", ")),
436-
Applicability::MachineApplicable,
437-
);
438-
}
418+
if trait_predicates.is_empty() {
419+
err.span_suggestion_verbose(
420+
generics.where_clause_span,
421+
"remove the `where` clause",
422+
String::new(),
423+
Applicability::MachineApplicable,
424+
);
425+
} else {
426+
let space = if generics.where_clause_span.is_empty() { " " } else { "" };
427+
err.span_suggestion_verbose(
428+
generics.where_clause_span,
429+
"copy the `where` clause predicates from the trait",
430+
format!("{space}where {}", trait_predicates.join(", ")),
431+
Applicability::MachineApplicable,
432+
);
439433
}
440434
}
441435

0 commit comments

Comments
 (0)