Skip to content

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

+1-1
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

+1-1
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

+2-2
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

+2-2
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

+2-2
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

+2-3
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

+2-5
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

+1-1
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

+2-5
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

+12-16
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
_ => {

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

+14-60
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! A mini version of ast::Ty, which is easier to use, and features an explicit `Self` type to use
22
//! when specifying impls to be derived.
33
4-
pub use PtrTy::*;
54
pub use Ty::*;
65

76
use rustc_ast::ptr::P;
@@ -11,16 +10,6 @@ use rustc_span::source_map::{respan, DUMMY_SP};
1110
use rustc_span::symbol::{kw, Ident, Symbol};
1211
use rustc_span::Span;
1312

14-
/// The types of pointers
15-
#[derive(Clone)]
16-
pub enum PtrTy {
17-
/// &'lifetime mut
18-
Borrowed(Option<Ident>, ast::Mutability),
19-
/// *mut
20-
#[allow(dead_code)]
21-
Raw(ast::Mutability),
22-
}
23-
2413
/// A path, e.g., `::std::option::Option::<i32>` (global). Has support
2514
/// for type parameters and a lifetime.
2615
#[derive(Clone)]
@@ -92,28 +81,17 @@ impl Path {
9281
#[derive(Clone)]
9382
pub enum Ty {
9483
Self_,
95-
/// &/Box/ Ty
96-
Ptr(Box<Ty>, PtrTy),
84+
/// A reference.
85+
Ref(Box<Ty>, ast::Mutability),
9786
/// `mod::mod::Type<[lifetime], [Params...]>`, including a plain type
9887
/// parameter, and things like `i32`
9988
Literal(Path),
10089
/// includes unit
10190
Tuple(Vec<Ty>),
10291
}
10392

104-
pub fn borrowed_ptrty() -> PtrTy {
105-
Borrowed(None, ast::Mutability::Not)
106-
}
107-
pub fn borrowed(ty: Box<Ty>) -> Ty {
108-
Ptr(ty, borrowed_ptrty())
109-
}
110-
111-
pub fn borrowed_explicit_self() -> Option<Option<PtrTy>> {
112-
Some(Some(borrowed_ptrty()))
113-
}
114-
115-
pub fn borrowed_self() -> Ty {
116-
borrowed(Box::new(Self_))
93+
pub fn self_ref() -> Ty {
94+
Ref(Box::new(Self_), ast::Mutability::Not)
11795
}
11896

11997
pub fn nil_ty() -> Ty {
@@ -136,20 +114,14 @@ impl Ty {
136114
self_ty: Ident,
137115
self_generics: &Generics,
138116
) -> P<ast::Ty> {
139-
match *self {
140-
Ptr(ref ty, ref ptr) => {
117+
match self {
118+
Ref(ty, mutbl) => {
141119
let raw_ty = ty.to_ty(cx, span, self_ty, self_generics);
142-
match *ptr {
143-
Borrowed(ref lt, mutbl) => {
144-
let lt = mk_lifetime(cx, span, lt);
145-
cx.ty_rptr(span, raw_ty, lt, mutbl)
146-
}
147-
Raw(mutbl) => cx.ty_ptr(span, raw_ty, mutbl),
148-
}
120+
cx.ty_rptr(span, raw_ty, None, *mutbl)
149121
}
150-
Literal(ref p) => p.to_ty(cx, span, self_ty, self_generics),
122+
Literal(p) => p.to_ty(cx, span, self_ty, self_generics),
151123
Self_ => cx.ty_path(self.to_path(cx, span, self_ty, self_generics)),
152-
Tuple(ref fields) => {
124+
Tuple(fields) => {
153125
let ty = ast::TyKind::Tup(
154126
fields.iter().map(|f| f.to_ty(cx, span, self_ty, self_generics)).collect(),
155127
);
@@ -186,7 +158,7 @@ impl Ty {
186158
cx.path_all(span, false, vec![self_ty], params)
187159
}
188160
Literal(ref p) => p.to_path(cx, span, self_ty, generics),
189-
Ptr(..) => cx.span_bug(span, "pointer in a path in generic `derive`"),
161+
Ref(..) => cx.span_bug(span, "ref in a path in generic `derive`"),
190162
Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"),
191163
}
192164
}
@@ -245,28 +217,10 @@ impl Bounds {
245217
}
246218
}
247219

248-
pub fn get_explicit_self(
249-
cx: &ExtCtxt<'_>,
250-
span: Span,
251-
self_ptr: &Option<PtrTy>,
252-
) -> (P<Expr>, ast::ExplicitSelf) {
220+
pub fn get_explicit_self(cx: &ExtCtxt<'_>, span: Span) -> (P<Expr>, ast::ExplicitSelf) {
253221
// this constructs a fresh `self` path
254222
let self_path = cx.expr_self(span);
255-
match *self_ptr {
256-
None => (self_path, respan(span, SelfKind::Value(ast::Mutability::Not))),
257-
Some(ref ptr) => {
258-
let self_ty = respan(
259-
span,
260-
match *ptr {
261-
Borrowed(ref lt, mutbl) => {
262-
let lt = lt.map(|s| cx.lifetime(span, s));
263-
SelfKind::Region(lt, mutbl)
264-
}
265-
Raw(_) => cx.span_bug(span, "attempted to use *self in deriving definition"),
266-
},
267-
);
268-
let self_expr = cx.expr_deref(span, self_path);
269-
(self_expr, self_ty)
270-
}
271-
}
223+
let self_ty = respan(span, SelfKind::Region(None, ast::Mutability::Not));
224+
let self_expr = cx.expr_deref(span, self_path);
225+
(self_expr, self_ty)
272226
}

compiler/rustc_builtin_macros/src/deriving/hash.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub fn expand_deriving_hash(
3030
methods: vec![MethodDef {
3131
name: sym::hash,
3232
generics: Bounds { bounds: vec![(typaram, vec![path_std!(hash::Hasher)])] },
33-
explicit_self: borrowed_explicit_self(),
34-
args: vec![(Ptr(Box::new(Literal(arg)), Borrowed(None, Mutability::Mut)), sym::state)],
33+
explicit_self: true,
34+
args: vec![(Ref(Box::new(Literal(arg)), Mutability::Mut), sym::state)],
3535
ret_ty: nil_ty(),
3636
attributes: vec![],
3737
unify_fieldless_variants: true,

0 commit comments

Comments
 (0)