From f0c861bfa95a497bcb48f81276136f38627fcbd1 Mon Sep 17 00:00:00 2001 From: rchaser53 Date: Mon, 11 Mar 2019 22:24:49 +0900 Subject: [PATCH] implement for const generics --- src/lists.rs | 8 ++++++-- src/spanned.rs | 2 +- src/types.rs | 26 ++++++++++++++++++++++---- tests/source/const_generics.rs | 32 ++++++++++++++++++++++++++++++++ tests/target/const_generics.rs | 24 ++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 tests/source/const_generics.rs create mode 100644 tests/target/const_generics.rs diff --git a/src/lists.rs b/src/lists.rs index cb404123af0..14b799622c9 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -622,8 +622,12 @@ pub fn extract_post_comment( } else { post_snippet }; - - if !post_snippet_trimmed.is_empty() { + // FIXME(#3441): post_snippet includes 'const' now + // it should not include here + let removed_newline_snippet = post_snippet_trimmed.trim(); + if !post_snippet_trimmed.is_empty() + && (removed_newline_snippet.starts_with("//") || removed_newline_snippet.starts_with("/*")) + { Some(post_snippet_trimmed.to_owned()) } else { None diff --git a/src/spanned.rs b/src/spanned.rs index 068a3b8b4f4..118b84526e7 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -168,7 +168,7 @@ impl Spanned for ast::GenericArg { match *self { ast::GenericArg::Lifetime(ref lt) => lt.ident.span, ast::GenericArg::Type(ref ty) => ty.span(), - ast::GenericArg::Const(..) => unreachable!(), // FIXME(#3336) + ast::GenericArg::Const(ref _const) => _const.value.span(), } } } diff --git a/src/types.rs b/src/types.rs index e2ddc49bf86..f8193e1c9f2 100644 --- a/src/types.rs +++ b/src/types.rs @@ -7,7 +7,7 @@ use syntax::symbol::keywords; use crate::config::lists::*; use crate::config::{IndentStyle, TypeDensity}; -use crate::expr::{rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix}; +use crate::expr::{format_expr, rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ExprType}; use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator}; use crate::macros::{rewrite_macro, MacroPosition}; use crate::overflow; @@ -132,6 +132,7 @@ where #[derive(Debug)] pub enum SegmentParam<'a> { + Const(&'a ast::AnonConst), LifeTime(&'a ast::Lifetime), Type(&'a ast::Ty), Binding(&'a ast::TypeBinding), @@ -142,7 +143,7 @@ impl<'a> SegmentParam<'a> { match arg { ast::GenericArg::Lifetime(ref lt) => SegmentParam::LifeTime(lt), ast::GenericArg::Type(ref ty) => SegmentParam::Type(ty), - ast::GenericArg::Const(..) => unreachable!(), // FIXME(#3336) + ast::GenericArg::Const(const_) => SegmentParam::Const(const_), } } } @@ -150,6 +151,7 @@ impl<'a> SegmentParam<'a> { impl<'a> Spanned for SegmentParam<'a> { fn span(&self) -> Span { match *self { + SegmentParam::Const(const_) => const_.value.span, SegmentParam::LifeTime(lt) => lt.ident.span, SegmentParam::Type(ty) => ty.span, SegmentParam::Binding(binding) => binding.span, @@ -160,6 +162,7 @@ impl<'a> Spanned for SegmentParam<'a> { impl<'a> Rewrite for SegmentParam<'a> { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { match *self { + SegmentParam::Const(const_) => const_.rewrite(context, shape), SegmentParam::LifeTime(lt) => lt.rewrite(context, shape), SegmentParam::Type(ty) => ty.rewrite(context, shape), SegmentParam::Binding(binding) => { @@ -454,7 +457,7 @@ impl Rewrite for ast::GenericArg { match *self { ast::GenericArg::Lifetime(ref lt) => lt.rewrite(context, shape), ast::GenericArg::Type(ref ty) => ty.rewrite(context, shape), - ast::GenericArg::Const(..) => unreachable!(), // FIXME(#3336) + ast::GenericArg::Const(ref const_) => const_.rewrite(context, shape), } } } @@ -482,6 +485,12 @@ fn rewrite_bounded_lifetime( } } +impl Rewrite for ast::AnonConst { + fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { + format_expr(&self.value, ExprType::SubExpression, context, shape) + } +} + impl Rewrite for ast::Lifetime { fn rewrite(&self, context: &RewriteContext<'_>, _: Shape) -> Option { Some(rewrite_ident(context, self.ident).to_owned()) @@ -525,7 +534,16 @@ impl Rewrite for ast::GenericParam { Some(ref rw) if !rw.is_empty() => result.push_str(&format!("{} ", rw)), _ => (), } - result.push_str(rewrite_ident(context, self.ident)); + + if let syntax::ast::GenericParamKind::Const { ref ty } = &self.kind { + result.push_str("const "); + result.push_str(rewrite_ident(context, self.ident)); + result.push_str(": "); + result.push_str(&ty.rewrite(context, shape)?); + } else { + result.push_str(rewrite_ident(context, self.ident)); + } + if !self.bounds.is_empty() { result.push_str(type_bound_colon(context)); result.push_str(&self.bounds.rewrite(context, shape)?) diff --git a/tests/source/const_generics.rs b/tests/source/const_generics.rs new file mode 100644 index 00000000000..420810b92ea --- /dev/null +++ b/tests/source/const_generics.rs @@ -0,0 +1,32 @@ +struct Message { + field2: Vec< + "MessageEntity" + >, + field3: Vec< + 1 + >, + field4: Vec< + 2 , 3 + >, + +} + +struct RectangularArray { + array: [[T; WIDTH]; HEIGHT], +} + +fn main() { + const X: usize = 7; + let x: RectangularArray; + let y: RectangularArray; +} + +fn foo() { + const Y: usize = X * 2; + static Z: (usize, usize) = (X, X); + + struct Foo([i32; X]); +} + +type Foo = [i32; N + 1]; diff --git a/tests/target/const_generics.rs b/tests/target/const_generics.rs new file mode 100644 index 00000000000..f60b7eb0880 --- /dev/null +++ b/tests/target/const_generics.rs @@ -0,0 +1,24 @@ +struct Message { + field2: Vec<"MessageEntity">, + field3: Vec<1>, + field4: Vec<2, 3>, +} + +struct RectangularArray { + array: [[T; WIDTH]; HEIGHT], +} + +fn main() { + const X: usize = 7; + let x: RectangularArray; + let y: RectangularArray; +} + +fn foo() { + const Y: usize = X * 2; + static Z: (usize, usize) = (X, X); + + struct Foo([i32; X]); +} + +type Foo = [i32; N + 1];