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.
This commit is contained in:
Dylan DPC 2023-05-04 00:17:24 +05:30 committed by GitHub
commit a2e4dab3aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 15 deletions

View File

@ -2822,7 +2822,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// but has nested obligations which are unsatisfied. // but has nested obligations which are unsatisfied.
for (base_t, _) in self.autoderef(base.span, base_t).silence_errors() { for (base_t, _) in self.autoderef(base.span, base_t).silence_errors() {
if let Some((_, index_ty, element_ty)) = if let Some((_, index_ty, element_ty)) =
self.find_and_report_unsatisfied_index_impl(expr.hir_id, base, base_t) self.find_and_report_unsatisfied_index_impl(base, base_t)
{ {
self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No); self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
return element_ty; return element_ty;
@ -2881,7 +2881,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// predicates cause this to be, so that the user can add them to fix their code. /// predicates cause this to be, so that the user can add them to fix their code.
fn find_and_report_unsatisfied_index_impl( fn find_and_report_unsatisfied_index_impl(
&self, &self,
index_expr_hir_id: HirId,
base_expr: &hir::Expr<'_>, base_expr: &hir::Expr<'_>,
base_ty: Ty<'tcx>, base_ty: Ty<'tcx>,
) -> Option<(ErrorGuaranteed, Ty<'tcx>, Ty<'tcx>)> { ) -> Option<(ErrorGuaranteed, Ty<'tcx>, Ty<'tcx>)> {
@ -2914,13 +2913,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// in the first place. // in the first place.
ocx.register_obligations(traits::predicates_for_generics( ocx.register_obligations(traits::predicates_for_generics(
|idx, span| { |idx, span| {
traits::ObligationCause::new( cause.clone().derived_cause(
base_expr.span, ty::Binder::dummy(ty::TraitPredicate {
self.body_id, trait_ref: impl_trait_ref,
if span.is_dummy() { polarity: ty::ImplPolarity::Positive,
traits::ExprItemObligation(impl_def_id, index_expr_hir_id, idx) constness: ty::BoundConstness::NotConst,
} else { }),
traits::ExprBindingObligation(impl_def_id, span, index_expr_hir_id, idx) |derived| {
traits::ImplDerivedObligation(Box::new(
traits::ImplDerivedObligationCause {
derived,
impl_or_alias_def_id: impl_def_id,
impl_def_predicate_index: Some(idx),
span,
},
))
}, },
) )
}, },

View File

@ -4,11 +4,14 @@ error[E0277]: the trait bound `K: Hash` is not satisfied
LL | map[k] LL | map[k]
| ^^^ the trait `Hash` is not implemented for `K` | ^^^ the trait `Hash` is not implemented for `K`
| |
note: required by a bound in `<HashMap<K, V> as Index<&K>>` note: required for `HashMap<K, V>` to implement `Index<&K>`
--> $DIR/bad-index-due-to-nested.rs:9:8 --> $DIR/bad-index-due-to-nested.rs:7:12
| |
LL | impl<K, V> Index<&K> for HashMap<K, V>
| ^^^^^^^^^ ^^^^^^^^^^^^^
LL | where
LL | K: Hash, LL | K: Hash,
| ^^^^ required by this bound in `<HashMap<K, V> as Index<&K>>` | ---- unsatisfied trait bound introduced here
help: consider restricting type parameter `K` help: consider restricting type parameter `K`
| |
LL | fn index<'a, K: std::hash::Hash, V>(map: &'a HashMap<K, V>, k: K) -> &'a V { 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
LL | map[k] LL | map[k]
| ^^^ the trait `Copy` is not implemented for `V` | ^^^ the trait `Copy` is not implemented for `V`
| |
note: required by a bound in `<HashMap<K, V> as Index<&K>>` note: required for `HashMap<K, V>` to implement `Index<&K>`
--> $DIR/bad-index-due-to-nested.rs:10:8 --> $DIR/bad-index-due-to-nested.rs:7:12
| |
LL | impl<K, V> Index<&K> for HashMap<K, V>
| ^^^^^^^^^ ^^^^^^^^^^^^^
...
LL | V: Copy, LL | V: Copy,
| ^^^^ required by this bound in `<HashMap<K, V> as Index<&K>>` | ---- unsatisfied trait bound introduced here
help: consider restricting type parameter `V` help: consider restricting type parameter `V`
| |
LL | fn index<'a, K, V: std::marker::Copy>(map: &'a HashMap<K, V>, k: K) -> &'a V { LL | fn index<'a, K, V: std::marker::Copy>(map: &'a HashMap<K, V>, k: K) -> &'a V {