Skip to content

Commit a2e4dab

Browse files
authored
Rollup merge of #110874 - compiler-errors:index-op-specific, r=oli-obk
Adjust obligation cause code for `find_and_report_unsatisfied_index_impl` Makes the error message a bit easier to read.
2 parents 80df4ab + 20742ea commit a2e4dab

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,7 +2822,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28222822
// but has nested obligations which are unsatisfied.
28232823
for (base_t, _) in self.autoderef(base.span, base_t).silence_errors() {
28242824
if let Some((_, index_ty, element_ty)) =
2825-
self.find_and_report_unsatisfied_index_impl(expr.hir_id, base, base_t)
2825+
self.find_and_report_unsatisfied_index_impl(base, base_t)
28262826
{
28272827
self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
28282828
return element_ty;
@@ -2881,7 +2881,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28812881
/// predicates cause this to be, so that the user can add them to fix their code.
28822882
fn find_and_report_unsatisfied_index_impl(
28832883
&self,
2884-
index_expr_hir_id: HirId,
28852884
base_expr: &hir::Expr<'_>,
28862885
base_ty: Ty<'tcx>,
28872886
) -> Option<(ErrorGuaranteed, Ty<'tcx>, Ty<'tcx>)> {
@@ -2914,13 +2913,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29142913
// in the first place.
29152914
ocx.register_obligations(traits::predicates_for_generics(
29162915
|idx, span| {
2917-
traits::ObligationCause::new(
2918-
base_expr.span,
2919-
self.body_id,
2920-
if span.is_dummy() {
2921-
traits::ExprItemObligation(impl_def_id, index_expr_hir_id, idx)
2922-
} else {
2923-
traits::ExprBindingObligation(impl_def_id, span, index_expr_hir_id, idx)
2916+
cause.clone().derived_cause(
2917+
ty::Binder::dummy(ty::TraitPredicate {
2918+
trait_ref: impl_trait_ref,
2919+
polarity: ty::ImplPolarity::Positive,
2920+
constness: ty::BoundConstness::NotConst,
2921+
}),
2922+
|derived| {
2923+
traits::ImplDerivedObligation(Box::new(
2924+
traits::ImplDerivedObligationCause {
2925+
derived,
2926+
impl_or_alias_def_id: impl_def_id,
2927+
impl_def_predicate_index: Some(idx),
2928+
span,
2929+
},
2930+
))
29242931
},
29252932
)
29262933
},

tests/ui/typeck/bad-index-due-to-nested.stderr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ error[E0277]: the trait bound `K: Hash` is not satisfied
44
LL | map[k]
55
| ^^^ the trait `Hash` is not implemented for `K`
66
|
7-
note: required by a bound in `<HashMap<K, V> as Index<&K>>`
8-
--> $DIR/bad-index-due-to-nested.rs:9:8
7+
note: required for `HashMap<K, V>` to implement `Index<&K>`
8+
--> $DIR/bad-index-due-to-nested.rs:7:12
99
|
10+
LL | impl<K, V> Index<&K> for HashMap<K, V>
11+
| ^^^^^^^^^ ^^^^^^^^^^^^^
12+
LL | where
1013
LL | K: Hash,
11-
| ^^^^ required by this bound in `<HashMap<K, V> as Index<&K>>`
14+
| ---- unsatisfied trait bound introduced here
1215
help: consider restricting type parameter `K`
1316
|
1417
LL | fn index<'a, K: std::hash::Hash, V>(map: &'a HashMap<K, V>, k: K) -> &'a V {
@@ -20,11 +23,14 @@ error[E0277]: the trait bound `V: Copy` is not satisfied
2023
LL | map[k]
2124
| ^^^ the trait `Copy` is not implemented for `V`
2225
|
23-
note: required by a bound in `<HashMap<K, V> as Index<&K>>`
24-
--> $DIR/bad-index-due-to-nested.rs:10:8
26+
note: required for `HashMap<K, V>` to implement `Index<&K>`
27+
--> $DIR/bad-index-due-to-nested.rs:7:12
2528
|
29+
LL | impl<K, V> Index<&K> for HashMap<K, V>
30+
| ^^^^^^^^^ ^^^^^^^^^^^^^
31+
...
2632
LL | V: Copy,
27-
| ^^^^ required by this bound in `<HashMap<K, V> as Index<&K>>`
33+
| ---- unsatisfied trait bound introduced here
2834
help: consider restricting type parameter `V`
2935
|
3036
LL | fn index<'a, K, V: std::marker::Copy>(map: &'a HashMap<K, V>, k: K) -> &'a V {

0 commit comments

Comments
 (0)