Skip to content

Conversation

@p3achyjr
Copy link

@p3achyjr p3achyjr commented Nov 5, 2020

Adds static semantic token modifier to associated functions, resolves #6194

Info

  • Associated functions are more-or-less equivalent to static methods in other languages. This PR checks, for each function, whether that function has a self_param, and whether it's enclosed in a trait/impl.

Changes

  • Added method is_associated to code_model::Function. This basically gets the source from the ast, and checks whether the enclosing scope is an impl or trait.
  • Added static to HighlightModifiers
  • Added unit test

Tests

  • Ran cargo test


/// whether this function is associated with some trait/impl
pub fn is_associated(self, db: &dyn HirDatabase) -> bool {
if let Some(_) = self.self_param(db) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be

if self.self_param(db).is_some()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 👍

…self

refactor logic into code_model.rs

address comments
if func.is_unsafe(db) {
h |= HighlightModifier::Unsafe;
}
if func.is_associated(db) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use something like this instead:

Suggested change
if func.is_associated(db) {
if func.is_assoc_item(db) && func.self_param(db).is_none() {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh dear, that was one confusing comment, sorry.

What I meant to say is that we already have as_assoc_item function we can use here, instead of implementing a new is_assoc_item:

https://github.com/rust-analyzer/rust-analyzer/blob/2f247140817c9cbd9009085c9f9ccedb4f6a718f/crates/hir/src/code_model.rs#L1002

Anatol Liu added 2 commits November 8, 2020 00:01
…self

refactor logic into code_model.rs

address comments
…self

refactor logic into code_model.rs

address comments
@matklad
Copy link
Contributor

matklad commented Nov 9, 2020

bors r+

Thanks!

@bors
Copy link
Contributor

bors bot commented Nov 9, 2020

@bors bors bot merged commit 0a715cf into rust-lang:master Nov 9, 2020
@p3achyjr
Copy link
Author

p3achyjr commented Nov 9, 2020

Thanks for accepting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Give associated functions the ‘static’ semantic token modifier

4 participants