From 72a16210613f0b25dc3f5608e491b94c18001f6d Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Tue, 28 Jun 2022 12:58:36 +1000
Subject: [PATCH 01/12] Use `split_{first,last}` in `cs_fold1`.

It makes the code a little nicer to read.
---
 .../src/deriving/generic/mod.rs                 | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
index 8347cded2fe0b..e6b21b3ff944e 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
@@ -1717,22 +1717,21 @@ where
 {
     match *substructure.fields {
         EnumMatching(.., ref all_fields) | Struct(_, ref all_fields) => {
-            let (base, all_fields) = match (all_fields.is_empty(), use_foldl) {
+            let (base, rest) = match (all_fields.is_empty(), use_foldl) {
                 (false, true) => {
-                    let field = &all_fields[0];
-                    let args = (field.span, field.self_.clone(), &field.other[..]);
-                    (b(cx, Some(args)), &all_fields[1..])
+                    let (first, rest) = all_fields.split_first().unwrap();
+                    let args = (first.span, first.self_.clone(), &first.other[..]);
+                    (b(cx, Some(args)), rest)
                 }
                 (false, false) => {
-                    let idx = all_fields.len() - 1;
-                    let field = &all_fields[idx];
-                    let args = (field.span, field.self_.clone(), &field.other[..]);
-                    (b(cx, Some(args)), &all_fields[..idx])
+                    let (last, rest) = all_fields.split_last().unwrap();
+                    let args = (last.span, last.self_.clone(), &last.other[..]);
+                    (b(cx, Some(args)), rest)
                 }
                 (true, _) => (b(cx, None), &all_fields[..]),
             };
 
-            cs_fold_fields(use_foldl, f, base, cx, all_fields)
+            cs_fold_fields(use_foldl, f, base, cx, rest)
         }
         EnumNonMatchingCollapsed(..) => {
             cs_fold_enumnonmatch(enum_nonmatch_f, cx, trait_span, substructure)

From 7a4fdcbbc50a177a766fceb3bd2b9d35b2b5678e Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Tue, 28 Jun 2022 08:56:54 +1000
Subject: [PATCH 02/12] Remove unnecessary fields from
 `EnumNonMatchingCollapsed`.

The `&[ast::Variant]` field isn't used.

The `Vec<Ident>` field is only used for its length, but that's always
the same as the length of the `&[Ident]` and so isn't necessary.
---
 .../src/deriving/cmp/ord.rs                   |  4 +--
 .../src/deriving/cmp/partial_eq.rs            |  2 +-
 .../src/deriving/cmp/partial_ord.rs           |  4 +--
 .../src/deriving/generic/mod.rs               | 34 +++++--------------
 4 files changed, 14 insertions(+), 30 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
index 2b3ac0a86c16c..5823d6fc40ba9 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
@@ -99,8 +99,8 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<
             cx.expr_match(span, new, vec![eq_arm, neq_arm])
         },
         cx.expr_path(equals_path.clone()),
-        Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
-            if self_args.len() != 2 {
+        Box::new(|cx, span, tag_tuple| {
+            if tag_tuple.len() != 2 {
                 cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`")
             } else {
                 ordering_collapsed(cx, span, tag_tuple)
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
index eead8b37024c0..a5b2e6b114fb0 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
@@ -48,7 +48,7 @@ pub fn expand_deriving_partial_eq(
                     None => cx.expr_bool(span, base),
                 }
             },
-            Box::new(|cx, span, _, _| cx.expr_bool(span, !base)),
+            Box::new(|cx, span, _| cx.expr_bool(span, !base)),
             cx,
             span,
             substr,
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
index d28ac822a1ed9..124e16197f2bf 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
@@ -102,8 +102,8 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
             cx.expr_match(span, new, vec![eq_arm, neq_arm])
         },
         equals_expr,
-        Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
-            if self_args.len() != 2 {
+        Box::new(|cx, span, tag_tuple| {
+            if tag_tuple.len() != 2 {
                 cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
             } else {
                 let lft = cx.expr_addr_of(span, cx.expr_ident(span, tag_tuple[0]));
diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
index e6b21b3ff944e..82eb064edb0b7 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
@@ -146,8 +146,6 @@
 //!
 //! ```{.text}
 //! EnumNonMatchingCollapsed(
-//!     vec![<ident of self>, <ident of __arg_1>],
-//!     &[<ast::Variant for C0>, <ast::Variant for C1>],
 //!     &[<ident for self index value>, <ident of __arg_1 index value>])
 //! ```
 //!
@@ -299,13 +297,10 @@ pub enum SubstructureFields<'a> {
     /// variant.
     EnumMatching(usize, usize, &'a ast::Variant, Vec<FieldInfo<'a>>),
 
-    /// Non-matching variants of the enum, but with all state hidden from
-    /// the consequent code. The first component holds `Ident`s for all of
-    /// the `Self` arguments; the second component is a slice of all of the
-    /// variants for the enum itself, and the third component is a list of
-    /// `Ident`s bound to the variant index values for each of the actual
-    /// input `Self` arguments.
-    EnumNonMatchingCollapsed(Vec<Ident>, &'a [ast::Variant], &'a [Ident]),
+    /// Non-matching variants of the enum, but with all state hidden from the
+    /// consequent code. The field is a list of `Ident`s bound to the variant
+    /// index values for each of the actual input `Self` arguments.
+    EnumNonMatchingCollapsed(&'a [Ident]),
 
     /// A static method where `Self` is a struct.
     StaticStruct(&'a ast::VariantData, StaticFields),
@@ -318,13 +313,10 @@ pub enum SubstructureFields<'a> {
 pub type CombineSubstructureFunc<'a> =
     Box<dyn FnMut(&mut ExtCtxt<'_>, Span, &Substructure<'_>) -> P<Expr> + 'a>;
 
-/// Deal with non-matching enum variants. The tuple is a list of
-/// identifiers (one for each `Self` argument, which could be any of the
-/// variants since they have been collapsed together) and the identifiers
-/// holding the variant index value for each of the `Self` arguments. The
-/// last argument is all the non-`Self` args of the method being derived.
+/// Deal with non-matching enum variants. The slice is the identifiers holding
+/// the variant index value for each of the `Self` arguments.
 pub type EnumNonMatchCollapsedFunc<'a> =
-    Box<dyn FnMut(&mut ExtCtxt<'_>, Span, (&[Ident], &[Ident]), &[P<Expr>]) -> P<Expr> + 'a>;
+    Box<dyn FnMut(&mut ExtCtxt<'_>, Span, &[Ident]) -> P<Expr> + 'a>;
 
 pub fn combine_substructure(
     f: CombineSubstructureFunc<'_>,
@@ -1184,11 +1176,6 @@ impl<'a> MethodDef<'a> {
             )
             .collect::<Vec<String>>();
 
-        let self_arg_idents = self_arg_names
-            .iter()
-            .map(|name| Ident::from_str_and_span(name, span))
-            .collect::<Vec<Ident>>();
-
         // The `vi_idents` will be bound, solely in the catch-all, to
         // a series of let statements mapping each self_arg to an int
         // value corresponding to its discriminant.
@@ -1203,8 +1190,7 @@ impl<'a> MethodDef<'a> {
         // Builds, via callback to call_substructure_method, the
         // delegated expression that handles the catch-all case,
         // using `__variants_tuple` to drive logic if necessary.
-        let catch_all_substructure =
-            EnumNonMatchingCollapsed(self_arg_idents, &variants, &vi_idents);
+        let catch_all_substructure = EnumNonMatchingCollapsed(&vi_idents);
 
         let first_fieldless = variants.iter().find(|v| v.data.fields().is_empty());
 
@@ -1657,9 +1643,7 @@ pub fn cs_fold_enumnonmatch(
     substructure: &Substructure<'_>,
 ) -> P<Expr> {
     match *substructure.fields {
-        EnumNonMatchingCollapsed(ref all_args, _, tuple) => {
-            enum_nonmatch_f(cx, trait_span, (&all_args[..], tuple), substructure.nonself_args)
-        }
+        EnumNonMatchingCollapsed(tuple) => enum_nonmatch_f(cx, trait_span, tuple),
         _ => cx.span_bug(trait_span, "cs_fold_enumnonmatch expected an EnumNonMatchingCollapsed"),
     }
 }

From d13fa0d21bd3080ae180e244274bde055d311e1d Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Tue, 28 Jun 2022 11:37:44 +1000
Subject: [PATCH 03/12] Remove `Substructure::method_ident`.

It's unused.
---
 .../rustc_builtin_macros/src/deriving/generic/mod.rs   | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
index 82eb064edb0b7..e84b9fe999722 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
@@ -253,8 +253,6 @@ pub struct MethodDef<'a> {
 pub struct Substructure<'a> {
     /// ident of self
     pub type_ident: Ident,
-    /// ident of the method
-    pub method_ident: Ident,
     /// dereferenced access to any [`Self_`] or [`Ptr(Self_, _)`][ptr] arguments
     ///
     /// [`Self_`]: ty::Ty::Self_
@@ -845,13 +843,7 @@ impl<'a> MethodDef<'a> {
         fields: &SubstructureFields<'_>,
     ) -> P<Expr> {
         let span = trait_.span;
-        let substructure = Substructure {
-            type_ident,
-            method_ident: Ident::new(self.name, span),
-            self_args,
-            nonself_args,
-            fields,
-        };
+        let substructure = Substructure { type_ident, self_args, nonself_args, fields };
         let mut f = self.combine_substructure.borrow_mut();
         let f: &mut CombineSubstructureFunc<'_> = &mut *f;
         f(cx, span, &substructure)

From 1254fe974dd33199130a1f01647992be986b5dbb Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Tue, 28 Jun 2022 11:44:37 +1000
Subject: [PATCH 04/12] Remove `{Method,Trait}Def::is_unsafe`.

They are always `false`.
---
 .../src/deriving/bounds.rs                     |  1 -
 .../rustc_builtin_macros/src/deriving/clone.rs |  2 --
 .../src/deriving/cmp/eq.rs                     |  2 --
 .../src/deriving/cmp/ord.rs                    |  2 --
 .../src/deriving/cmp/partial_eq.rs             |  2 --
 .../src/deriving/cmp/partial_ord.rs            |  2 --
 .../rustc_builtin_macros/src/deriving/debug.rs |  2 --
 .../src/deriving/decodable.rs                  |  2 --
 .../src/deriving/default.rs                    |  2 --
 .../src/deriving/encodable.rs                  |  2 --
 .../src/deriving/generic/mod.rs                | 18 ++----------------
 .../rustc_builtin_macros/src/deriving/hash.rs  |  2 --
 12 files changed, 2 insertions(+), 37 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/bounds.rs b/compiler/rustc_builtin_macros/src/deriving/bounds.rs
index 12ef166b8b051..5ef68c6aeaa59 100644
--- a/compiler/rustc_builtin_macros/src/deriving/bounds.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/bounds.rs
@@ -19,7 +19,6 @@ pub fn expand_deriving_copy(
         path: path_std!(marker::Copy),
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: true,
         methods: Vec::new(),
         associated_types: Vec::new(),
diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs
index 8331710699063..be88ad5e5a961 100644
--- a/compiler/rustc_builtin_macros/src/deriving/clone.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs
@@ -81,7 +81,6 @@ pub fn expand_deriving_clone(
         path: path_std!(clone::Clone),
         additional_bounds: bounds,
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: true,
         methods: vec![MethodDef {
             name: sym::clone,
@@ -90,7 +89,6 @@ pub fn expand_deriving_clone(
             args: Vec::new(),
             ret_ty: Self_,
             attributes: attrs,
-            is_unsafe: false,
             unify_fieldless_variants: false,
             combine_substructure: substructure,
         }],
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
index f54ded3c87cab..63665165f6759 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
@@ -27,7 +27,6 @@ pub fn expand_deriving_eq(
         path: path_std!(cmp::Eq),
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: true,
         methods: vec![MethodDef {
             name: sym::assert_receiver_is_total_eq,
@@ -36,7 +35,6 @@ pub fn expand_deriving_eq(
             args: vec![],
             ret_ty: nil_ty(),
             attributes: attrs,
-            is_unsafe: false,
             unify_fieldless_variants: true,
             combine_substructure: combine_substructure(Box::new(|a, b, c| {
                 cs_total_eq_assert(a, b, c)
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
index 5823d6fc40ba9..b2ca2cc929296 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
@@ -23,7 +23,6 @@ pub fn expand_deriving_ord(
         path: path_std!(cmp::Ord),
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: false,
         methods: vec![MethodDef {
             name: sym::cmp,
@@ -32,7 +31,6 @@ pub fn expand_deriving_ord(
             args: vec![(borrowed_self(), sym::other)],
             ret_ty: Literal(path_std!(cmp::Ordering)),
             attributes: attrs,
-            is_unsafe: false,
             unify_fieldless_variants: true,
             combine_substructure: combine_substructure(Box::new(|a, b, c| cs_cmp(a, b, c))),
         }],
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
index a5b2e6b114fb0..fda25c1ff03f3 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
@@ -73,7 +73,6 @@ pub fn expand_deriving_partial_eq(
                 args: vec![(borrowed_self(), sym::other)],
                 ret_ty: Literal(path_local!(bool)),
                 attributes: attrs,
-                is_unsafe: false,
                 unify_fieldless_variants: true,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| $f(a, b, c))),
             }
@@ -102,7 +101,6 @@ pub fn expand_deriving_partial_eq(
         path: path_std!(cmp::PartialEq),
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: false,
         methods,
         associated_types: Vec::new(),
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
index 124e16197f2bf..fb98218a002e4 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
@@ -33,7 +33,6 @@ pub fn expand_deriving_partial_ord(
         args: vec![(borrowed_self(), sym::other)],
         ret_ty,
         attributes: attrs,
-        is_unsafe: false,
         unify_fieldless_variants: true,
         combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
             cs_partial_cmp(cx, span, substr)
@@ -46,7 +45,6 @@ pub fn expand_deriving_partial_ord(
         path: path_std!(cmp::PartialOrd),
         additional_bounds: vec![],
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: false,
         methods: vec![partial_cmp_def],
         associated_types: Vec::new(),
diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs
index 1fffd6f9727d7..66676bcf28b7c 100644
--- a/compiler/rustc_builtin_macros/src/deriving/debug.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs
@@ -25,7 +25,6 @@ pub fn expand_deriving_debug(
         path: path_std!(fmt::Debug),
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: false,
         methods: vec![MethodDef {
             name: sym::fmt,
@@ -34,7 +33,6 @@ pub fn expand_deriving_debug(
             args: vec![(fmtr, sym::f)],
             ret_ty: Literal(path_std!(fmt::Result)),
             attributes: Vec::new(),
-            is_unsafe: false,
             unify_fieldless_variants: false,
             combine_substructure: combine_substructure(Box::new(|a, b, c| {
                 show_substructure(a, b, c)
diff --git a/compiler/rustc_builtin_macros/src/deriving/decodable.rs b/compiler/rustc_builtin_macros/src/deriving/decodable.rs
index b39f35a9d4056..d335131a5f16a 100644
--- a/compiler/rustc_builtin_macros/src/deriving/decodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/decodable.rs
@@ -26,7 +26,6 @@ pub fn expand_deriving_rustc_decodable(
         path: Path::new_(vec![krate, sym::Decodable], None, vec![], PathKind::Global),
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: false,
         methods: vec![MethodDef {
             name: sym::decode,
@@ -56,7 +55,6 @@ pub fn expand_deriving_rustc_decodable(
                 PathKind::Std,
             )),
             attributes: Vec::new(),
-            is_unsafe: false,
             unify_fieldless_variants: false,
             combine_substructure: combine_substructure(Box::new(|a, b, c| {
                 decodable_substructure(a, b, c, krate)
diff --git a/compiler/rustc_builtin_macros/src/deriving/default.rs b/compiler/rustc_builtin_macros/src/deriving/default.rs
index b49331e28753d..7ff7a5c405060 100644
--- a/compiler/rustc_builtin_macros/src/deriving/default.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/default.rs
@@ -30,7 +30,6 @@ pub fn expand_deriving_default(
         path: Path::new(vec![kw::Default, sym::Default]),
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: false,
         methods: vec![MethodDef {
             name: kw::Default,
@@ -39,7 +38,6 @@ pub fn expand_deriving_default(
             args: Vec::new(),
             ret_ty: Self_,
             attributes: attrs,
-            is_unsafe: false,
             unify_fieldless_variants: false,
             combine_substructure: combine_substructure(Box::new(|cx, trait_span, substr| {
                 match substr.fields {
diff --git a/compiler/rustc_builtin_macros/src/deriving/encodable.rs b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
index 6151a80a56d58..01f4a2c05c59c 100644
--- a/compiler/rustc_builtin_macros/src/deriving/encodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
@@ -111,7 +111,6 @@ pub fn expand_deriving_rustc_encodable(
         path: Path::new_(vec![krate, sym::Encodable], None, vec![], PathKind::Global),
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: false,
         methods: vec![MethodDef {
             name: sym::encode,
@@ -141,7 +140,6 @@ pub fn expand_deriving_rustc_encodable(
                 PathKind::Std,
             )),
             attributes: Vec::new(),
-            is_unsafe: false,
             unify_fieldless_variants: false,
             combine_substructure: combine_substructure(Box::new(|a, b, c| {
                 encodable_substructure(a, b, c, krate)
diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
index e84b9fe999722..2a0386b47395a 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
@@ -210,9 +210,6 @@ pub struct TraitDef<'a> {
     /// Any extra lifetimes and/or bounds, e.g., `D: serialize::Decoder`
     pub generics: Bounds,
 
-    /// Is it an `unsafe` trait?
-    pub is_unsafe: bool,
-
     /// Can this trait be derived for unions?
     pub supports_unions: bool,
 
@@ -240,9 +237,6 @@ pub struct MethodDef<'a> {
 
     pub attributes: Vec<ast::Attribute>,
 
-    // Is it an `unsafe fn`?
-    pub is_unsafe: bool,
-
     /// Can we combine fieldless variants for enums into a single match arm?
     pub unify_fieldless_variants: bool,
 
@@ -717,14 +711,12 @@ impl<'a> TraitDef<'a> {
         let mut a = vec![attr, unused_qual];
         a.extend(self.attributes.iter().cloned());
 
-        let unsafety = if self.is_unsafe { ast::Unsafe::Yes(self.span) } else { ast::Unsafe::No };
-
         cx.item(
             self.span,
             Ident::empty(),
             a,
             ast::ItemKind::Impl(Box::new(ast::Impl {
-                unsafety,
+                unsafety: ast::Unsafe::No,
                 polarity: ast::ImplPolarity::Positive,
                 defaultness: ast::Defaultness::Final,
                 constness: ast::Const::No,
@@ -939,15 +931,9 @@ impl<'a> MethodDef<'a> {
         let fn_decl = cx.fn_decl(args, ast::FnRetTy::Ty(ret_type));
         let body_block = cx.block_expr(body);
 
-        let unsafety = if self.is_unsafe { ast::Unsafe::Yes(span) } else { ast::Unsafe::No };
-
         let trait_lo_sp = span.shrink_to_lo();
 
-        let sig = ast::FnSig {
-            header: ast::FnHeader { unsafety, ext: ast::Extern::None, ..ast::FnHeader::default() },
-            decl: fn_decl,
-            span,
-        };
+        let sig = ast::FnSig { header: ast::FnHeader::default(), decl: fn_decl, span };
         let defaultness = ast::Defaultness::Final;
 
         // Create the method.
diff --git a/compiler/rustc_builtin_macros/src/deriving/hash.rs b/compiler/rustc_builtin_macros/src/deriving/hash.rs
index f1d46f03bad8f..da88e01a30afb 100644
--- a/compiler/rustc_builtin_macros/src/deriving/hash.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/hash.rs
@@ -26,7 +26,6 @@ pub fn expand_deriving_hash(
         path,
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
-        is_unsafe: false,
         supports_unions: false,
         methods: vec![MethodDef {
             name: sym::hash,
@@ -35,7 +34,6 @@ pub fn expand_deriving_hash(
             args: vec![(Ptr(Box::new(Literal(arg)), Borrowed(None, Mutability::Mut)), sym::state)],
             ret_ty: nil_ty(),
             attributes: vec![],
-            is_unsafe: false,
             unify_fieldless_variants: true,
             combine_substructure: combine_substructure(Box::new(|a, b, c| {
                 hash_substructure(a, b, c)

From 89f6917a49df12f16cbbf0479143d6d5bc3e8ae2 Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Tue, 28 Jun 2022 11:56:03 +1000
Subject: [PATCH 05/12] Remove `Substructure::self_args`.

It's unused.
---
 .../src/deriving/generic/mod.rs               | 20 ++-----------------
 1 file changed, 2 insertions(+), 18 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
index 2a0386b47395a..0b2f62cacc2d4 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
@@ -247,12 +247,7 @@ pub struct MethodDef<'a> {
 pub struct Substructure<'a> {
     /// ident of self
     pub type_ident: Ident,
-    /// dereferenced access to any [`Self_`] or [`Ptr(Self_, _)`][ptr] arguments
-    ///
-    /// [`Self_`]: ty::Ty::Self_
-    /// [ptr]: ty::Ty::Ptr
-    pub self_args: &'a [P<Expr>],
-    /// verbatim access to any other arguments
+    /// verbatim access to any non-self arguments
     pub nonself_args: &'a [P<Expr>],
     pub fields: &'a SubstructureFields<'a>,
 }
@@ -753,7 +748,6 @@ impl<'a> TraitDef<'a> {
                         self,
                         struct_def,
                         type_ident,
-                        &self_args,
                         &nonself_args,
                     )
                 } else {
@@ -802,7 +796,6 @@ impl<'a> TraitDef<'a> {
                         self,
                         enum_def,
                         type_ident,
-                        &self_args,
                         &nonself_args,
                     )
                 } else {
@@ -830,12 +823,11 @@ impl<'a> MethodDef<'a> {
         cx: &mut ExtCtxt<'_>,
         trait_: &TraitDef<'_>,
         type_ident: Ident,
-        self_args: &[P<Expr>],
         nonself_args: &[P<Expr>],
         fields: &SubstructureFields<'_>,
     ) -> P<Expr> {
         let span = trait_.span;
-        let substructure = Substructure { type_ident, self_args, nonself_args, fields };
+        let substructure = Substructure { type_ident, nonself_args, fields };
         let mut f = self.combine_substructure.borrow_mut();
         let f: &mut CombineSubstructureFunc<'_> = &mut *f;
         f(cx, span, &substructure)
@@ -1053,7 +1045,6 @@ impl<'a> MethodDef<'a> {
             cx,
             trait_,
             type_ident,
-            self_args,
             nonself_args,
             &Struct(struct_def, fields),
         );
@@ -1074,7 +1065,6 @@ impl<'a> MethodDef<'a> {
         trait_: &TraitDef<'_>,
         struct_def: &VariantData,
         type_ident: Ident,
-        self_args: &[P<Expr>],
         nonself_args: &[P<Expr>],
     ) -> P<Expr> {
         let summary = trait_.summarise_struct(cx, struct_def);
@@ -1083,7 +1073,6 @@ impl<'a> MethodDef<'a> {
             cx,
             trait_,
             type_ident,
-            self_args,
             nonself_args,
             &StaticStruct(struct_def, summary),
         )
@@ -1267,7 +1256,6 @@ impl<'a> MethodDef<'a> {
                     cx,
                     trait_,
                     type_ident,
-                    &self_args[..],
                     nonself_args,
                     &substructure,
                 );
@@ -1286,7 +1274,6 @@ impl<'a> MethodDef<'a> {
                     cx,
                     trait_,
                     type_ident,
-                    &self_args[..],
                     nonself_args,
                     &substructure,
                 ))
@@ -1357,7 +1344,6 @@ impl<'a> MethodDef<'a> {
                 cx,
                 trait_,
                 type_ident,
-                &self_args[..],
                 nonself_args,
                 &catch_all_substructure,
             );
@@ -1455,7 +1441,6 @@ impl<'a> MethodDef<'a> {
         trait_: &TraitDef<'_>,
         enum_def: &EnumDef,
         type_ident: Ident,
-        self_args: &[P<Expr>],
         nonself_args: &[P<Expr>],
     ) -> P<Expr> {
         let summary = enum_def
@@ -1471,7 +1456,6 @@ impl<'a> MethodDef<'a> {
             cx,
             trait_,
             type_ident,
-            self_args,
             nonself_args,
             &StaticEnum(enum_def, summary),
         )

From 57d56891d226836d2fa88e03dcf07a4fd829ebfc Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Tue, 28 Jun 2022 12:16:12 +1000
Subject: [PATCH 06/12] Remove some unnecessary `pub`s.

---
 compiler/rustc_builtin_macros/src/deriving/generic/mod.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
index 0b2f62cacc2d4..3a7295a76a3ca 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
@@ -1576,7 +1576,7 @@ impl<'a> TraitDef<'a> {
 
 // helpful premade recipes
 
-pub fn cs_fold_fields<'a, F>(
+fn cs_fold_fields<'a, F>(
     use_foldl: bool,
     mut f: F,
     base: P<Expr>,
@@ -1598,7 +1598,7 @@ where
     }
 }
 
-pub fn cs_fold_enumnonmatch(
+fn cs_fold_enumnonmatch(
     mut enum_nonmatch_f: EnumNonMatchCollapsedFunc<'_>,
     cx: &mut ExtCtxt<'_>,
     trait_span: Span,
@@ -1610,7 +1610,7 @@ pub fn cs_fold_enumnonmatch(
     }
 }
 
-pub fn cs_fold_static(cx: &mut ExtCtxt<'_>, trait_span: Span) -> P<Expr> {
+fn cs_fold_static(cx: &mut ExtCtxt<'_>, trait_span: Span) -> P<Expr> {
     cx.span_bug(trait_span, "static function in `derive`")
 }
 

From 623ebbe42a72bd9942bb62526a8a0b2981448a53 Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Tue, 28 Jun 2022 15:09:37 +1000
Subject: [PATCH 07/12] Remove some commented-out code.

This was accidentally left behind in a previous commit.
---
 compiler/rustc_builtin_macros/src/deriving/debug.rs | 2 --
 1 file changed, 2 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs
index 66676bcf28b7c..3a0b8c9117916 100644
--- a/compiler/rustc_builtin_macros/src/deriving/debug.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs
@@ -62,8 +62,6 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
     let (is_struct, args_per_field) = match vdata {
         ast::VariantData::Unit(..) => {
             // Special fast path for unit variants.
-            //let fn_path_write_str = cx.std_path(&[sym::fmt, sym::Formatter, sym::write_str]);
-            //return cx.expr_call_global(span, fn_path_write_str, vec![fmt, name]);
             assert!(fields.is_empty());
             (false, 0)
         }

From 78ec19ffe63e793d5241a729e23969623b85d9df Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Tue, 28 Jun 2022 15:23:16 +1000
Subject: [PATCH 08/12] `expand_deriving_clone` tweaks.

Improve a comment, and panic on an impossible code path.
---
 .../src/deriving/clone.rs                     | 50 ++++++++-----------
 1 file changed, 22 insertions(+), 28 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs
index be88ad5e5a961..d713b7e4a73f3 100644
--- a/compiler/rustc_builtin_macros/src/deriving/clone.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs
@@ -15,23 +15,22 @@ pub fn expand_deriving_clone(
     item: &Annotatable,
     push: &mut dyn FnMut(Annotatable),
 ) {
-    // check if we can use a short form
+    // The simple form is `fn clone(&self) -> Self { *self }`, possibly with
+    // some additional `AssertParamIsClone` assertions.
     //
-    // the short form is `fn clone(&self) -> Self { *self }`
-    //
-    // we can use the short form if:
-    // - the item is Copy (unfortunately, all we can check is whether it's also deriving Copy)
-    // - there are no generic parameters (after specialization this limitation can be removed)
-    //      if we used the short form with generics, we'd have to bound the generics with
-    //      Clone + Copy, and then there'd be no Clone impl at all if the user fills in something
-    //      that is Clone but not Copy. and until specialization we can't write both impls.
-    // - the item is a union with Copy fields
-    //      Unions with generic parameters still can derive Clone because they require Copy
-    //      for deriving, Clone alone is not enough.
-    //      Wherever Clone is implemented for fields is irrelevant so we don't assert it.
+    // We can use the simple form if either of the following are true.
+    // - The type derives Copy and there are no generic parameters.  (If we
+    //   used the simple form with generics, we'd have to bound the generics
+    //   with Clone + Copy, and then there'd be no Clone impl at all if the
+    //   user fills in something that is Clone but not Copy. After
+    //   specialization we can remove this no-generics limitation.)
+    // - The item is a union. (Unions with generic parameters still can derive
+    //   Clone because they require Copy for deriving, Clone alone is not
+    //   enough. Whether Clone is implemented for fields is irrelevant so we
+    //   don't assert it.)
     let bounds;
     let substructure;
-    let is_shallow;
+    let is_simple;
     match *item {
         Annotatable::Item(ref annitem) => match annitem.kind {
             ItemKind::Struct(_, Generics { ref params, .. })
@@ -44,30 +43,25 @@ pub fn expand_deriving_clone(
                         .any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. }))
                 {
                     bounds = vec![];
-                    is_shallow = true;
+                    is_simple = true;
                     substructure = combine_substructure(Box::new(|c, s, sub| {
-                        cs_clone_shallow("Clone", c, s, sub, false)
+                        cs_clone_simple("Clone", c, s, sub, false)
                     }));
                 } else {
                     bounds = vec![];
-                    is_shallow = false;
+                    is_simple = false;
                     substructure =
                         combine_substructure(Box::new(|c, s, sub| cs_clone("Clone", c, s, sub)));
                 }
             }
             ItemKind::Union(..) => {
                 bounds = vec![Literal(path_std!(marker::Copy))];
-                is_shallow = true;
+                is_simple = true;
                 substructure = combine_substructure(Box::new(|c, s, sub| {
-                    cs_clone_shallow("Clone", c, s, sub, true)
+                    cs_clone_simple("Clone", c, s, sub, true)
                 }));
             }
-            _ => {
-                bounds = vec![];
-                is_shallow = false;
-                substructure =
-                    combine_substructure(Box::new(|c, s, sub| cs_clone("Clone", c, s, sub)));
-            }
+            _ => cx.span_bug(span, "`#[derive(Clone)]` on wrong item kind"),
         },
 
         _ => cx.span_bug(span, "`#[derive(Clone)]` on trait item or impl item"),
@@ -95,10 +89,10 @@ pub fn expand_deriving_clone(
         associated_types: Vec::new(),
     };
 
-    trait_def.expand_ext(cx, mitem, item, push, is_shallow)
+    trait_def.expand_ext(cx, mitem, item, push, is_simple)
 }
 
-fn cs_clone_shallow(
+fn cs_clone_simple(
     name: &str,
     cx: &mut ExtCtxt<'_>,
     trait_span: Span,
@@ -141,7 +135,7 @@ fn cs_clone_shallow(
             }
             _ => cx.span_bug(
                 trait_span,
-                &format!("unexpected substructure in shallow `derive({})`", name),
+                &format!("unexpected substructure in simple `derive({})`", name),
             ),
         }
     }

From b94246693ac3ad95ee942ef77aed8e762474ddf7 Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Thu, 30 Jun 2022 09:15:07 +1000
Subject: [PATCH 09/12] Simplify pointer handling.

The existing derive code allows for various possibilities that aren't
needed in practice, which complicates the code. There are only a few
auto-derived traits and new ones are unlikely, so this commit simplifies
things.

- `PtrTy` has been eliminated. The `Raw` variant was never used, and the
  lifetime for the `Borrowed` variant was always `None`. That left just
  the mutability field, which has been inlined as necessary.
- `MethodDef::explicit_self` was a confusing `Option<Option<PtrTy>>`.
  Indicating either `&self` or nothing. It's now a `bool`.
- `borrowed_self` is renamed as `self_ref`.
- `Ty::Ptr` is renamed to `Ty::Ref`.
---
 .../src/deriving/clone.rs                     |  2 +-
 .../src/deriving/cmp/eq.rs                    |  2 +-
 .../src/deriving/cmp/ord.rs                   |  4 +-
 .../src/deriving/cmp/partial_eq.rs            |  4 +-
 .../src/deriving/cmp/partial_ord.rs           |  4 +-
 .../src/deriving/debug.rs                     |  5 +-
 .../src/deriving/decodable.rs                 |  7 +-
 .../src/deriving/default.rs                   |  2 +-
 .../src/deriving/encodable.rs                 |  7 +-
 .../src/deriving/generic/mod.rs               | 28 +++----
 .../src/deriving/generic/ty.rs                | 74 ++++---------------
 .../rustc_builtin_macros/src/deriving/hash.rs |  4 +-
 12 files changed, 43 insertions(+), 100 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs
index d713b7e4a73f3..05d1fdbd51336 100644
--- a/compiler/rustc_builtin_macros/src/deriving/clone.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs
@@ -79,7 +79,7 @@ pub fn expand_deriving_clone(
         methods: vec![MethodDef {
             name: sym::clone,
             generics: Bounds::empty(),
-            explicit_self: borrowed_explicit_self(),
+            explicit_self: true,
             args: Vec::new(),
             ret_ty: Self_,
             attributes: attrs,
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
index 63665165f6759..caf4d3a5f772e 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
@@ -31,7 +31,7 @@ pub fn expand_deriving_eq(
         methods: vec![MethodDef {
             name: sym::assert_receiver_is_total_eq,
             generics: Bounds::empty(),
-            explicit_self: borrowed_explicit_self(),
+            explicit_self: true,
             args: vec![],
             ret_ty: nil_ty(),
             attributes: attrs,
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
index b2ca2cc929296..ae1bfea20d892 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
@@ -27,8 +27,8 @@ pub fn expand_deriving_ord(
         methods: vec![MethodDef {
             name: sym::cmp,
             generics: Bounds::empty(),
-            explicit_self: borrowed_explicit_self(),
-            args: vec![(borrowed_self(), sym::other)],
+            explicit_self: true,
+            args: vec![(self_ref(), sym::other)],
             ret_ty: Literal(path_std!(cmp::Ordering)),
             attributes: attrs,
             unify_fieldless_variants: true,
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
index fda25c1ff03f3..f086e7c16fd9c 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
@@ -69,8 +69,8 @@ pub fn expand_deriving_partial_eq(
             MethodDef {
                 name: $name,
                 generics: Bounds::empty(),
-                explicit_self: borrowed_explicit_self(),
-                args: vec![(borrowed_self(), sym::other)],
+                explicit_self: true,
+                args: vec![(self_ref(), sym::other)],
                 ret_ty: Literal(path_local!(bool)),
                 attributes: attrs,
                 unify_fieldless_variants: true,
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
index fb98218a002e4..3c674f318afc9 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
@@ -29,8 +29,8 @@ pub fn expand_deriving_partial_ord(
     let partial_cmp_def = MethodDef {
         name: sym::partial_cmp,
         generics: Bounds::empty(),
-        explicit_self: borrowed_explicit_self(),
-        args: vec![(borrowed_self(), sym::other)],
+        explicit_self: true,
+        args: vec![(self_ref(), sym::other)],
         ret_ty,
         attributes: attrs,
         unify_fieldless_variants: true,
diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs
index 3a0b8c9117916..8da624c4b826c 100644
--- a/compiler/rustc_builtin_macros/src/deriving/debug.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs
@@ -16,8 +16,7 @@ pub fn expand_deriving_debug(
     push: &mut dyn FnMut(Annotatable),
 ) {
     // &mut ::std::fmt::Formatter
-    let fmtr =
-        Ptr(Box::new(Literal(path_std!(fmt::Formatter))), Borrowed(None, ast::Mutability::Mut));
+    let fmtr = Ref(Box::new(Literal(path_std!(fmt::Formatter))), ast::Mutability::Mut);
 
     let trait_def = TraitDef {
         span,
@@ -29,7 +28,7 @@ pub fn expand_deriving_debug(
         methods: vec![MethodDef {
             name: sym::fmt,
             generics: Bounds::empty(),
-            explicit_self: borrowed_explicit_self(),
+            explicit_self: true,
             args: vec![(fmtr, sym::f)],
             ret_ty: Literal(path_std!(fmt::Result)),
             attributes: Vec::new(),
diff --git a/compiler/rustc_builtin_macros/src/deriving/decodable.rs b/compiler/rustc_builtin_macros/src/deriving/decodable.rs
index d335131a5f16a..d649ae7c25e26 100644
--- a/compiler/rustc_builtin_macros/src/deriving/decodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/decodable.rs
@@ -35,11 +35,8 @@ pub fn expand_deriving_rustc_decodable(
                     vec![Path::new_(vec![krate, sym::Decoder], None, vec![], PathKind::Global)],
                 )],
             },
-            explicit_self: None,
-            args: vec![(
-                Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mut)),
-                sym::d,
-            )],
+            explicit_self: false,
+            args: vec![(Ref(Box::new(Literal(Path::new_local(typaram))), Mutability::Mut), sym::d)],
             ret_ty: Literal(Path::new_(
                 pathvec_std!(result::Result),
                 None,
diff --git a/compiler/rustc_builtin_macros/src/deriving/default.rs b/compiler/rustc_builtin_macros/src/deriving/default.rs
index 7ff7a5c405060..d41b25343b08e 100644
--- a/compiler/rustc_builtin_macros/src/deriving/default.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/default.rs
@@ -34,7 +34,7 @@ pub fn expand_deriving_default(
         methods: vec![MethodDef {
             name: kw::Default,
             generics: Bounds::empty(),
-            explicit_self: None,
+            explicit_self: false,
             args: Vec::new(),
             ret_ty: Self_,
             attributes: attrs,
diff --git a/compiler/rustc_builtin_macros/src/deriving/encodable.rs b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
index 01f4a2c05c59c..f8bc2f5bb8562 100644
--- a/compiler/rustc_builtin_macros/src/deriving/encodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
@@ -120,11 +120,8 @@ pub fn expand_deriving_rustc_encodable(
                     vec![Path::new_(vec![krate, sym::Encoder], None, vec![], PathKind::Global)],
                 )],
             },
-            explicit_self: borrowed_explicit_self(),
-            args: vec![(
-                Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mut)),
-                sym::s,
-            )],
+            explicit_self: true,
+            args: vec![(Ref(Box::new(Literal(Path::new_local(typaram))), Mutability::Mut), sym::s)],
             ret_ty: Literal(Path::new_(
                 pathvec_std!(result::Result),
                 None,
diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
index 3a7295a76a3ca..ff431c8de5d3f 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
@@ -188,7 +188,7 @@ use rustc_expand::base::{Annotatable, ExtCtxt};
 use rustc_span::symbol::{kw, sym, Ident, Symbol};
 use rustc_span::Span;
 
-use ty::{Bounds, Path, Ptr, PtrTy, Self_, Ty};
+use ty::{Bounds, Path, Ref, Self_, Ty};
 
 use crate::deriving;
 
@@ -224,10 +224,8 @@ pub struct MethodDef<'a> {
     /// List of generics, e.g., `R: rand::Rng`
     pub generics: Bounds,
 
-    /// Whether there is a self argument (outer Option) i.e., whether
-    /// this is a static function, and whether it is a pointer (inner
-    /// Option)
-    pub explicit_self: Option<Option<PtrTy>>,
+    /// Is there is a `&self` argument? If not, it is a static function.
+    pub explicit_self: bool,
 
     /// Arguments other than the self argument
     pub args: Vec<(Ty, Symbol)>,
@@ -844,7 +842,7 @@ impl<'a> MethodDef<'a> {
     }
 
     fn is_static(&self) -> bool {
-        self.explicit_self.is_none()
+        !self.explicit_self
     }
 
     fn split_self_nonself_args(
@@ -857,17 +855,15 @@ impl<'a> MethodDef<'a> {
         let mut self_args = Vec::new();
         let mut nonself_args = Vec::new();
         let mut arg_tys = Vec::new();
-        let mut nonstatic = false;
         let span = trait_.span;
 
-        let ast_explicit_self = self.explicit_self.as_ref().map(|self_ptr| {
-            let (self_expr, explicit_self) = ty::get_explicit_self(cx, span, self_ptr);
-
+        let ast_explicit_self = if self.explicit_self {
+            let (self_expr, explicit_self) = ty::get_explicit_self(cx, span);
             self_args.push(self_expr);
-            nonstatic = true;
-
-            explicit_self
-        });
+            Some(explicit_self)
+        } else {
+            None
+        };
 
         for (ty, name) in self.args.iter() {
             let ast_ty = ty.to_ty(cx, span, type_ident, generics);
@@ -879,10 +875,10 @@ impl<'a> MethodDef<'a> {
             match *ty {
                 // for static methods, just treat any Self
                 // arguments as a normal arg
-                Self_ if nonstatic => {
+                Self_ if !self.is_static() => {
                     self_args.push(arg_expr);
                 }
-                Ptr(ref ty, _) if matches!(**ty, Self_) && nonstatic => {
+                Ref(ref ty, _) if matches!(**ty, Self_) && !self.is_static() => {
                     self_args.push(cx.expr_deref(span, arg_expr))
                 }
                 _ => {
diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
index 7a41800325084..ed911ff0be929 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
@@ -1,7 +1,6 @@
 //! A mini version of ast::Ty, which is easier to use, and features an explicit `Self` type to use
 //! when specifying impls to be derived.
 
-pub use PtrTy::*;
 pub use Ty::*;
 
 use rustc_ast::ptr::P;
@@ -11,16 +10,6 @@ use rustc_span::source_map::{respan, DUMMY_SP};
 use rustc_span::symbol::{kw, Ident, Symbol};
 use rustc_span::Span;
 
-/// The types of pointers
-#[derive(Clone)]
-pub enum PtrTy {
-    /// &'lifetime mut
-    Borrowed(Option<Ident>, ast::Mutability),
-    /// *mut
-    #[allow(dead_code)]
-    Raw(ast::Mutability),
-}
-
 /// A path, e.g., `::std::option::Option::<i32>` (global). Has support
 /// for type parameters and a lifetime.
 #[derive(Clone)]
@@ -92,8 +81,8 @@ impl Path {
 #[derive(Clone)]
 pub enum Ty {
     Self_,
-    /// &/Box/ Ty
-    Ptr(Box<Ty>, PtrTy),
+    /// A reference.
+    Ref(Box<Ty>, ast::Mutability),
     /// `mod::mod::Type<[lifetime], [Params...]>`, including a plain type
     /// parameter, and things like `i32`
     Literal(Path),
@@ -101,19 +90,8 @@ pub enum Ty {
     Tuple(Vec<Ty>),
 }
 
-pub fn borrowed_ptrty() -> PtrTy {
-    Borrowed(None, ast::Mutability::Not)
-}
-pub fn borrowed(ty: Box<Ty>) -> Ty {
-    Ptr(ty, borrowed_ptrty())
-}
-
-pub fn borrowed_explicit_self() -> Option<Option<PtrTy>> {
-    Some(Some(borrowed_ptrty()))
-}
-
-pub fn borrowed_self() -> Ty {
-    borrowed(Box::new(Self_))
+pub fn self_ref() -> Ty {
+    Ref(Box::new(Self_), ast::Mutability::Not)
 }
 
 pub fn nil_ty() -> Ty {
@@ -136,20 +114,14 @@ impl Ty {
         self_ty: Ident,
         self_generics: &Generics,
     ) -> P<ast::Ty> {
-        match *self {
-            Ptr(ref ty, ref ptr) => {
+        match self {
+            Ref(ty, mutbl) => {
                 let raw_ty = ty.to_ty(cx, span, self_ty, self_generics);
-                match *ptr {
-                    Borrowed(ref lt, mutbl) => {
-                        let lt = mk_lifetime(cx, span, lt);
-                        cx.ty_rptr(span, raw_ty, lt, mutbl)
-                    }
-                    Raw(mutbl) => cx.ty_ptr(span, raw_ty, mutbl),
-                }
+                cx.ty_rptr(span, raw_ty, None, *mutbl)
             }
-            Literal(ref p) => p.to_ty(cx, span, self_ty, self_generics),
+            Literal(p) => p.to_ty(cx, span, self_ty, self_generics),
             Self_ => cx.ty_path(self.to_path(cx, span, self_ty, self_generics)),
-            Tuple(ref fields) => {
+            Tuple(fields) => {
                 let ty = ast::TyKind::Tup(
                     fields.iter().map(|f| f.to_ty(cx, span, self_ty, self_generics)).collect(),
                 );
@@ -186,7 +158,7 @@ impl Ty {
                 cx.path_all(span, false, vec![self_ty], params)
             }
             Literal(ref p) => p.to_path(cx, span, self_ty, generics),
-            Ptr(..) => cx.span_bug(span, "pointer in a path in generic `derive`"),
+            Ref(..) => cx.span_bug(span, "ref in a path in generic `derive`"),
             Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"),
         }
     }
@@ -245,28 +217,10 @@ impl Bounds {
     }
 }
 
-pub fn get_explicit_self(
-    cx: &ExtCtxt<'_>,
-    span: Span,
-    self_ptr: &Option<PtrTy>,
-) -> (P<Expr>, ast::ExplicitSelf) {
+pub fn get_explicit_self(cx: &ExtCtxt<'_>, span: Span) -> (P<Expr>, ast::ExplicitSelf) {
     // this constructs a fresh `self` path
     let self_path = cx.expr_self(span);
-    match *self_ptr {
-        None => (self_path, respan(span, SelfKind::Value(ast::Mutability::Not))),
-        Some(ref ptr) => {
-            let self_ty = respan(
-                span,
-                match *ptr {
-                    Borrowed(ref lt, mutbl) => {
-                        let lt = lt.map(|s| cx.lifetime(span, s));
-                        SelfKind::Region(lt, mutbl)
-                    }
-                    Raw(_) => cx.span_bug(span, "attempted to use *self in deriving definition"),
-                },
-            );
-            let self_expr = cx.expr_deref(span, self_path);
-            (self_expr, self_ty)
-        }
-    }
+    let self_ty = respan(span, SelfKind::Region(None, ast::Mutability::Not));
+    let self_expr = cx.expr_deref(span, self_path);
+    (self_expr, self_ty)
 }
diff --git a/compiler/rustc_builtin_macros/src/deriving/hash.rs b/compiler/rustc_builtin_macros/src/deriving/hash.rs
index da88e01a30afb..c01e68ad358ef 100644
--- a/compiler/rustc_builtin_macros/src/deriving/hash.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/hash.rs
@@ -30,8 +30,8 @@ pub fn expand_deriving_hash(
         methods: vec![MethodDef {
             name: sym::hash,
             generics: Bounds { bounds: vec![(typaram, vec![path_std!(hash::Hasher)])] },
-            explicit_self: borrowed_explicit_self(),
-            args: vec![(Ptr(Box::new(Literal(arg)), Borrowed(None, Mutability::Mut)), sym::state)],
+            explicit_self: true,
+            args: vec![(Ref(Box::new(Literal(arg)), Mutability::Mut), sym::state)],
             ret_ty: nil_ty(),
             attributes: vec![],
             unify_fieldless_variants: true,

From 18fef6bbd73d8d2dc721df458cb3b2a22e6b79c9 Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Thu, 30 Jun 2022 10:16:34 +1000
Subject: [PATCH 10/12] Remove lifetime support in deriving code.

It's unused.
---
 .../src/deriving/cmp/partial_ord.rs           |  1 -
 .../src/deriving/decodable.rs                 |  6 ++--
 .../src/deriving/encodable.rs                 |  6 ++--
 .../src/deriving/generic/ty.rs                | 28 ++++---------------
 .../rustc_builtin_macros/src/deriving/hash.rs |  2 +-
 5 files changed, 11 insertions(+), 32 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
index 3c674f318afc9..dfc3e8c6ef2b7 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
@@ -18,7 +18,6 @@ pub fn expand_deriving_partial_ord(
     let ordering_ty = Literal(path_std!(cmp::Ordering));
     let ret_ty = Literal(Path::new_(
         pathvec_std!(option::Option),
-        None,
         vec![Box::new(ordering_ty)],
         PathKind::Std,
     ));
diff --git a/compiler/rustc_builtin_macros/src/deriving/decodable.rs b/compiler/rustc_builtin_macros/src/deriving/decodable.rs
index d649ae7c25e26..4bd8340e099df 100644
--- a/compiler/rustc_builtin_macros/src/deriving/decodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/decodable.rs
@@ -23,7 +23,7 @@ pub fn expand_deriving_rustc_decodable(
     let trait_def = TraitDef {
         span,
         attributes: Vec::new(),
-        path: Path::new_(vec![krate, sym::Decodable], None, vec![], PathKind::Global),
+        path: Path::new_(vec![krate, sym::Decodable], vec![], PathKind::Global),
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
         supports_unions: false,
@@ -32,19 +32,17 @@ pub fn expand_deriving_rustc_decodable(
             generics: Bounds {
                 bounds: vec![(
                     typaram,
-                    vec![Path::new_(vec![krate, sym::Decoder], None, vec![], PathKind::Global)],
+                    vec![Path::new_(vec![krate, sym::Decoder], vec![], PathKind::Global)],
                 )],
             },
             explicit_self: false,
             args: vec![(Ref(Box::new(Literal(Path::new_local(typaram))), Mutability::Mut), sym::d)],
             ret_ty: Literal(Path::new_(
                 pathvec_std!(result::Result),
-                None,
                 vec![
                     Box::new(Self_),
                     Box::new(Literal(Path::new_(
                         vec![typaram, sym::Error],
-                        None,
                         vec![],
                         PathKind::Local,
                     ))),
diff --git a/compiler/rustc_builtin_macros/src/deriving/encodable.rs b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
index f8bc2f5bb8562..829e258c36b42 100644
--- a/compiler/rustc_builtin_macros/src/deriving/encodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
@@ -108,7 +108,7 @@ pub fn expand_deriving_rustc_encodable(
     let trait_def = TraitDef {
         span,
         attributes: Vec::new(),
-        path: Path::new_(vec![krate, sym::Encodable], None, vec![], PathKind::Global),
+        path: Path::new_(vec![krate, sym::Encodable], vec![], PathKind::Global),
         additional_bounds: Vec::new(),
         generics: Bounds::empty(),
         supports_unions: false,
@@ -117,19 +117,17 @@ pub fn expand_deriving_rustc_encodable(
             generics: Bounds {
                 bounds: vec![(
                     typaram,
-                    vec![Path::new_(vec![krate, sym::Encoder], None, vec![], PathKind::Global)],
+                    vec![Path::new_(vec![krate, sym::Encoder], vec![], PathKind::Global)],
                 )],
             },
             explicit_self: true,
             args: vec![(Ref(Box::new(Literal(Path::new_local(typaram))), Mutability::Mut), sym::s)],
             ret_ty: Literal(Path::new_(
                 pathvec_std!(result::Result),
-                None,
                 vec![
                     Box::new(Tuple(Vec::new())),
                     Box::new(Literal(Path::new_(
                         vec![typaram, sym::Error],
-                        None,
                         vec![],
                         PathKind::Local,
                     ))),
diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
index ed911ff0be929..6b2f31c64e14d 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
@@ -11,11 +11,10 @@ use rustc_span::symbol::{kw, Ident, Symbol};
 use rustc_span::Span;
 
 /// A path, e.g., `::std::option::Option::<i32>` (global). Has support
-/// for type parameters and a lifetime.
+/// for type parameters.
 #[derive(Clone)]
 pub struct Path {
     path: Vec<Symbol>,
-    lifetime: Option<Ident>,
     params: Vec<Box<Ty>>,
     kind: PathKind,
 }
@@ -29,18 +28,13 @@ pub enum PathKind {
 
 impl Path {
     pub fn new(path: Vec<Symbol>) -> Path {
-        Path::new_(path, None, Vec::new(), PathKind::Std)
+        Path::new_(path, Vec::new(), PathKind::Std)
     }
     pub fn new_local(path: Symbol) -> Path {
-        Path::new_(vec![path], None, Vec::new(), PathKind::Local)
+        Path::new_(vec![path], Vec::new(), PathKind::Local)
     }
-    pub fn new_(
-        path: Vec<Symbol>,
-        lifetime: Option<Ident>,
-        params: Vec<Box<Ty>>,
-        kind: PathKind,
-    ) -> Path {
-        Path { path, lifetime, params, kind }
+    pub fn new_(path: Vec<Symbol>, params: Vec<Box<Ty>>, kind: PathKind) -> Path {
+        Path { path, params, kind }
     }
 
     pub fn to_ty(
@@ -60,10 +54,8 @@ impl Path {
         self_generics: &Generics,
     ) -> ast::Path {
         let mut idents = self.path.iter().map(|s| Ident::new(*s, span)).collect();
-        let lt = mk_lifetimes(cx, span, &self.lifetime);
         let tys = self.params.iter().map(|t| t.to_ty(cx, span, self_ty, self_generics));
-        let params =
-            lt.into_iter().map(GenericArg::Lifetime).chain(tys.map(GenericArg::Type)).collect();
+        let params = tys.map(GenericArg::Type).collect();
 
         match self.kind {
             PathKind::Global => cx.path_all(span, true, idents, params),
@@ -98,14 +90,6 @@ pub fn nil_ty() -> Ty {
     Tuple(Vec::new())
 }
 
-fn mk_lifetime(cx: &ExtCtxt<'_>, span: Span, lt: &Option<Ident>) -> Option<ast::Lifetime> {
-    lt.map(|ident| cx.lifetime(span, ident))
-}
-
-fn mk_lifetimes(cx: &ExtCtxt<'_>, span: Span, lt: &Option<Ident>) -> Vec<ast::Lifetime> {
-    mk_lifetime(cx, span, lt).into_iter().collect()
-}
-
 impl Ty {
     pub fn to_ty(
         &self,
diff --git a/compiler/rustc_builtin_macros/src/deriving/hash.rs b/compiler/rustc_builtin_macros/src/deriving/hash.rs
index c01e68ad358ef..906eef32f798d 100644
--- a/compiler/rustc_builtin_macros/src/deriving/hash.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/hash.rs
@@ -15,7 +15,7 @@ pub fn expand_deriving_hash(
     item: &Annotatable,
     push: &mut dyn FnMut(Annotatable),
 ) {
-    let path = Path::new_(pathvec_std!(hash::Hash), None, vec![], PathKind::Std);
+    let path = Path::new_(pathvec_std!(hash::Hash), vec![], PathKind::Std);
 
     let typaram = sym::__H;
 

From 00307a5b6f1d81fb5d19f1ef7235ff235ce75d50 Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Thu, 30 Jun 2022 10:20:19 +1000
Subject: [PATCH 11/12] Rename `Ty::Literal` as `Ty::Path`.

Because a `Literal` is a type of expression, and is simply the wrong
name for this.
---
 compiler/rustc_builtin_macros/src/deriving/clone.rs    |  2 +-
 compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs  |  2 +-
 .../src/deriving/cmp/partial_eq.rs                     |  2 +-
 .../src/deriving/cmp/partial_ord.rs                    |  9 +++------
 compiler/rustc_builtin_macros/src/deriving/debug.rs    |  4 ++--
 .../rustc_builtin_macros/src/deriving/decodable.rs     | 10 +++-------
 .../rustc_builtin_macros/src/deriving/encodable.rs     | 10 +++-------
 .../rustc_builtin_macros/src/deriving/generic/ty.rs    |  6 +++---
 compiler/rustc_builtin_macros/src/deriving/hash.rs     |  2 +-
 9 files changed, 18 insertions(+), 29 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs
index 05d1fdbd51336..1c507678489fe 100644
--- a/compiler/rustc_builtin_macros/src/deriving/clone.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs
@@ -55,7 +55,7 @@ pub fn expand_deriving_clone(
                 }
             }
             ItemKind::Union(..) => {
-                bounds = vec![Literal(path_std!(marker::Copy))];
+                bounds = vec![Path(path_std!(marker::Copy))];
                 is_simple = true;
                 substructure = combine_substructure(Box::new(|c, s, sub| {
                     cs_clone_simple("Clone", c, s, sub, true)
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
index ae1bfea20d892..c7850cd4b4cf6 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
@@ -29,7 +29,7 @@ pub fn expand_deriving_ord(
             generics: Bounds::empty(),
             explicit_self: true,
             args: vec![(self_ref(), sym::other)],
-            ret_ty: Literal(path_std!(cmp::Ordering)),
+            ret_ty: Path(path_std!(cmp::Ordering)),
             attributes: attrs,
             unify_fieldless_variants: true,
             combine_substructure: combine_substructure(Box::new(|a, b, c| cs_cmp(a, b, c))),
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
index f086e7c16fd9c..ca5ca29eb826a 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
@@ -71,7 +71,7 @@ pub fn expand_deriving_partial_eq(
                 generics: Bounds::empty(),
                 explicit_self: true,
                 args: vec![(self_ref(), sym::other)],
-                ret_ty: Literal(path_local!(bool)),
+                ret_ty: Path(path_local!(bool)),
                 attributes: attrs,
                 unify_fieldless_variants: true,
                 combine_substructure: combine_substructure(Box::new(|a, b, c| $f(a, b, c))),
diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
index dfc3e8c6ef2b7..07db82fee935b 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
@@ -15,12 +15,9 @@ pub fn expand_deriving_partial_ord(
     item: &Annotatable,
     push: &mut dyn FnMut(Annotatable),
 ) {
-    let ordering_ty = Literal(path_std!(cmp::Ordering));
-    let ret_ty = Literal(Path::new_(
-        pathvec_std!(option::Option),
-        vec![Box::new(ordering_ty)],
-        PathKind::Std,
-    ));
+    let ordering_ty = Path(path_std!(cmp::Ordering));
+    let ret_ty =
+        Path(Path::new_(pathvec_std!(option::Option), vec![Box::new(ordering_ty)], PathKind::Std));
 
     let inline = cx.meta_word(span, sym::inline);
     let attrs = vec![cx.attribute(inline)];
diff --git a/compiler/rustc_builtin_macros/src/deriving/debug.rs b/compiler/rustc_builtin_macros/src/deriving/debug.rs
index 8da624c4b826c..1411c60c0bfd5 100644
--- a/compiler/rustc_builtin_macros/src/deriving/debug.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs
@@ -16,7 +16,7 @@ pub fn expand_deriving_debug(
     push: &mut dyn FnMut(Annotatable),
 ) {
     // &mut ::std::fmt::Formatter
-    let fmtr = Ref(Box::new(Literal(path_std!(fmt::Formatter))), ast::Mutability::Mut);
+    let fmtr = Ref(Box::new(Path(path_std!(fmt::Formatter))), ast::Mutability::Mut);
 
     let trait_def = TraitDef {
         span,
@@ -30,7 +30,7 @@ pub fn expand_deriving_debug(
             generics: Bounds::empty(),
             explicit_self: true,
             args: vec![(fmtr, sym::f)],
-            ret_ty: Literal(path_std!(fmt::Result)),
+            ret_ty: Path(path_std!(fmt::Result)),
             attributes: Vec::new(),
             unify_fieldless_variants: false,
             combine_substructure: combine_substructure(Box::new(|a, b, c| {
diff --git a/compiler/rustc_builtin_macros/src/deriving/decodable.rs b/compiler/rustc_builtin_macros/src/deriving/decodable.rs
index 4bd8340e099df..16154fb4d031b 100644
--- a/compiler/rustc_builtin_macros/src/deriving/decodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/decodable.rs
@@ -36,16 +36,12 @@ pub fn expand_deriving_rustc_decodable(
                 )],
             },
             explicit_self: false,
-            args: vec![(Ref(Box::new(Literal(Path::new_local(typaram))), Mutability::Mut), sym::d)],
-            ret_ty: Literal(Path::new_(
+            args: vec![(Ref(Box::new(Path(Path::new_local(typaram))), Mutability::Mut), sym::d)],
+            ret_ty: Path(Path::new_(
                 pathvec_std!(result::Result),
                 vec![
                     Box::new(Self_),
-                    Box::new(Literal(Path::new_(
-                        vec![typaram, sym::Error],
-                        vec![],
-                        PathKind::Local,
-                    ))),
+                    Box::new(Path(Path::new_(vec![typaram, sym::Error], vec![], PathKind::Local))),
                 ],
                 PathKind::Std,
             )),
diff --git a/compiler/rustc_builtin_macros/src/deriving/encodable.rs b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
index 829e258c36b42..7cce1a0c2a2d8 100644
--- a/compiler/rustc_builtin_macros/src/deriving/encodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
@@ -121,16 +121,12 @@ pub fn expand_deriving_rustc_encodable(
                 )],
             },
             explicit_self: true,
-            args: vec![(Ref(Box::new(Literal(Path::new_local(typaram))), Mutability::Mut), sym::s)],
-            ret_ty: Literal(Path::new_(
+            args: vec![(Ref(Box::new(Path(Path::new_local(typaram))), Mutability::Mut), sym::s)],
+            ret_ty: Path(Path::new_(
                 pathvec_std!(result::Result),
                 vec![
                     Box::new(Tuple(Vec::new())),
-                    Box::new(Literal(Path::new_(
-                        vec![typaram, sym::Error],
-                        vec![],
-                        PathKind::Local,
-                    ))),
+                    Box::new(Path(Path::new_(vec![typaram, sym::Error], vec![], PathKind::Local))),
                 ],
                 PathKind::Std,
             )),
diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
index 6b2f31c64e14d..c2dcff36c397a 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
@@ -77,7 +77,7 @@ pub enum Ty {
     Ref(Box<Ty>, ast::Mutability),
     /// `mod::mod::Type<[lifetime], [Params...]>`, including a plain type
     /// parameter, and things like `i32`
-    Literal(Path),
+    Path(Path),
     /// includes unit
     Tuple(Vec<Ty>),
 }
@@ -103,7 +103,7 @@ impl Ty {
                 let raw_ty = ty.to_ty(cx, span, self_ty, self_generics);
                 cx.ty_rptr(span, raw_ty, None, *mutbl)
             }
-            Literal(p) => p.to_ty(cx, span, self_ty, self_generics),
+            Path(p) => p.to_ty(cx, span, self_ty, self_generics),
             Self_ => cx.ty_path(self.to_path(cx, span, self_ty, self_generics)),
             Tuple(fields) => {
                 let ty = ast::TyKind::Tup(
@@ -141,7 +141,7 @@ impl Ty {
 
                 cx.path_all(span, false, vec![self_ty], params)
             }
-            Literal(ref p) => p.to_path(cx, span, self_ty, generics),
+            Path(ref p) => p.to_path(cx, span, self_ty, generics),
             Ref(..) => cx.span_bug(span, "ref in a path in generic `derive`"),
             Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"),
         }
diff --git a/compiler/rustc_builtin_macros/src/deriving/hash.rs b/compiler/rustc_builtin_macros/src/deriving/hash.rs
index 906eef32f798d..641744c47ce43 100644
--- a/compiler/rustc_builtin_macros/src/deriving/hash.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/hash.rs
@@ -31,7 +31,7 @@ pub fn expand_deriving_hash(
             name: sym::hash,
             generics: Bounds { bounds: vec![(typaram, vec![path_std!(hash::Hasher)])] },
             explicit_self: true,
-            args: vec![(Ref(Box::new(Literal(arg)), Mutability::Mut), sym::state)],
+            args: vec![(Ref(Box::new(Path(arg)), Mutability::Mut), sym::state)],
             ret_ty: nil_ty(),
             attributes: vec![],
             unify_fieldless_variants: true,

From 85e8d94e05959e3ad57738351ce56f95f15a26a6 Mon Sep 17 00:00:00 2001
From: Nicholas Nethercote <n.nethercote@gmail.com>
Date: Thu, 30 Jun 2022 10:22:41 +1000
Subject: [PATCH 12/12] Change `Ty::Tuple` to `Ty::Unit`.

Because that's all that is needed in practice.
---
 .../rustc_builtin_macros/src/deriving/cmp/eq.rs  |  2 +-
 .../src/deriving/encodable.rs                    |  2 +-
 .../src/deriving/generic/ty.rs                   | 16 +++++-----------
 .../rustc_builtin_macros/src/deriving/hash.rs    |  2 +-
 4 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
index caf4d3a5f772e..cb2ad283a1971 100644
--- a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
@@ -33,7 +33,7 @@ pub fn expand_deriving_eq(
             generics: Bounds::empty(),
             explicit_self: true,
             args: vec![],
-            ret_ty: nil_ty(),
+            ret_ty: Unit,
             attributes: attrs,
             unify_fieldless_variants: true,
             combine_substructure: combine_substructure(Box::new(|a, b, c| {
diff --git a/compiler/rustc_builtin_macros/src/deriving/encodable.rs b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
index 7cce1a0c2a2d8..7dc0584618d66 100644
--- a/compiler/rustc_builtin_macros/src/deriving/encodable.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/encodable.rs
@@ -125,7 +125,7 @@ pub fn expand_deriving_rustc_encodable(
             ret_ty: Path(Path::new_(
                 pathvec_std!(result::Result),
                 vec![
-                    Box::new(Tuple(Vec::new())),
+                    Box::new(Unit),
                     Box::new(Path(Path::new_(vec![typaram, sym::Error], vec![], PathKind::Local))),
                 ],
                 PathKind::Std,
diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
index c2dcff36c397a..4b20d87629d96 100644
--- a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
@@ -78,18 +78,14 @@ pub enum Ty {
     /// `mod::mod::Type<[lifetime], [Params...]>`, including a plain type
     /// parameter, and things like `i32`
     Path(Path),
-    /// includes unit
-    Tuple(Vec<Ty>),
+    /// For () return types.
+    Unit,
 }
 
 pub fn self_ref() -> Ty {
     Ref(Box::new(Self_), ast::Mutability::Not)
 }
 
-pub fn nil_ty() -> Ty {
-    Tuple(Vec::new())
-}
-
 impl Ty {
     pub fn to_ty(
         &self,
@@ -105,10 +101,8 @@ impl Ty {
             }
             Path(p) => p.to_ty(cx, span, self_ty, self_generics),
             Self_ => cx.ty_path(self.to_path(cx, span, self_ty, self_generics)),
-            Tuple(fields) => {
-                let ty = ast::TyKind::Tup(
-                    fields.iter().map(|f| f.to_ty(cx, span, self_ty, self_generics)).collect(),
-                );
+            Unit => {
+                let ty = ast::TyKind::Tup(vec![]);
                 cx.ty(span, ty)
             }
         }
@@ -143,7 +137,7 @@ impl Ty {
             }
             Path(ref p) => p.to_path(cx, span, self_ty, generics),
             Ref(..) => cx.span_bug(span, "ref in a path in generic `derive`"),
-            Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"),
+            Unit => cx.span_bug(span, "unit in a path in generic `derive`"),
         }
     }
 }
diff --git a/compiler/rustc_builtin_macros/src/deriving/hash.rs b/compiler/rustc_builtin_macros/src/deriving/hash.rs
index 641744c47ce43..9790449c4b331 100644
--- a/compiler/rustc_builtin_macros/src/deriving/hash.rs
+++ b/compiler/rustc_builtin_macros/src/deriving/hash.rs
@@ -32,7 +32,7 @@ pub fn expand_deriving_hash(
             generics: Bounds { bounds: vec![(typaram, vec![path_std!(hash::Hasher)])] },
             explicit_self: true,
             args: vec![(Ref(Box::new(Path(arg)), Mutability::Mut), sym::state)],
-            ret_ty: nil_ty(),
+            ret_ty: Unit,
             attributes: vec![],
             unify_fieldless_variants: true,
             combine_substructure: combine_substructure(Box::new(|a, b, c| {