Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add assist to generate a type alias for a function #18385

Merged
merged 4 commits into from
Oct 30, 2024

Conversation

Giga-Bowser
Copy link
Contributor

closes #18343
Technically this doesn't support generating for <...> lifetimes, but I'm not very familiar with them, and they seem a bit outside of the scope of an assist like this. If somebody has a way to generate them feel free to add it.

Also, when generating aliases with self parameters, they are left unnamed even in the generate_fn_type_alias_named assist. I think this behavior makes the most sense but you could also name them something like this and add a placeholder to change the name if that's desired.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 23, 2024
No idea how this slipped past me
crates/ide-assists/src/handlers/generate_fn_type_alias.rs Outdated Show resolved Hide resolved
crates/ide-assists/src/handlers/generate_fn_type_alias.rs Outdated Show resolved Hide resolved
crates/ide-assists/src/handlers/generate_fn_type_alias.rs Outdated Show resolved Hide resolved
None => "".into(),
};

let fn_unsafe = if func_node.unsafe_token().is_some() { "unsafe " } else { "" };
Copy link
Member

Choose a reason for hiding this comment

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

There are more qualifiers to handle, const and async (keep ordering in mind, the qualifiers allow only a specific order)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Function pointers cannot be const nor async. I suppose I could generate an alias returning Future for async functions, let me know if this is desirable. I'm not really sure of an equivalent for a const functions, but a non-const function pointer should accept a const function as an argument, so I think it's fine to just ignore a const qualifier.

crates/ide-assists/src/handlers/generate_fn_type_alias.rs Outdated Show resolved Hide resolved
crates/ide-assists/src/handlers/generate_fn_type_alias.rs Outdated Show resolved Hide resolved
@Veykril
Copy link
Member

Veykril commented Oct 24, 2024

Technically this doesn't support generating for <...> lifetimes, but I'm not very familiar with them, and they seem a bit outside of the scope of an assist like this. If somebody has a way to generate them feel free to add it.

That's fine. One thing that would be nice to have though is that any generic parameters declared on the function are lifted to the type alias, that is:

fn f<T>(t: T) {} becomes type TFn<T> = fn(T).

Also, when generating aliases with self parameters, they are left unnamed even in the generate_fn_type_alias_named assist. I think this behavior makes the most sense but you could also name them something like this and add a placeholder to change the name if that's desired.

Yes thats fine that way I'd say

@Giga-Bowser
Copy link
Contributor Author

One thing that would be nice to have though is that any generic parameters declared on the function are lifted to the type alias, that is:

fn f<T>(t: T) {} becomes type TFn<T> = fn(T).

Unless I misunderstand, this code does that. For example, the test generate_fn_alias_unnamed_generics ensures the assist turns

fn foo<A, B>(a: A, b: B) -> i32 { return 42; }

into

type FooFn<A, B> = fn(A, B) -> i32;

@Veykril Veykril added this pull request to the merge queue Oct 30, 2024
@Veykril
Copy link
Member

Veykril commented Oct 30, 2024

Thanks!

Merged via the queue into rust-lang:master with commit bb3239d Oct 30, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assist to derive type alias = signature for a function
3 participants