-
Notifications
You must be signed in to change notification settings - Fork 13.8k
builtin trait to abstract over function pointers #99531
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
Conversation
Hey! It looks like you've submitted a new PR for the library teams!
If this PR contains changes to any rust-lang/rust
public library APIs then please comment with @rustbot label +T-libs-api -T-libs
to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.
Examples of T-libs-api
changes:
- Stabilizing library features
- Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
- Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
- Changing public documentation in ways that create new stability guarantees
- Changing observable runtime behavior of library APIs
Some changes occurred to MIR optimizations
cc @rust-lang/wg-mir-opt
Some changes occurred to the CTFE / Miri engine
cc @rust-lang/miri
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are implementing expose_addr
, not addr
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that means? 😅 what's the correct mir statement here, simply CastKind::Misc
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no MIR statement for this, https://doc.rust-lang.org/nightly/std/primitive.pointer.html#method.addr uses transmute
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should just cast to *const ()
or some other pointer to avoid any kind of provenance worries.
Oh lol it wants to use FnPtr
for things that have nothing to do with function pointers? :(
This comment has been minimized.
This comment has been minimized.
Oh lol it wants to use
FnPtr
for things that have nothing to do with function pointers? :(
You see this a lot with certain other traits, like AFAIK you can't (easily?) get errors saying IntoIterator
isn't implemented, because it finds the blanket impl of IntoIterator
for any Iterator
and decides that is where it draws the line. (quick example on playground - you may not have noticed it before because of how connected IntoIterator
and Iterator
are but it's technically wrong)
I think what needs to happen is we need to distinguish between these two:
- "covered" blanket
impl
impl<T> Foo for X<T> where T: Bar {}
- this is fine to continue saying that
Bar
isn't implemented, since thisimpl
"peels off" an application of theX
constructor - in a sense,
T: Bar
is "more specific" thanX<T>: Foo
- this is fine to continue saying that
- "uncovered" blanket
impl
impl<T> Foo for T where T: Bar {}
- this is where I think we should stop diagnosing it as
T: Bar
and switch toT: Foo
- we could still potentially mention this
impl
with itsBar
constraint in the same way we already list other examples of inapplicableimpl
s (as ahelp
message), though we should be careful to retain the ability to always hide some traits
- this is where I think we should stop diagnosing it as
@lcnr Does this sound reasonable? Maybe an issue or MCP should be opened about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should move this to trait_ref_is_knowable
instead
☔ The latest upstream changes (presumably #99567) made this pull request unmergeable. Please resolve the merge conflicts.
@lcnr Does this sound reasonable? Maybe an issue or MCP should be opened about this.
yeah, i looked into the diagnostics impact of this in #99719 and it looks promising.
fixed diagnostics now, might rework the approach before merging though 😁 (i also have to add tests)
@bors try @rust-timer queue
Awaiting bors try build completion.
@rustbot label: +S-waiting-on-perf
⌛ Trying commit 9d0a8e5 with merge 2e62312963f4b040a4e7aaec816772554c4baa1d...
Awaiting bors try build completion.
@rustbot label: +S-waiting-on-perf
☀️ Try build successful - checks-actions
Build commit: 2e62312963f4b040a4e7aaec816772554c4baa1d (2e62312963f4b040a4e7aaec816772554c4baa1d
)
Queued 2e62312963f4b040a4e7aaec816772554c4baa1d with parent 2fdbf07, future comparison URL.
Finished benchmarking commit (2e62312963f4b040a4e7aaec816772554c4baa1d): comparison url. Instruction count
Max RSS (memory usage)Results
CyclesResults
If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Footnotes |
so, the major issue is that the FnPtr
impls are added to https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait_def/struct.TraitImpls.html#structfield.blanket_impls instead of a new entry in non_blanket_impls
. I think it's easiest to change FunctionSimplifiedType
to not include the input count and add a hack which adds all impls with a Self: FnPtr
bound to that map instead of the general one 🤔
i have to admit that i don't like that too much 😁
Looking forward to the MCP here. I am following along on this ticket because I'm interested in seeing the c_unwind
feature be stabilized. Built in trait impls on the new function types are a blocker for that. Is there a way to have a smaller change land that would unblock stabilizing the c_unwind
feature?
Adding only the c_unwind instances without also adding a dozen other ABIs like the last PR did, might work.
...pls, r=thomcc Add default trait implementations for "c-unwind" ABI function pointers Following up on rust-lang#92964, only add default trait implementations for the `c-unwind` family of function pointers. The previous attempt in rust-lang#92964 added trait implementations for many more ABIs and ran into concerns regarding the increase in size of the libcore rlib. An attempt to abstract away function pointer types behind a unified trait to reduce the duplication of trait impls is being discussed in rust-lang#99531 but this change looks to be blocked on a lang MCP. Following `@RalfJung's` suggestion in rust-lang#99531 (comment), this commit is another cut at rust-lang#92964 but it _only_ adds the impls for `extern "C-unwind" fn` and `unsafe extern "C-unwind" fn`. I am interested in landing this patch to unblock the stabilization of the `c_unwind` feature. RFC: rust-lang/rfcs#2945 Tracking Issue: rust-lang#74990
I think @rust-lang/lang should just chime in here. Likely we just need an FCP to land this under a feature. I think before landing it might be "important" enough to write a small RFC.
I'd definitely like to see this land, that would also unblock (or at least strongly simplify) a bunch of important work on the const-eval and pattern-match side (#105750 would be the start).
@lcnr is there an implementation plan that just needs someone to "do it", or are there still open conceptual questions? @eddyb mentioned a super elaborate plan but if nobody has the time to work on that, I'd rather not wait for that to materialize...
@jackh726 if the trait remains private/unstable, I am not sure that we need an RFC. But we'd definitely need T-lang FCP for the magic trait, yeah.
Yeah, I think I meant before stabilization we might want an RFC. For unstable, we can land with just FCP.
@lcnr is there an implementation plan that just needs someone to "do it", or are there still open conceptual questions? @eddyb mentioned a super elaborate plan but if nobody has the time to work on that, I'd rather not wait for that to materialize...
for me the only issue was perf (and lang approval ofc 😁). have an idea how to fix that which is:
- for https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/trait_def/struct.TraitImpls.html add a third field:
fn_ptr_impls: Vec<DefId>
which includes all impls which have a generic paramT
as the self type with aT: FnPtr
bound - in the methods accessing
TraitImpls
, also iterate over these impls if they could apply. Only iterate over them if theself_ty
is either an function pointer, a generic param with aT: FnPtr
bound in theparam_env
, or something without asimplified_type
(e.g. an inference var)
this approach has a similar core idea to what i've described in https://rust-lang.zulipchat.com/#narrow/stream/247081-t-compiler.2Fperformance/topic/Compile-time.20case-study.3A.20AWS.20crates/near/315550450
I myself don't have the capacity to implement myself but could (loosely) mentor.
@lcnr and I discussed this at some point. I'm in favor of having this special trait. I prefer to fix the perf solution by having the compiler be aware of T: FnPtr
where-clauses that are in scope and which are required, because this solution would scale to supporting other similar traits like Tuple
. I think this is what @lcnr was describing in their most recent comment, as well.
Following up on #92964, only add default trait implementations for the `c-unwind` family of function pointers. The previous attempt in #92964 added trait implementations for many more ABIs and ran into concerns regarding the increase in size of the libcore rlib. An attempt to abstract away function pointer types behind a unified trait to reduce the duplication of trait impls is being discussed in #99531 but this change looks to be blocked on a lang MCP. Following @RalfJung's suggestion in rust-lang/rust#99531 (comment), this commit is another cut at #92964 but it _only_ adds the impls for `extern "C-unwind" fn` and `unsafe extern "C-unwind" fn`. I am interested in landing this patch to unblock the stabilization of the `c_unwind` feature. RFC: rust-lang/rfcs#2945 Tracking Issue: rust-lang/rust#74990
...omcc Add default trait implementations for "c-unwind" ABI function pointers Following up on #92964, only add default trait implementations for the `c-unwind` family of function pointers. The previous attempt in #92964 added trait implementations for many more ABIs and ran into concerns regarding the increase in size of the libcore rlib. An attempt to abstract away function pointer types behind a unified trait to reduce the duplication of trait impls is being discussed in #99531 but this change looks to be blocked on a lang MCP. Following `@RalfJung's` suggestion in rust-lang/rust#99531 (comment), this commit is another cut at #92964 but it _only_ adds the impls for `extern "C-unwind" fn` and `unsafe extern "C-unwind" fn`. I am interested in landing this patch to unblock the stabilization of the `c_unwind` feature. RFC: rust-lang/rfcs#2945 Tracking Issue: rust-lang/rust#74990
Closing in favor of #108080
Add a builtin `FnPtr` trait that is implemented for all function pointers r? `@ghost` Rebased version of rust-lang#99531 (plus adjustments mentioned in the PR). If perf is happy with this version, I would like to land it, even if the diagnostics fix in 9df8e1befb5031a5bf9d8dfe25170620642d3c59 only works for `FnPtr` specifically, and does not generally improve blanket impls.
Add a builtin `FnPtr` trait that is implemented for all function pointers r? `@ghost` Rebased version of rust-lang/rust#99531 (plus adjustments mentioned in the PR). If perf is happy with this version, I would like to land it, even if the diagnostics fix in 9df8e1befb5031a5bf9d8dfe25170620642d3c59 only works for `FnPtr` specifically, and does not generally improve blanket impls.
implements and uses #92964 (comment)
the tragedy of
rust/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
Lines 154 to 156 in a7468c6
until we figure out 68dea3aaadcc62837ffe7aa6a923fc9bedb6c786 i don't think we can land this. i generally have some ideas here but all of them as somewhat annoying. i think that generally the way trait errors are reported can and should be reworked. not sure what's the best short-term solution here
r? @rust-lang/types cc @RalfJung (from #92964)