Skip to content

Commit ab9109a

Browse files
authored
Rollup merge of #141333 - compiler-errors:param-env-candidate-unnorm, r=lcnr
Use `DeepRejectCtxt` in `assemble_inherent_candidates_from_param` Fixes rust-lang/trait-system-refactor-initiative#214 We were not properly considering unnormalized param-env candidates in `assemble_inherent_candidates_from_param`. r? lcnr
2 parents 6606fd9 + ad59f0b commit ab9109a

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(rustc::diagnostic_outside_of_impl)]
33
#![allow(rustc::untranslatable_diagnostic)]
44
#![feature(array_windows)]
5+
#![feature(assert_matches)]
56
#![feature(box_patterns)]
67
#![feature(if_let_guard)]
78
#![feature(iter_intersperse)]

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::debug_assert_matches;
12
use std::cell::{Cell, RefCell};
23
use std::cmp::max;
34
use std::ops::Deref;
@@ -16,7 +17,7 @@ use rustc_infer::traits::ObligationCauseCode;
1617
use rustc_middle::middle::stability;
1718
use rustc_middle::query::Providers;
1819
use rustc_middle::ty::elaborate::supertrait_def_ids;
19-
use rustc_middle::ty::fast_reject::{TreatParams, simplify_type};
20+
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams, simplify_type};
2021
use rustc_middle::ty::{
2122
self, AssocItem, AssocItemContainer, GenericArgs, GenericArgsRef, GenericParamDefKind,
2223
ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt, Upcast,
@@ -807,8 +808,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
807808
);
808809
}
809810
}
810-
ty::Param(p) => {
811-
self.assemble_inherent_candidates_from_param(p);
811+
ty::Param(_) => {
812+
self.assemble_inherent_candidates_from_param(raw_self_ty);
812813
}
813814
ty::Bool
814815
| ty::Char
@@ -909,18 +910,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
909910
}
910911

911912
#[instrument(level = "debug", skip(self))]
912-
fn assemble_inherent_candidates_from_param(&mut self, param_ty: ty::ParamTy) {
913+
fn assemble_inherent_candidates_from_param(&mut self, param_ty: Ty<'tcx>) {
914+
debug_assert_matches!(param_ty.kind(), ty::Param(_));
915+
916+
let tcx = self.tcx;
913917
let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| {
914918
let bound_predicate = predicate.kind();
915919
match bound_predicate.skip_binder() {
916-
ty::ClauseKind::Trait(trait_predicate) => {
917-
match *trait_predicate.trait_ref.self_ty().kind() {
918-
ty::Param(p) if p == param_ty => {
919-
Some(bound_predicate.rebind(trait_predicate.trait_ref))
920-
}
921-
_ => None,
922-
}
923-
}
920+
ty::ClauseKind::Trait(trait_predicate) => DeepRejectCtxt::relate_rigid_rigid(tcx)
921+
.types_may_unify(param_ty, trait_predicate.trait_ref.self_ty())
922+
.then(|| bound_predicate.rebind(trait_predicate.trait_ref)),
924923
ty::ClauseKind::RegionOutlives(_)
925924
| ty::ClauseKind::TypeOutlives(_)
926925
| ty::ClauseKind::Projection(_)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Regression test for <https://github.com/rust-lang/trait-system-refactor-initiative/issues/214>.
7+
// See comment below.
8+
9+
trait A {
10+
fn hello(&self) {}
11+
}
12+
13+
trait B {
14+
fn hello(&self) {}
15+
}
16+
17+
impl<T> A for T {}
18+
impl<T> B for T {}
19+
20+
fn test<F, R>(q: F::Item)
21+
where
22+
F: Iterator<Item = R>,
23+
// We want to prefer `A` for `R.hello()`
24+
F::Item: A,
25+
{
26+
q.hello();
27+
}
28+
29+
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ check-pass
2+
//@ compile-flags: -Znext-solver
3+
4+
// Regression test for <https://github.com/rust-lang/trait-system-refactor-initiative/issues/214>.
5+
6+
fn execute<K, F, R>(q: F::Item) -> R
7+
where
8+
F: Iterator<Item = R>,
9+
// Both of the below bounds should be considered for `.into()`, and then be combined
10+
// into a single `R: Into<?0>` bound which can be inferred to `?0 = R`.
11+
F::Item: Into<K>,
12+
R: Into<String>,
13+
{
14+
q.into()
15+
}
16+
17+
fn main() {}

0 commit comments

Comments
 (0)