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
44 changes: 22 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ vfs-notify = { path = "./crates/vfs-notify", version = "0.0.0" }
vfs = { path = "./crates/vfs", version = "0.0.0" }
edition = { path = "./crates/edition", version = "0.0.0" }

ra-ap-rustc_lexer = { version = "0.165", default-features = false }
ra-ap-rustc_parse_format = { version = "0.165", default-features = false }
ra-ap-rustc_index = { version = "0.165", default-features = false }
ra-ap-rustc_abi = { version = "0.165", default-features = false }
ra-ap-rustc_pattern_analysis = { version = "0.165", default-features = false }
ra-ap-rustc_ast_ir = { version = "0.165", default-features = false }
ra-ap-rustc_type_ir = { version = "0.165", default-features = false }
ra-ap-rustc_next_trait_solver = { version = "0.165", default-features = false }
ra-ap-rustc_lexer = { version = "0.166", default-features = false }
ra-ap-rustc_parse_format = { version = "0.166", default-features = false }
ra-ap-rustc_index = { version = "0.166", default-features = false }
ra-ap-rustc_abi = { version = "0.166", default-features = false }
ra-ap-rustc_pattern_analysis = { version = "0.166", default-features = false }
ra-ap-rustc_ast_ir = { version = "0.166", default-features = false }
ra-ap-rustc_type_ir = { version = "0.166", default-features = false }
ra-ap-rustc_next_trait_solver = { version = "0.166", default-features = false }

# local crates that aren't published to crates.io. These should not have versions.

Expand Down
45 changes: 30 additions & 15 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,
TypeVisitableExt, VisitorResult,
fast_reject::{TreatParams, simplify_type},
inherent::{BoundExistentialPredicates, IntoKind},
};
Expand All @@ -54,6 +54,7 @@ use crate::{
obligation_ctxt::ObligationCtxt,
util::clauses_as_obligations,
},
ret,
traits::ParamEnvAndCrate,
};

Expand Down Expand Up @@ -835,27 +836,34 @@ impl<'db> TraitImpls<'db> {
}
}

pub fn for_each_crate_and_block(
pub fn for_each_crate_and_block<R: VisitorResult>(
db: &'db dyn HirDatabase,
krate: Crate,
block: Option<BlockIdLt<'db>>,
for_each: &mut dyn FnMut(&TraitImpls<'db>),
) {
for_each: &mut dyn FnMut(&TraitImpls<'db>) -> R,
) -> R {
let blocks = std::iter::successors(block, |block| block.module(db).block(db));
blocks.filter_map(|block| Self::for_block(db, block)).for_each(&mut *for_each);
Self::for_crate_and_deps(db, krate).iter().map(|it| &**it).for_each(for_each);
for impl_ in blocks.filter_map(|block| Self::for_block(db, block)) {
ret!(for_each(impl_));
}
for impl_ in Self::for_crate_and_deps(db, krate) {
ret!(for_each(impl_));
}
R::output()
}

/// Like [`Self::for_each_crate_and_block()`], but takes in account two blocks, one for a trait and one for a self type.
pub fn for_each_crate_and_block_trait_and_type(
pub fn for_each_crate_and_block_trait_and_type<R: VisitorResult>(
db: &'db dyn HirDatabase,
krate: Crate,
type_block: Option<BlockIdLt<'db>>,
trait_block: Option<BlockIdLt<'db>>,
for_each: &mut dyn FnMut(&TraitImpls<'db>),
) {
for_each: &mut dyn FnMut(&TraitImpls<'db>) -> R,
) -> R {
let in_self_and_deps = TraitImpls::for_crate_and_deps(db, krate);
in_self_and_deps.iter().for_each(|impls| for_each(impls));
for impl_ in in_self_and_deps {
ret!(for_each(impl_));
}

// We must not provide duplicate impls to the solver. Therefore we work with the following strategy:
// start from each block, and walk ancestors until you meet the other block. If they never meet,
Expand All @@ -874,13 +882,20 @@ impl<'db> TraitImpls<'db> {
.filter_map(move |block| TraitImpls::for_block(db, block))
};
if trait_block == type_block {
blocks_iter(trait_block)
.filter_map(|block| TraitImpls::for_block(db, block))
.for_each(for_each);
for impl_ in
blocks_iter(trait_block).filter_map(|block| TraitImpls::for_block(db, block))
{
ret!(for_each(impl_));
}
} else {
for_each_block(trait_block, type_block).for_each(&mut *for_each);
for_each_block(type_block, trait_block).for_each(for_each);
for impl_ in for_each_block(trait_block, type_block) {
ret!(for_each(impl_));
}
for impl_ in for_each_block(type_block, trait_block) {
ret!(for_each(impl_));
}
}
R::output()
}
}

Expand Down
37 changes: 23 additions & 14 deletions crates/hir-ty/src/next_solver/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use rustc_index::bit_set::DenseBitSet;
use rustc_type_ir::{
AliasTy, BoundVar, CoroutineWitnessTypes, DebruijnIndex, EarlyBinder, FlagComputation, Flags,
FnSigKind, GenericArgKind, GenericTypeVisitable, ImplPolarity, InferTy, Interner, TraitRef,
TypeFlags, TypeVisitableExt, Upcast, Variance,
TypeFlags, TypeVisitableExt, Upcast, Variance, VisitorResult,
elaborate::elaborate,
error::TypeError,
fast_reject,
Expand All @@ -54,6 +54,7 @@ use crate::{
TraitAssocTyId, TraitIdWrapper, TypeAliasIdWrapper, UnevaluatedConst, Unnormalized,
util::{explicit_item_bounds, explicit_item_self_bounds},
},
ret,
};

use super::{
Expand Down Expand Up @@ -1601,12 +1602,12 @@ impl<'db> Interner for DbInterner<'db> {
def_id.0.trait_items(self.db()).associated_types().map(|id| id.into())
}

fn for_each_relevant_impl(
fn for_each_relevant_impl<R: VisitorResult>(
self,
trait_def_id: Self::TraitId,
self_ty: Self::Ty,
mut f: impl FnMut(Self::ImplId),
) {
mut f: impl FnMut(Self::ImplId) -> R,
) -> R {
let krate = self.krate.expect("trait solving requires setting `DbInterner::krate`");
let trait_block = trait_def_id.0.loc(self.db).container.block(self.db);
let mut consider_impls_for_simplified_type = |simp: SimplifiedType<'_>| {
Expand Down Expand Up @@ -1641,13 +1642,14 @@ impl<'db> Interner for DbInterner<'db> {
let (regular_impls, builtin_derive_impls) =
impls.for_trait_and_self_ty(trait_def_id.0, &simp);
for &impl_ in regular_impls {
f(impl_.into());
ret!(f(impl_.into()));
}
for &impl_ in builtin_derive_impls {
f(impl_.into());
ret!(f(impl_.into()));
}
R::output()
},
);
)
};

match self_ty.kind() {
Expand Down Expand Up @@ -1676,7 +1678,7 @@ impl<'db> Interner for DbInterner<'db> {
let simp =
fast_reject::simplify_type(self, self_ty, fast_reject::TreatParams::AsRigid)
.unwrap();
consider_impls_for_simplified_type(simp);
ret!(consider_impls_for_simplified_type(simp));
}

// HACK: For integer and float variables we have to manually look at all impls
Expand Down Expand Up @@ -1704,7 +1706,7 @@ impl<'db> Interner for DbInterner<'db> {
SimplifiedType::Uint(Usize),
];
for simp in possible_integers {
consider_impls_for_simplified_type(simp);
ret!(consider_impls_for_simplified_type(simp));
}
}

Expand All @@ -1719,7 +1721,7 @@ impl<'db> Interner for DbInterner<'db> {
];

for simp in possible_floats {
consider_impls_for_simplified_type(simp);
ret!(consider_impls_for_simplified_type(simp));
}
}

Expand Down Expand Up @@ -1748,15 +1750,22 @@ impl<'db> Interner for DbInterner<'db> {
self.for_each_blanket_impl(trait_def_id, f)
}

fn for_each_blanket_impl(self, trait_def_id: Self::TraitId, mut f: impl FnMut(Self::ImplId)) {
let Some(krate) = self.krate else { return };
fn for_each_blanket_impl<R: VisitorResult>(
self,
trait_def_id: Self::TraitId,
mut f: impl FnMut(Self::ImplId) -> R,
) -> R {
let Some(krate) = self.krate else {
return R::output();
};
let block = trait_def_id.0.loc(self.db).container.block(self.db);

TraitImpls::for_each_crate_and_block(self.db, krate, block, &mut |impls| {
for &impl_ in impls.blanket_impls(trait_def_id.0) {
f(impl_.into());
ret!(f(impl_.into()));
}
});
R::output()
})
}

fn has_item_definition(self, _def_id: Self::ImplOrTraitAssocTermId) -> bool {
Expand Down
12 changes: 12 additions & 0 deletions crates/hir-ty/src/next_solver/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,15 @@ pub(crate) fn clauses_as_obligations<'db>(
recursion_depth: 0,
})
}

/// Copied from
/// <https://github.com/jdonszelmann/rust/blob/180725cff61d00dd1b9c35fc720a8befaec5d46b/compiler/rustc_middle/src/ty/context/impl_interner.rs#L534-L541>
#[macro_export]
macro_rules! ret {
($e: expr) => {
match $e.branch() {
::std::ops::ControlFlow::Break(b) => return R::from_residual(b),
::std::ops::ControlFlow::Continue(()) => {}
}
};
}