mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Resolve associated types with type anchors
This commit is contained in:
parent
444f6caaba
commit
9fe10a9606
@ -20,7 +20,7 @@ use hir_def::{
|
|||||||
use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
|
use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
|
||||||
use hir_ty::{
|
use hir_ty::{
|
||||||
diagnostics::{record_literal_missing_fields, record_pattern_missing_fields},
|
diagnostics::{record_literal_missing_fields, record_pattern_missing_fields},
|
||||||
InferenceResult, Substitution,
|
InferenceResult, Substitution, TyLoweringContext,
|
||||||
};
|
};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
@ -466,7 +466,20 @@ fn resolve_hir_path_(
|
|||||||
prefer_value_ns: bool,
|
prefer_value_ns: bool,
|
||||||
) -> Option<PathResolution> {
|
) -> Option<PathResolution> {
|
||||||
let types = || {
|
let types = || {
|
||||||
let (ty, remaining) = resolver.resolve_path_in_type_ns(db.upcast(), path.mod_path())?;
|
let (ty, unresolved) = match path.type_anchor() {
|
||||||
|
Some(type_ref) => {
|
||||||
|
let (_, res) = TyLoweringContext::new(db, resolver).lower_ty_ext(type_ref);
|
||||||
|
res.map(|ty_ns| (ty_ns, path.segments().first()))
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let (ty, remaining) =
|
||||||
|
resolver.resolve_path_in_type_ns(db.upcast(), path.mod_path())?;
|
||||||
|
match remaining {
|
||||||
|
Some(remaining) if remaining > 1 => None,
|
||||||
|
_ => Some((ty, path.segments().get(1))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}?;
|
||||||
let res = match ty {
|
let res = match ty {
|
||||||
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
|
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
|
||||||
TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
|
TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
|
||||||
@ -478,18 +491,14 @@ fn resolve_hir_path_(
|
|||||||
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
|
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
|
||||||
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
|
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
|
||||||
};
|
};
|
||||||
match remaining {
|
match unresolved {
|
||||||
Some(1) => {
|
Some(unresolved) => res
|
||||||
let unresolved = path.segments().get(1)?;
|
.assoc_type_shorthand_candidates(db, |name, alias| {
|
||||||
res.assoc_type_shorthand_candidates(db, |name, alias| {
|
|
||||||
(name == unresolved.name).then(|| alias)
|
(name == unresolved.name).then(|| alias)
|
||||||
})
|
})
|
||||||
.map(TypeAlias::from)
|
.map(TypeAlias::from)
|
||||||
.map(Into::into)
|
.map(Into::into)
|
||||||
.map(PathResolution::Def)
|
.map(PathResolution::Def),
|
||||||
}
|
|
||||||
// ambiguous
|
|
||||||
Some(_) => None,
|
|
||||||
None => Some(res),
|
None => Some(res),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -146,7 +146,7 @@ impl<'a> TyLoweringContext<'a> {
|
|||||||
self.lower_ty_ext(type_ref).0
|
self.lower_ty_ext(type_ref).0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lower_ty_ext(&self, type_ref: &TypeRef) -> (Ty, Option<TypeNs>) {
|
pub fn lower_ty_ext(&self, type_ref: &TypeRef) -> (Ty, Option<TypeNs>) {
|
||||||
let mut res = None;
|
let mut res = None;
|
||||||
let ty = match type_ref {
|
let ty = match type_ref {
|
||||||
TypeRef::Never => TyKind::Never.intern(&Interner),
|
TypeRef::Never => TyKind::Never.intern(&Interner),
|
||||||
|
@ -3841,6 +3841,27 @@ fn foo() {}
|
|||||||
r#"
|
r#"
|
||||||
fn foo<T: A>() where T::Assoc$0: {}
|
fn foo<T: A>() where T::Assoc$0: {}
|
||||||
|
|
||||||
|
trait A {
|
||||||
|
type Assoc;
|
||||||
|
}"#,
|
||||||
|
expect![[r#"
|
||||||
|
*Assoc*
|
||||||
|
|
||||||
|
```rust
|
||||||
|
test
|
||||||
|
```
|
||||||
|
|
||||||
|
```rust
|
||||||
|
type Assoc
|
||||||
|
```
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
check(
|
||||||
|
r#"
|
||||||
|
fn foo<T: A>() {
|
||||||
|
let _: <T>::Assoc$0;
|
||||||
|
}
|
||||||
|
|
||||||
trait A {
|
trait A {
|
||||||
type Assoc;
|
type Assoc;
|
||||||
}"#,
|
}"#,
|
||||||
@ -3874,6 +3895,6 @@ trait A where
|
|||||||
type Assoc
|
type Assoc
|
||||||
```
|
```
|
||||||
"#]],
|
"#]],
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user