Fix assoc type shorthand from method bounds

In code like this:
```rust
impl<T> Option<T> {
    fn as_deref(&self) -> T::Target where T: Deref {}
}
```

when trying to resolve the associated type `T::Target`, we were only
looking at the bounds on the impl (where the type parameter is defined),
but the method can add additional bounds that can also be used to refer
to associated types. Hence, when resolving such an associated type, it's
not enough to just know the type parameter T, we also need to know
exactly where we are currently.

This fixes #11364 (beta apparently switched some bounds around).
This commit is contained in:
Florian Diebold 2022-02-03 12:43:15 +01:00
parent 9cb6e3a190
commit 4ed5fe1554
9 changed files with 107 additions and 67 deletions

View file

@ -12,7 +12,7 @@ use hir_def::{
AsMacroCall, FunctionId, TraitId, VariantId,
};
use hir_expand::{name::AsName, ExpansionInfo, MacroCallId};
use hir_ty::{associated_type_shorthand_candidates, Interner};
use hir_ty::Interner;
use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::{smallvec, SmallVec};
@ -50,7 +50,7 @@ pub enum PathResolution {
}
impl PathResolution {
fn in_type_ns(&self) -> Option<TypeNs> {
pub(crate) fn in_type_ns(&self) -> Option<TypeNs> {
match self {
PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())),
PathResolution::Def(ModuleDef::BuiltinType(builtin)) => {
@ -80,18 +80,6 @@ impl PathResolution {
}
}
}
/// Returns an iterator over associated types that may be specified after this path (using
/// `Ty::Assoc` syntax).
pub fn assoc_type_shorthand_candidates<R>(
&self,
db: &dyn HirDatabase,
mut cb: impl FnMut(&Name, TypeAlias) -> Option<R>,
) -> Option<R> {
associated_type_shorthand_candidates(db, self.in_type_ns()?, |name, _, id| {
cb(name, id.into())
})
}
}
#[derive(Debug)]
@ -1314,4 +1302,20 @@ impl<'a> SemanticsScope<'a> {
let path = Path::from_src(path.clone(), &ctx)?;
resolve_hir_path(self.db, &self.resolver, &path)
}
/// Iterates over associated types that may be specified after the given path (using
/// `Ty::Assoc` syntax).
pub fn assoc_type_shorthand_candidates<R>(
&self,
resolution: &PathResolution,
mut cb: impl FnMut(&Name, TypeAlias) -> Option<R>,
) -> Option<R> {
let def = self.resolver.generic_def()?;
hir_ty::associated_type_shorthand_candidates(
self.db,
def,
resolution.in_type_ns()?,
|name, _, id| cb(name, id.into()),
)
}
}