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`.
This commit is contained in:
parent
78ec19ffe6
commit
b94246693a
12 changed files with 43 additions and 100 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue