Skip to content

Commit 400c929

Browse files
committed
refactor: merge RenderContext::is_deprecated and RenderContext::is_deprecated_assoc_item
1 parent caa860f commit 400c929

8 files changed

Lines changed: 48 additions & 31 deletions

File tree

crates/ide-completion/src/render.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,32 @@ impl<'a> RenderContext<'a> {
9292
&& self.completion.token.parent().is_some_and(|it| it.kind() == SyntaxKind::MACRO_CALL)
9393
}
9494

95-
fn is_deprecated(&self, def: impl HasAttrs) -> bool {
96-
def.attrs(self.db()).is_deprecated()
97-
}
98-
99-
fn is_deprecated_assoc_item(&self, as_assoc_item: impl AsAssocItem) -> bool {
95+
/// Whether `def` is deprecated.
96+
///
97+
/// This can happen for two reasons:
98+
/// - the def is marked with `#[deprecated]`
99+
/// - the def is an assoc item whose trait is deprecated
100+
///
101+
/// In order to be able to check for the latter, we'd ideally want to `try_as_dyn<_, dyn AsAssocItem>(def)`
102+
/// (see [`try_as_dyn`][]), but that function is currently unstable. Therefore, we employ a hack instead:
103+
/// if `def` can be an assoc item, it should be passed to this method as follows:
104+
/// ```ignore
105+
/// self.is_deprecated(def, Some(def))
106+
/// ```
107+
/// otherwise, it should be passed as:
108+
/// ```ignore
109+
/// self.is_deprecated(def, None)
110+
/// ```
111+
///
112+
/// [`try_as_dyn`]: https://doc.rust-lang.org/std/any/fn.try_as_dyn.html
113+
fn is_deprecated(&self, def: impl HasAttrs, def_as_assoc_item: Option<hir::AssocItem>) -> bool {
100114
let db = self.db();
101-
let assoc = match as_assoc_item.as_assoc_item(db) {
102-
Some(assoc) => assoc,
103-
None => return false,
104-
};
105-
106-
let is_assoc_deprecated = match assoc {
107-
hir::AssocItem::Function(it) => self.is_deprecated(it),
108-
hir::AssocItem::Const(it) => self.is_deprecated(it),
109-
hir::AssocItem::TypeAlias(it) => self.is_deprecated(it),
110-
};
111-
is_assoc_deprecated
112-
|| assoc
113-
.container_or_implemented_trait(db)
114-
.is_some_and(|trait_| self.is_deprecated(trait_))
115+
def.attrs(db).is_deprecated()
116+
|| def_as_assoc_item
117+
.and_then(|assoc| assoc.container_or_implemented_trait(db))
118+
.is_some_and(|trait_| {
119+
self.is_deprecated(trait_, None /* traits can't be assoc items */)
120+
})
115121
}
116122

117123
// FIXME: remove this
@@ -128,7 +134,7 @@ pub(crate) fn render_field(
128134
ty: &hir::Type<'_>,
129135
) -> CompletionItem {
130136
let db = ctx.db();
131-
let is_deprecated = ctx.is_deprecated(field);
137+
let is_deprecated = ctx.is_deprecated(field, None /* fields can't be assoc items */);
132138
let name = field.name(db);
133139
let (name, escaped_name) =
134140
(name.as_str().to_smolstr(), name.display_no_db(ctx.completion.edition).to_smolstr());
@@ -575,10 +581,15 @@ fn scope_def_docs(db: &RootDatabase, resolution: ScopeDef) -> Option<Documentati
575581
}
576582

577583
fn scope_def_is_deprecated(ctx: &RenderContext<'_>, resolution: ScopeDef) -> bool {
584+
let db = ctx.db();
578585
match resolution {
579-
ScopeDef::ModuleDef(it) => ctx.is_deprecated(it) || ctx.is_deprecated_assoc_item(it),
580-
ScopeDef::GenericParam(it) => ctx.is_deprecated(it),
581-
ScopeDef::AdtSelfType(it) => ctx.is_deprecated(it),
586+
ScopeDef::ModuleDef(it) => ctx.is_deprecated(it, it.as_assoc_item(db)),
587+
ScopeDef::GenericParam(it) => {
588+
ctx.is_deprecated(it, None /* generic params can't be assoc items */)
589+
}
590+
ScopeDef::AdtSelfType(it) => {
591+
ctx.is_deprecated(it, None /* `Self` can't be an assoc item */)
592+
}
582593
_ => false,
583594
}
584595
}

crates/ide-completion/src/render/const_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn render(ctx: RenderContext<'_>, const_: hir::Const) -> Option<CompletionItem>
2121
let mut item =
2222
CompletionItem::new(SymbolKind::Const, ctx.source_range(), name, ctx.completion.edition);
2323
item.set_documentation(ctx.docs(const_))
24-
.set_deprecated(ctx.is_deprecated(const_) || ctx.is_deprecated_assoc_item(const_))
24+
.set_deprecated(ctx.is_deprecated(const_, const_.as_assoc_item(db)))
2525
.detail(detail)
2626
.set_relevance(ctx.completion_relevance());
2727

crates/ide-completion/src/render/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn render(
147147
detail(ctx.completion, func)
148148
};
149149
item.set_documentation(ctx.docs(func))
150-
.set_deprecated(ctx.is_deprecated(func) || ctx.is_deprecated_assoc_item(func))
150+
.set_deprecated(ctx.is_deprecated(func, func.as_assoc_item(db)))
151151
.detail(detail)
152152
.lookup_by(name.as_str().to_smolstr());
153153

crates/ide-completion/src/render/literal.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,12 @@ impl Variant {
189189

190190
fn is_deprecated(self, ctx: &RenderContext<'_>) -> bool {
191191
match self {
192-
Variant::Struct(it) => ctx.is_deprecated(it),
193-
Variant::EnumVariant(it) => ctx.is_deprecated(it),
192+
Variant::Struct(it) => {
193+
ctx.is_deprecated(it, None /* structs can't be assoc items */)
194+
}
195+
Variant::EnumVariant(it) => {
196+
ctx.is_deprecated(it, None /* enum variants can't be assoc items */)
197+
}
194198
}
195199
}
196200

crates/ide-completion/src/render/macro_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn render(
6464
label(&ctx, needs_bang, bra, ket, &name.to_smolstr()),
6565
completion.edition,
6666
);
67-
item.set_deprecated(ctx.is_deprecated(macro_))
67+
item.set_deprecated(ctx.is_deprecated(macro_, None /* macros can't be assoc items */))
6868
.detail(macro_.display(completion.db, completion.display_target).to_string())
6969
.set_documentation(docs)
7070
.set_relevance(ctx.completion_relevance());

crates/ide-completion/src/render/pattern.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ fn build_completion(
126126
ctx.completion.edition,
127127
);
128128
item.set_documentation(ctx.docs(def))
129-
.set_deprecated(ctx.is_deprecated(def))
129+
.set_deprecated(
130+
ctx.is_deprecated(def, None /* the two current `def` arguments to this function, `Struct` and `EnumVariant`, both can't be assoc items */),
131+
)
130132
.detail(&pat)
131133
.lookup_by(lookup)
132134
.set_relevance(relevance);

crates/ide-completion/src/render/type_alias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn render(
4747
ctx.completion.edition,
4848
);
4949
item.set_documentation(ctx.docs(type_alias))
50-
.set_deprecated(ctx.is_deprecated(type_alias) || ctx.is_deprecated_assoc_item(type_alias))
50+
.set_deprecated(ctx.is_deprecated(type_alias, type_alias.as_assoc_item(db)))
5151
.detail(detail)
5252
.set_relevance(ctx.completion_relevance());
5353

crates/ide-completion/src/render/union_literal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub(crate) fn render_union_literal(
9595
);
9696

9797
item.set_documentation(ctx.docs(un))
98-
.set_deprecated(ctx.is_deprecated(un))
98+
.set_deprecated(ctx.is_deprecated(un, None /* unions can't be assoc items */))
9999
.detail(detail)
100100
.set_relevance(ctx.completion_relevance());
101101

0 commit comments

Comments
 (0)