Skip to content

Fix derive bounds for fully-qualified field types #139272

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 24 additions & 9 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub(crate) use SubstructureFields::*;
use rustc_ast::ptr::P;
use rustc_ast::{
self as ast, AnonConst, BindingMode, ByRef, EnumDef, Expr, GenericArg, GenericParamKind,
Generics, Mutability, PatKind, VariantData,
Generics, Mutability, PatKind, QSelf, VariantData,
};
use rustc_attr_parsing::{AttributeKind, AttributeParser, ReprPacked};
use rustc_expand::base::{Annotatable, ExtCtxt};
Expand Down Expand Up @@ -409,6 +409,22 @@ fn find_type_parameters(
type_params: Vec<TypeParameter>,
}

impl Visitor<'_, '_> {
fn is_path_from_ty_param(&self, qself: &Option<P<QSelf>>, path: &ast::Path) -> bool {
if let Some(qself) = qself
&& let ast::TyKind::Path(qself, path) = &qself.ty.kind
{
self.is_path_from_ty_param(&qself, &path)
} else if let Some(segment) = path.segments.first()
&& self.ty_param_names.contains(&segment.ident.name)
{
true
} else {
false
}
}
}

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();
Expand All @@ -420,14 +436,13 @@ fn find_type_parameters(
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)
{
self.type_params.push(TypeParameter {
bound_generic_params: self.bound_generic_params_stack.clone(),
ty: P(ty.clone()),
});
if let ast::TyKind::Path(qself, path) = &ty.kind {
if self.is_path_from_ty_param(qself, path) {
self.type_params.push(TypeParameter {
bound_generic_params: self.bound_generic_params_stack.clone(),
ty: P(ty.clone()),
});
}
}

visit::walk_ty(self, ty);
Expand Down
103 changes: 103 additions & 0 deletions tests/ui/deriving/deriving-associated-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ struct TupleStruct<A, B: DeclaredTrait, C>(
<i32 as DeclaredTrait>::Type,
) where C: WhereTrait;

#[derive(PartialEq, Debug)]
struct TupleStructJustQSelf<B: DeclaredTrait, C>(
<B>::Type,
<B as DeclaredTrait>::Type,
Option<<B as DeclaredTrait>::Type>,
<C as WhereTrait>::Type,
Option<<C as WhereTrait>::Type>,
<i32 as DeclaredTrait>::Type,
) where C: WhereTrait;

#[derive(PartialEq, Debug)]
pub struct Struct<A, B: DeclaredTrait, C> where C: WhereTrait {
m1: module::Type,
Expand All @@ -62,6 +72,16 @@ pub struct Struct<A, B: DeclaredTrait, C> where C: WhereTrait {
d: <i32 as DeclaredTrait>::Type,
}

#[derive(PartialEq, Debug)]
pub struct StructJustQSelf<B: DeclaredTrait, C> where C: WhereTrait {
b1: <B>::Type,
b3: <B as DeclaredTrait>::Type,
b4: Option<<B as DeclaredTrait>::Type>,
c3: <C as WhereTrait>::Type,
c4: Option<<C as WhereTrait>::Type>,
d: <i32 as DeclaredTrait>::Type,
}

#[derive(PartialEq, Debug)]
enum Enum<A, B: DeclaredTrait, C> where C: WhereTrait {
Unit,
Expand Down Expand Up @@ -101,6 +121,29 @@ enum Enum<A, B: DeclaredTrait, C> where C: WhereTrait {
},
}

#[derive(PartialEq, Debug)]
enum EnumJustQSelf<B: DeclaredTrait, C> where C: WhereTrait {
Unit,
Seq(
<B>::Type,
<B as DeclaredTrait>::Type,
Option<<B as DeclaredTrait>::Type>,
<C>::Type,
<C as WhereTrait>::Type,
Option<<C as WhereTrait>::Type>,
<i32 as DeclaredTrait>::Type,
),
Map {
b1: <B>::Type,
b3: <B as DeclaredTrait>::Type,
b4: Option<<B as DeclaredTrait>::Type>,
c1: <C>::Type,
c3: <C as WhereTrait>::Type,
c4: Option<<C as WhereTrait>::Type>,
d: <i32 as DeclaredTrait>::Type,
},
}

fn main() {
let e: TupleStruct<
i32,
Expand All @@ -125,6 +168,19 @@ fn main() {
);
assert_eq!(e, e);

let e: TupleStructJustQSelf<
i32,
i32,
> = TupleStructJustQSelf(
0,
0,
None,
0,
None,
0,
);
assert_eq!(e, e);

let e: Struct<
i32,
i32,
Expand All @@ -148,6 +204,19 @@ fn main() {
};
assert_eq!(e, e);

let e: StructJustQSelf<
i32,
i32,
> = StructJustQSelf {
b1: 0,
b3: 0,
b4: None,
c3: 0,
c4: None,
d: 0,
};
assert_eq!(e, e);

let e = Enum::Unit::<i32, i32, i32>;
assert_eq!(e, e);

Expand Down Expand Up @@ -196,4 +265,38 @@ fn main() {
d: 0,
};
assert_eq!(e, e);

let e: EnumJustQSelf<
i32,
i32,
> = EnumJustQSelf::Unit;
assert_eq!(e, e);

let e: EnumJustQSelf<
i32,
i32,
> = EnumJustQSelf::Seq(
0,
0,
None,
0,
0,
None,
0,
);
assert_eq!(e, e);

let e: EnumJustQSelf<
i32,
i32,
> = EnumJustQSelf::Map {
b1: 0,
b3: 0,
b4: None,
c1: 0,
c3: 0,
c4: None,
d: 0,
};
assert_eq!(e, e);
}
Loading