Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/hir-ty/src/method_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use hir_def::{
};
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_type_ir::{
TypeVisitableExt, VisitorResult,
TypeFoldable, TypeVisitableExt, VisitorResult,
fast_reject::{TreatParams, simplify_type},
inherent::{BoundExistentialPredicates, IntoKind},
try_visit,
Expand All @@ -49,6 +49,7 @@ use crate::{
SimplifiedType, SolverDefId, TraitRef, Ty, TyKind, TypingMode, Unnormalized,
infer::{
BoundRegionConversionTime, DbInternerInferExt, InferCtxt, InferOk,
resolve::ReplaceInferWithError,
select::ImplSource,
traits::{Obligation, ObligationCause, PredicateObligations},
},
Expand Down Expand Up @@ -510,8 +511,15 @@ pub(crate) fn find_matching_impl<'db>(
return None;
}

// Selection may leave region inference variables unresolved; replace them before they escape
// this inference context.
//
// FIXME: decide whether inferred regions should be replaced with error or erased.
match impl_source {
ImplSource::UserDefined(impl_source) => Some((impl_source.impl_def_id, impl_source.args)),
ImplSource::UserDefined(impl_source) => Some((
impl_source.impl_def_id,
impl_source.args.fold_with(&mut ReplaceInferWithError::new(infcx.interner)),
)),
ImplSource::Param(_) | ImplSource::Builtin(..) => None,
}
}
Expand Down
23 changes: 23 additions & 0 deletions crates/hir-ty/src/mir/eval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,29 @@ fn main() {
);
}

#[test]
fn region_infer_var_does_not_escape_impl_lookup() {
check_pass(
r#"
//- minicore: fn, sized

trait Bound<T> {}
trait Trait { fn f(self); }

struct S;

impl<'a> Bound<&'a ()> for S {}
impl<'a, T: Bound<&'a ()>> Trait for T {
fn f(self) { make::<'a>(); }
}

fn make<'a>() -> impl Fn(&'a ()) { |_| {} }

fn main() { S.f(); }
"#,
);
}

#[test]
fn index_of_slice_should_preserve_len() {
check_pass(
Expand Down