Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 849ac25

Browse files
bors[bot]0xPoejonas-schievink
authored
11713: Complete associated consts in patterns r=jonas-schievink a=hi-rustin Try close rust-lang/rust-analyzer#11555 Co-authored-by: hi-rustin <rustin.liu@gmail.com> Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents 6ad8c02 + e756408 commit 849ac25

File tree

2 files changed

+103
-18
lines changed

2 files changed

+103
-18
lines changed

crates/ide_completion/src/completions/pattern.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,30 @@ fn pattern_path_completion(
137137
}
138138
}
139139
}
140-
hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Enum(e))) => {
141-
cov_mark::hit!(enum_plain_qualified_use_tree);
142-
e.variants(ctx.db)
143-
.into_iter()
144-
.for_each(|variant| acc.add_enum_variant(ctx, variant, None));
145-
}
146-
res @ (hir::PathResolution::TypeParam(_) | hir::PathResolution::SelfType(_)) => {
140+
res @ (hir::PathResolution::TypeParam(_)
141+
| hir::PathResolution::SelfType(_)
142+
| hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Struct(_)))
143+
| hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Enum(_)))
144+
| hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Union(_)))) => {
147145
let ty = match res {
148146
hir::PathResolution::TypeParam(param) => param.ty(ctx.db),
149147
hir::PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
148+
hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Struct(s))) => {
149+
s.ty(ctx.db)
150+
}
151+
hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Enum(e))) => {
152+
cov_mark::hit!(enum_plain_qualified_use_tree);
153+
e.variants(ctx.db)
154+
.into_iter()
155+
.for_each(|variant| acc.add_enum_variant(ctx, variant, None));
156+
e.ty(ctx.db)
157+
}
158+
hir::PathResolution::Def(hir::ModuleDef::Adt(hir::Adt::Union(u))) => {
159+
u.ty(ctx.db)
160+
}
150161
_ => return,
151162
};
152163

153-
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
154-
e.variants(ctx.db)
155-
.into_iter()
156-
.for_each(|variant| acc.add_enum_variant(ctx, variant, None));
157-
}
158-
159164
let traits_in_scope = ctx.scope.visible_traits();
160165
let mut seen = FxHashSet::default();
161166
ty.iterate_path_candidates(
@@ -165,12 +170,19 @@ fn pattern_path_completion(
165170
ctx.module,
166171
None,
167172
|item| {
168-
// Note associated consts cannot be referenced in patterns
169-
if let AssocItem::TypeAlias(ta) = item {
170-
// We might iterate candidates of a trait multiple times here, so deduplicate them.
171-
if seen.insert(item) {
172-
acc.add_type_alias(ctx, ta);
173+
match item {
174+
AssocItem::TypeAlias(ta) => {
175+
// We might iterate candidates of a trait multiple times here, so deduplicate them.
176+
if seen.insert(item) {
177+
acc.add_type_alias(ctx, ta);
178+
}
179+
}
180+
AssocItem::Const(c) => {
181+
if seen.insert(item) {
182+
acc.add_const(ctx, c);
183+
}
173184
}
185+
_ => {}
174186
}
175187
None::<()>
176188
},

crates/ide_completion/src/tests/pattern.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ fn func() {
306306
ev TupleV(…) TupleV(u32)
307307
ev RecordV {…} RecordV { field: u32 }
308308
ev UnitV UnitV
309+
ct ASSOC_CONST const ASSOC_CONST: ()
309310
"#]],
310311
);
311312
}
@@ -444,3 +445,75 @@ fn foo() {
444445
expect![[r#""#]],
445446
);
446447
}
448+
449+
#[test]
450+
fn completes_associated_const() {
451+
check_empty(
452+
r#"
453+
#[derive(PartialEq, Eq)]
454+
struct Ty(u8);
455+
456+
impl Ty {
457+
const ABC: Self = Self(0);
458+
}
459+
460+
fn f(t: Ty) {
461+
match t {
462+
Ty::$0 => {}
463+
_ => {}
464+
}
465+
}
466+
"#,
467+
expect![[r#"
468+
ct ABC const ABC: Self
469+
"#]],
470+
);
471+
472+
check_empty(
473+
r#"
474+
enum MyEnum {}
475+
476+
impl MyEnum {
477+
pub const A: i32 = 123;
478+
pub const B: i32 = 456;
479+
}
480+
481+
fn f(e: MyEnum) {
482+
match e {
483+
MyEnum::$0 => {}
484+
_ => {}
485+
}
486+
}
487+
"#,
488+
expect![[r#"
489+
ct A pub const A: i32
490+
ct B pub const B: i32
491+
"#]],
492+
);
493+
494+
check_empty(
495+
r#"
496+
#[repr(C)]
497+
union U {
498+
i: i32,
499+
f: f32,
500+
}
501+
502+
impl U {
503+
pub const C: i32 = 123;
504+
pub const D: i32 = 456;
505+
}
506+
507+
fn f(u: U) {
508+
match u {
509+
U::$0 => {}
510+
_ => {}
511+
}
512+
}
513+
"#,
514+
expect![[r#"
515+
ct C pub const C: i32
516+
ct D pub const D: i32
517+
"#]],
518+
)
519+
}

0 commit comments

Comments
 (0)