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

Uses of trait objects that could be replaced with static polymorphism #14

Open
kmcallister opened this issue Nov 19, 2014 · 5 comments
Open
Labels
A-lint Area: New lints E-hard Call for participation: This a hard problem and requires more experience or effort to work on L-perf Lint: Belongs in the perf lint group S-needs-discussion Status: Needs further discussion before merging or work can be started T-middle Type: Probably requires verifiying types

Comments

@kmcallister
Copy link

Not sure how to detect these, but it seems important.

@Manishearth
Copy link
Member

So basically suggest the usage of a union type instead of the trait object?

Yeah, I'm not sure what the heuristics should be either.

@jdm
Copy link

jdm commented Nov 20, 2014

I think @kmcallister is referring to fn foo<T: SomeTrait>(t: &T) vs. fn foo(t: &SomeTrait) rather than explicit ADTs.

@Manishearth
Copy link
Member

Oh, I see.

@Manishearth Manishearth added E-hard Call for participation: This a hard problem and requires more experience or effort to work on T-middle Type: Probably requires verifiying types A-lint Area: New lints S-needs-discussion Status: Needs further discussion before merging or work can be started labels Aug 11, 2015
@mcarton mcarton added the L-perf Lint: Belongs in the perf lint group label Jun 27, 2016
@coder543
Copy link

coder543 commented Oct 6, 2017

It seems like the main requirement for this would be to look at every function declaration, such as fn foo(t: &SomeTrait), and then be able to determine that SomeTrait is a trait and not a struct or enum. Once you have that info, you can choose to emit a lint or not.

Maybe the RLS could help with this? not sure.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 10, 2017

Detecting whether the argument is a trait object is easy. Since any generic type with a bound on a trait can be turned into a trait object of that type, this would be a trivial transformation. The issues with transforming a random function from dynamic dispatch to static dispatch is that someone might be using the function by passing it a trait object. They might not have a chance to do anything else. So we'd also need to add a ?Sized bound.

In gennral you can convert any fn foo(t: &SomeTrait) to fn foo<T: SomeTrait + ?Sized>(t: &T) without breaking anything.

Additionally there are cases where we do not want to use static dispatch in order to reduce compiletimes of downstream crates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-hard Call for participation: This a hard problem and requires more experience or effort to work on L-perf Lint: Belongs in the perf lint group S-needs-discussion Status: Needs further discussion before merging or work can be started T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

6 participants