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

When deriveing, account for HRTB on BareFn fields #125987

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,15 @@ fn find_type_parameters(

impl<'a, 'b> visit::Visitor<'a> for Visitor<'a, 'b> {
fn visit_ty(&mut self, ty: &'a ast::Ty) {
let stack_len = self.bound_generic_params_stack.len();
if let ast::TyKind::BareFn(bare_fn) = &ty.kind
&& !bare_fn.generic_params.is_empty()
{
// Given a field `x: for<'a> fn(T::SomeType<'a>)`, we wan't to account for `'a` so
// that we generate `where for<'a> T::SomeType<'a>: ::core::clone::Clone`. #122622
self.bound_generic_params_stack.extend(bare_fn.generic_params.iter().cloned());
}

if let ast::TyKind::Path(_, path) = &ty.kind
&& let Some(segment) = path.segments.first()
&& self.ty_param_names.contains(&segment.ident.name)
Expand All @@ -422,7 +431,8 @@ fn find_type_parameters(
});
}

visit::walk_ty(self, ty)
visit::walk_ty(self, ty);
self.bound_generic_params_stack.truncate(stack_len);
}

// Place bound generic params on a stack, to extract them when a type is encountered.
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/derives/derive-hrtb-for-bare-fn-field-with-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ run-pass
// Issue #122622: `#[derive(Clone)]` should work for HRTB function type taking an associated type
#![allow(dead_code)]
trait SomeTrait {
type SomeType<'a>;
}

#[derive(Clone)]
struct Foo<T: SomeTrait> {
x: for<'a> fn(T::SomeType<'a>)
}

fn main() {}
Loading