Skip to content

Commit ac5366b

Browse files
committed
Taking review into account
1 parent 4d7e014 commit ac5366b

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+16-20
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,17 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
628628
if let Some(parent_trait_pred) = parent_trait_pred {
629629
real_trait_pred = parent_trait_pred;
630630
}
631-
let Some(real_ty) = real_trait_pred.self_ty().no_bound_vars() else {
632-
continue;
633-
};
631+
632+
// Skipping binder here, remapping below
633+
let real_ty = real_trait_pred.self_ty().skip_binder();
634634

635635
if let ty::Ref(region, base_ty, mutbl) = *real_ty.kind() {
636636
let mut autoderef = Autoderef::new(self, param_env, body_id, span, base_ty, span);
637637
if let Some(steps) = autoderef.find_map(|(ty, steps)| {
638638
// Re-add the `&`
639639
let ty = self.tcx.mk_ref(region, TypeAndMut { ty, mutbl });
640+
641+
// Remapping bound vars here
640642
let real_trait_pred_and_ty =
641643
real_trait_pred.map_bound(|inner_trait_pred| (inner_trait_pred, ty));
642644
let obligation = self
@@ -661,6 +663,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
661663
}
662664
} else if real_trait_pred != trait_pred {
663665
// This branch addresses #87437.
666+
667+
// Remapping bound vars here
664668
let real_trait_pred_and_base_ty =
665669
real_trait_pred.map_bound(|inner_trait_pred| (inner_trait_pred, base_ty));
666670
let obligation = self.mk_trait_obligation_with_new_self_ty(
@@ -723,6 +727,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
723727
err: &mut Diagnostic,
724728
trait_pred: ty::PolyTraitPredicate<'tcx>,
725729
) -> bool {
730+
// Skipping binder here, remapping below
726731
let self_ty = trait_pred.self_ty().skip_binder();
727732

728733
let (def_id, output_ty, callable) = match *self_ty.kind() {
@@ -732,8 +737,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
732737
};
733738
let msg = format!("use parentheses to call the {}", callable);
734739

740+
// "We should really create a single list of bound vars from the combined vars
741+
// from the predicate and function, but instead we just liberate the function bound vars"
735742
let output_ty = self.tcx.liberate_late_bound_regions(def_id, output_ty);
736743

744+
// Remapping bound vars here
737745
let trait_pred_and_self = trait_pred.map_bound(|trait_pred| (trait_pred, output_ty));
738746

739747
let new_obligation =
@@ -876,12 +884,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
876884
// Because of this, we modify the error to refer to the original obligation and
877885
// return early in the caller.
878886

879-
let msg = format!(
880-
"the trait bound `{}: {}` is not satisfied",
881-
// Safe to skip binder here
882-
old_pred.self_ty().skip_binder(),
883-
old_pred.print_modifiers_and_trait_path(),
884-
);
887+
let msg = format!("the trait bound `{}` is not satisfied", old_pred);
885888
if has_custom_message {
886889
err.note(&msg);
887890
} else {
@@ -997,7 +1000,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
9971000
return false;
9981001
}
9991002

1000-
// We skip binder here
1003+
// Skipping binder here, remapping below
10011004
let mut suggested_ty = trait_pred.self_ty().skip_binder();
10021005

10031006
for refs_remaining in 0..refs_number {
@@ -1006,7 +1009,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
10061009
};
10071010
suggested_ty = *inner_ty;
10081011

1009-
// We remap bounds here
1012+
// Remapping bound vars here
10101013
let trait_pred_and_suggested_ty =
10111014
trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
10121015

@@ -1132,22 +1135,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
11321135
return;
11331136
}
11341137

1138+
// Skipping binder here, remapping below
11351139
if let ty::Ref(region, t_type, mutability) = *trait_pred.skip_binder().self_ty().kind()
11361140
{
1137-
if region.is_late_bound() || t_type.has_escaping_bound_vars() {
1138-
// Avoid debug assertion in `mk_obligation_for_def_id`.
1139-
//
1140-
// If the self type has escaping bound vars then it's not
1141-
// going to be the type of an expression, so the suggestion
1142-
// probably won't apply anyway.
1143-
return;
1144-
}
1145-
11461141
let suggested_ty = match mutability {
11471142
hir::Mutability::Mut => self.tcx.mk_imm_ref(region, t_type),
11481143
hir::Mutability::Not => self.tcx.mk_mut_ref(region, t_type),
11491144
};
11501145

1146+
// Remapping bound vars here
11511147
let trait_pred_and_suggested_ty =
11521148
trait_pred.map_bound(|trait_pred| (trait_pred, suggested_ty));
11531149

src/test/ui/binop/issue-77910-1.stderr

+4-10
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ LL | assert_eq!(foo, y);
1212
error[E0277]: `for<'r> fn(&'r i32) -> &'r i32 {foo}` doesn't implement `Debug`
1313
--> $DIR/issue-77910-1.rs:8:5
1414
|
15+
LL | fn foo(s: &i32) -> &i32 {
16+
| --- consider calling this function
17+
...
1518
LL | assert_eq!(foo, y);
1619
| ^^^^^^^^^^^^^^^^^^ `for<'r> fn(&'r i32) -> &'r i32 {foo}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
1720
|
1821
= help: the trait `Debug` is not implemented for `for<'r> fn(&'r i32) -> &'r i32 {foo}`
19-
= help: the following other types implement trait `Debug`:
20-
extern "C" fn() -> Ret
21-
extern "C" fn(A) -> Ret
22-
extern "C" fn(A, ...) -> Ret
23-
extern "C" fn(A, B) -> Ret
24-
extern "C" fn(A, B, ...) -> Ret
25-
extern "C" fn(A, B, C) -> Ret
26-
extern "C" fn(A, B, C, ...) -> Ret
27-
extern "C" fn(A, B, C, D) -> Ret
28-
and 68 others
22+
= help: use parentheses to call the function: `foo(s)`
2923
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
3024

3125
error: aborting due to 2 previous errors

src/test/ui/suggestions/imm-ref-trait-object-literal-bound-regions.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | foo::<S>(s);
55
| ^^^^^^^^ the trait `for<'b> Trait` is not implemented for `&'b S`
66
|
77
= help: the trait `Trait` is implemented for `&'a mut S`
8+
= note: `for<'b> Trait` is implemented for `&'b mut S`, but not for `&'b S`
89
note: required by a bound in `foo`
910
--> $DIR/imm-ref-trait-object-literal-bound-regions.rs:11:20
1011
|

0 commit comments

Comments
 (0)