Skip to content

Commit 66544b5

Browse files
authored
Rollup merge of #105985 - compiler-errors:method-chain-nitpicks, r=estebank
Method chain nitpicks Just fixing some little things I didn't see in review from that method chain PR. r? `@estebank`
2 parents ec7eb5b + 85a9d85 commit 66544b5

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,27 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> {
1414
fn tag(&self) -> &'static str {
1515
"CollectAllMismatches"
1616
}
17+
1718
fn tcx(&self) -> TyCtxt<'tcx> {
1819
self.infcx.tcx
1920
}
21+
2022
fn intercrate(&self) -> bool {
2123
false
2224
}
25+
2326
fn param_env(&self) -> ty::ParamEnv<'tcx> {
2427
self.param_env
2528
}
29+
2630
fn a_is_expected(&self) -> bool {
2731
true
28-
} // irrelevant
32+
}
33+
2934
fn mark_ambiguous(&mut self) {
3035
bug!()
3136
}
37+
3238
fn relate_with_variance<T: Relate<'tcx>>(
3339
&mut self,
3440
_: ty::Variance,
@@ -38,22 +44,28 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> {
3844
) -> RelateResult<'tcx, T> {
3945
self.relate(a, b)
4046
}
47+
4148
fn regions(
4249
&mut self,
4350
a: ty::Region<'tcx>,
4451
_b: ty::Region<'tcx>,
4552
) -> RelateResult<'tcx, ty::Region<'tcx>> {
4653
Ok(a)
4754
}
55+
4856
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
49-
if a == b || matches!(a.kind(), ty::Infer(_)) || matches!(b.kind(), ty::Infer(_)) {
50-
return Ok(a);
51-
}
52-
relate::super_relate_tys(self, a, b).or_else(|e| {
53-
self.errors.push(e);
54-
Ok(a)
57+
self.infcx.probe(|_| {
58+
if a.is_ty_infer() || b.is_ty_infer() {
59+
Ok(a)
60+
} else {
61+
self.infcx.super_combine_tys(self, a, b).or_else(|e| {
62+
self.errors.push(e);
63+
Ok(a)
64+
})
65+
}
5566
})
5667
}
68+
5769
fn consts(
5870
&mut self,
5971
a: ty::Const<'tcx>,
@@ -64,6 +76,7 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> {
6476
}
6577
relate::super_relate_consts(self, a, b) // could do something similar here for constants!
6678
}
79+
6780
fn binders<T: Relate<'tcx>>(
6881
&mut self,
6982
a: ty::Binder<'tcx, T>,

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+20-31
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ pub trait TypeErrCtxtExt<'tcx> {
335335
err: &mut Diagnostic,
336336
trait_pred: ty::PolyTraitPredicate<'tcx>,
337337
);
338-
fn function_argument_obligation(
338+
fn note_function_argument_obligation(
339339
&self,
340340
arg_hir_id: HirId,
341341
err: &mut Diagnostic,
@@ -2909,7 +2909,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
29092909
ref parent_code,
29102910
..
29112911
} => {
2912-
self.function_argument_obligation(
2912+
self.note_function_argument_obligation(
29132913
arg_hir_id,
29142914
err,
29152915
parent_code,
@@ -3141,23 +3141,20 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
31413141
);
31423142
}
31433143
}
3144-
fn function_argument_obligation(
3144+
fn note_function_argument_obligation(
31453145
&self,
31463146
arg_hir_id: HirId,
31473147
err: &mut Diagnostic,
31483148
parent_code: &ObligationCauseCode<'tcx>,
31493149
param_env: ty::ParamEnv<'tcx>,
3150-
predicate: ty::Predicate<'tcx>,
3150+
failed_pred: ty::Predicate<'tcx>,
31513151
call_hir_id: HirId,
31523152
) {
31533153
let tcx = self.tcx;
31543154
let hir = tcx.hir();
3155-
if let Some(Node::Expr(expr)) = hir.find(arg_hir_id) {
3156-
let parent_id = hir.get_parent_item(arg_hir_id);
3157-
let typeck_results: &TypeckResults<'tcx> = match &self.typeck_results {
3158-
Some(t) if t.hir_owner == parent_id => t,
3159-
_ => self.tcx.typeck(parent_id.def_id),
3160-
};
3155+
if let Some(Node::Expr(expr)) = hir.find(arg_hir_id)
3156+
&& let Some(typeck_results) = &self.typeck_results
3157+
{
31613158
if let hir::Expr { kind: hir::ExprKind::Block(..), .. } = expr {
31623159
let expr = expr.peel_blocks();
31633160
let ty = typeck_results.expr_ty_adjusted_opt(expr).unwrap_or(tcx.ty_error());
@@ -3182,37 +3179,29 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
31823179
let mut type_diffs = vec![];
31833180

31843181
if let ObligationCauseCode::ExprBindingObligation(def_id, _, _, idx) = parent_code.deref()
3185-
&& let predicates = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx)
3186-
&& let Some(pred) = predicates.predicates.get(*idx)
3182+
&& let Some(node_substs) = typeck_results.node_substs_opt(call_hir_id)
3183+
&& let where_clauses = self.tcx.predicates_of(def_id).instantiate(self.tcx, node_substs)
3184+
&& let Some(where_pred) = where_clauses.predicates.get(*idx)
31873185
{
3188-
if let Ok(trait_pred) = pred.kind().try_map_bound(|pred| match pred {
3189-
ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) => Ok(trait_pred),
3190-
_ => Err(()),
3191-
})
3192-
&& let Ok(trait_predicate) = predicate.kind().try_map_bound(|pred| match pred {
3193-
ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) => Ok(trait_pred),
3194-
_ => Err(()),
3195-
})
3186+
if let Some(where_pred) = where_pred.to_opt_poly_trait_pred()
3187+
&& let Some(failed_pred) = failed_pred.to_opt_poly_trait_pred()
31963188
{
31973189
let mut c = CollectAllMismatches {
31983190
infcx: self.infcx,
31993191
param_env,
32003192
errors: vec![],
32013193
};
3202-
if let Ok(_) = c.relate(trait_pred, trait_predicate) {
3194+
if let Ok(_) = c.relate(where_pred, failed_pred) {
32033195
type_diffs = c.errors;
32043196
}
3205-
} else if let ty::PredicateKind::Clause(
3206-
ty::Clause::Projection(proj)
3207-
) = pred.kind().skip_binder()
3208-
&& let ty::PredicateKind::Clause(
3209-
ty::Clause::Projection(projection)
3210-
) = predicate.kind().skip_binder()
3197+
} else if let Some(where_pred) = where_pred.to_opt_poly_projection_pred()
3198+
&& let Some(failed_pred) = failed_pred.to_opt_poly_projection_pred()
3199+
&& let Some(found) = failed_pred.skip_binder().term.ty()
32113200
{
32123201
type_diffs = vec![
32133202
Sorts(ty::error::ExpectedFound {
3214-
expected: self.tcx.mk_ty(ty::Alias(ty::Projection, proj.projection_ty)),
3215-
found: projection.term.ty().unwrap(),
3203+
expected: self.tcx.mk_ty(ty::Alias(ty::Projection, where_pred.skip_binder().projection_ty)),
3204+
found,
32163205
}),
32173206
];
32183207
}
@@ -3227,9 +3216,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
32273216
// If the expression we're calling on is a binding, we want to point at the
32283217
// `let` when talking about the type. Otherwise we'll point at every part
32293218
// of the method chain with the type.
3230-
self.point_at_chain(binding_expr, typeck_results, type_diffs, param_env, err);
3219+
self.point_at_chain(binding_expr, &typeck_results, type_diffs, param_env, err);
32313220
} else {
3232-
self.point_at_chain(expr, typeck_results, type_diffs, param_env, err);
3221+
self.point_at_chain(expr, &typeck_results, type_diffs, param_env, err);
32333222
}
32343223
}
32353224
let call_node = hir.find(call_hir_id);

Diff for: src/test/ui/iterators/invalid-iterator-chain.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ LL | .sum::<i32>(),
5252
<i32 as Sum<&'a i32>>
5353
<i32 as Sum>
5454
note: the method call chain might not have had the expected associated types
55-
--> $DIR/invalid-iterator-chain.rs:20:14
55+
--> $DIR/invalid-iterator-chain.rs:25:14
5656
|
5757
LL | vec![0, 1]
5858
| ---------- this expression has type `Vec<{integer}>`
5959
LL | .iter()
6060
| ------ `Iterator::Item` is `&{integer}` here
6161
LL | .map(|x| x * 2)
62-
| ^^^^^^^^^^^^^^ `Iterator::Item` changed to `{integer}` here
62+
| -------------- `Iterator::Item` changed to `{integer}` here
6363
LL | .map(|x| x as f64)
6464
| ----------------- `Iterator::Item` changed to `f64` here
6565
LL | .map(|x| x as i64)
@@ -84,14 +84,14 @@ LL | .sum::<i32>(),
8484
<i32 as Sum<&'a i32>>
8585
<i32 as Sum>
8686
note: the method call chain might not have had the expected associated types
87-
--> $DIR/invalid-iterator-chain.rs:32:14
87+
--> $DIR/invalid-iterator-chain.rs:33:14
8888
|
8989
LL | vec![0, 1]
9090
| ---------- this expression has type `Vec<{integer}>`
9191
LL | .iter()
9292
| ------ `Iterator::Item` is `&{integer}` here
9393
LL | .map(|x| x * 2)
94-
| ^^^^^^^^^^^^^^ `Iterator::Item` changed to `{integer}` here
94+
| -------------- `Iterator::Item` changed to `{integer}` here
9595
LL | .map(|x| x as f64)
9696
| ^^^^^^^^^^^^^^^^^ `Iterator::Item` changed to `f64` here
9797
LL | .filter(|x| *x > 0.0)

0 commit comments

Comments
 (0)