-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Implement Copy
/Clone
for closures
#44551
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
@@ -2084,6 +2083,22 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> { | |||
Where(ty::Binder(tys.to_vec())) | |||
} | |||
|
|||
ty::TyClosure(def_id, substs) => { | |||
let trait_id = obligation.predicate.def_id(); | |||
let copy_closures = |
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.
Don't you need to check the feature-gate status of the crate that defined the closure, to avoid ICEs?
Crate A:
#![feature(clone_closures)]
pub trait Generic {
fn foo<T: Clone>(self, t: T);
}
pub fn example<G: Generic>(g: G) {
g.foo(|| {});
}
Crate B:
// no feature flags!
extern crate a;
struct S;
impl Generic for S {
fn foo<T: Clone>(self, t: T) { let _ = t.clone(); }
}
fn main() {
// trans will monomorphize this, and will try to evaluate `[closure]: Clone`, which
// will fail and cause an ICE.
foo::example(S);
}
Another option would be to make all closures Copy
& Clone
in trans, but that would be visible through specialization etc. so I'm not sure it's the better idea.
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.
@arielb1 Yes you're right. I'm working on it.
src/librustc_mir/shim.rs
Outdated
@@ -613,7 +619,12 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> { | |||
self.block(vec![], TerminatorKind::Resume, true); | |||
} | |||
|
|||
fn tuple_shim(&mut self, tys: &ty::Slice<ty::Ty<'tcx>>) { | |||
fn tuple_like_shim(&mut self, tys: &[ty::Ty<'tcx>], kind: AggregateKind<'tcx>) { | |||
match kind { |
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.
Is there a reason for this assertion? This method works just as well for structs and univariant enums.
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.
Indeed, but it won't work for arrays. I guess the match
can be extended in the future if needed.
☔ The latest upstream changes (presumably #44634) made this pull request unmergeable. Please resolve the merge conflicts. |
52f677c
to
75c4487
Compare
d5c2a99
to
3fa3fe0
Compare
@bors r+ |
📌 Commit 3fa3fe0 has been approved by |
Implement `Copy`/`Clone` for closures Implement RFC [#2132](rust-lang/rfcs#2132) (tracking issue: #44490). NB: I'm not totally sure about the whole feature gates thing, that's my first PR of this kind...
☀️ Test successful - status-appveyor, status-travis |
Implement RFC #2132 (tracking issue: #44490).
NB: I'm not totally sure about the whole feature gates thing, that's my first PR of this kind...