mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-15 22:54:00 +00:00
Auto merge of #14526 - rust-lang:revert-14521-dev, r=Veykril
Revert "Add bounds for fields in derive macro" Reverts rust-lang/rust-analyzer#14521 as it introduces too many mismatches
This commit is contained in:
commit
972f1313eb
3 changed files with 27 additions and 66 deletions
|
@ -16,7 +16,7 @@ struct Foo;
|
||||||
#[derive(Copy)]
|
#[derive(Copy)]
|
||||||
struct Foo;
|
struct Foo;
|
||||||
|
|
||||||
impl < > core::marker::Copy for Foo< > where {}"#]],
|
impl < > core::marker::Copy for Foo< > {}"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ macro Copy {}
|
||||||
#[derive(Copy)]
|
#[derive(Copy)]
|
||||||
struct Foo;
|
struct Foo;
|
||||||
|
|
||||||
impl < > crate ::marker::Copy for Foo< > where {}"#]],
|
impl < > crate ::marker::Copy for Foo< > {}"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ struct Foo<A, B>;
|
||||||
#[derive(Copy)]
|
#[derive(Copy)]
|
||||||
struct Foo<A, B>;
|
struct Foo<A, B>;
|
||||||
|
|
||||||
impl <A: core::marker::Copy, B: core::marker::Copy, > core::marker::Copy for Foo<A, B, > where {}"#]],
|
impl <T0: core::marker::Copy, T1: core::marker::Copy, > core::marker::Copy for Foo<T0, T1, > {}"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ struct Foo<A, B, 'a, 'b>;
|
||||||
#[derive(Copy)]
|
#[derive(Copy)]
|
||||||
struct Foo<A, B, 'a, 'b>;
|
struct Foo<A, B, 'a, 'b>;
|
||||||
|
|
||||||
impl <A: core::marker::Copy, B: core::marker::Copy, > core::marker::Copy for Foo<A, B, > where {}"#]],
|
impl <T0: core::marker::Copy, T1: core::marker::Copy, > core::marker::Copy for Foo<T0, T1, > {}"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +90,7 @@ struct Foo<A, B>;
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Foo<A, B>;
|
struct Foo<A, B>;
|
||||||
|
|
||||||
impl <A: core::clone::Clone, B: core::clone::Clone, > core::clone::Clone for Foo<A, B, > where {}"#]],
|
impl <T0: core::clone::Clone, T1: core::clone::Clone, > core::clone::Clone for Foo<T0, T1, > {}"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,6 +106,6 @@ struct Foo<const X: usize, T>(u32);
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Foo<const X: usize, T>(u32);
|
struct Foo<const X: usize, T>(u32);
|
||||||
|
|
||||||
impl <const X: usize, T: core::clone::Clone, > core::clone::Clone for Foo<X, T, > where u32: core::clone::Clone, {}"#]],
|
impl <const T0: usize, T1: core::clone::Clone, > core::clone::Clone for Foo<T0, T1, > {}"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
//! Builtin derives.
|
//! Builtin derives.
|
||||||
|
|
||||||
use base_db::{CrateOrigin, LangCrateOrigin};
|
use base_db::{CrateOrigin, LangCrateOrigin};
|
||||||
use either::Either;
|
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
use crate::tt::{self, TokenId};
|
use crate::tt::{self, TokenId};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, AstNode, HasGenericParams, HasModuleItem, HasName, HasTypeBounds},
|
ast::{self, AstNode, HasGenericParams, HasModuleItem, HasName},
|
||||||
match_ast,
|
match_ast,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -61,11 +60,8 @@ pub fn find_builtin_derive(ident: &name::Name) -> Option<BuiltinDeriveExpander>
|
||||||
|
|
||||||
struct BasicAdtInfo {
|
struct BasicAdtInfo {
|
||||||
name: tt::Ident,
|
name: tt::Ident,
|
||||||
/// first field is the name, and
|
/// `Some(ty)` if it's a const param of type `ty`, `None` if it's a type param.
|
||||||
/// second field is `Some(ty)` if it's a const param of type `ty`, `None` if it's a type param.
|
param_types: Vec<Option<tt::Subtree>>,
|
||||||
/// third fields is where bounds, if any
|
|
||||||
param_types: Vec<(tt::Subtree, Option<tt::Subtree>, Option<tt::Subtree>)>,
|
|
||||||
field_types: Vec<tt::Subtree>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
|
fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
|
||||||
|
@ -79,34 +75,17 @@ fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
|
||||||
ExpandError::Other("no item found".into())
|
ExpandError::Other("no item found".into())
|
||||||
})?;
|
})?;
|
||||||
let node = item.syntax();
|
let node = item.syntax();
|
||||||
let (name, params, fields) = match_ast! {
|
let (name, params) = match_ast! {
|
||||||
match node {
|
match node {
|
||||||
ast::Struct(it) => {
|
ast::Struct(it) => (it.name(), it.generic_param_list()),
|
||||||
(it.name(), it.generic_param_list(), it.field_list().into_iter().collect::<Vec<_>>())
|
ast::Enum(it) => (it.name(), it.generic_param_list()),
|
||||||
},
|
ast::Union(it) => (it.name(), it.generic_param_list()),
|
||||||
ast::Enum(it) => (it.name(), it.generic_param_list(), it.variant_list().into_iter().flat_map(|x| x.variants()).filter_map(|x| x.field_list()).collect()),
|
|
||||||
ast::Union(it) => (it.name(), it.generic_param_list(), it.record_field_list().into_iter().map(|x| ast::FieldList::RecordFieldList(x)).collect()),
|
|
||||||
_ => {
|
_ => {
|
||||||
debug!("unexpected node is {:?}", node);
|
debug!("unexpected node is {:?}", node);
|
||||||
return Err(ExpandError::Other("expected struct, enum or union".into()))
|
return Err(ExpandError::Other("expected struct, enum or union".into()))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let field_types = fields
|
|
||||||
.into_iter()
|
|
||||||
.flat_map(|f| match f {
|
|
||||||
ast::FieldList::RecordFieldList(x) => Either::Left(
|
|
||||||
x.fields()
|
|
||||||
.filter_map(|x| x.ty())
|
|
||||||
.map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0),
|
|
||||||
),
|
|
||||||
ast::FieldList::TupleFieldList(x) => Either::Right(
|
|
||||||
x.fields()
|
|
||||||
.filter_map(|x| x.ty())
|
|
||||||
.map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0),
|
|
||||||
),
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
let name = name.ok_or_else(|| {
|
let name = name.ok_or_else(|| {
|
||||||
debug!("parsed item has no name");
|
debug!("parsed item has no name");
|
||||||
ExpandError::Other("missing name".into())
|
ExpandError::Other("missing name".into())
|
||||||
|
@ -118,17 +97,7 @@ fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|param_list| param_list.type_or_const_params())
|
.flat_map(|param_list| param_list.type_or_const_params())
|
||||||
.map(|param| {
|
.map(|param| {
|
||||||
let name = param
|
if let ast::TypeOrConstParam::Const(param) = param {
|
||||||
.name()
|
|
||||||
.map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0)
|
|
||||||
.unwrap_or_else(tt::Subtree::empty);
|
|
||||||
let bounds = match ¶m {
|
|
||||||
ast::TypeOrConstParam::Type(x) => {
|
|
||||||
x.type_bound_list().map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0)
|
|
||||||
}
|
|
||||||
ast::TypeOrConstParam::Const(_) => None,
|
|
||||||
};
|
|
||||||
let ty = if let ast::TypeOrConstParam::Const(param) = param {
|
|
||||||
let ty = param
|
let ty = param
|
||||||
.ty()
|
.ty()
|
||||||
.map(|ty| mbe::syntax_node_to_token_tree(ty.syntax()).0)
|
.map(|ty| mbe::syntax_node_to_token_tree(ty.syntax()).0)
|
||||||
|
@ -136,11 +105,10 @@ fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
|
||||||
Some(ty)
|
Some(ty)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
}
|
||||||
(name, ty, bounds)
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
Ok(BasicAdtInfo { name: name_token, param_types, field_types })
|
Ok(BasicAdtInfo { name: name_token, param_types })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResult<tt::Subtree> {
|
fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResult<tt::Subtree> {
|
||||||
|
@ -148,16 +116,16 @@ fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResu
|
||||||
Ok(info) => info,
|
Ok(info) => info,
|
||||||
Err(e) => return ExpandResult::with_err(tt::Subtree::empty(), e),
|
Err(e) => return ExpandResult::with_err(tt::Subtree::empty(), e),
|
||||||
};
|
};
|
||||||
let mut where_block = vec![];
|
|
||||||
let (params, args): (Vec<_>, Vec<_>) = info
|
let (params, args): (Vec<_>, Vec<_>) = info
|
||||||
.param_types
|
.param_types
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(ident, param_ty, bound)| {
|
.enumerate()
|
||||||
|
.map(|(idx, param_ty)| {
|
||||||
|
let ident = tt::Leaf::Ident(tt::Ident {
|
||||||
|
span: tt::TokenId::unspecified(),
|
||||||
|
text: format!("T{idx}").into(),
|
||||||
|
});
|
||||||
let ident_ = ident.clone();
|
let ident_ = ident.clone();
|
||||||
if let Some(b) = bound {
|
|
||||||
let ident = ident.clone();
|
|
||||||
where_block.push(quote! { #ident : #b , });
|
|
||||||
}
|
|
||||||
if let Some(ty) = param_ty {
|
if let Some(ty) = param_ty {
|
||||||
(quote! { const #ident : #ty , }, quote! { #ident_ , })
|
(quote! { const #ident : #ty , }, quote! { #ident_ , })
|
||||||
} else {
|
} else {
|
||||||
|
@ -166,16 +134,9 @@ fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResu
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unzip();
|
.unzip();
|
||||||
|
|
||||||
where_block.extend(info.field_types.iter().map(|x| {
|
|
||||||
let x = x.clone();
|
|
||||||
let bound = trait_path.clone();
|
|
||||||
quote! { #x : #bound , }
|
|
||||||
}));
|
|
||||||
|
|
||||||
let name = info.name;
|
let name = info.name;
|
||||||
let expanded = quote! {
|
let expanded = quote! {
|
||||||
impl < ##params > #trait_path for #name < ##args > where ##where_block {}
|
impl < ##params > #trait_path for #name < ##args > {}
|
||||||
};
|
};
|
||||||
ExpandResult::ok(expanded)
|
ExpandResult::ok(expanded)
|
||||||
}
|
}
|
||||||
|
|
|
@ -471,7 +471,7 @@ struct Foo {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
Clone
|
Clone
|
||||||
impl < >core::clone::Clone for Foo< >where{}
|
impl < >core::clone::Clone for Foo< >{}
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -488,7 +488,7 @@ struct Foo {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
Copy
|
Copy
|
||||||
impl < >core::marker::Copy for Foo< >where{}
|
impl < >core::marker::Copy for Foo< >{}
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -504,7 +504,7 @@ struct Foo {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
Copy
|
Copy
|
||||||
impl < >core::marker::Copy for Foo< >where{}
|
impl < >core::marker::Copy for Foo< >{}
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
check(
|
check(
|
||||||
|
@ -516,7 +516,7 @@ struct Foo {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
Clone
|
Clone
|
||||||
impl < >core::clone::Clone for Foo< >where{}
|
impl < >core::clone::Clone for Foo< >{}
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue