Skip to content

Specialize builder functions where unambiguous #549

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

Merged
merged 2 commits into from
Jul 19, 2022

Conversation

afetisov
Copy link
Contributor

Remove generic parameters which are actually used only with a single concrete type, substituting that
type instead.

E.g. a function fn foo<E: Make<Box<Expr>>, T: Make<Box<Type>>(e: E, t: T) is turned into a function
fn foo(e: Box<Expr>, t: Box<Type>).

This doesn't restrict the usages of the function in any way, since the only type which is Make<Box<Expr>>
is Box<Expr> itself. These generic parameters are strictly redundant. They produce generic bloat, harm
compile times, complicate the API, and prevent type inference for function arguments (i.e. if foo(f1(), f2())
and either f1 or f2 is generic in its return type, then type inference will fail).

Using the concrete types is strictly better, and opens the possibility of more ergonomic API with better type
inference.

Also remove the explicit type casts which were required only due to the overly generic API, and can now be safely elided.

This PR was split from #485. As far as I can tell from the discussion in #485, this change is uncontroversial.

This PR builds on the branch of #548 and should be merged after it to avoid merge conflicts.

afetisov added 2 commits July 18, 2022 21:34
Remove generic parameters which are actually used only with a single concrete type, substituting that
type instead.

E.g. a function `fn foo<E: Make<Box<Expr>>, T: Make<Box<Type>>(e: E, t: T)` is turned into a function
`fn foo(e: Box<Expr>, t: Box<Type>)`.

This doesn't restrict the usages of the function in any way, since the only type which is `Make<Box<Expr>>`
is `Box<Expr>` itself. These generic parameters are strictly redundant. They produce generic bloat, harm
compile times, complicate the API, and prevent type inference for function arguments (i.e. if `foo(f1(), f2())`
and either `f1` or `f2` is generic in its return type, then type inference will fail).

Using the concrete types is strictly better, and opens the possibility of more ergonomic API with better type
inference.
@kkysen kkysen force-pushed the specialize-builder branch from d7ebcd4 to 06f6c41 Compare July 19, 2022 04:34
@kkysen
Copy link
Contributor

kkysen commented Jul 19, 2022

Rebased against master after merging #548 to master so I can review a cleaner diff.

Copy link
Contributor

@kkysen kkysen left a comment

Choose a reason for hiding this comment

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

As you noted, @fw-immunant already reviewed and approved these changes in #485 (comment), and I agree they are very helpful in reducing unnecessary generics and improving inference.

This does cause some clippy::boxed_local warnings, but I'll fix them separately (#550), as it's not a tiny fix.

@kkysen kkysen merged commit 0e0f83a into immunant:master Jul 19, 2022
kkysen added a commit that referenced this pull request Jul 19, 2022
… `Vec::new()`/`vec![]`, which were surfaced by #549 but missed in that PR.
kkysen added a commit that referenced this pull request Jul 19, 2022
…ing in #549, as well as the cascading de-`Box`ing.
@afetisov afetisov deleted the specialize-builder branch July 19, 2022 10:10
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.

2 participants