diff options
| author | bors <bors@rust-lang.org> | 2018-06-21 20:58:51 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-06-21 20:58:51 +0000 |
| commit | 662c70a59f79919640465b8753f790af822e96c8 (patch) | |
| tree | 6cdf8eebd7ef3e59549aa057f85475d86286359b /src/libsyntax_ext | |
| parent | f790780451a724dce9be0ba03ff217955036ff88 (diff) | |
| parent | daf7e359a10306c004bdffd06b6432998d70b858 (diff) | |
| download | rust-662c70a59f79919640465b8753f790af822e96c8.tar.gz rust-662c70a59f79919640465b8753f790af822e96c8.zip | |
Auto merge of #48149 - varkor:generics-generalisation, r=petrochenkov
The Great Generics Generalisation: HIR Edition This is essentially a followup to https://github.com/rust-lang/rust/pull/45930, consolidating the use of separate lifetime and type vectors into single kinds vectors wherever possible. This is intended to provide more of the groundwork for const generics (https://github.com/rust-lang/rust/issues/44580). r? @eddyb cc @yodaldevoid
Diffstat (limited to 'src/libsyntax_ext')
| -rw-r--r-- | src/libsyntax_ext/deriving/clone.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/cmp/eq.rs | 4 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/generic/mod.rs | 95 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/generic/ty.rs | 64 | ||||
| -rw-r--r-- | src/libsyntax_ext/deriving/mod.rs | 9 | ||||
| -rw-r--r-- | src/libsyntax_ext/env.rs | 9 |
6 files changed, 85 insertions, 104 deletions
diff --git a/src/libsyntax_ext/deriving/clone.rs b/src/libsyntax_ext/deriving/clone.rs index dec24d13c9b..9aeac5b1ddb 100644 --- a/src/libsyntax_ext/deriving/clone.rs +++ b/src/libsyntax_ext/deriving/clone.rs @@ -13,6 +13,7 @@ use deriving::generic::*; use deriving::generic::ty::*; use syntax::ast::{self, Expr, Generics, ItemKind, MetaItem, VariantData}; +use syntax::ast::GenericArg; use syntax::attr; use syntax::ext::base::{Annotatable, ExtCtxt}; use syntax::ext::build::AstBuilder; @@ -48,7 +49,10 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt, ItemKind::Struct(_, Generics { ref params, .. }) | ItemKind::Enum(_, Generics { ref params, .. }) => { if attr::contains_name(&annitem.attrs, "rustc_copy_clone_marker") && - !params.iter().any(|param| param.is_type_param()) + !params.iter().any(|param| match param.kind { + ast::GenericParamKind::Type { .. } => true, + _ => false, + }) { bounds = vec![]; is_shallow = true; @@ -123,7 +127,7 @@ fn cs_clone_shallow(name: &str, let span = span.with_ctxt(cx.backtrace()); let assert_path = cx.path_all(span, true, cx.std_path(&["clone", helper_name]), - vec![], vec![ty], vec![]); + vec![GenericArg::Type(ty)], vec![]); stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path))); } fn process_variant(cx: &mut ExtCtxt, stmts: &mut Vec<ast::Stmt>, variant: &VariantData) { diff --git a/src/libsyntax_ext/deriving/cmp/eq.rs b/src/libsyntax_ext/deriving/cmp/eq.rs index 237c8654edf..00ab39032ac 100644 --- a/src/libsyntax_ext/deriving/cmp/eq.rs +++ b/src/libsyntax_ext/deriving/cmp/eq.rs @@ -12,7 +12,7 @@ use deriving::path_std; use deriving::generic::*; use deriving::generic::ty::*; -use syntax::ast::{self, Expr, MetaItem}; +use syntax::ast::{self, Expr, MetaItem, GenericArg}; use syntax::ext::base::{Annotatable, ExtCtxt}; use syntax::ext::build::AstBuilder; use syntax::ptr::P; @@ -62,7 +62,7 @@ fn cs_total_eq_assert(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) let span = span.with_ctxt(cx.backtrace()); let assert_path = cx.path_all(span, true, cx.std_path(&["cmp", helper_name]), - vec![], vec![ty], vec![]); + vec![GenericArg::Type(ty)], vec![]); stmts.push(cx.stmt_let_type_only(span, cx.ty_path(assert_path))); } fn process_variant(cx: &mut ExtCtxt, stmts: &mut Vec<ast::Stmt>, variant: &ast::VariantData) { diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index 80f65957c39..672726d1475 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -192,10 +192,8 @@ use std::collections::HashSet; use std::vec; use rustc_target::spec::abi::Abi; -use syntax::ast::{ - self, BinOpKind, EnumDef, Expr, GenericParam, Generics, Ident, PatKind, VariantData -}; - +use syntax::ast::{self, BinOpKind, EnumDef, Expr, Generics, Ident, PatKind}; +use syntax::ast::{VariantData, GenericParamKind, GenericArg}; use syntax::attr; use syntax::ext::base::{Annotatable, ExtCtxt}; use syntax::ext::build::AstBuilder; @@ -424,7 +422,10 @@ impl<'a> TraitDef<'a> { ast::ItemKind::Struct(_, ref generics) | ast::ItemKind::Enum(_, ref generics) | ast::ItemKind::Union(_, ref generics) => { - !generics.params.iter().any(|p| p.is_type_param()) + !generics.params.iter().any(|param| match param.kind { + ast::GenericParamKind::Type { .. } => true, + _ => false, + }) } _ => { // Non-ADT derive is an error, but it should have been @@ -548,30 +549,27 @@ impl<'a> TraitDef<'a> { .to_generics(cx, self.span, type_ident, generics); // Create the generic parameters - params.extend(generics.params.iter().map(|param| { - match *param { - ref l @ GenericParam::Lifetime(_) => l.clone(), - GenericParam::Type(ref ty_param) => { - // I don't think this can be moved out of the loop, since - // a TyParamBound requires an ast id - let mut bounds: Vec<_> = - // extra restrictions on the generics parameters to the - // type being derived upon - self.additional_bounds.iter().map(|p| { - cx.typarambound(p.to_path(cx, self.span, - type_ident, generics)) - }).collect(); - - // require the current trait - bounds.push(cx.typarambound(trait_path.clone())); - - // also add in any bounds from the declaration - for declared_bound in ty_param.bounds.iter() { - bounds.push((*declared_bound).clone()); - } - - GenericParam::Type(cx.typaram(self.span, ty_param.ident, vec![], bounds, None)) + params.extend(generics.params.iter().map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => param.clone(), + GenericParamKind::Type { .. } => { + // I don't think this can be moved out of the loop, since + // a GenericBound requires an ast id + let mut bounds: Vec<_> = + // extra restrictions on the generics parameters to the + // type being derived upon + self.additional_bounds.iter().map(|p| { + cx.trait_bound(p.to_path(cx, self.span, type_ident, generics)) + }).collect(); + + // require the current trait + bounds.push(cx.trait_bound(trait_path.clone())); + + // also add in any bounds from the declaration + for declared_bound in ¶m.bounds { + bounds.push((*declared_bound).clone()); } + + cx.typaram(self.span, param.ident, vec![], bounds, None) } })); @@ -608,8 +606,8 @@ impl<'a> TraitDef<'a> { // Extra scope required here so ty_params goes out of scope before params is moved let mut ty_params = params.iter() - .filter_map(|param| match *param { - ast::GenericParam::Type(ref t) => Some(t), + .filter_map(|param| match param.kind { + ast::GenericParamKind::Type { .. } => Some(param), _ => None, }) .peekable(); @@ -636,12 +634,12 @@ impl<'a> TraitDef<'a> { let mut bounds: Vec<_> = self.additional_bounds .iter() .map(|p| { - cx.typarambound(p.to_path(cx, self.span, type_ident, generics)) + cx.trait_bound(p.to_path(cx, self.span, type_ident, generics)) }) .collect(); // require the current trait - bounds.push(cx.typarambound(trait_path.clone())); + bounds.push(cx.trait_bound(trait_path.clone())); let predicate = ast::WhereBoundPredicate { span: self.span, @@ -666,31 +664,18 @@ impl<'a> TraitDef<'a> { // Create the reference to the trait. let trait_ref = cx.trait_ref(trait_path); - // Create the type parameters on the `self` path. - let self_ty_params = generics.params - .iter() - .filter_map(|param| match *param { - GenericParam::Type(ref ty_param) - => Some(cx.ty_ident(self.span, ty_param.ident)), - _ => None, - }) - .collect(); - - let self_lifetimes: Vec<ast::Lifetime> = generics.params - .iter() - .filter_map(|param| match *param { - GenericParam::Lifetime(ref ld) => Some(ld.lifetime), - _ => None, - }) - .collect(); + let self_params: Vec<_> = generics.params.iter().map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => { + GenericArg::Lifetime(cx.lifetime(self.span, param.ident)) + } + GenericParamKind::Type { .. } => { + GenericArg::Type(cx.ty_ident(self.span, param.ident)) + } + }).collect(); // Create the type of `self`. - let self_type = cx.ty_path(cx.path_all(self.span, - false, - vec![type_ident], - self_lifetimes, - self_ty_params, - Vec::new())); + let path = cx.path_all(self.span, false, vec![type_ident], self_params, vec![]); + let self_type = cx.ty_path(path); let attr = cx.attribute(self.span, cx.meta_word(self.span, diff --git a/src/libsyntax_ext/deriving/generic/ty.rs b/src/libsyntax_ext/deriving/generic/ty.rs index 25a29694488..0b809ab585c 100644 --- a/src/libsyntax_ext/deriving/generic/ty.rs +++ b/src/libsyntax_ext/deriving/generic/ty.rs @@ -15,7 +15,7 @@ pub use self::PtrTy::*; pub use self::Ty::*; use syntax::ast; -use syntax::ast::{Expr, GenericParam, Generics, Ident, SelfKind}; +use syntax::ast::{Expr, GenericParamKind, Generics, Ident, SelfKind, GenericArg}; use syntax::ext::base::ExtCtxt; use syntax::ext::build::AstBuilder; use syntax::codemap::{respan, DUMMY_SP}; @@ -86,15 +86,20 @@ impl<'a> Path<'a> { -> ast::Path { let mut idents = self.path.iter().map(|s| cx.ident_of(*s)).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)).collect(); + let tys: Vec<P<ast::Ty>> = + self.params.iter().map(|t| t.to_ty(cx, span, self_ty, self_generics)).collect(); + let params = lt.into_iter() + .map(|lt| GenericArg::Lifetime(lt)) + .chain(tys.into_iter().map(|ty| GenericArg::Type(ty))) + .collect(); match self.kind { - PathKind::Global => cx.path_all(span, true, idents, lt, tys, Vec::new()), - PathKind::Local => cx.path_all(span, false, idents, lt, tys, Vec::new()), + PathKind::Global => cx.path_all(span, true, idents, params, Vec::new()), + PathKind::Local => cx.path_all(span, false, idents, params, Vec::new()), PathKind::Std => { let def_site = DUMMY_SP.apply_mark(cx.current_expansion.mark); idents.insert(0, Ident::new(keywords::DollarCrate.name(), def_site)); - cx.path_all(span, false, idents, lt, tys, Vec::new()) + cx.path_all(span, false, idents, params, Vec::new()) } } @@ -180,34 +185,22 @@ impl<'a> Ty<'a> { cx: &ExtCtxt, span: Span, self_ty: Ident, - self_generics: &Generics) + generics: &Generics) -> ast::Path { match *self { Self_ => { - let self_params = self_generics.params - .iter() - .filter_map(|param| match *param { - GenericParam::Type(ref ty_param) => Some(cx.ty_ident(span, ty_param.ident)), - _ => None, - }) - .collect(); - - let lifetimes: Vec<ast::Lifetime> = self_generics.params - .iter() - .filter_map(|param| match *param { - GenericParam::Lifetime(ref ld) => Some(ld.lifetime), - _ => None, - }) - .collect(); + let params: Vec<_> = generics.params.iter().map(|param| match param.kind { + GenericParamKind::Lifetime { .. } => { + GenericArg::Lifetime(ast::Lifetime { id: param.id, ident: param.ident }) + } + GenericParamKind::Type { .. } => { + GenericArg::Type(cx.ty_ident(span, param.ident)) + } + }).collect(); - cx.path_all(span, - false, - vec![self_ty], - lifetimes, - self_params, - Vec::new()) + cx.path_all(span, false, vec![self_ty], params, vec![]) } - Literal(ref p) => p.to_path(cx, span, self_ty, self_generics), + Literal(ref p) => p.to_path(cx, span, self_ty, generics), Ptr(..) => cx.span_bug(span, "pointer in a path in generic `derive`"), Tuple(..) => cx.span_bug(span, "tuple in a path in generic `derive`"), } @@ -222,17 +215,17 @@ fn mk_ty_param(cx: &ExtCtxt, bounds: &[Path], self_ident: Ident, self_generics: &Generics) - -> ast::TyParam { + -> ast::GenericParam { let bounds = bounds.iter() .map(|b| { let path = b.to_path(cx, span, self_ident, self_generics); - cx.typarambound(path) + cx.trait_bound(path) }) .collect(); cx.typaram(span, cx.ident_of(name), attrs.to_owned(), bounds, None) } -fn mk_generics(params: Vec<GenericParam>, span: Span) -> Generics { +fn mk_generics(params: Vec<ast::GenericParam>, span: Span) -> Generics { Generics { params, where_clause: ast::WhereClause { @@ -268,17 +261,14 @@ impl<'a> LifetimeBounds<'a> { .iter() .map(|&(lt, ref bounds)| { let bounds = bounds.iter() - .map(|b| cx.lifetime(span, Ident::from_str(b))) - .collect(); - GenericParam::Lifetime(cx.lifetime_def(span, Ident::from_str(lt), vec![], bounds)) + .map(|b| ast::GenericBound::Outlives(cx.lifetime(span, Ident::from_str(b)))); + cx.lifetime_def(span, Ident::from_str(lt), vec![], bounds.collect()) }) .chain(self.bounds .iter() .map(|t| { let (name, ref bounds) = *t; - GenericParam::Type(mk_ty_param( - cx, span, name, &[], &bounds, self_ty, self_generics - )) + mk_ty_param(cx, span, name, &[], &bounds, self_ty, self_generics) }) ) .collect(); diff --git a/src/libsyntax_ext/deriving/mod.rs b/src/libsyntax_ext/deriving/mod.rs index a5b348a661a..6ff385b18e8 100644 --- a/src/libsyntax_ext/deriving/mod.rs +++ b/src/libsyntax_ext/deriving/mod.rs @@ -134,9 +134,12 @@ fn hygienic_type_parameter(item: &Annotatable, base: &str) -> String { match item.node { ast::ItemKind::Struct(_, ast::Generics { ref params, .. }) | ast::ItemKind::Enum(_, ast::Generics { ref params, .. }) => { - for param in params.iter() { - if let ast::GenericParam::Type(ref ty) = *param{ - typaram.push_str(&ty.ident.as_str()); + for param in params { + match param.kind { + ast::GenericParamKind::Type { .. } => { + typaram.push_str(¶m.ident.as_str()); + } + _ => {} } } } diff --git a/src/libsyntax_ext/env.rs b/src/libsyntax_ext/env.rs index 4e1af108ab4..bbc5b03d688 100644 --- a/src/libsyntax_ext/env.rs +++ b/src/libsyntax_ext/env.rs @@ -13,7 +13,7 @@ // interface. // -use syntax::ast::{self, Ident}; +use syntax::ast::{self, Ident, GenericArg}; use syntax::ext::base::*; use syntax::ext::base; use syntax::ext::build::AstBuilder; @@ -39,12 +39,11 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, cx.expr_path(cx.path_all(sp, true, cx.std_path(&["option", "Option", "None"]), - Vec::new(), - vec![cx.ty_rptr(sp, + vec![GenericArg::Type(cx.ty_rptr(sp, cx.ty_ident(sp, Ident::from_str("str")), Some(lt), - ast::Mutability::Immutable)], - Vec::new())) + ast::Mutability::Immutable))], + vec![])) } Ok(s) => { cx.expr_call_global(sp, |
