mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-12 08:36:03 +00:00
Merge #11713
11713: Complete associated consts in patterns r=jonas-schievink a=hi-rustin Try close https://github.com/rust-analyzer/rust-analyzer/issues/11555 Co-authored-by: hi-rustin <rustin.liu@gmail.com> Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
commit
849ac25f05
@ -137,25 +137,30 @@ fn pattern_path_completion(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Enum(e))) => {
|
res @ (hir::PathResolution::TypeParam(_)
|
||||||
cov_mark::hit!(enum_plain_qualified_use_tree);
|
| hir::PathResolution::SelfType(_)
|
||||||
e.variants(ctx.db)
|
| hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Struct(_)))
|
||||||
.into_iter()
|
| hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Enum(_)))
|
||||||
.for_each(|variant| acc.add_enum_variant(ctx, variant, None));
|
| hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Union(_)))) => {
|
||||||
}
|
|
||||||
res @ (hir::PathResolution::TypeParam(_) | hir::PathResolution::SelfType(_)) => {
|
|
||||||
let ty = match res {
|
let ty = match res {
|
||||||
hir::PathResolution::TypeParam(param) => param.ty(ctx.db),
|
hir::PathResolution::TypeParam(param) => param.ty(ctx.db),
|
||||||
hir::PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
|
hir::PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
|
||||||
|
hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Struct(s))) => {
|
||||||
|
s.ty(ctx.db)
|
||||||
|
}
|
||||||
|
hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Enum(e))) => {
|
||||||
|
cov_mark::hit!(enum_plain_qualified_use_tree);
|
||||||
|
e.variants(ctx.db)
|
||||||
|
.into_iter()
|
||||||
|
.for_each(|variant| acc.add_enum_variant(ctx, variant, None));
|
||||||
|
e.ty(ctx.db)
|
||||||
|
}
|
||||||
|
hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Union(u))) => {
|
||||||
|
u.ty(ctx.db)
|
||||||
|
}
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
|
|
||||||
e.variants(ctx.db)
|
|
||||||
.into_iter()
|
|
||||||
.for_each(|variant| acc.add_enum_variant(ctx, variant, None));
|
|
||||||
}
|
|
||||||
|
|
||||||
let traits_in_scope = ctx.scope.visible_traits();
|
let traits_in_scope = ctx.scope.visible_traits();
|
||||||
let mut seen = FxHashSet::default();
|
let mut seen = FxHashSet::default();
|
||||||
ty.iterate_path_candidates(
|
ty.iterate_path_candidates(
|
||||||
@ -165,12 +170,19 @@ fn pattern_path_completion(
|
|||||||
ctx.module,
|
ctx.module,
|
||||||
None,
|
None,
|
||||||
|item| {
|
|item| {
|
||||||
// Note associated consts cannot be referenced in patterns
|
match item {
|
||||||
if let AssocItem::TypeAlias(ta) = item {
|
AssocItem::TypeAlias(ta) => {
|
||||||
// We might iterate candidates of a trait multiple times here, so deduplicate them.
|
// We might iterate candidates of a trait multiple times here, so deduplicate them.
|
||||||
if seen.insert(item) {
|
if seen.insert(item) {
|
||||||
acc.add_type_alias(ctx, ta);
|
acc.add_type_alias(ctx, ta);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
AssocItem::Const(c) => {
|
||||||
|
if seen.insert(item) {
|
||||||
|
acc.add_const(ctx, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
None::<()>
|
None::<()>
|
||||||
},
|
},
|
||||||
|
@ -306,6 +306,7 @@ fn func() {
|
|||||||
ev TupleV(…) TupleV(u32)
|
ev TupleV(…) TupleV(u32)
|
||||||
ev RecordV {…} RecordV { field: u32 }
|
ev RecordV {…} RecordV { field: u32 }
|
||||||
ev UnitV UnitV
|
ev UnitV UnitV
|
||||||
|
ct ASSOC_CONST const ASSOC_CONST: ()
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -444,3 +445,75 @@ fn foo() {
|
|||||||
expect![[r#""#]],
|
expect![[r#""#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn completes_associated_const() {
|
||||||
|
check_empty(
|
||||||
|
r#"
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
|
struct Ty(u8);
|
||||||
|
|
||||||
|
impl Ty {
|
||||||
|
const ABC: Self = Self(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn f(t: Ty) {
|
||||||
|
match t {
|
||||||
|
Ty::$0 => {}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
ct ABC const ABC: Self
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
|
||||||
|
check_empty(
|
||||||
|
r#"
|
||||||
|
enum MyEnum {}
|
||||||
|
|
||||||
|
impl MyEnum {
|
||||||
|
pub const A: i32 = 123;
|
||||||
|
pub const B: i32 = 456;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn f(e: MyEnum) {
|
||||||
|
match e {
|
||||||
|
MyEnum::$0 => {}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
ct A pub const A: i32
|
||||||
|
ct B pub const B: i32
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
|
||||||
|
check_empty(
|
||||||
|
r#"
|
||||||
|
#[repr(C)]
|
||||||
|
union U {
|
||||||
|
i: i32,
|
||||||
|
f: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl U {
|
||||||
|
pub const C: i32 = 123;
|
||||||
|
pub const D: i32 = 456;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn f(u: U) {
|
||||||
|
match u {
|
||||||
|
U::$0 => {}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
ct C pub const C: i32
|
||||||
|
ct D pub const D: i32
|
||||||
|
"#]],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user