Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b942466

Browse files
committed
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`.
1 parent 78ec19f commit b942466

File tree

12 files changed

+43
-100
lines changed

12 files changed

+43
-100
lines changed

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn expand_deriving_clone(
7979
methods: vec![MethodDef {
8080
name: sym::clone,
8181
generics: Bounds::empty(),
82-
explicit_self: borrowed_explicit_self(),
82+
explicit_self: true,
8383
args: Vec::new(),
8484
ret_ty: Self_,
8585
attributes: attrs,

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn expand_deriving_eq(
3131
methods: vec![MethodDef {
3232
name: sym::assert_receiver_is_total_eq,
3333
generics: Bounds::empty(),
34-
explicit_self: borrowed_explicit_self(),
34+
explicit_self: true,
3535
args: vec![],
3636
ret_ty: nil_ty(),
3737
attributes: attrs,

compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ pub fn expand_deriving_ord(
2727
methods: vec![MethodDef {
2828
name: sym::cmp,
2929
generics: Bounds::empty(),
30-
explicit_self: borrowed_explicit_self(),
31-
args: vec![(borrowed_self(), sym::other)],
30+
explicit_self: true,
31+
args: vec![(self_ref(), sym::other)],
3232
ret_ty: Literal(path_std!(cmp::Ordering)),
3333
attributes: attrs,
3434
unify_fieldless_variants: true,

compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ pub fn expand_deriving_partial_eq(
6969
MethodDef {
7070
name: $name,
7171
generics: Bounds::empty(),
72-
explicit_self: borrowed_explicit_self(),
73-
args: vec![(borrowed_self(), sym::other)],
72+
explicit_self: true,
73+
args: vec![(self_ref(), sym::other)],
7474
ret_ty: Literal(path_local!(bool)),
7575
attributes: attrs,
7676
unify_fieldless_variants: true,

compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ pub fn expand_deriving_partial_ord(
2929
let partial_cmp_def = MethodDef {
3030
name: sym::partial_cmp,
3131
generics: Bounds::empty(),
32-
explicit_self: borrowed_explicit_self(),
33-
args: vec![(borrowed_self(), sym::other)],
32+
explicit_self: true,
33+
args: vec![(self_ref(), sym::other)],
3434
ret_ty,
3535
attributes: attrs,
3636
unify_fieldless_variants: true,

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ pub fn expand_deriving_debug(
1616
push: &mut dyn FnMut(Annotatable),
1717
) {
1818
// &mut ::std::fmt::Formatter
19-
let fmtr =
20-
Ptr(Box::new(Literal(path_std!(fmt::Formatter))), Borrowed(None, ast::Mutability::Mut));
19+
let fmtr = Ref(Box::new(Literal(path_std!(fmt::Formatter))), ast::Mutability::Mut);
2120

2221
let trait_def = TraitDef {
2322
span,
@@ -29,7 +28,7 @@ pub fn expand_deriving_debug(
2928
methods: vec![MethodDef {
3029
name: sym::fmt,
3130
generics: Bounds::empty(),
32-
explicit_self: borrowed_explicit_self(),
31+
explicit_self: true,
3332
args: vec![(fmtr, sym::f)],
3433
ret_ty: Literal(path_std!(fmt::Result)),
3534
attributes: Vec::new(),

compiler/rustc_builtin_macros/src/deriving/decodable.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,8 @@ pub fn expand_deriving_rustc_decodable(
3535
vec![Path::new_(vec![krate, sym::Decoder], None, vec![], PathKind::Global)],
3636
)],
3737
},
38-
explicit_self: None,
39-
args: vec![(
40-
Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mut)),
41-
sym::d,
42-
)],
38+
explicit_self: false,
39+
args: vec![(Ref(Box::new(Literal(Path::new_local(typaram))), Mutability::Mut), sym::d)],
4340
ret_ty: Literal(Path::new_(
4441
pathvec_std!(result::Result),
4542
None,

compiler/rustc_builtin_macros/src/deriving/default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn expand_deriving_default(
3434
methods: vec![MethodDef {
3535
name: kw::Default,
3636
generics: Bounds::empty(),
37-
explicit_self: None,
37+
explicit_self: false,
3838
args: Vec::new(),
3939
ret_ty: Self_,
4040
attributes: attrs,

compiler/rustc_builtin_macros/src/deriving/encodable.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,8 @@ pub fn expand_deriving_rustc_encodable(
120120
vec![Path::new_(vec![krate, sym::Encoder], None, vec![], PathKind::Global)],
121121
)],
122122
},
123-
explicit_self: borrowed_explicit_self(),
124-
args: vec![(
125-
Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mut)),
126-
sym::s,
127-
)],
123+
explicit_self: true,
124+
args: vec![(Ref(Box::new(Literal(Path::new_local(typaram))), Mutability::Mut), sym::s)],
128125
ret_ty: Literal(Path::new_(
129126
pathvec_std!(result::Result),
130127
None,

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ use rustc_expand::base::{Annotatable, ExtCtxt};
188188
use rustc_span::symbol::{kw, sym, Ident, Symbol};
189189
use rustc_span::Span;
190190

191-
use ty::{Bounds, Path, Ptr, PtrTy, Self_, Ty};
191+
use ty::{Bounds, Path, Ref, Self_, Ty};
192192

193193
use crate::deriving;
194194

@@ -224,10 +224,8 @@ pub struct MethodDef<'a> {
224224
/// List of generics, e.g., `R: rand::Rng`
225225
pub generics: Bounds,
226226

227-
/// Whether there is a self argument (outer Option) i.e., whether
228-
/// this is a static function, and whether it is a pointer (inner
229-
/// Option)
230-
pub explicit_self: Option<Option<PtrTy>>,
227+
/// Is there is a `&self` argument? If not, it is a static function.
228+
pub explicit_self: bool,
231229

232230
/// Arguments other than the self argument
233231
pub args: Vec<(Ty, Symbol)>,
@@ -844,7 +842,7 @@ impl<'a> MethodDef<'a> {
844842
}
845843

846844
fn is_static(&self) -> bool {
847-
self.explicit_self.is_none()
845+
!self.explicit_self
848846
}
849847

850848
fn split_self_nonself_args(
@@ -857,17 +855,15 @@ impl<'a> MethodDef<'a> {
857855
let mut self_args = Vec::new();
858856
let mut nonself_args = Vec::new();
859857
let mut arg_tys = Vec::new();
860-
let mut nonstatic = false;
861858
let span = trait_.span;
862859

863-
let ast_explicit_self = self.explicit_self.as_ref().map(|self_ptr| {
864-
let (self_expr, explicit_self) = ty::get_explicit_self(cx, span, self_ptr);
865-
860+
let ast_explicit_self = if self.explicit_self {
861+
let (self_expr, explicit_self) = ty::get_explicit_self(cx, span);
866862
self_args.push(self_expr);
867-
nonstatic = true;
868-
869-
explicit_self
870-
});
863+
Some(explicit_self)
864+
} else {
865+
None
866+
};
871867

872868
for (ty, name) in self.args.iter() {
873869
let ast_ty = ty.to_ty(cx, span, type_ident, generics);
@@ -879,10 +875,10 @@ impl<'a> MethodDef<'a> {
879875
match *ty {
880876
// for static methods, just treat any Self
881877
// arguments as a normal arg
882-
Self_ if nonstatic => {
878+
Self_ if !self.is_static() => {
883879
self_args.push(arg_expr);
884880
}
885-
Ptr(ref ty, _) if matches!(**ty, Self_) && nonstatic => {
881+
Ref(ref ty, _) if matches!(**ty, Self_) && !self.is_static() => {
886882
self_args.push(cx.expr_deref(span, arg_expr))
887883
}
888884
_ => {

0 commit comments

Comments
 (0)