Skip to content

Commit 01120f1

Browse files
committed
Auto merge of rust-lang#14528 - HKalbasi:revert-14526-revert-14521-dev, r=Veykril
Add bounds for fields in derive macro, second try
2 parents 972f131 + c54cb88 commit 01120f1

File tree

5 files changed

+187
-30
lines changed

5 files changed

+187
-30
lines changed

crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct Foo;
1616
#[derive(Copy)]
1717
struct Foo;
1818
19-
impl < > core::marker::Copy for Foo< > {}"#]],
19+
impl < > core::marker::Copy for Foo< > where {}"#]],
2020
);
2121
}
2222

@@ -41,7 +41,7 @@ macro Copy {}
4141
#[derive(Copy)]
4242
struct Foo;
4343
44-
impl < > crate ::marker::Copy for Foo< > {}"#]],
44+
impl < > crate ::marker::Copy for Foo< > where {}"#]],
4545
);
4646
}
4747

@@ -57,7 +57,7 @@ struct Foo<A, B>;
5757
#[derive(Copy)]
5858
struct Foo<A, B>;
5959
60-
impl <T0: core::marker::Copy, T1: core::marker::Copy, > core::marker::Copy for Foo<T0, T1, > {}"#]],
60+
impl <A: core::marker::Copy, B: core::marker::Copy, > core::marker::Copy for Foo<A, B, > where {}"#]],
6161
);
6262
}
6363

@@ -74,7 +74,7 @@ struct Foo<A, B, 'a, 'b>;
7474
#[derive(Copy)]
7575
struct Foo<A, B, 'a, 'b>;
7676
77-
impl <T0: core::marker::Copy, T1: core::marker::Copy, > core::marker::Copy for Foo<T0, T1, > {}"#]],
77+
impl <A: core::marker::Copy, B: core::marker::Copy, > core::marker::Copy for Foo<A, B, > where {}"#]],
7878
);
7979
}
8080

@@ -90,7 +90,7 @@ struct Foo<A, B>;
9090
#[derive(Clone)]
9191
struct Foo<A, B>;
9292
93-
impl <T0: core::clone::Clone, T1: core::clone::Clone, > core::clone::Clone for Foo<T0, T1, > {}"#]],
93+
impl <A: core::clone::Clone, B: core::clone::Clone, > core::clone::Clone for Foo<A, B, > where {}"#]],
9494
);
9595
}
9696

@@ -106,6 +106,6 @@ struct Foo<const X: usize, T>(u32);
106106
#[derive(Clone)]
107107
struct Foo<const X: usize, T>(u32);
108108
109-
impl <const T0: usize, T1: core::clone::Clone, > core::clone::Clone for Foo<T0, T1, > {}"#]],
109+
impl <const X: usize, T: core::clone::Clone, > core::clone::Clone for Foo<X, T, > where {}"#]],
110110
);
111111
}

crates/hir-expand/src/builtin_derive_macro.rs

+111-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! Builtin derives.
22
33
use base_db::{CrateOrigin, LangCrateOrigin};
4+
use std::collections::HashSet;
45
use tracing::debug;
56

67
use crate::tt::{self, TokenId};
78
use syntax::{
8-
ast::{self, AstNode, HasGenericParams, HasModuleItem, HasName},
9+
ast::{self, AstNode, HasGenericParams, HasModuleItem, HasName, HasTypeBounds, PathType},
910
match_ast,
1011
};
1112

@@ -60,8 +61,11 @@ pub fn find_builtin_derive(ident: &name::Name) -> Option<BuiltinDeriveExpander>
6061

6162
struct BasicAdtInfo {
6263
name: tt::Ident,
63-
/// `Some(ty)` if it's a const param of type `ty`, `None` if it's a type param.
64-
param_types: Vec<Option<tt::Subtree>>,
64+
/// first field is the name, and
65+
/// second field is `Some(ty)` if it's a const param of type `ty`, `None` if it's a type param.
66+
/// third fields is where bounds, if any
67+
param_types: Vec<(tt::Subtree, Option<tt::Subtree>, Option<tt::Subtree>)>,
68+
associated_types: Vec<tt::Subtree>,
6569
}
6670

6771
fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
@@ -86,46 +90,126 @@ fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
8690
},
8791
}
8892
};
89-
let name = name.ok_or_else(|| {
90-
debug!("parsed item has no name");
91-
ExpandError::Other("missing name".into())
92-
})?;
93-
let name_token_id =
94-
token_map.token_by_range(name.syntax().text_range()).unwrap_or_else(TokenId::unspecified);
95-
let name_token = tt::Ident { span: name_token_id, text: name.text().into() };
93+
let mut param_type_set: HashSet<String> = HashSet::new();
9694
let param_types = params
9795
.into_iter()
9896
.flat_map(|param_list| param_list.type_or_const_params())
9997
.map(|param| {
100-
if let ast::TypeOrConstParam::Const(param) = param {
98+
let name = {
99+
let this = param.name();
100+
match this {
101+
Some(x) => {
102+
param_type_set.insert(x.to_string());
103+
mbe::syntax_node_to_token_tree(x.syntax()).0
104+
}
105+
None => tt::Subtree::empty(),
106+
}
107+
};
108+
let bounds = match &param {
109+
ast::TypeOrConstParam::Type(x) => {
110+
x.type_bound_list().map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0)
111+
}
112+
ast::TypeOrConstParam::Const(_) => None,
113+
};
114+
let ty = if let ast::TypeOrConstParam::Const(param) = param {
101115
let ty = param
102116
.ty()
103117
.map(|ty| mbe::syntax_node_to_token_tree(ty.syntax()).0)
104118
.unwrap_or_else(tt::Subtree::empty);
105119
Some(ty)
106120
} else {
107121
None
108-
}
122+
};
123+
(name, ty, bounds)
109124
})
110125
.collect();
111-
Ok(BasicAdtInfo { name: name_token, param_types })
126+
let is_associated_type = |p: &PathType| {
127+
if let Some(p) = p.path() {
128+
if let Some(parent) = p.qualifier() {
129+
if let Some(x) = parent.segment() {
130+
if let Some(x) = x.path_type() {
131+
if let Some(x) = x.path() {
132+
if let Some(pname) = x.as_single_name_ref() {
133+
if param_type_set.contains(&pname.to_string()) {
134+
// <T as Trait>::Assoc
135+
return true;
136+
}
137+
}
138+
}
139+
}
140+
}
141+
if let Some(pname) = parent.as_single_name_ref() {
142+
if param_type_set.contains(&pname.to_string()) {
143+
// T::Assoc
144+
return true;
145+
}
146+
}
147+
}
148+
}
149+
false
150+
};
151+
let associated_types = node
152+
.descendants()
153+
.filter_map(PathType::cast)
154+
.filter(is_associated_type)
155+
.map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0)
156+
.collect::<Vec<_>>();
157+
let name = name.ok_or_else(|| {
158+
debug!("parsed item has no name");
159+
ExpandError::Other("missing name".into())
160+
})?;
161+
let name_token_id =
162+
token_map.token_by_range(name.syntax().text_range()).unwrap_or_else(TokenId::unspecified);
163+
let name_token = tt::Ident { span: name_token_id, text: name.text().into() };
164+
Ok(BasicAdtInfo { name: name_token, param_types, associated_types })
112165
}
113166

167+
/// Given that we are deriving a trait `DerivedTrait` for a type like:
168+
///
169+
/// ```ignore (only-for-syntax-highlight)
170+
/// struct Struct<'a, ..., 'z, A, B: DeclaredTrait, C, ..., Z> where C: WhereTrait {
171+
/// a: A,
172+
/// b: B::Item,
173+
/// b1: <B as DeclaredTrait>::Item,
174+
/// c1: <C as WhereTrait>::Item,
175+
/// c2: Option<<C as WhereTrait>::Item>,
176+
/// ...
177+
/// }
178+
/// ```
179+
///
180+
/// create an impl like:
181+
///
182+
/// ```ignore (only-for-syntax-highlight)
183+
/// impl<'a, ..., 'z, A, B: DeclaredTrait, C, ... Z> where
184+
/// C: WhereTrait,
185+
/// A: DerivedTrait + B1 + ... + BN,
186+
/// B: DerivedTrait + B1 + ... + BN,
187+
/// C: DerivedTrait + B1 + ... + BN,
188+
/// B::Item: DerivedTrait + B1 + ... + BN,
189+
/// <C as WhereTrait>::Item: DerivedTrait + B1 + ... + BN,
190+
/// ...
191+
/// {
192+
/// ...
193+
/// }
194+
/// ```
195+
///
196+
/// where B1, ..., BN are the bounds given by `bounds_paths`.'. Z is a phantom type, and
197+
/// therefore does not get bound by the derived trait.
114198
fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResult<tt::Subtree> {
115199
let info = match parse_adt(tt) {
116200
Ok(info) => info,
117201
Err(e) => return ExpandResult::with_err(tt::Subtree::empty(), e),
118202
};
203+
let mut where_block = vec![];
119204
let (params, args): (Vec<_>, Vec<_>) = info
120205
.param_types
121206
.into_iter()
122-
.enumerate()
123-
.map(|(idx, param_ty)| {
124-
let ident = tt::Leaf::Ident(tt::Ident {
125-
span: tt::TokenId::unspecified(),
126-
text: format!("T{idx}").into(),
127-
});
207+
.map(|(ident, param_ty, bound)| {
128208
let ident_ = ident.clone();
209+
if let Some(b) = bound {
210+
let ident = ident.clone();
211+
where_block.push(quote! { #ident : #b , });
212+
}
129213
if let Some(ty) = param_ty {
130214
(quote! { const #ident : #ty , }, quote! { #ident_ , })
131215
} else {
@@ -134,9 +218,16 @@ fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResu
134218
}
135219
})
136220
.unzip();
221+
222+
where_block.extend(info.associated_types.iter().map(|x| {
223+
let x = x.clone();
224+
let bound = trait_path.clone();
225+
quote! { #x : #bound , }
226+
}));
227+
137228
let name = info.name;
138229
let expanded = quote! {
139-
impl < ##params > #trait_path for #name < ##args > {}
230+
impl < ##params > #trait_path for #name < ##args > where ##where_block {}
140231
};
141232
ExpandResult::ok(expanded)
142233
}

crates/hir-ty/src/tests/traits.rs

+60
Original file line numberDiff line numberDiff line change
@@ -4315,3 +4315,63 @@ impl Trait for () {
43154315
"#,
43164316
);
43174317
}
4318+
4319+
#[test]
4320+
fn derive_macro_bounds() {
4321+
check_types(
4322+
r#"
4323+
//- minicore: clone, derive
4324+
#[derive(Clone)]
4325+
struct Copy;
4326+
struct NotCopy;
4327+
#[derive(Clone)]
4328+
struct Generic<T>(T);
4329+
trait Tr {
4330+
type Assoc;
4331+
}
4332+
impl Tr for Copy {
4333+
type Assoc = NotCopy;
4334+
}
4335+
#[derive(Clone)]
4336+
struct AssocGeneric<T: Tr>(T::Assoc);
4337+
4338+
#[derive(Clone)]
4339+
struct AssocGeneric2<T: Tr>(<T as Tr>::Assoc);
4340+
4341+
#[derive(Clone)]
4342+
struct AssocGeneric3<T: Tr>(Generic<T::Assoc>);
4343+
4344+
#[derive(Clone)]
4345+
struct Vec<T>();
4346+
4347+
#[derive(Clone)]
4348+
struct R1(Vec<R2>);
4349+
#[derive(Clone)]
4350+
struct R2(R1);
4351+
4352+
fn f() {
4353+
let x = (&Copy).clone();
4354+
//^ Copy
4355+
let x = (&NotCopy).clone();
4356+
//^ &NotCopy
4357+
let x = (&Generic(Copy)).clone();
4358+
//^ Generic<Copy>
4359+
let x = (&Generic(NotCopy)).clone();
4360+
//^ &Generic<NotCopy>
4361+
let x: &AssocGeneric<Copy> = &AssocGeneric(NotCopy);
4362+
let x = x.clone();
4363+
//^ &AssocGeneric<Copy>
4364+
let x: &AssocGeneric2<Copy> = &AssocGeneric2(NotCopy);
4365+
let x = x.clone();
4366+
//^ &AssocGeneric2<Copy>
4367+
let x: &AssocGeneric3<Copy> = &AssocGeneric3(Generic(NotCopy));
4368+
let x = x.clone();
4369+
//^ &AssocGeneric3<Copy>
4370+
let x = (&R1(Vec())).clone();
4371+
//^ R1
4372+
let x = (&R2(R1(Vec()))).clone();
4373+
//^ R2
4374+
}
4375+
"#,
4376+
);
4377+
}

crates/ide/src/expand_macro.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ struct Foo {}
471471
"#,
472472
expect![[r#"
473473
Clone
474-
impl < >core::clone::Clone for Foo< >{}
474+
impl < >core::clone::Clone for Foo< >where{}
475475
"#]],
476476
);
477477
}
@@ -488,7 +488,7 @@ struct Foo {}
488488
"#,
489489
expect![[r#"
490490
Copy
491-
impl < >core::marker::Copy for Foo< >{}
491+
impl < >core::marker::Copy for Foo< >where{}
492492
"#]],
493493
);
494494
}
@@ -504,7 +504,7 @@ struct Foo {}
504504
"#,
505505
expect![[r#"
506506
Copy
507-
impl < >core::marker::Copy for Foo< >{}
507+
impl < >core::marker::Copy for Foo< >where{}
508508
"#]],
509509
);
510510
check(
@@ -516,7 +516,7 @@ struct Foo {}
516516
"#,
517517
expect![[r#"
518518
Clone
519-
impl < >core::clone::Clone for Foo< >{}
519+
impl < >core::clone::Clone for Foo< >where{}
520520
"#]],
521521
);
522522
}

crates/test-utils/src/minicore.rs

+6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ pub mod clone {
143143
pub trait Clone: Sized {
144144
fn clone(&self) -> Self;
145145
}
146+
147+
impl<T> Clone for &T {
148+
fn clone(&self) -> Self {
149+
*self
150+
}
151+
}
146152
// region:derive
147153
#[rustc_builtin_macro]
148154
pub macro Clone($item:item) {}

0 commit comments

Comments
 (0)