diff --git a/compiler/rustc_builtin_macros/src/deriving/clone.rs b/compiler/rustc_builtin_macros/src/deriving/clone.rs index 0a55d4e0ce0a638287fea46d604df06727104de5..a67d16d6b2f206c3f859b4d13e21327455897590 100644 --- a/compiler/rustc_builtin_macros/src/deriving/clone.rs +++ b/compiler/rustc_builtin_macros/src/deriving/clone.rs @@ -80,7 +80,7 @@ pub fn expand_deriving_clone( name: sym::clone, generics: Bounds::empty(), explicit_self: true, - args: Vec::new(), + nonself_args: Vec::new(), ret_ty: Self_, attributes: attrs, unify_fieldless_variants: false, diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs index c1a2ebcc14601e5d384de4a2ad54be2395f3c590..4e798bf6acb108ea90b37332ae105f77d6c5357a 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs @@ -32,7 +32,7 @@ pub fn expand_deriving_eq( name: sym::assert_receiver_is_total_eq, generics: Bounds::empty(), explicit_self: true, - args: vec![], + nonself_args: vec![], ret_ty: Unit, attributes: attrs, unify_fieldless_variants: true, diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs index bec59aac5eee1d13ba4f90eb3eea69423795bf5a..d80a2293e6674b0c2c0d36dd24488a7098170f53 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs @@ -28,7 +28,7 @@ pub fn expand_deriving_ord( name: sym::cmp, generics: Bounds::empty(), explicit_self: true, - args: vec![(self_ref(), sym::other)], + nonself_args: vec![(self_ref(), sym::other)], ret_ty: Path(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 b44c290d12f567990efef4176040cfa6e5e858d0..a9a0634836b839132f58fefa26b3ab54a34f5125 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs @@ -69,7 +69,7 @@ fn cs_ne(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOr name: $name, generics: Bounds::empty(), explicit_self: true, - args: vec![(self_ref(), sym::other)], + nonself_args: vec![(self_ref(), sym::other)], ret_ty: Path(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 5769f08f494824d7e8d8fa9be4361793a5c9ee82..c8c9a6fbb5793769bd81fa32368fa8eb1294a010 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs @@ -26,7 +26,7 @@ pub fn expand_deriving_partial_ord( name: sym::partial_cmp, generics: Bounds::empty(), explicit_self: true, - args: vec![(self_ref(), sym::other)], + nonself_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 e04898287608b756523f714298de5b43b525d0cd..71f77ea8b6a397f858eff9cc39249868fd2b4dfc 100644 --- a/compiler/rustc_builtin_macros/src/deriving/debug.rs +++ b/compiler/rustc_builtin_macros/src/deriving/debug.rs @@ -28,7 +28,7 @@ pub fn expand_deriving_debug( name: sym::fmt, generics: Bounds::empty(), explicit_self: true, - args: vec![(fmtr, sym::f)], + nonself_args: vec![(fmtr, sym::f)], ret_ty: Path(path_std!(fmt::Result)), attributes: Vec::new(), unify_fieldless_variants: false, @@ -53,7 +53,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_> // We want to make sure we have the ctxt set so that we can use unstable methods let span = cx.with_def_site_ctxt(span); let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked)); - let fmt = substr.nonself_args[0].clone(); + let fmt = substr.nonselflike_args[0].clone(); // Struct and tuples are similar enough that we use the same code for both, // with some extra pieces for structs due to the field names. diff --git a/compiler/rustc_builtin_macros/src/deriving/decodable.rs b/compiler/rustc_builtin_macros/src/deriving/decodable.rs index b9f2a75082224a5647b856ca092cc165ea205c30..d688143a2a5c622e56102406b81ca1bee70b1fbe 100644 --- a/compiler/rustc_builtin_macros/src/deriving/decodable.rs +++ b/compiler/rustc_builtin_macros/src/deriving/decodable.rs @@ -36,7 +36,10 @@ pub fn expand_deriving_rustc_decodable( )], }, explicit_self: false, - args: vec![(Ref(Box::new(Path(Path::new_local(typaram))), Mutability::Mut), sym::d)], + nonself_args: vec![( + Ref(Box::new(Path(Path::new_local(typaram))), Mutability::Mut), + sym::d, + )], ret_ty: Path(Path::new_( pathvec_std!(result::Result), vec![ @@ -63,7 +66,7 @@ fn decodable_substructure( substr: &Substructure<'_>, krate: Symbol, ) -> BlockOrExpr { - let decoder = substr.nonself_args[0].clone(); + let decoder = substr.nonselflike_args[0].clone(); let recurse = vec![ Ident::new(krate, trait_span), Ident::new(sym::Decodable, trait_span), diff --git a/compiler/rustc_builtin_macros/src/deriving/default.rs b/compiler/rustc_builtin_macros/src/deriving/default.rs index 90d5cdbc0a0783ac494e2b7941859875f1734f5b..5177690917f21759da87a64be8c7cbcbc889602b 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( name: kw::Default, generics: Bounds::empty(), explicit_self: false, - args: Vec::new(), + nonself_args: Vec::new(), ret_ty: Self_, attributes: attrs, unify_fieldless_variants: false, diff --git a/compiler/rustc_builtin_macros/src/deriving/encodable.rs b/compiler/rustc_builtin_macros/src/deriving/encodable.rs index 0dfce114bfc5399af72a809b64cc3d80721cc0e3..c89558f6b86eb7f51ca53aadfba04be305c81505 100644 --- a/compiler/rustc_builtin_macros/src/deriving/encodable.rs +++ b/compiler/rustc_builtin_macros/src/deriving/encodable.rs @@ -120,7 +120,10 @@ pub fn expand_deriving_rustc_encodable( )], }, explicit_self: true, - args: vec![(Ref(Box::new(Path(Path::new_local(typaram))), Mutability::Mut), sym::s)], + nonself_args: vec![( + Ref(Box::new(Path(Path::new_local(typaram))), Mutability::Mut), + sym::s, + )], ret_ty: Path(Path::new_( pathvec_std!(result::Result), vec![ @@ -147,7 +150,7 @@ fn encodable_substructure( substr: &Substructure<'_>, krate: Symbol, ) -> BlockOrExpr { - let encoder = substr.nonself_args[0].clone(); + let encoder = substr.nonselflike_args[0].clone(); // throw an underscore in front to suppress unused variable warnings let blkarg = Ident::new(sym::_e, trait_span); let blkencoder = cx.expr_ident(trait_span, blkarg); diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index e618255b0c6fd4b0866d4b58a67bb14f1334f4d3..cafca507bd44662199f2ecf9ab14cff82cd956e4 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -227,8 +227,8 @@ pub struct MethodDef<'a> { /// 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)>, + /// Arguments other than the self argument. + pub nonself_args: Vec<(Ty, Symbol)>, /// Returns type pub ret_ty: Ty, @@ -245,8 +245,8 @@ pub struct MethodDef<'a> { pub struct Substructure<'a> { /// ident of self pub type_ident: Ident, - /// verbatim access to any non-self arguments - pub nonself_args: &'a [P], + /// verbatim access to any non-selflike arguments + pub nonselflike_args: &'a [P], pub fields: &'a SubstructureFields<'a>, } @@ -782,8 +782,8 @@ fn expand_struct_def( .methods .iter() .map(|method_def| { - let (explicit_self, self_args, nonself_args, tys) = - method_def.split_self_nonself_args(cx, self, type_ident, generics); + let (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) = + method_def.extract_arg_details(cx, self, type_ident, generics); let body = if from_scratch || method_def.is_static() { method_def.expand_static_struct_method_body( @@ -791,7 +791,7 @@ fn expand_struct_def( self, struct_def, type_ident, - &nonself_args, + &nonselflike_args, ) } else { method_def.expand_struct_method_body( @@ -799,14 +799,22 @@ fn expand_struct_def( self, struct_def, type_ident, - &self_args, - &nonself_args, + &selflike_args, + &nonselflike_args, use_temporaries, is_packed, ) }; - method_def.create_method(cx, self, type_ident, generics, explicit_self, tys, body) + method_def.create_method( + cx, + self, + type_ident, + generics, + explicit_self, + nonself_arg_tys, + body, + ) }) .collect(); @@ -831,8 +839,8 @@ fn expand_enum_def( .methods .iter() .map(|method_def| { - let (explicit_self, self_args, nonself_args, tys) = - method_def.split_self_nonself_args(cx, self, type_ident, generics); + let (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) = + method_def.extract_arg_details(cx, self, type_ident, generics); let body = if from_scratch || method_def.is_static() { method_def.expand_static_enum_method_body( @@ -840,7 +848,7 @@ fn expand_enum_def( self, enum_def, type_ident, - &nonself_args, + &nonselflike_args, ) } else { method_def.expand_enum_method_body( @@ -848,12 +856,20 @@ fn expand_enum_def( self, enum_def, type_ident, - self_args, - &nonself_args, + selflike_args, + &nonselflike_args, ) }; - method_def.create_method(cx, self, type_ident, generics, explicit_self, tys, body) + method_def.create_method( + cx, + self, + type_ident, + generics, + explicit_self, + nonself_arg_tys, + body, + ) }) .collect(); @@ -867,11 +883,11 @@ fn call_substructure_method( cx: &mut ExtCtxt<'_>, trait_: &TraitDef<'_>, type_ident: Ident, - nonself_args: &[P], + nonselflike_args: &[P], fields: &SubstructureFields<'_>, ) -> BlockOrExpr { let span = trait_.span; - let substructure = Substructure { type_ident, nonself_args, fields }; + let substructure = Substructure { type_ident, nonselflike_args, fields }; let mut f = self.combine_substructure.borrow_mut(); let f: &mut CombineSubstructureFunc<'_> = &mut *f; f(cx, span, &substructure) @@ -891,49 +907,52 @@ fn is_static(&self) -> bool { !self.explicit_self } - fn split_self_nonself_args( + // The return value includes: + // - explicit_self: The `&self` arg, if present. + // - selflike_args: Expressions for `&self` (if present) and also any other + // args with the same type (e.g. the `other` arg in `PartialEq::eq`). + // - nonselflike_args: Expressions for all the remaining args. + // - nonself_arg_tys: Additional information about all the args other than + // `&self`. + fn extract_arg_details( &self, cx: &mut ExtCtxt<'_>, trait_: &TraitDef<'_>, type_ident: Ident, generics: &Generics, ) -> (Option, Vec>, Vec>, Vec<(Ident, P)>) { - let mut self_args = Vec::new(); - let mut nonself_args = Vec::new(); - let mut arg_tys = Vec::new(); + let mut selflike_args = Vec::new(); + let mut nonselflike_args = Vec::new(); + let mut nonself_arg_tys = Vec::new(); let span = trait_.span; - let ast_explicit_self = if self.explicit_self { + let explicit_self = if self.explicit_self { let (self_expr, explicit_self) = ty::get_explicit_self(cx, span); - self_args.push(self_expr); + selflike_args.push(self_expr); Some(explicit_self) } else { None }; - for (ty, name) in self.args.iter() { + for (ty, name) in self.nonself_args.iter() { let ast_ty = ty.to_ty(cx, span, type_ident, generics); let ident = Ident::new(*name, span); - arg_tys.push((ident, ast_ty)); + nonself_arg_tys.push((ident, ast_ty)); let arg_expr = cx.expr_ident(span, ident); match *ty { // for static methods, just treat any Self // arguments as a normal arg - Self_ if !self.is_static() => { - self_args.push(arg_expr); - } Ref(ref ty, _) if matches!(**ty, Self_) && !self.is_static() => { - self_args.push(cx.expr_deref(span, arg_expr)) - } - _ => { - nonself_args.push(arg_expr); + selflike_args.push(cx.expr_deref(span, arg_expr)) } + Self_ => cx.span_bug(span, "`Self` in non-return position"), + _ => nonselflike_args.push(arg_expr), } } - (ast_explicit_self, self_args, nonself_args, arg_tys) + (explicit_self, selflike_args, nonselflike_args, nonself_arg_tys) } fn create_method( @@ -943,7 +962,7 @@ fn create_method( type_ident: Ident, generics: &Generics, explicit_self: Option, - arg_types: Vec<(Ident, P)>, + nonself_arg_tys: Vec<(Ident, P)>, body: BlockOrExpr, ) -> P { let span = trait_.span; @@ -951,12 +970,13 @@ fn create_method( let fn_generics = self.generics.to_generics(cx, span, type_ident, generics); let args = { - let self_args = explicit_self.map(|explicit_self| { + let self_arg = explicit_self.map(|explicit_self| { let ident = Ident::with_dummy_span(kw::SelfLower).with_span_pos(span); ast::Param::from_self(ast::AttrVec::default(), explicit_self, ident) }); - let nonself_args = arg_types.into_iter().map(|(name, ty)| cx.param(span, name, ty)); - self_args.into_iter().chain(nonself_args).collect() + let nonself_args = + nonself_arg_tys.into_iter().map(|(name, ty)| cx.param(span, name, ty)); + self_arg.into_iter().chain(nonself_args).collect() }; let ret_type = self.get_ret_ty(cx, trait_, generics, type_ident); @@ -1024,8 +1044,8 @@ fn expand_struct_method_body<'b>( trait_: &TraitDef<'b>, struct_def: &'b VariantData, type_ident: Ident, - self_args: &[P], - nonself_args: &[P], + selflike_args: &[P], + nonselflike_args: &[P], use_temporaries: bool, is_packed: bool, ) -> BlockOrExpr { @@ -1033,9 +1053,9 @@ fn expand_struct_method_body<'b>( let span = trait_.span; let mut patterns = Vec::new(); - for (i, self_arg) in self_args.iter().enumerate() { + for (i, selflike_arg) in selflike_args.iter().enumerate() { let ident_exprs = if !is_packed { - trait_.create_struct_field_accesses(cx, self_arg, struct_def) + trait_.create_struct_field_accesses(cx, selflike_arg, struct_def) } else { // Get the pattern for the let-destructuring. // @@ -1084,7 +1104,7 @@ fn expand_struct_method_body<'b>( cx, trait_, type_ident, - nonself_args, + nonselflike_args, &Struct(struct_def, fields), ); @@ -1092,8 +1112,10 @@ fn expand_struct_method_body<'b>( body } else { // Do the let-destructuring. - let mut stmts: Vec<_> = iter::zip(self_args, patterns) - .map(|(arg_expr, pat)| cx.stmt_let_pat(span, pat, arg_expr.clone())) + let mut stmts: Vec<_> = iter::zip(selflike_args, patterns) + .map(|(selflike_arg_expr, pat)| { + cx.stmt_let_pat(span, pat, selflike_arg_expr.clone()) + }) .collect(); stmts.extend(std::mem::take(&mut body.0)); BlockOrExpr(stmts, body.1) @@ -1106,7 +1128,7 @@ fn expand_static_struct_method_body( trait_: &TraitDef<'_>, struct_def: &VariantData, type_ident: Ident, - nonself_args: &[P], + nonselflike_args: &[P], ) -> BlockOrExpr { let summary = trait_.summarise_struct(cx, struct_def); @@ -1114,7 +1136,7 @@ fn expand_static_struct_method_body( cx, trait_, type_ident, - nonself_args, + nonselflike_args, &StaticStruct(struct_def, summary), ) } @@ -1148,7 +1170,7 @@ fn expand_static_struct_method_body( /// } /// } /// ``` - /// Creates a match for a tuple of all `self_args`, where either all + /// Creates a match for a tuple of all `selflike_args`, where either all /// variants match, or it falls into a catch-all for when one variant /// does not match. /// @@ -1161,33 +1183,33 @@ fn expand_static_struct_method_body( /// a simple equality check (for PartialEq). /// /// The catch-all handler is provided access the variant index values - /// for each of the self-args, carried in precomputed variables. + /// for each of the selflike_args, carried in precomputed variables. fn expand_enum_method_body<'b>( &self, cx: &mut ExtCtxt<'_>, trait_: &TraitDef<'b>, enum_def: &'b EnumDef, type_ident: Ident, - mut self_args: Vec>, - nonself_args: &[P], + mut selflike_args: Vec>, + nonselflike_args: &[P], ) -> BlockOrExpr { let span = trait_.span; let variants = &enum_def.variants; - let self_arg_names = iter::once("__self".to_string()) + let selflike_arg_names = iter::once("__self".to_string()) .chain( - self_args + selflike_args .iter() .enumerate() .skip(1) - .map(|(arg_count, _self_arg)| format!("__arg_{}", arg_count)), + .map(|(arg_count, _selflike_arg)| format!("__arg_{}", arg_count)), ) .collect::>(); // The `vi_idents` will be bound, solely in the catch-all, to - // a series of let statements mapping each self_arg to an int + // a series of let statements mapping each selflike_arg to an int // value corresponding to its discriminant. - let vi_idents = self_arg_names + let vi_idents = selflike_arg_names .iter() .map(|name| { let vi_suffix = format!("{}_vi", name); @@ -1206,18 +1228,18 @@ fn expand_enum_method_body<'b>( // (Variant1, Variant1, ...) => Body1 // (Variant2, Variant2, ...) => Body2 // ... - // where each tuple has length = self_args.len() + // where each tuple has length = selflike_args.len() let mut match_arms: Vec = variants .iter() .enumerate() .filter(|&(_, v)| !(self.unify_fieldless_variants && v.data.fields().is_empty())) .map(|(index, variant)| { - let mk_self_pat = |cx: &mut ExtCtxt<'_>, self_arg_name: &str| { + let mk_selflike_pat = |cx: &mut ExtCtxt<'_>, selflike_arg_name: &str| { let (p, idents) = trait_.create_enum_variant_pattern( cx, type_ident, variant, - self_arg_name, + selflike_arg_name, ast::Mutability::Not, ); (cx.pat(span, PatKind::Ref(p, ast::Mutability::Not)), idents) @@ -1225,17 +1247,17 @@ fn expand_enum_method_body<'b>( // A single arm has form (&VariantK, &VariantK, ...) => BodyK // (see "Final wrinkle" note below for why.) - let mut subpats = Vec::with_capacity(self_arg_names.len()); - let mut self_pats_idents = Vec::with_capacity(self_arg_names.len() - 1); - let first_self_pat_idents = { - let (p, idents) = mk_self_pat(cx, &self_arg_names[0]); + let mut subpats = Vec::with_capacity(selflike_arg_names.len()); + let mut selflike_pats_idents = Vec::with_capacity(selflike_arg_names.len() - 1); + let first_selflike_pat_idents = { + let (p, idents) = mk_selflike_pat(cx, &selflike_arg_names[0]); subpats.push(p); idents }; - for self_arg_name in &self_arg_names[1..] { - let (p, idents) = mk_self_pat(cx, &self_arg_name); + for selflike_arg_name in &selflike_arg_names[1..] { + let (p, idents) = mk_selflike_pat(cx, &selflike_arg_name); subpats.push(p); - self_pats_idents.push(idents); + selflike_pats_idents.push(idents); } // Here is the pat = `(&VariantK, &VariantK, ...)` @@ -1250,24 +1272,24 @@ fn expand_enum_method_body<'b>( // we are in. // All of the Self args have the same variant in these - // cases. So we transpose the info in self_pats_idents + // cases. So we transpose the info in selflike_pats_idents // to gather the getter expressions together, in the // form that EnumMatching expects. // The transposition is driven by walking across the - // arg fields of the variant for the first self pat. - let field_tuples = first_self_pat_idents + // arg fields of the variant for the first selflike pat. + let field_tuples = first_selflike_pat_idents .into_iter() .enumerate() // For each arg field of self, pull out its getter expr ... .map(|(field_index, (span, opt_ident, self_getter_expr, attrs))| { // ... but FieldInfo also wants getter expr // for matching other arguments of Self type; - // so walk across the *other* self_pats_idents + // so walk across the *other* selflike_pats_idents // and pull out getter for same field in each // of them (using `field_index` tracked above). // That is the heart of the transposition. - let others = self_pats_idents + let others = selflike_pats_idents .iter() .map(|fields| { let (_, _opt_ident, ref other_getter_expr, _) = fields[field_index]; @@ -1298,7 +1320,13 @@ fn expand_enum_method_body<'b>( // Build up code associated with such a case. let substructure = EnumMatching(index, variants.len(), variant, field_tuples); let arm_expr = self - .call_substructure_method(cx, trait_, type_ident, nonself_args, &substructure) + .call_substructure_method( + cx, + trait_, + type_ident, + nonselflike_args, + &substructure, + ) .into_expr(cx, span); cx.arm(span, single_pat, arm_expr) @@ -1316,13 +1344,13 @@ fn expand_enum_method_body<'b>( cx, trait_, type_ident, - nonself_args, + nonselflike_args, &substructure, ) .into_expr(cx, span), ) } - _ if variants.len() > 1 && self_args.len() > 1 => { + _ if variants.len() > 1 && selflike_args.len() > 1 => { // Since we know that all the arguments will match if we reach // the match expression we add the unreachable intrinsics as the // result of the catch all which should help llvm in optimizing it @@ -1349,8 +1377,8 @@ fn expand_enum_method_body<'b>( // catch-all `_` match, it would trigger the // unreachable-pattern error. // - if variants.len() > 1 && self_args.len() > 1 { - // Build a series of let statements mapping each self_arg + if variants.len() > 1 && selflike_args.len() > 1 { + // Build a series of let statements mapping each selflike_arg // to its discriminant value. // // i.e., for `enum E { A, B(1), C(T, T) }`, and a deriving @@ -1365,10 +1393,14 @@ fn expand_enum_method_body<'b>( // We also build an expression which checks whether all discriminants are equal: // `__self_vi == __arg_1_vi && __self_vi == __arg_2_vi && ...` let mut discriminant_test = cx.expr_bool(span, true); - for (i, (&ident, self_arg)) in iter::zip(&vi_idents, &self_args).enumerate() { - let self_addr = cx.expr_addr_of(span, self_arg.clone()); - let variant_value = - deriving::call_intrinsic(cx, span, sym::discriminant_value, vec![self_addr]); + for (i, (&ident, selflike_arg)) in iter::zip(&vi_idents, &selflike_args).enumerate() { + let selflike_addr = cx.expr_addr_of(span, selflike_arg.clone()); + let variant_value = deriving::call_intrinsic( + cx, + span, + sym::discriminant_value, + vec![selflike_addr], + ); let let_stmt = cx.stmt_let(span, false, ident, variant_value); index_let_stmts.push(let_stmt); @@ -1389,18 +1421,18 @@ fn expand_enum_method_body<'b>( cx, trait_, type_ident, - nonself_args, + nonselflike_args, &catch_all_substructure, ) .into_expr(cx, span); - // Final wrinkle: the self_args are expressions that deref + // Final wrinkle: the selflike_args are expressions that deref // down to desired places, but we cannot actually deref // them when they are fed as r-values into a tuple // expression; here add a layer of borrowing, turning // `(*self, *__arg_0, ...)` into `(&*self, &*__arg_0, ...)`. - self_args.map_in_place(|self_arg| cx.expr_addr_of(span, self_arg)); - let match_arg = cx.expr(span, ast::ExprKind::Tup(self_args)); + selflike_args.map_in_place(|selflike_arg| cx.expr_addr_of(span, selflike_arg)); + let match_arg = cx.expr(span, ast::ExprKind::Tup(selflike_args)); // Lastly we create an expression which branches on all discriminants being equal // if discriminant_test { @@ -1469,16 +1501,16 @@ fn expand_enum_method_body<'b>( BlockOrExpr(vec![], Some(deriving::call_unreachable(cx, span))) } else { - // Final wrinkle: the self_args are expressions that deref + // Final wrinkle: the selflike_args are expressions that deref // down to desired places, but we cannot actually deref // them when they are fed as r-values into a tuple // expression; here add a layer of borrowing, turning // `(*self, *__arg_0, ...)` into `(&*self, &*__arg_0, ...)`. - self_args.map_in_place(|self_arg| cx.expr_addr_of(span, self_arg)); - let match_arg = if self_args.len() == 1 { - self_args.pop().unwrap() + selflike_args.map_in_place(|selflike_arg| cx.expr_addr_of(span, selflike_arg)); + let match_arg = if selflike_args.len() == 1 { + selflike_args.pop().unwrap() } else { - cx.expr(span, ast::ExprKind::Tup(self_args)) + cx.expr(span, ast::ExprKind::Tup(selflike_args)) }; BlockOrExpr(vec![], Some(cx.expr_match(span, match_arg, match_arms))) } @@ -1490,7 +1522,7 @@ fn expand_static_enum_method_body( trait_: &TraitDef<'_>, enum_def: &EnumDef, type_ident: Ident, - nonself_args: &[P], + nonselflike_args: &[P], ) -> BlockOrExpr { let summary = enum_def .variants @@ -1505,7 +1537,7 @@ fn expand_static_enum_method_body( cx, trait_, type_ident, - nonself_args, + nonselflike_args, &StaticEnum(enum_def, summary), ) } @@ -1609,7 +1641,7 @@ fn create_struct_pattern( fn create_struct_field_accesses( &self, cx: &mut ExtCtxt<'_>, - mut self_arg: &P, + mut selflike_arg: &P, struct_def: &'a VariantData, ) -> Vec<(Span, Option, P, &'a [ast::Attribute])> { let mut ident_exprs = Vec::new(); @@ -1617,8 +1649,8 @@ fn create_struct_field_accesses( let sp = struct_field.span.with_ctxt(self.span.ctxt()); // We don't the need the deref, if there is one. - if let ast::ExprKind::Unary(ast::UnOp::Deref, inner) = &self_arg.kind { - self_arg = inner; + if let ast::ExprKind::Unary(ast::UnOp::Deref, inner) = &selflike_arg.kind { + selflike_arg = inner; } // Note: we must use `struct_field.span` rather than `span` in the @@ -1628,7 +1660,7 @@ fn create_struct_field_accesses( let val = cx.expr( sp, ast::ExprKind::Field( - self_arg.clone(), + selflike_arg.clone(), struct_field.ident.unwrap_or_else(|| { Ident::from_str_and_span(&i.to_string(), struct_field.span) }), diff --git a/compiler/rustc_builtin_macros/src/deriving/hash.rs b/compiler/rustc_builtin_macros/src/deriving/hash.rs index c3f7d09886b3a23681dc8e6f5e2f7bee1a5370d9..6ff36e7f4ed032e62d34d9e2f5d184cb31ffa805 100644 --- a/compiler/rustc_builtin_macros/src/deriving/hash.rs +++ b/compiler/rustc_builtin_macros/src/deriving/hash.rs @@ -30,7 +30,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(Path(arg)), Mutability::Mut), sym::state)], + nonself_args: vec![(Ref(Box::new(Path(arg)), Mutability::Mut), sym::state)], ret_ty: Unit, attributes: vec![], unify_fieldless_variants: true, @@ -49,7 +49,7 @@ fn hash_substructure( trait_span: Span, substr: &Substructure<'_>, ) -> BlockOrExpr { - let [state_expr] = substr.nonself_args else { + let [state_expr] = substr.nonselflike_args else { cx.span_bug(trait_span, "incorrect number of arguments in `derive(Hash)`"); }; let call_hash = |span, thing_expr| {