1use std::cell::RefCell;
178use std::ops::Not;
179use std::{iter, vec};
180
181pub(crate) use StaticFields::*;
182pub(crate) use SubstructureFields::*;
183use rustc_ast::token::{IdentIsRaw, LitKind, Token, TokenKind};
184use rustc_ast::tokenstream::{DelimSpan, Spacing, TokenTree};
185use rustc_ast::{
186 self as ast, AnonConst, AttrArgs, BindingMode, ByRef, DelimArgs, EnumDef, Expr, GenericArg,
187 GenericParamKind, Generics, Mutability, PatKind, Safety, VariantData,
188};
189use rustc_attr_parsing::AttributeParser;
190use rustc_expand::base::{Annotatable, ExtCtxt};
191use rustc_hir::Attribute;
192use rustc_hir::attrs::{AttributeKind, ReprPacked};
193use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
194use thin_vec::{ThinVec, thin_vec};
195use ty::{Bounds, Path, Ref, Self_, Ty};
196
197use crate::{deriving, errors};
198
199pub(crate) mod ty;
200
201pub(crate) struct TraitDef<'a> {
202 pub span: Span,
204
205 pub path: Path,
207
208 pub skip_path_as_bound: bool,
210
211 pub needs_copy_as_bound_if_packed: bool,
213
214 pub additional_bounds: Vec<Ty>,
217
218 pub supports_unions: bool,
220
221 pub methods: Vec<MethodDef<'a>>,
222
223 pub associated_types: Vec<(Ident, Ty)>,
224
225 pub is_const: bool,
226
227 pub is_staged_api_crate: bool,
228
229 pub safety: Safety,
231
232 pub document: bool,
234}
235
236pub(crate) struct MethodDef<'a> {
237 pub name: Symbol,
239 pub generics: Bounds,
241
242 pub explicit_self: bool,
244
245 pub nonself_args: Vec<(Ty, Symbol)>,
247
248 pub ret_ty: Ty,
250
251 pub attributes: ast::AttrVec,
252
253 pub fieldless_variants_strategy: FieldlessVariantsStrategy,
254
255 pub combine_substructure: RefCell<CombineSubstructureFunc<'a>>,
256}
257
258#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for FieldlessVariantsStrategy {
#[inline]
fn eq(&self, other: &FieldlessVariantsStrategy) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
260pub(crate) enum FieldlessVariantsStrategy {
261 Unify,
265 Default,
268 SpecializeIfAllVariantsFieldless,
272}
273
274pub(crate) struct Substructure<'a> {
276 pub type_ident: Ident,
278 pub nonselflike_args: &'a [Box<Expr>],
281 pub fields: &'a SubstructureFields<'a>,
282}
283
284pub(crate) struct FieldInfo {
286 pub span: Span,
287 pub name: Option<Ident>,
290 pub self_expr: Box<Expr>,
293 pub other_selflike_exprs: Vec<Box<Expr>>,
296 pub maybe_scalar: bool,
297}
298
299#[derive(#[automatically_derived]
impl ::core::marker::Copy for IsTuple { }Copy, #[automatically_derived]
impl ::core::clone::Clone for IsTuple {
#[inline]
fn clone(&self) -> IsTuple { *self }
}Clone)]
300pub(crate) enum IsTuple {
301 No,
302 Yes,
303}
304
305pub(crate) enum StaticFields<'a> {
307 Unnamed(Vec<Span>, IsTuple),
309 Named(Vec<(Ident, Span, Option<&'a AnonConst>)>),
311}
312
313pub(crate) enum SubstructureFields<'a> {
315 Struct(&'a ast::VariantData, Vec<FieldInfo>),
317
318 AllFieldlessEnum(&'a ast::EnumDef),
322
323 EnumMatching(&'a ast::Variant, Vec<FieldInfo>),
327
328 EnumDiscr(FieldInfo, Option<Box<Expr>>),
332
333 StaticStruct(&'a ast::VariantData, StaticFields<'a>),
335
336 StaticEnum(&'a ast::EnumDef),
338}
339
340pub(crate) type CombineSubstructureFunc<'a> =
343 Box<dyn FnMut(&ExtCtxt<'_>, Span, &Substructure<'_>) -> BlockOrExpr + 'a>;
344
345pub(crate) fn combine_substructure(
346 f: CombineSubstructureFunc<'_>,
347) -> RefCell<CombineSubstructureFunc<'_>> {
348 RefCell::new(f)
349}
350
351struct TypeParameter {
352 bound_generic_params: ThinVec<ast::GenericParam>,
353 ty: Box<ast::Ty>,
354}
355
356pub(crate) struct BlockOrExpr(ThinVec<ast::Stmt>, Option<Box<Expr>>);
363
364impl BlockOrExpr {
365 pub(crate) fn new_stmts(stmts: ThinVec<ast::Stmt>) -> BlockOrExpr {
366 BlockOrExpr(stmts, None)
367 }
368
369 pub(crate) fn new_expr(expr: Box<Expr>) -> BlockOrExpr {
370 BlockOrExpr(ThinVec::new(), Some(expr))
371 }
372
373 pub(crate) fn new_mixed(stmts: ThinVec<ast::Stmt>, expr: Option<Box<Expr>>) -> BlockOrExpr {
374 BlockOrExpr(stmts, expr)
375 }
376
377 fn into_block(mut self, cx: &ExtCtxt<'_>, span: Span) -> Box<ast::Block> {
379 if let Some(expr) = self.1 {
380 self.0.push(cx.stmt_expr(expr));
381 }
382 cx.block(span, self.0)
383 }
384
385 fn into_expr(self, cx: &ExtCtxt<'_>, span: Span) -> Box<Expr> {
387 if self.0.is_empty() {
388 match self.1 {
389 None => cx.expr_block(cx.block(span, ThinVec::new())),
390 Some(expr) => expr,
391 }
392 } else if let [stmt] = self.0.as_slice()
393 && let ast::StmtKind::Expr(expr) = &stmt.kind
394 && self.1.is_none()
395 {
396 expr.clone()
398 } else {
399 cx.expr_block(self.into_block(cx, span))
401 }
402 }
403}
404
405fn find_type_parameters(
410 ty: &ast::Ty,
411 ty_param_names: &[Symbol],
412 cx: &ExtCtxt<'_>,
413) -> Vec<TypeParameter> {
414 use rustc_ast::visit;
415
416 struct Visitor<'a, 'b> {
417 cx: &'a ExtCtxt<'b>,
418 ty_param_names: &'a [Symbol],
419 bound_generic_params_stack: ThinVec<ast::GenericParam>,
420 type_params: Vec<TypeParameter>,
421 }
422
423 impl<'a, 'b> visit::Visitor<'a> for Visitor<'a, 'b> {
424 fn visit_ty(&mut self, ty: &'a ast::Ty) {
425 let stack_len = self.bound_generic_params_stack.len();
426 if let ast::TyKind::FnPtr(fn_ptr) = &ty.kind
427 && !fn_ptr.generic_params.is_empty()
428 {
429 self.bound_generic_params_stack.extend(fn_ptr.generic_params.iter().cloned());
432 }
433
434 if let ast::TyKind::Path(_, path) = &ty.kind
435 && let Some(segment) = path.segments.first()
436 && self.ty_param_names.contains(&segment.ident.name)
437 {
438 self.type_params.push(TypeParameter {
439 bound_generic_params: self.bound_generic_params_stack.clone(),
440 ty: Box::new(ty.clone()),
441 });
442 }
443
444 visit::walk_ty(self, ty);
445 self.bound_generic_params_stack.truncate(stack_len);
446 }
447
448 fn visit_poly_trait_ref(&mut self, trait_ref: &'a ast::PolyTraitRef) {
450 let stack_len = self.bound_generic_params_stack.len();
451 self.bound_generic_params_stack.extend(trait_ref.bound_generic_params.iter().cloned());
452
453 visit::walk_poly_trait_ref(self, trait_ref);
454
455 self.bound_generic_params_stack.truncate(stack_len);
456 }
457
458 fn visit_mac_call(&mut self, mac: &ast::MacCall) {
459 self.cx.dcx().emit_err(errors::DeriveMacroCall { span: mac.span() });
460 }
461 }
462
463 let mut visitor = Visitor {
464 cx,
465 ty_param_names,
466 bound_generic_params_stack: ThinVec::new(),
467 type_params: Vec::new(),
468 };
469 visit::Visitor::visit_ty(&mut visitor, ty);
470
471 visitor.type_params
472}
473
474impl<'a> TraitDef<'a> {
475 pub(crate) fn expand(
476 self,
477 cx: &ExtCtxt<'_>,
478 mitem: &ast::MetaItem,
479 item: &'a Annotatable,
480 push: &mut dyn FnMut(Annotatable),
481 ) {
482 self.expand_ext(cx, mitem, item, push, false);
483 }
484
485 pub(crate) fn expand_ext(
486 self,
487 cx: &ExtCtxt<'_>,
488 mitem: &ast::MetaItem,
489 item: &'a Annotatable,
490 push: &mut dyn FnMut(Annotatable),
491 from_scratch: bool,
492 ) {
493 match item {
494 Annotatable::Item(item) => {
495 let is_packed = #[allow(non_exhaustive_omitted_patterns)] match AttributeParser::parse_limited(cx.sess,
&item.attrs, sym::repr, item.span, item.id, None) {
Some(Attribute::Parsed(AttributeKind::Repr { reprs, .. })) if
reprs.iter().any(|(x, _)|
#[allow(non_exhaustive_omitted_patterns)] match x {
ReprPacked(..) => true,
_ => false,
}) => true,
_ => false,
}matches!(
496 AttributeParser::parse_limited(cx.sess, &item.attrs, sym::repr, item.span, item.id, None),
497 Some(Attribute::Parsed(AttributeKind::Repr { reprs, .. })) if reprs.iter().any(|(x, _)| matches!(x, ReprPacked(..)))
498 );
499
500 let newitem = match &item.kind {
501 ast::ItemKind::Struct(ident, generics, struct_def) => self.expand_struct_def(
502 cx,
503 struct_def,
504 *ident,
505 generics,
506 from_scratch,
507 is_packed,
508 ),
509 ast::ItemKind::Enum(ident, generics, enum_def) => {
510 self.expand_enum_def(cx, enum_def, *ident, generics, from_scratch)
516 }
517 ast::ItemKind::Union(ident, generics, struct_def) => {
518 if self.supports_unions {
519 self.expand_struct_def(
520 cx,
521 struct_def,
522 *ident,
523 generics,
524 from_scratch,
525 is_packed,
526 )
527 } else {
528 cx.dcx().emit_err(errors::DeriveUnion { span: mitem.span });
529 return;
530 }
531 }
532 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
533 };
534 let mut attrs = newitem.attrs.clone();
537 attrs.extend(
538 item.attrs
539 .iter()
540 .filter(|a| {
541 a.has_any_name(&[
542 sym::allow,
543 sym::expect,
544 sym::warn,
545 sym::deny,
546 sym::forbid,
547 sym::stable,
548 sym::unstable,
549 ])
550 })
551 .cloned(),
552 );
553 push(Annotatable::Item(Box::new(ast::Item { attrs, ..(*newitem).clone() })))
554 }
555 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
556 }
557 }
558
559 fn create_derived_impl(
595 &self,
596 cx: &ExtCtxt<'_>,
597 type_ident: Ident,
598 generics: &Generics,
599 field_tys: Vec<&ast::Ty>,
600 methods: Vec<Box<ast::AssocItem>>,
601 is_packed: bool,
602 ) -> Box<ast::Item> {
603 let trait_path = self.path.to_path(cx, self.span, type_ident, generics);
604
605 let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| {
607 Box::new(ast::AssocItem {
608 id: ast::DUMMY_NODE_ID,
609 span: self.span,
610 vis: ast::Visibility {
611 span: self.span.shrink_to_lo(),
612 kind: ast::VisibilityKind::Inherited,
613 tokens: None,
614 },
615 attrs: ast::AttrVec::new(),
616 kind: ast::AssocItemKind::Type(Box::new(ast::TyAlias {
617 defaultness: ast::Defaultness::Implicit,
618 ident,
619 generics: Generics::default(),
620 after_where_clause: ast::WhereClause::default(),
621 bounds: Vec::new(),
622 ty: Some(type_def.to_ty(cx, self.span, type_ident, generics)),
623 })),
624 tokens: None,
625 })
626 });
627
628 let mut where_clause = ast::WhereClause::default();
629 where_clause.span = generics.where_clause.span;
630 let ctxt = self.span.ctxt();
631 let span = generics.span.with_ctxt(ctxt);
632
633 let params: ThinVec<_> = generics
635 .params
636 .iter()
637 .map(|param| match ¶m.kind {
638 GenericParamKind::Lifetime { .. } => param.clone(),
639 GenericParamKind::Type { .. } => {
640 let span = param.ident.span.with_ctxt(ctxt);
643 let bounds: Vec<_> = self
644 .additional_bounds
645 .iter()
646 .map(|p| {
647 cx.trait_bound(p.to_path(cx, span, type_ident, generics), self.is_const)
648 })
649 .chain(
650 self.skip_path_as_bound.not().then(|| {
652 let mut trait_path = trait_path.clone();
653 trait_path.span = span;
654 cx.trait_bound(trait_path, self.is_const)
655 }),
656 )
657 .chain({
658 if is_packed && self.needs_copy_as_bound_if_packed {
660 let p = generic::ty::Path::new({
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[sym::marker, sym::Copy]))
})deriving::path_std!(marker::Copy);
661 Some(cx.trait_bound(
662 p.to_path(cx, span, type_ident, generics),
663 self.is_const,
664 ))
665 } else {
666 None
667 }
668 })
669 .chain(
670 param.bounds.iter().cloned(),
672 )
673 .collect();
674
675 cx.typaram(span, param.ident, bounds, None)
676 }
677 GenericParamKind::Const { ty, span, .. } => {
678 let const_nodefault_kind = GenericParamKind::Const {
679 ty: ty.clone(),
680 span: span.with_ctxt(ctxt),
681
682 default: None,
684 };
685 let mut param_clone = param.clone();
686 param_clone.kind = const_nodefault_kind;
687 param_clone
688 }
689 })
690 .map(|mut param| {
691 param.attrs.clear();
694 param
695 })
696 .collect();
697
698 where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| {
700 ast::WherePredicate {
701 attrs: clause.attrs.clone(),
702 kind: clause.kind.clone(),
703 id: ast::DUMMY_NODE_ID,
704 span: clause.span.with_ctxt(ctxt),
705 is_placeholder: false,
706 }
707 }));
708
709 let ty_param_names: Vec<Symbol> = params
710 .iter()
711 .filter(|param| #[allow(non_exhaustive_omitted_patterns)] match param.kind {
ast::GenericParamKind::Type { .. } => true,
_ => false,
}matches!(param.kind, ast::GenericParamKind::Type { .. }))
712 .map(|ty_param| ty_param.ident.name)
713 .collect();
714
715 if !ty_param_names.is_empty() {
716 for field_ty in field_tys {
717 let field_ty_params = find_type_parameters(&field_ty, &ty_param_names, cx);
718
719 for field_ty_param in field_ty_params {
720 if let ast::TyKind::Path(_, p) = &field_ty_param.ty.kind
722 && let [sole_segment] = &*p.segments
723 && ty_param_names.contains(&sole_segment.ident.name)
724 {
725 continue;
726 }
727 let mut bounds: Vec<_> = self
728 .additional_bounds
729 .iter()
730 .map(|p| {
731 cx.trait_bound(
732 p.to_path(cx, self.span, type_ident, generics),
733 self.is_const,
734 )
735 })
736 .collect();
737
738 if !self.skip_path_as_bound {
740 bounds.push(cx.trait_bound(trait_path.clone(), self.is_const));
741 }
742
743 if is_packed && self.needs_copy_as_bound_if_packed {
745 let p = generic::ty::Path::new({
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[sym::marker, sym::Copy]))
})deriving::path_std!(marker::Copy);
746 bounds.push(cx.trait_bound(
747 p.to_path(cx, self.span, type_ident, generics),
748 self.is_const,
749 ));
750 }
751
752 if !bounds.is_empty() {
753 let predicate = ast::WhereBoundPredicate {
754 bound_generic_params: field_ty_param.bound_generic_params,
755 bounded_ty: field_ty_param.ty,
756 bounds,
757 };
758
759 let kind = ast::WherePredicateKind::BoundPredicate(predicate);
760 let predicate = ast::WherePredicate {
761 attrs: ThinVec::new(),
762 kind,
763 id: ast::DUMMY_NODE_ID,
764 span: self.span,
765 is_placeholder: false,
766 };
767 where_clause.predicates.push(predicate);
768 }
769 }
770 }
771 }
772
773 let trait_generics = Generics { params, where_clause, span };
774
775 let trait_ref = cx.trait_ref(trait_path);
777
778 let self_params: Vec<_> = generics
779 .params
780 .iter()
781 .map(|param| match param.kind {
782 GenericParamKind::Lifetime { .. } => {
783 GenericArg::Lifetime(cx.lifetime(param.ident.span.with_ctxt(ctxt), param.ident))
784 }
785 GenericParamKind::Type { .. } => {
786 GenericArg::Type(cx.ty_ident(param.ident.span.with_ctxt(ctxt), param.ident))
787 }
788 GenericParamKind::Const { .. } => {
789 GenericArg::Const(cx.const_ident(param.ident.span.with_ctxt(ctxt), param.ident))
790 }
791 })
792 .collect();
793
794 let path =
796 cx.path_all(type_ident.span.with_ctxt(ctxt), false, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[type_ident]))vec![type_ident], self_params);
797 let self_type = cx.ty_path(path);
798 let rustc_const_unstable =
799 cx.path_ident(self.span, Ident::new(sym::rustc_const_unstable, self.span));
800
801 let mut attrs = {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(cx.attr_word(sym::automatically_derived, self.span));
vec
}thin_vec![cx.attr_word(sym::automatically_derived, self.span),];
802
803 if self.is_const && self.is_staged_api_crate {
807 attrs.push(
808 cx.attr_nested(
809 rustc_ast::AttrItem {
810 unsafety: Safety::Default,
811 path: rustc_const_unstable,
812 args: rustc_ast::ast::AttrItemKind::Unparsed(AttrArgs::Delimited(
813 DelimArgs {
814 dspan: DelimSpan::from_single(self.span),
815 delim: rustc_ast::token::Delimiter::Parenthesis,
816 tokens: [
817 TokenKind::Ident(sym::feature, IdentIsRaw::No),
818 TokenKind::Eq,
819 TokenKind::lit(LitKind::Str, sym::derive_const, None),
820 TokenKind::Comma,
821 TokenKind::Ident(sym::issue, IdentIsRaw::No),
822 TokenKind::Eq,
823 TokenKind::lit(LitKind::Str, sym::derive_const_issue, None),
824 ]
825 .into_iter()
826 .map(|kind| {
827 TokenTree::Token(
828 Token { kind, span: self.span },
829 Spacing::Alone,
830 )
831 })
832 .collect(),
833 },
834 )),
835 tokens: None,
836 },
837 self.span,
838 ),
839 )
840 }
841
842 if !self.document {
843 attrs.push(cx.attr_nested_word(sym::doc, sym::hidden, self.span));
844 }
845
846 cx.item(
847 self.span,
848 attrs,
849 ast::ItemKind::Impl(ast::Impl {
850 generics: trait_generics,
851 of_trait: Some(Box::new(ast::TraitImplHeader {
852 safety: self.safety,
853 polarity: ast::ImplPolarity::Positive,
854 defaultness: ast::Defaultness::Implicit,
855 trait_ref,
856 })),
857 constness: if self.is_const { ast::Const::Yes(DUMMY_SP) } else { ast::Const::No },
858 self_ty: self_type,
859 items: methods.into_iter().chain(associated_types).collect(),
860 }),
861 )
862 }
863
864 fn expand_struct_def(
865 &self,
866 cx: &ExtCtxt<'_>,
867 struct_def: &'a VariantData,
868 type_ident: Ident,
869 generics: &Generics,
870 from_scratch: bool,
871 is_packed: bool,
872 ) -> Box<ast::Item> {
873 let field_tys = Vec::from_iter(struct_def.fields().iter().map(|field| &*field.ty));
874
875 let methods = self
876 .methods
877 .iter()
878 .map(|method_def| {
879 let (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) =
880 method_def.extract_arg_details(cx, self, type_ident, generics);
881
882 let body = if from_scratch || method_def.is_static() {
883 method_def.expand_static_struct_method_body(
884 cx,
885 self,
886 struct_def,
887 type_ident,
888 &nonselflike_args,
889 )
890 } else {
891 method_def.expand_struct_method_body(
892 cx,
893 self,
894 struct_def,
895 type_ident,
896 &selflike_args,
897 &nonselflike_args,
898 is_packed,
899 )
900 };
901
902 method_def.create_method(
903 cx,
904 self,
905 type_ident,
906 generics,
907 explicit_self,
908 nonself_arg_tys,
909 body,
910 )
911 })
912 .collect();
913
914 self.create_derived_impl(cx, type_ident, generics, field_tys, methods, is_packed)
915 }
916
917 fn expand_enum_def(
918 &self,
919 cx: &ExtCtxt<'_>,
920 enum_def: &'a EnumDef,
921 type_ident: Ident,
922 generics: &Generics,
923 from_scratch: bool,
924 ) -> Box<ast::Item> {
925 let field_tys = Vec::from_iter(
926 enum_def
927 .variants
928 .iter()
929 .flat_map(|variant| variant.data.fields())
930 .map(|field| &*field.ty),
931 );
932
933 let methods = self
934 .methods
935 .iter()
936 .map(|method_def| {
937 let (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) =
938 method_def.extract_arg_details(cx, self, type_ident, generics);
939
940 let body = if from_scratch || method_def.is_static() {
941 method_def.expand_static_enum_method_body(
942 cx,
943 self,
944 enum_def,
945 type_ident,
946 &nonselflike_args,
947 )
948 } else {
949 method_def.expand_enum_method_body(
950 cx,
951 self,
952 enum_def,
953 type_ident,
954 selflike_args,
955 &nonselflike_args,
956 )
957 };
958
959 method_def.create_method(
960 cx,
961 self,
962 type_ident,
963 generics,
964 explicit_self,
965 nonself_arg_tys,
966 body,
967 )
968 })
969 .collect();
970
971 let is_packed = false; self.create_derived_impl(cx, type_ident, generics, field_tys, methods, is_packed)
973 }
974}
975
976impl<'a> MethodDef<'a> {
977 fn call_substructure_method(
978 &self,
979 cx: &ExtCtxt<'_>,
980 trait_: &TraitDef<'_>,
981 type_ident: Ident,
982 nonselflike_args: &[Box<Expr>],
983 fields: &SubstructureFields<'_>,
984 ) -> BlockOrExpr {
985 let span = trait_.span;
986 let substructure = Substructure { type_ident, nonselflike_args, fields };
987 let mut f = self.combine_substructure.borrow_mut();
988 let f: &mut CombineSubstructureFunc<'_> = &mut *f;
989 f(cx, span, &substructure)
990 }
991
992 fn is_static(&self) -> bool {
993 !self.explicit_self
994 }
995
996 fn extract_arg_details(
1004 &self,
1005 cx: &ExtCtxt<'_>,
1006 trait_: &TraitDef<'_>,
1007 type_ident: Ident,
1008 generics: &Generics,
1009 ) -> (Option<ast::ExplicitSelf>, ThinVec<Box<Expr>>, Vec<Box<Expr>>, Vec<(Ident, Box<ast::Ty>)>)
1010 {
1011 let mut selflike_args = ThinVec::new();
1012 let mut nonselflike_args = Vec::new();
1013 let mut nonself_arg_tys = Vec::new();
1014 let span = trait_.span;
1015
1016 let explicit_self = self.explicit_self.then(|| {
1017 let (self_expr, explicit_self) = ty::get_explicit_self(cx, span);
1018 selflike_args.push(self_expr);
1019 explicit_self
1020 });
1021
1022 for (ty, name) in self.nonself_args.iter() {
1023 let ast_ty = ty.to_ty(cx, span, type_ident, generics);
1024 let ident = Ident::new(*name, span);
1025 nonself_arg_tys.push((ident, ast_ty));
1026
1027 let arg_expr = cx.expr_ident(span, ident);
1028
1029 match ty {
1030 Ref(box Self_, _) if !self.is_static() => selflike_args.push(arg_expr),
1032 Self_ => cx.dcx().span_bug(span, "`Self` in non-return position"),
1033 _ => nonselflike_args.push(arg_expr),
1034 }
1035 }
1036
1037 (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys)
1038 }
1039
1040 fn create_method(
1041 &self,
1042 cx: &ExtCtxt<'_>,
1043 trait_: &TraitDef<'_>,
1044 type_ident: Ident,
1045 generics: &Generics,
1046 explicit_self: Option<ast::ExplicitSelf>,
1047 nonself_arg_tys: Vec<(Ident, Box<ast::Ty>)>,
1048 body: BlockOrExpr,
1049 ) -> Box<ast::AssocItem> {
1050 let span = trait_.span;
1051 let fn_generics = self.generics.to_generics(cx, span, type_ident, generics);
1053
1054 let args = {
1055 let self_arg = explicit_self.map(|explicit_self| {
1056 let ident = Ident::with_dummy_span(kw::SelfLower).with_span_pos(span);
1057 ast::Param::from_self(ast::AttrVec::default(), explicit_self, ident)
1058 });
1059 let nonself_args =
1060 nonself_arg_tys.into_iter().map(|(name, ty)| cx.param(span, name, ty));
1061 self_arg.into_iter().chain(nonself_args).collect()
1062 };
1063
1064 let ret_type = if let Ty::Unit = &self.ret_ty {
1065 ast::FnRetTy::Default(span)
1066 } else {
1067 ast::FnRetTy::Ty(self.ret_ty.to_ty(cx, span, type_ident, generics))
1068 };
1069
1070 let method_ident = Ident::new(self.name, span);
1071 let fn_decl = cx.fn_decl(args, ret_type);
1072 let body_block = body.into_block(cx, span);
1073
1074 let trait_lo_sp = span.shrink_to_lo();
1075
1076 let sig = ast::FnSig { header: ast::FnHeader::default(), decl: fn_decl, span };
1077 let defaultness = ast::Defaultness::Implicit;
1078
1079 Box::new(ast::AssocItem {
1081 id: ast::DUMMY_NODE_ID,
1082 attrs: self.attributes.clone(),
1083 span,
1084 vis: ast::Visibility {
1085 span: trait_lo_sp,
1086 kind: ast::VisibilityKind::Inherited,
1087 tokens: None,
1088 },
1089 kind: ast::AssocItemKind::Fn(Box::new(ast::Fn {
1090 defaultness,
1091 sig,
1092 ident: method_ident,
1093 generics: fn_generics,
1094 contract: None,
1095 body: Some(body_block),
1096 define_opaque: None,
1097 eii_impls: ThinVec::new(),
1098 })),
1099 tokens: None,
1100 })
1101 }
1102
1103 fn expand_struct_method_body<'b>(
1139 &self,
1140 cx: &ExtCtxt<'_>,
1141 trait_: &TraitDef<'b>,
1142 struct_def: &'b VariantData,
1143 type_ident: Ident,
1144 selflike_args: &[Box<Expr>],
1145 nonselflike_args: &[Box<Expr>],
1146 is_packed: bool,
1147 ) -> BlockOrExpr {
1148 if !(selflike_args.len() == 1 || selflike_args.len() == 2) {
::core::panicking::panic("assertion failed: selflike_args.len() == 1 || selflike_args.len() == 2")
};assert!(selflike_args.len() == 1 || selflike_args.len() == 2);
1149
1150 let selflike_fields =
1151 trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, is_packed);
1152 self.call_substructure_method(
1153 cx,
1154 trait_,
1155 type_ident,
1156 nonselflike_args,
1157 &Struct(struct_def, selflike_fields),
1158 )
1159 }
1160
1161 fn expand_static_struct_method_body(
1162 &self,
1163 cx: &ExtCtxt<'_>,
1164 trait_: &TraitDef<'a>,
1165 struct_def: &'a VariantData,
1166 type_ident: Ident,
1167 nonselflike_args: &[Box<Expr>],
1168 ) -> BlockOrExpr {
1169 let summary = trait_.summarise_struct(cx, struct_def);
1170
1171 self.call_substructure_method(
1172 cx,
1173 trait_,
1174 type_ident,
1175 nonselflike_args,
1176 &StaticStruct(struct_def, summary),
1177 )
1178 }
1179
1180 fn expand_enum_method_body<'b>(
1216 &self,
1217 cx: &ExtCtxt<'_>,
1218 trait_: &TraitDef<'b>,
1219 enum_def: &'b EnumDef,
1220 type_ident: Ident,
1221 mut selflike_args: ThinVec<Box<Expr>>,
1222 nonselflike_args: &[Box<Expr>],
1223 ) -> BlockOrExpr {
1224 if !!selflike_args.is_empty() {
{
::core::panicking::panic_fmt(format_args!("static methods must use `expand_static_enum_method_body`"));
}
};assert!(
1225 !selflike_args.is_empty(),
1226 "static methods must use `expand_static_enum_method_body`",
1227 );
1228
1229 let span = trait_.span;
1230 let variants = &enum_def.variants;
1231
1232 let unify_fieldless_variants =
1234 self.fieldless_variants_strategy == FieldlessVariantsStrategy::Unify;
1235
1236 if variants.is_empty() {
1240 selflike_args.truncate(1);
1241 let match_arg = cx.expr_deref(span, selflike_args.pop().unwrap());
1242 let match_arms = ThinVec::new();
1243 let expr = cx.expr_match(span, match_arg, match_arms);
1244 return BlockOrExpr(ThinVec::new(), Some(expr));
1245 }
1246
1247 let prefixes = iter::once("__self".to_string())
1248 .chain(
1249 selflike_args
1250 .iter()
1251 .enumerate()
1252 .skip(1)
1253 .map(|(arg_count, _selflike_arg)| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("__arg{0}", arg_count))
})format!("__arg{arg_count}")),
1254 )
1255 .collect::<Vec<String>>();
1256
1257 let get_discr_pieces = |cx: &ExtCtxt<'_>| {
1266 let discr_idents: Vec<_> = prefixes
1267 .iter()
1268 .map(|name| Ident::from_str_and_span(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_discr", name))
})format!("{name}_discr"), span))
1269 .collect();
1270
1271 let mut discr_exprs: Vec<_> = discr_idents
1272 .iter()
1273 .map(|&ident| cx.expr_addr_of(span, cx.expr_ident(span, ident)))
1274 .collect();
1275
1276 let self_expr = discr_exprs.remove(0);
1277 let other_selflike_exprs = discr_exprs;
1278 let discr_field =
1279 FieldInfo { span, name: None, self_expr, other_selflike_exprs, maybe_scalar: true };
1280
1281 let discr_let_stmts: ThinVec<_> = iter::zip(&discr_idents, &selflike_args)
1282 .map(|(&ident, selflike_arg)| {
1283 let variant_value = deriving::call_intrinsic(
1284 cx,
1285 span,
1286 sym::discriminant_value,
1287 {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(selflike_arg.clone());
vec
}thin_vec![selflike_arg.clone()],
1288 );
1289 cx.stmt_let(span, false, ident, variant_value)
1290 })
1291 .collect();
1292
1293 (discr_field, discr_let_stmts)
1294 };
1295
1296 let all_fieldless = variants.iter().all(|v| v.data.fields().is_empty());
1299 if all_fieldless {
1300 if variants.len() > 1 {
1301 match self.fieldless_variants_strategy {
1302 FieldlessVariantsStrategy::Unify => {
1303 let (discr_field, mut discr_let_stmts) = get_discr_pieces(cx);
1307 let mut discr_check = self.call_substructure_method(
1308 cx,
1309 trait_,
1310 type_ident,
1311 nonselflike_args,
1312 &EnumDiscr(discr_field, None),
1313 );
1314 discr_let_stmts.append(&mut discr_check.0);
1315 return BlockOrExpr(discr_let_stmts, discr_check.1);
1316 }
1317 FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless => {
1318 return self.call_substructure_method(
1319 cx,
1320 trait_,
1321 type_ident,
1322 nonselflike_args,
1323 &AllFieldlessEnum(enum_def),
1324 );
1325 }
1326 FieldlessVariantsStrategy::Default => (),
1327 }
1328 } else if let [variant] = variants.as_slice() {
1329 return self.call_substructure_method(
1332 cx,
1333 trait_,
1334 type_ident,
1335 nonselflike_args,
1336 &EnumMatching(variant, Vec::new()),
1337 );
1338 }
1339 }
1340
1341 let mut match_arms: ThinVec<ast::Arm> = variants
1347 .iter()
1348 .filter(|&v| !(unify_fieldless_variants && v.data.fields().is_empty()))
1349 .map(|variant| {
1350 let fields = trait_.create_struct_pattern_fields(cx, &variant.data, &prefixes);
1354
1355 let sp = variant.span.with_ctxt(trait_.span.ctxt());
1356 let variant_path = cx.path(sp, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[type_ident, variant.ident]))vec![type_ident, variant.ident]);
1357 let by_ref = ByRef::No; let mut subpats = trait_.create_struct_patterns(
1359 cx,
1360 variant_path,
1361 &variant.data,
1362 &prefixes,
1363 by_ref,
1364 );
1365
1366 let single_pat = if subpats.len() == 1 {
1368 subpats.pop().unwrap()
1369 } else {
1370 cx.pat_tuple(span, subpats)
1371 };
1372
1373 let substructure = EnumMatching(variant, fields);
1382 let arm_expr = self
1383 .call_substructure_method(
1384 cx,
1385 trait_,
1386 type_ident,
1387 nonselflike_args,
1388 &substructure,
1389 )
1390 .into_expr(cx, span);
1391
1392 cx.arm(span, single_pat, arm_expr)
1393 })
1394 .collect();
1395
1396 let first_fieldless = variants.iter().find(|v| v.data.fields().is_empty());
1398 let default = match first_fieldless {
1399 Some(v) if unify_fieldless_variants => {
1400 Some(
1404 self.call_substructure_method(
1405 cx,
1406 trait_,
1407 type_ident,
1408 nonselflike_args,
1409 &EnumMatching(v, Vec::new()),
1410 )
1411 .into_expr(cx, span),
1412 )
1413 }
1414 _ if variants.len() > 1 && selflike_args.len() > 1 => {
1415 Some(deriving::call_unreachable(cx, span))
1419 }
1420 _ => None,
1421 };
1422 if let Some(arm) = default {
1423 match_arms.push(cx.arm(span, cx.pat_wild(span), arm));
1424 }
1425
1426 let get_match_expr = |mut selflike_args: ThinVec<Box<Expr>>| {
1435 let match_arg = if selflike_args.len() == 1 {
1436 selflike_args.pop().unwrap()
1437 } else {
1438 cx.expr(span, ast::ExprKind::Tup(selflike_args))
1439 };
1440 cx.expr_match(span, match_arg, match_arms)
1441 };
1442
1443 if unify_fieldless_variants && variants.len() > 1 {
1447 let (discr_field, mut discr_let_stmts) = get_discr_pieces(cx);
1448
1449 let mut discr_check_plus_match = self.call_substructure_method(
1451 cx,
1452 trait_,
1453 type_ident,
1454 nonselflike_args,
1455 &EnumDiscr(discr_field, Some(get_match_expr(selflike_args))),
1456 );
1457 discr_let_stmts.append(&mut discr_check_plus_match.0);
1458 BlockOrExpr(discr_let_stmts, discr_check_plus_match.1)
1459 } else {
1460 BlockOrExpr(ThinVec::new(), Some(get_match_expr(selflike_args)))
1461 }
1462 }
1463
1464 fn expand_static_enum_method_body(
1465 &self,
1466 cx: &ExtCtxt<'_>,
1467 trait_: &TraitDef<'_>,
1468 enum_def: &EnumDef,
1469 type_ident: Ident,
1470 nonselflike_args: &[Box<Expr>],
1471 ) -> BlockOrExpr {
1472 self.call_substructure_method(
1473 cx,
1474 trait_,
1475 type_ident,
1476 nonselflike_args,
1477 &StaticEnum(enum_def),
1478 )
1479 }
1480}
1481
1482impl<'a> TraitDef<'a> {
1484 fn summarise_struct(&self, cx: &ExtCtxt<'_>, struct_def: &'a VariantData) -> StaticFields<'a> {
1485 let mut named_idents = Vec::new();
1486 let mut just_spans = Vec::new();
1487 for field in struct_def.fields() {
1488 let sp = field.span.with_ctxt(self.span.ctxt());
1489 match field.ident {
1490 Some(ident) => named_idents.push((ident, sp, field.default.as_ref())),
1491 _ => just_spans.push(sp),
1492 }
1493 }
1494
1495 let is_tuple = match struct_def {
1496 ast::VariantData::Tuple(..) => IsTuple::Yes,
1497 _ => IsTuple::No,
1498 };
1499 match (just_spans.is_empty(), named_idents.is_empty()) {
1500 (false, false) => cx
1501 .dcx()
1502 .span_bug(self.span, "a struct with named and unnamed fields in generic `derive`"),
1503 (_, false) => Named(named_idents),
1505 (false, _) => Unnamed(just_spans, is_tuple),
1507 _ => Named(Vec::new()),
1509 }
1510 }
1511
1512 fn create_struct_patterns(
1513 &self,
1514 cx: &ExtCtxt<'_>,
1515 struct_path: ast::Path,
1516 struct_def: &'a VariantData,
1517 prefixes: &[String],
1518 by_ref: ByRef,
1519 ) -> ThinVec<ast::Pat> {
1520 prefixes
1521 .iter()
1522 .map(|prefix| {
1523 let pieces_iter =
1524 struct_def.fields().iter().enumerate().map(|(i, struct_field)| {
1525 let sp = struct_field.span.with_ctxt(self.span.ctxt());
1526 let ident = self.mk_pattern_ident(prefix, i);
1527 let path = ident.with_span_pos(sp);
1528 (
1529 sp,
1530 struct_field.ident,
1531 cx.pat(
1532 path.span,
1533 PatKind::Ident(BindingMode(by_ref, Mutability::Not), path, None),
1534 ),
1535 )
1536 });
1537
1538 let struct_path = struct_path.clone();
1539 match *struct_def {
1540 VariantData::Struct { .. } => {
1541 let field_pats = pieces_iter
1542 .map(|(sp, ident, pat)| {
1543 if ident.is_none() {
1544 cx.dcx().span_bug(
1545 sp,
1546 "a braced struct with unnamed fields in `derive`",
1547 );
1548 }
1549 ast::PatField {
1550 ident: ident.unwrap(),
1551 is_shorthand: false,
1552 attrs: ast::AttrVec::new(),
1553 id: ast::DUMMY_NODE_ID,
1554 span: pat.span.with_ctxt(self.span.ctxt()),
1555 pat: Box::new(pat),
1556 is_placeholder: false,
1557 }
1558 })
1559 .collect();
1560 cx.pat_struct(self.span, struct_path, field_pats)
1561 }
1562 VariantData::Tuple(..) => {
1563 let subpats = pieces_iter.map(|(_, _, subpat)| subpat).collect();
1564 cx.pat_tuple_struct(self.span, struct_path, subpats)
1565 }
1566 VariantData::Unit(..) => cx.pat_path(self.span, struct_path),
1567 }
1568 })
1569 .collect()
1570 }
1571
1572 fn create_fields<F>(&self, struct_def: &'a VariantData, mk_exprs: F) -> Vec<FieldInfo>
1573 where
1574 F: Fn(usize, &ast::FieldDef, Span) -> Vec<Box<ast::Expr>>,
1575 {
1576 struct_def
1577 .fields()
1578 .iter()
1579 .enumerate()
1580 .map(|(i, struct_field)| {
1581 let sp = struct_field.span.with_ctxt(self.span.ctxt());
1584 let mut exprs: Vec<_> = mk_exprs(i, struct_field, sp);
1585 let self_expr = exprs.remove(0);
1586 let other_selflike_exprs = exprs;
1587 FieldInfo {
1588 span: sp.with_ctxt(self.span.ctxt()),
1589 name: struct_field.ident,
1590 self_expr,
1591 other_selflike_exprs,
1592 maybe_scalar: struct_field.ty.peel_refs().kind.maybe_scalar(),
1593 }
1594 })
1595 .collect()
1596 }
1597
1598 fn mk_pattern_ident(&self, prefix: &str, i: usize) -> Ident {
1599 Ident::from_str_and_span(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_{1}", prefix, i))
})format!("{prefix}_{i}"), self.span)
1600 }
1601
1602 fn create_struct_pattern_fields(
1603 &self,
1604 cx: &ExtCtxt<'_>,
1605 struct_def: &'a VariantData,
1606 prefixes: &[String],
1607 ) -> Vec<FieldInfo> {
1608 self.create_fields(struct_def, |i, _struct_field, sp| {
1609 prefixes
1610 .iter()
1611 .map(|prefix| {
1612 let ident = self.mk_pattern_ident(prefix, i);
1613 cx.expr_path(cx.path_ident(sp, ident))
1614 })
1615 .collect()
1616 })
1617 }
1618
1619 fn create_struct_field_access_fields(
1620 &self,
1621 cx: &ExtCtxt<'_>,
1622 selflike_args: &[Box<Expr>],
1623 struct_def: &'a VariantData,
1624 is_packed: bool,
1625 ) -> Vec<FieldInfo> {
1626 self.create_fields(struct_def, |i, struct_field, sp| {
1627 selflike_args
1628 .iter()
1629 .map(|selflike_arg| {
1630 let mut field_expr = cx.expr(
1635 sp,
1636 ast::ExprKind::Field(
1637 selflike_arg.clone(),
1638 struct_field.ident.unwrap_or_else(|| {
1639 Ident::from_str_and_span(&i.to_string(), struct_field.span)
1640 }),
1641 ),
1642 );
1643 if is_packed {
1644 field_expr = cx.expr_block(
1647 cx.block(struct_field.span, {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(cx.stmt_expr(field_expr));
vec
}thin_vec![cx.stmt_expr(field_expr)]),
1648 );
1649 }
1650 cx.expr_addr_of(sp, field_expr)
1651 })
1652 .collect()
1653 })
1654 }
1655}
1656
1657pub(crate) enum CsFold<'a> {
1661 Single(&'a FieldInfo),
1664
1665 Combine(Span, Box<Expr>, Box<Expr>),
1668
1669 Fieldless,
1671}
1672
1673pub(crate) fn cs_fold<F>(
1676 use_foldl: bool,
1677 cx: &ExtCtxt<'_>,
1678 trait_span: Span,
1679 substructure: &Substructure<'_>,
1680 mut f: F,
1681) -> Box<Expr>
1682where
1683 F: FnMut(&ExtCtxt<'_>, CsFold<'_>) -> Box<Expr>,
1684{
1685 match substructure.fields {
1686 EnumMatching(.., all_fields) | Struct(_, all_fields) => {
1687 if all_fields.is_empty() {
1688 return f(cx, CsFold::Fieldless);
1689 }
1690
1691 let (base_field, rest) = if use_foldl {
1692 all_fields.split_first().unwrap()
1693 } else {
1694 all_fields.split_last().unwrap()
1695 };
1696
1697 let base_expr = f(cx, CsFold::Single(base_field));
1698
1699 let op = |old, field: &FieldInfo| {
1700 let new = f(cx, CsFold::Single(field));
1701 f(cx, CsFold::Combine(field.span, old, new))
1702 };
1703
1704 if use_foldl {
1705 rest.iter().fold(base_expr, op)
1706 } else {
1707 rest.iter().rfold(base_expr, op)
1708 }
1709 }
1710 EnumDiscr(discr_field, match_expr) => {
1711 let discr_check_expr = f(cx, CsFold::Single(discr_field));
1712 if let Some(match_expr) = match_expr {
1713 if use_foldl {
1714 f(cx, CsFold::Combine(trait_span, discr_check_expr, match_expr.clone()))
1715 } else {
1716 f(cx, CsFold::Combine(trait_span, match_expr.clone(), discr_check_expr))
1717 }
1718 } else {
1719 discr_check_expr
1720 }
1721 }
1722 StaticEnum(..) | StaticStruct(..) => {
1723 cx.dcx().span_bug(trait_span, "static function in `derive`")
1724 }
1725 AllFieldlessEnum(..) => cx.dcx().span_bug(trait_span, "fieldless enum in `derive`"),
1726 }
1727}