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::warn,
544 sym::deny,
545 sym::forbid,
546 sym::stable,
547 sym::unstable,
548 ])
549 })
550 .cloned(),
551 );
552 push(Annotatable::Item(Box::new(ast::Item { attrs, ..(*newitem).clone() })))
553 }
554 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
555 }
556 }
557
558 fn create_derived_impl(
594 &self,
595 cx: &ExtCtxt<'_>,
596 type_ident: Ident,
597 generics: &Generics,
598 field_tys: Vec<&ast::Ty>,
599 methods: Vec<Box<ast::AssocItem>>,
600 is_packed: bool,
601 ) -> Box<ast::Item> {
602 let trait_path = self.path.to_path(cx, self.span, type_ident, generics);
603
604 let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| {
606 Box::new(ast::AssocItem {
607 id: ast::DUMMY_NODE_ID,
608 span: self.span,
609 vis: ast::Visibility {
610 span: self.span.shrink_to_lo(),
611 kind: ast::VisibilityKind::Inherited,
612 tokens: None,
613 },
614 attrs: ast::AttrVec::new(),
615 kind: ast::AssocItemKind::Type(Box::new(ast::TyAlias {
616 defaultness: ast::Defaultness::Implicit,
617 ident,
618 generics: Generics::default(),
619 after_where_clause: ast::WhereClause::default(),
620 bounds: Vec::new(),
621 ty: Some(type_def.to_ty(cx, self.span, type_ident, generics)),
622 })),
623 tokens: None,
624 })
625 });
626
627 let mut where_clause = ast::WhereClause::default();
628 where_clause.span = generics.where_clause.span;
629 let ctxt = self.span.ctxt();
630 let span = generics.span.with_ctxt(ctxt);
631
632 let params: ThinVec<_> = generics
634 .params
635 .iter()
636 .map(|param| match ¶m.kind {
637 GenericParamKind::Lifetime { .. } => param.clone(),
638 GenericParamKind::Type { .. } => {
639 let span = param.ident.span.with_ctxt(ctxt);
642 let bounds: Vec<_> = self
643 .additional_bounds
644 .iter()
645 .map(|p| {
646 cx.trait_bound(p.to_path(cx, span, type_ident, generics), self.is_const)
647 })
648 .chain(
649 self.skip_path_as_bound.not().then(|| {
651 let mut trait_path = trait_path.clone();
652 trait_path.span = span;
653 cx.trait_bound(trait_path, self.is_const)
654 }),
655 )
656 .chain({
657 if is_packed && self.needs_copy_as_bound_if_packed {
659 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);
660 Some(cx.trait_bound(
661 p.to_path(cx, span, type_ident, generics),
662 self.is_const,
663 ))
664 } else {
665 None
666 }
667 })
668 .chain(
669 param.bounds.iter().cloned(),
671 )
672 .collect();
673
674 cx.typaram(span, param.ident, bounds, None)
675 }
676 GenericParamKind::Const { ty, span, .. } => {
677 let const_nodefault_kind = GenericParamKind::Const {
678 ty: ty.clone(),
679 span: span.with_ctxt(ctxt),
680
681 default: None,
683 };
684 let mut param_clone = param.clone();
685 param_clone.kind = const_nodefault_kind;
686 param_clone
687 }
688 })
689 .map(|mut param| {
690 param.attrs.clear();
693 param
694 })
695 .collect();
696
697 where_clause.predicates.extend(generics.where_clause.predicates.iter().map(|clause| {
699 ast::WherePredicate {
700 attrs: clause.attrs.clone(),
701 kind: clause.kind.clone(),
702 id: ast::DUMMY_NODE_ID,
703 span: clause.span.with_ctxt(ctxt),
704 is_placeholder: false,
705 }
706 }));
707
708 let ty_param_names: Vec<Symbol> = params
709 .iter()
710 .filter(|param| #[allow(non_exhaustive_omitted_patterns)] match param.kind {
ast::GenericParamKind::Type { .. } => true,
_ => false,
}matches!(param.kind, ast::GenericParamKind::Type { .. }))
711 .map(|ty_param| ty_param.ident.name)
712 .collect();
713
714 if !ty_param_names.is_empty() {
715 for field_ty in field_tys {
716 let field_ty_params = find_type_parameters(&field_ty, &ty_param_names, cx);
717
718 for field_ty_param in field_ty_params {
719 if let ast::TyKind::Path(_, p) = &field_ty_param.ty.kind
721 && let [sole_segment] = &*p.segments
722 && ty_param_names.contains(&sole_segment.ident.name)
723 {
724 continue;
725 }
726 let mut bounds: Vec<_> = self
727 .additional_bounds
728 .iter()
729 .map(|p| {
730 cx.trait_bound(
731 p.to_path(cx, self.span, type_ident, generics),
732 self.is_const,
733 )
734 })
735 .collect();
736
737 if !self.skip_path_as_bound {
739 bounds.push(cx.trait_bound(trait_path.clone(), self.is_const));
740 }
741
742 if is_packed && self.needs_copy_as_bound_if_packed {
744 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);
745 bounds.push(cx.trait_bound(
746 p.to_path(cx, self.span, type_ident, generics),
747 self.is_const,
748 ));
749 }
750
751 if !bounds.is_empty() {
752 let predicate = ast::WhereBoundPredicate {
753 bound_generic_params: field_ty_param.bound_generic_params,
754 bounded_ty: field_ty_param.ty,
755 bounds,
756 };
757
758 let kind = ast::WherePredicateKind::BoundPredicate(predicate);
759 let predicate = ast::WherePredicate {
760 attrs: ThinVec::new(),
761 kind,
762 id: ast::DUMMY_NODE_ID,
763 span: self.span,
764 is_placeholder: false,
765 };
766 where_clause.predicates.push(predicate);
767 }
768 }
769 }
770 }
771
772 let trait_generics = Generics { params, where_clause, span };
773
774 let trait_ref = cx.trait_ref(trait_path);
776
777 let self_params: Vec<_> = generics
778 .params
779 .iter()
780 .map(|param| match param.kind {
781 GenericParamKind::Lifetime { .. } => {
782 GenericArg::Lifetime(cx.lifetime(param.ident.span.with_ctxt(ctxt), param.ident))
783 }
784 GenericParamKind::Type { .. } => {
785 GenericArg::Type(cx.ty_ident(param.ident.span.with_ctxt(ctxt), param.ident))
786 }
787 GenericParamKind::Const { .. } => {
788 GenericArg::Const(cx.const_ident(param.ident.span.with_ctxt(ctxt), param.ident))
789 }
790 })
791 .collect();
792
793 let path =
795 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);
796 let self_type = cx.ty_path(path);
797 let rustc_const_unstable =
798 cx.path_ident(self.span, Ident::new(sym::rustc_const_unstable, self.span));
799
800 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),];
801
802 if self.is_const && self.is_staged_api_crate {
806 attrs.push(
807 cx.attr_nested(
808 rustc_ast::AttrItem {
809 unsafety: Safety::Default,
810 path: rustc_const_unstable,
811 args: rustc_ast::ast::AttrItemKind::Unparsed(AttrArgs::Delimited(
812 DelimArgs {
813 dspan: DelimSpan::from_single(self.span),
814 delim: rustc_ast::token::Delimiter::Parenthesis,
815 tokens: [
816 TokenKind::Ident(sym::feature, IdentIsRaw::No),
817 TokenKind::Eq,
818 TokenKind::lit(LitKind::Str, sym::derive_const, None),
819 TokenKind::Comma,
820 TokenKind::Ident(sym::issue, IdentIsRaw::No),
821 TokenKind::Eq,
822 TokenKind::lit(LitKind::Str, sym::derive_const_issue, None),
823 ]
824 .into_iter()
825 .map(|kind| {
826 TokenTree::Token(
827 Token { kind, span: self.span },
828 Spacing::Alone,
829 )
830 })
831 .collect(),
832 },
833 )),
834 tokens: None,
835 },
836 self.span,
837 ),
838 )
839 }
840
841 if !self.document {
842 attrs.push(cx.attr_nested_word(sym::doc, sym::hidden, self.span));
843 }
844
845 cx.item(
846 self.span,
847 attrs,
848 ast::ItemKind::Impl(ast::Impl {
849 generics: trait_generics,
850 of_trait: Some(Box::new(ast::TraitImplHeader {
851 safety: self.safety,
852 polarity: ast::ImplPolarity::Positive,
853 defaultness: ast::Defaultness::Implicit,
854 trait_ref,
855 })),
856 constness: if self.is_const { ast::Const::Yes(DUMMY_SP) } else { ast::Const::No },
857 self_ty: self_type,
858 items: methods.into_iter().chain(associated_types).collect(),
859 }),
860 )
861 }
862
863 fn expand_struct_def(
864 &self,
865 cx: &ExtCtxt<'_>,
866 struct_def: &'a VariantData,
867 type_ident: Ident,
868 generics: &Generics,
869 from_scratch: bool,
870 is_packed: bool,
871 ) -> Box<ast::Item> {
872 let field_tys = Vec::from_iter(struct_def.fields().iter().map(|field| &*field.ty));
873
874 let methods = self
875 .methods
876 .iter()
877 .map(|method_def| {
878 let (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) =
879 method_def.extract_arg_details(cx, self, type_ident, generics);
880
881 let body = if from_scratch || method_def.is_static() {
882 method_def.expand_static_struct_method_body(
883 cx,
884 self,
885 struct_def,
886 type_ident,
887 &nonselflike_args,
888 )
889 } else {
890 method_def.expand_struct_method_body(
891 cx,
892 self,
893 struct_def,
894 type_ident,
895 &selflike_args,
896 &nonselflike_args,
897 is_packed,
898 )
899 };
900
901 method_def.create_method(
902 cx,
903 self,
904 type_ident,
905 generics,
906 explicit_self,
907 nonself_arg_tys,
908 body,
909 )
910 })
911 .collect();
912
913 self.create_derived_impl(cx, type_ident, generics, field_tys, methods, is_packed)
914 }
915
916 fn expand_enum_def(
917 &self,
918 cx: &ExtCtxt<'_>,
919 enum_def: &'a EnumDef,
920 type_ident: Ident,
921 generics: &Generics,
922 from_scratch: bool,
923 ) -> Box<ast::Item> {
924 let field_tys = Vec::from_iter(
925 enum_def
926 .variants
927 .iter()
928 .flat_map(|variant| variant.data.fields())
929 .map(|field| &*field.ty),
930 );
931
932 let methods = self
933 .methods
934 .iter()
935 .map(|method_def| {
936 let (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) =
937 method_def.extract_arg_details(cx, self, type_ident, generics);
938
939 let body = if from_scratch || method_def.is_static() {
940 method_def.expand_static_enum_method_body(
941 cx,
942 self,
943 enum_def,
944 type_ident,
945 &nonselflike_args,
946 )
947 } else {
948 method_def.expand_enum_method_body(
949 cx,
950 self,
951 enum_def,
952 type_ident,
953 selflike_args,
954 &nonselflike_args,
955 )
956 };
957
958 method_def.create_method(
959 cx,
960 self,
961 type_ident,
962 generics,
963 explicit_self,
964 nonself_arg_tys,
965 body,
966 )
967 })
968 .collect();
969
970 let is_packed = false; self.create_derived_impl(cx, type_ident, generics, field_tys, methods, is_packed)
972 }
973}
974
975impl<'a> MethodDef<'a> {
976 fn call_substructure_method(
977 &self,
978 cx: &ExtCtxt<'_>,
979 trait_: &TraitDef<'_>,
980 type_ident: Ident,
981 nonselflike_args: &[Box<Expr>],
982 fields: &SubstructureFields<'_>,
983 ) -> BlockOrExpr {
984 let span = trait_.span;
985 let substructure = Substructure { type_ident, nonselflike_args, fields };
986 let mut f = self.combine_substructure.borrow_mut();
987 let f: &mut CombineSubstructureFunc<'_> = &mut *f;
988 f(cx, span, &substructure)
989 }
990
991 fn is_static(&self) -> bool {
992 !self.explicit_self
993 }
994
995 fn extract_arg_details(
1003 &self,
1004 cx: &ExtCtxt<'_>,
1005 trait_: &TraitDef<'_>,
1006 type_ident: Ident,
1007 generics: &Generics,
1008 ) -> (Option<ast::ExplicitSelf>, ThinVec<Box<Expr>>, Vec<Box<Expr>>, Vec<(Ident, Box<ast::Ty>)>)
1009 {
1010 let mut selflike_args = ThinVec::new();
1011 let mut nonselflike_args = Vec::new();
1012 let mut nonself_arg_tys = Vec::new();
1013 let span = trait_.span;
1014
1015 let explicit_self = self.explicit_self.then(|| {
1016 let (self_expr, explicit_self) = ty::get_explicit_self(cx, span);
1017 selflike_args.push(self_expr);
1018 explicit_self
1019 });
1020
1021 for (ty, name) in self.nonself_args.iter() {
1022 let ast_ty = ty.to_ty(cx, span, type_ident, generics);
1023 let ident = Ident::new(*name, span);
1024 nonself_arg_tys.push((ident, ast_ty));
1025
1026 let arg_expr = cx.expr_ident(span, ident);
1027
1028 match ty {
1029 Ref(box Self_, _) if !self.is_static() => selflike_args.push(arg_expr),
1031 Self_ => cx.dcx().span_bug(span, "`Self` in non-return position"),
1032 _ => nonselflike_args.push(arg_expr),
1033 }
1034 }
1035
1036 (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys)
1037 }
1038
1039 fn create_method(
1040 &self,
1041 cx: &ExtCtxt<'_>,
1042 trait_: &TraitDef<'_>,
1043 type_ident: Ident,
1044 generics: &Generics,
1045 explicit_self: Option<ast::ExplicitSelf>,
1046 nonself_arg_tys: Vec<(Ident, Box<ast::Ty>)>,
1047 body: BlockOrExpr,
1048 ) -> Box<ast::AssocItem> {
1049 let span = trait_.span;
1050 let fn_generics = self.generics.to_generics(cx, span, type_ident, generics);
1052
1053 let args = {
1054 let self_arg = explicit_self.map(|explicit_self| {
1055 let ident = Ident::with_dummy_span(kw::SelfLower).with_span_pos(span);
1056 ast::Param::from_self(ast::AttrVec::default(), explicit_self, ident)
1057 });
1058 let nonself_args =
1059 nonself_arg_tys.into_iter().map(|(name, ty)| cx.param(span, name, ty));
1060 self_arg.into_iter().chain(nonself_args).collect()
1061 };
1062
1063 let ret_type = if let Ty::Unit = &self.ret_ty {
1064 ast::FnRetTy::Default(span)
1065 } else {
1066 ast::FnRetTy::Ty(self.ret_ty.to_ty(cx, span, type_ident, generics))
1067 };
1068
1069 let method_ident = Ident::new(self.name, span);
1070 let fn_decl = cx.fn_decl(args, ret_type);
1071 let body_block = body.into_block(cx, span);
1072
1073 let trait_lo_sp = span.shrink_to_lo();
1074
1075 let sig = ast::FnSig { header: ast::FnHeader::default(), decl: fn_decl, span };
1076 let defaultness = ast::Defaultness::Implicit;
1077
1078 Box::new(ast::AssocItem {
1080 id: ast::DUMMY_NODE_ID,
1081 attrs: self.attributes.clone(),
1082 span,
1083 vis: ast::Visibility {
1084 span: trait_lo_sp,
1085 kind: ast::VisibilityKind::Inherited,
1086 tokens: None,
1087 },
1088 kind: ast::AssocItemKind::Fn(Box::new(ast::Fn {
1089 defaultness,
1090 sig,
1091 ident: method_ident,
1092 generics: fn_generics,
1093 contract: None,
1094 body: Some(body_block),
1095 define_opaque: None,
1096 eii_impls: ThinVec::new(),
1097 })),
1098 tokens: None,
1099 })
1100 }
1101
1102 fn expand_struct_method_body<'b>(
1138 &self,
1139 cx: &ExtCtxt<'_>,
1140 trait_: &TraitDef<'b>,
1141 struct_def: &'b VariantData,
1142 type_ident: Ident,
1143 selflike_args: &[Box<Expr>],
1144 nonselflike_args: &[Box<Expr>],
1145 is_packed: bool,
1146 ) -> BlockOrExpr {
1147 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);
1148
1149 let selflike_fields =
1150 trait_.create_struct_field_access_fields(cx, selflike_args, struct_def, is_packed);
1151 self.call_substructure_method(
1152 cx,
1153 trait_,
1154 type_ident,
1155 nonselflike_args,
1156 &Struct(struct_def, selflike_fields),
1157 )
1158 }
1159
1160 fn expand_static_struct_method_body(
1161 &self,
1162 cx: &ExtCtxt<'_>,
1163 trait_: &TraitDef<'a>,
1164 struct_def: &'a VariantData,
1165 type_ident: Ident,
1166 nonselflike_args: &[Box<Expr>],
1167 ) -> BlockOrExpr {
1168 let summary = trait_.summarise_struct(cx, struct_def);
1169
1170 self.call_substructure_method(
1171 cx,
1172 trait_,
1173 type_ident,
1174 nonselflike_args,
1175 &StaticStruct(struct_def, summary),
1176 )
1177 }
1178
1179 fn expand_enum_method_body<'b>(
1215 &self,
1216 cx: &ExtCtxt<'_>,
1217 trait_: &TraitDef<'b>,
1218 enum_def: &'b EnumDef,
1219 type_ident: Ident,
1220 mut selflike_args: ThinVec<Box<Expr>>,
1221 nonselflike_args: &[Box<Expr>],
1222 ) -> BlockOrExpr {
1223 if !!selflike_args.is_empty() {
{
::core::panicking::panic_fmt(format_args!("static methods must use `expand_static_enum_method_body`"));
}
};assert!(
1224 !selflike_args.is_empty(),
1225 "static methods must use `expand_static_enum_method_body`",
1226 );
1227
1228 let span = trait_.span;
1229 let variants = &enum_def.variants;
1230
1231 let unify_fieldless_variants =
1233 self.fieldless_variants_strategy == FieldlessVariantsStrategy::Unify;
1234
1235 if variants.is_empty() {
1239 selflike_args.truncate(1);
1240 let match_arg = cx.expr_deref(span, selflike_args.pop().unwrap());
1241 let match_arms = ThinVec::new();
1242 let expr = cx.expr_match(span, match_arg, match_arms);
1243 return BlockOrExpr(ThinVec::new(), Some(expr));
1244 }
1245
1246 let prefixes = iter::once("__self".to_string())
1247 .chain(
1248 selflike_args
1249 .iter()
1250 .enumerate()
1251 .skip(1)
1252 .map(|(arg_count, _selflike_arg)| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("__arg{0}", arg_count))
})format!("__arg{arg_count}")),
1253 )
1254 .collect::<Vec<String>>();
1255
1256 let get_discr_pieces = |cx: &ExtCtxt<'_>| {
1265 let discr_idents: Vec<_> = prefixes
1266 .iter()
1267 .map(|name| Ident::from_str_and_span(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_discr", name))
})format!("{name}_discr"), span))
1268 .collect();
1269
1270 let mut discr_exprs: Vec<_> = discr_idents
1271 .iter()
1272 .map(|&ident| cx.expr_addr_of(span, cx.expr_ident(span, ident)))
1273 .collect();
1274
1275 let self_expr = discr_exprs.remove(0);
1276 let other_selflike_exprs = discr_exprs;
1277 let discr_field =
1278 FieldInfo { span, name: None, self_expr, other_selflike_exprs, maybe_scalar: true };
1279
1280 let discr_let_stmts: ThinVec<_> = iter::zip(&discr_idents, &selflike_args)
1281 .map(|(&ident, selflike_arg)| {
1282 let variant_value = deriving::call_intrinsic(
1283 cx,
1284 span,
1285 sym::discriminant_value,
1286 {
let len = [()].len();
let mut vec = ::thin_vec::ThinVec::with_capacity(len);
vec.push(selflike_arg.clone());
vec
}thin_vec![selflike_arg.clone()],
1287 );
1288 cx.stmt_let(span, false, ident, variant_value)
1289 })
1290 .collect();
1291
1292 (discr_field, discr_let_stmts)
1293 };
1294
1295 let all_fieldless = variants.iter().all(|v| v.data.fields().is_empty());
1298 if all_fieldless {
1299 if variants.len() > 1 {
1300 match self.fieldless_variants_strategy {
1301 FieldlessVariantsStrategy::Unify => {
1302 let (discr_field, mut discr_let_stmts) = get_discr_pieces(cx);
1306 let mut discr_check = self.call_substructure_method(
1307 cx,
1308 trait_,
1309 type_ident,
1310 nonselflike_args,
1311 &EnumDiscr(discr_field, None),
1312 );
1313 discr_let_stmts.append(&mut discr_check.0);
1314 return BlockOrExpr(discr_let_stmts, discr_check.1);
1315 }
1316 FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless => {
1317 return self.call_substructure_method(
1318 cx,
1319 trait_,
1320 type_ident,
1321 nonselflike_args,
1322 &AllFieldlessEnum(enum_def),
1323 );
1324 }
1325 FieldlessVariantsStrategy::Default => (),
1326 }
1327 } else if let [variant] = variants.as_slice() {
1328 return self.call_substructure_method(
1331 cx,
1332 trait_,
1333 type_ident,
1334 nonselflike_args,
1335 &EnumMatching(variant, Vec::new()),
1336 );
1337 }
1338 }
1339
1340 let mut match_arms: ThinVec<ast::Arm> = variants
1346 .iter()
1347 .filter(|&v| !(unify_fieldless_variants && v.data.fields().is_empty()))
1348 .map(|variant| {
1349 let fields = trait_.create_struct_pattern_fields(cx, &variant.data, &prefixes);
1353
1354 let sp = variant.span.with_ctxt(trait_.span.ctxt());
1355 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]);
1356 let by_ref = ByRef::No; let mut subpats = trait_.create_struct_patterns(
1358 cx,
1359 variant_path,
1360 &variant.data,
1361 &prefixes,
1362 by_ref,
1363 );
1364
1365 let single_pat = if subpats.len() == 1 {
1367 subpats.pop().unwrap()
1368 } else {
1369 cx.pat_tuple(span, subpats)
1370 };
1371
1372 let substructure = EnumMatching(variant, fields);
1381 let arm_expr = self
1382 .call_substructure_method(
1383 cx,
1384 trait_,
1385 type_ident,
1386 nonselflike_args,
1387 &substructure,
1388 )
1389 .into_expr(cx, span);
1390
1391 cx.arm(span, single_pat, arm_expr)
1392 })
1393 .collect();
1394
1395 let first_fieldless = variants.iter().find(|v| v.data.fields().is_empty());
1397 let default = match first_fieldless {
1398 Some(v) if unify_fieldless_variants => {
1399 Some(
1403 self.call_substructure_method(
1404 cx,
1405 trait_,
1406 type_ident,
1407 nonselflike_args,
1408 &EnumMatching(v, Vec::new()),
1409 )
1410 .into_expr(cx, span),
1411 )
1412 }
1413 _ if variants.len() > 1 && selflike_args.len() > 1 => {
1414 Some(deriving::call_unreachable(cx, span))
1418 }
1419 _ => None,
1420 };
1421 if let Some(arm) = default {
1422 match_arms.push(cx.arm(span, cx.pat_wild(span), arm));
1423 }
1424
1425 let get_match_expr = |mut selflike_args: ThinVec<Box<Expr>>| {
1434 let match_arg = if selflike_args.len() == 1 {
1435 selflike_args.pop().unwrap()
1436 } else {
1437 cx.expr(span, ast::ExprKind::Tup(selflike_args))
1438 };
1439 cx.expr_match(span, match_arg, match_arms)
1440 };
1441
1442 if unify_fieldless_variants && variants.len() > 1 {
1446 let (discr_field, mut discr_let_stmts) = get_discr_pieces(cx);
1447
1448 let mut discr_check_plus_match = self.call_substructure_method(
1450 cx,
1451 trait_,
1452 type_ident,
1453 nonselflike_args,
1454 &EnumDiscr(discr_field, Some(get_match_expr(selflike_args))),
1455 );
1456 discr_let_stmts.append(&mut discr_check_plus_match.0);
1457 BlockOrExpr(discr_let_stmts, discr_check_plus_match.1)
1458 } else {
1459 BlockOrExpr(ThinVec::new(), Some(get_match_expr(selflike_args)))
1460 }
1461 }
1462
1463 fn expand_static_enum_method_body(
1464 &self,
1465 cx: &ExtCtxt<'_>,
1466 trait_: &TraitDef<'_>,
1467 enum_def: &EnumDef,
1468 type_ident: Ident,
1469 nonselflike_args: &[Box<Expr>],
1470 ) -> BlockOrExpr {
1471 self.call_substructure_method(
1472 cx,
1473 trait_,
1474 type_ident,
1475 nonselflike_args,
1476 &StaticEnum(enum_def),
1477 )
1478 }
1479}
1480
1481impl<'a> TraitDef<'a> {
1483 fn summarise_struct(&self, cx: &ExtCtxt<'_>, struct_def: &'a VariantData) -> StaticFields<'a> {
1484 let mut named_idents = Vec::new();
1485 let mut just_spans = Vec::new();
1486 for field in struct_def.fields() {
1487 let sp = field.span.with_ctxt(self.span.ctxt());
1488 match field.ident {
1489 Some(ident) => named_idents.push((ident, sp, field.default.as_ref())),
1490 _ => just_spans.push(sp),
1491 }
1492 }
1493
1494 let is_tuple = match struct_def {
1495 ast::VariantData::Tuple(..) => IsTuple::Yes,
1496 _ => IsTuple::No,
1497 };
1498 match (just_spans.is_empty(), named_idents.is_empty()) {
1499 (false, false) => cx
1500 .dcx()
1501 .span_bug(self.span, "a struct with named and unnamed fields in generic `derive`"),
1502 (_, false) => Named(named_idents),
1504 (false, _) => Unnamed(just_spans, is_tuple),
1506 _ => Named(Vec::new()),
1508 }
1509 }
1510
1511 fn create_struct_patterns(
1512 &self,
1513 cx: &ExtCtxt<'_>,
1514 struct_path: ast::Path,
1515 struct_def: &'a VariantData,
1516 prefixes: &[String],
1517 by_ref: ByRef,
1518 ) -> ThinVec<ast::Pat> {
1519 prefixes
1520 .iter()
1521 .map(|prefix| {
1522 let pieces_iter =
1523 struct_def.fields().iter().enumerate().map(|(i, struct_field)| {
1524 let sp = struct_field.span.with_ctxt(self.span.ctxt());
1525 let ident = self.mk_pattern_ident(prefix, i);
1526 let path = ident.with_span_pos(sp);
1527 (
1528 sp,
1529 struct_field.ident,
1530 cx.pat(
1531 path.span,
1532 PatKind::Ident(BindingMode(by_ref, Mutability::Not), path, None),
1533 ),
1534 )
1535 });
1536
1537 let struct_path = struct_path.clone();
1538 match *struct_def {
1539 VariantData::Struct { .. } => {
1540 let field_pats = pieces_iter
1541 .map(|(sp, ident, pat)| {
1542 if ident.is_none() {
1543 cx.dcx().span_bug(
1544 sp,
1545 "a braced struct with unnamed fields in `derive`",
1546 );
1547 }
1548 ast::PatField {
1549 ident: ident.unwrap(),
1550 is_shorthand: false,
1551 attrs: ast::AttrVec::new(),
1552 id: ast::DUMMY_NODE_ID,
1553 span: pat.span.with_ctxt(self.span.ctxt()),
1554 pat: Box::new(pat),
1555 is_placeholder: false,
1556 }
1557 })
1558 .collect();
1559 cx.pat_struct(self.span, struct_path, field_pats)
1560 }
1561 VariantData::Tuple(..) => {
1562 let subpats = pieces_iter.map(|(_, _, subpat)| subpat).collect();
1563 cx.pat_tuple_struct(self.span, struct_path, subpats)
1564 }
1565 VariantData::Unit(..) => cx.pat_path(self.span, struct_path),
1566 }
1567 })
1568 .collect()
1569 }
1570
1571 fn create_fields<F>(&self, struct_def: &'a VariantData, mk_exprs: F) -> Vec<FieldInfo>
1572 where
1573 F: Fn(usize, &ast::FieldDef, Span) -> Vec<Box<ast::Expr>>,
1574 {
1575 struct_def
1576 .fields()
1577 .iter()
1578 .enumerate()
1579 .map(|(i, struct_field)| {
1580 let sp = struct_field.span.with_ctxt(self.span.ctxt());
1583 let mut exprs: Vec<_> = mk_exprs(i, struct_field, sp);
1584 let self_expr = exprs.remove(0);
1585 let other_selflike_exprs = exprs;
1586 FieldInfo {
1587 span: sp.with_ctxt(self.span.ctxt()),
1588 name: struct_field.ident,
1589 self_expr,
1590 other_selflike_exprs,
1591 maybe_scalar: struct_field.ty.peel_refs().kind.maybe_scalar(),
1592 }
1593 })
1594 .collect()
1595 }
1596
1597 fn mk_pattern_ident(&self, prefix: &str, i: usize) -> Ident {
1598 Ident::from_str_and_span(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}_{1}", prefix, i))
})format!("{prefix}_{i}"), self.span)
1599 }
1600
1601 fn create_struct_pattern_fields(
1602 &self,
1603 cx: &ExtCtxt<'_>,
1604 struct_def: &'a VariantData,
1605 prefixes: &[String],
1606 ) -> Vec<FieldInfo> {
1607 self.create_fields(struct_def, |i, _struct_field, sp| {
1608 prefixes
1609 .iter()
1610 .map(|prefix| {
1611 let ident = self.mk_pattern_ident(prefix, i);
1612 cx.expr_path(cx.path_ident(sp, ident))
1613 })
1614 .collect()
1615 })
1616 }
1617
1618 fn create_struct_field_access_fields(
1619 &self,
1620 cx: &ExtCtxt<'_>,
1621 selflike_args: &[Box<Expr>],
1622 struct_def: &'a VariantData,
1623 is_packed: bool,
1624 ) -> Vec<FieldInfo> {
1625 self.create_fields(struct_def, |i, struct_field, sp| {
1626 selflike_args
1627 .iter()
1628 .map(|selflike_arg| {
1629 let mut field_expr = cx.expr(
1634 sp,
1635 ast::ExprKind::Field(
1636 selflike_arg.clone(),
1637 struct_field.ident.unwrap_or_else(|| {
1638 Ident::from_str_and_span(&i.to_string(), struct_field.span)
1639 }),
1640 ),
1641 );
1642 if is_packed {
1643 field_expr = cx.expr_block(
1646 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)]),
1647 );
1648 }
1649 cx.expr_addr_of(sp, field_expr)
1650 })
1651 .collect()
1652 })
1653 }
1654}
1655
1656pub(crate) enum CsFold<'a> {
1660 Single(&'a FieldInfo),
1663
1664 Combine(Span, Box<Expr>, Box<Expr>),
1667
1668 Fieldless,
1670}
1671
1672pub(crate) fn cs_fold<F>(
1675 use_foldl: bool,
1676 cx: &ExtCtxt<'_>,
1677 trait_span: Span,
1678 substructure: &Substructure<'_>,
1679 mut f: F,
1680) -> Box<Expr>
1681where
1682 F: FnMut(&ExtCtxt<'_>, CsFold<'_>) -> Box<Expr>,
1683{
1684 match substructure.fields {
1685 EnumMatching(.., all_fields) | Struct(_, all_fields) => {
1686 if all_fields.is_empty() {
1687 return f(cx, CsFold::Fieldless);
1688 }
1689
1690 let (base_field, rest) = if use_foldl {
1691 all_fields.split_first().unwrap()
1692 } else {
1693 all_fields.split_last().unwrap()
1694 };
1695
1696 let base_expr = f(cx, CsFold::Single(base_field));
1697
1698 let op = |old, field: &FieldInfo| {
1699 let new = f(cx, CsFold::Single(field));
1700 f(cx, CsFold::Combine(field.span, old, new))
1701 };
1702
1703 if use_foldl {
1704 rest.iter().fold(base_expr, op)
1705 } else {
1706 rest.iter().rfold(base_expr, op)
1707 }
1708 }
1709 EnumDiscr(discr_field, match_expr) => {
1710 let discr_check_expr = f(cx, CsFold::Single(discr_field));
1711 if let Some(match_expr) = match_expr {
1712 if use_foldl {
1713 f(cx, CsFold::Combine(trait_span, discr_check_expr, match_expr.clone()))
1714 } else {
1715 f(cx, CsFold::Combine(trait_span, match_expr.clone(), discr_check_expr))
1716 }
1717 } else {
1718 discr_check_expr
1719 }
1720 }
1721 StaticEnum(..) | StaticStruct(..) => {
1722 cx.dcx().span_bug(trait_span, "static function in `derive`")
1723 }
1724 AllFieldlessEnum(..) => cx.dcx().span_bug(trait_span, "fieldless enum in `derive`"),
1725 }
1726}