Skip to content

Commit

Permalink
fix: completes non exhaustive variant within the defining crate
Browse files Browse the repository at this point in the history
  • Loading branch information
yue4u committed Jun 23, 2022
1 parent 6fc5c3c commit 472ae16
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
18 changes: 18 additions & 0 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3497,12 +3497,30 @@ impl HasCrate for AssocItem {
}
}

impl HasCrate for Struct {
fn krate(&self, db: &dyn HirDatabase) -> Crate {
self.module(db).krate()
}
}

impl HasCrate for Union {
fn krate(&self, db: &dyn HirDatabase) -> Crate {
self.module(db).krate()
}
}

impl HasCrate for Field {
fn krate(&self, db: &dyn HirDatabase) -> Crate {
self.parent_def(db).module(db).krate()
}
}

impl HasCrate for Variant {
fn krate(&self, db: &dyn HirDatabase) -> Crate {
self.module(db).krate()
}
}

impl HasCrate for Function {
fn krate(&self, db: &dyn HirDatabase) -> Crate {
self.module(db).krate()
Expand Down
11 changes: 6 additions & 5 deletions crates/ide-completion/src/render/variant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Code common to structs, unions, and enum variants.

use crate::context::CompletionContext;
use hir::{db::HirDatabase, HasAttrs, HasVisibility, HirDisplay, StructKind};
use hir::{db::HirDatabase, HasAttrs, HasCrate, HasVisibility, HirDisplay, StructKind};
use ide_db::SnippetCap;
use itertools::Itertools;
use syntax::SmolStr;
Expand Down Expand Up @@ -70,7 +70,7 @@ pub(crate) fn render_tuple_lit(
pub(crate) fn visible_fields(
ctx: &CompletionContext,
fields: &[hir::Field],
item: impl HasAttrs,
item: impl HasAttrs + HasCrate + Copy,
) -> Option<(Vec<hir::Field>, bool)> {
let module = ctx.module;
let n_fields = fields.len();
Expand All @@ -79,9 +79,10 @@ pub(crate) fn visible_fields(
.filter(|field| field.is_visible_from(ctx.db, module))
.copied()
.collect::<Vec<_>>();

let fields_omitted =
n_fields - fields.len() > 0 || item.attrs(ctx.db).by_key("non_exhaustive").exists();
let has_invisible_field = n_fields - fields.len() > 0;
let is_foreign_non_exhaustive = item.attrs(ctx.db).by_key("non_exhaustive").exists()
&& item.krate(ctx.db) != module.krate();
let fields_omitted = has_invisible_field || is_foreign_non_exhaustive;
Some((fields, fields_omitted))
}

Expand Down
40 changes: 40 additions & 0 deletions crates/ide-completion/src/tests/special.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,46 @@ impl Foo {
);
}

#[test]
fn completes_non_exhaustive_variant_within_the_defining_crate() {
check(
r#"
enum Foo {
#[non_exhaustive]
Bar,
Baz,
}
fn foo(self) {
Foo::$0
}
"#,
expect![[r#"
ev Bar Bar
ev Baz Baz
"#]],
);

check(
r#"
//- /main.rs crate:main deps:e
fn foo(self) {
e::Foo::$0
}
//- /e.rs crate:e
enum Foo {
#[non_exhaustive]
Bar,
Baz,
}
"#,
expect![[r#"
ev Baz Baz
"#]],
);
}

#[test]
fn completes_primitive_assoc_const() {
cov_mark::check!(completes_primitive_assoc_const);
Expand Down

0 comments on commit 472ae16

Please sign in to comment.