Merge pull request #3441 from rchaser53/const-generics

implement for const generics
This commit is contained in:
Seiichi Uchida 2019-03-14 22:50:20 +09:00 committed by GitHub
commit 331a0500ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 7 deletions

View File

@ -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

View File

@ -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(),
}
}
}

View File

@ -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<String> {
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<String> {
format_expr(&self.value, ExprType::SubExpression, context, shape)
}
}
impl Rewrite for ast::Lifetime {
fn rewrite(&self, context: &RewriteContext<'_>, _: Shape) -> Option<String> {
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)?)

View File

@ -0,0 +1,32 @@
struct Message {
field2: Vec<
"MessageEntity"
>,
field3: Vec<
1
>,
field4: Vec<
2 , 3
>,
}
struct RectangularArray<T, const WIDTH: usize, const HEIGHT: usize> {
array: [[T; WIDTH]; HEIGHT],
}
fn main() {
const X: usize = 7;
let x: RectangularArray<i32, 2, 4>;
let y: RectangularArray<i32, X, {2
* 2} >;
}
fn foo<const X: usize>() {
const Y: usize = X * 2;
static Z: (usize, usize) = (X, X);
struct Foo([i32; X]);
}
type Foo<const N: usize> = [i32; N + 1];

View File

@ -0,0 +1,24 @@
struct Message {
field2: Vec<"MessageEntity">,
field3: Vec<1>,
field4: Vec<2, 3>,
}
struct RectangularArray<T, const WIDTH: usize, const HEIGHT: usize> {
array: [[T; WIDTH]; HEIGHT],
}
fn main() {
const X: usize = 7;
let x: RectangularArray<i32, 2, 4>;
let y: RectangularArray<i32, X, { 2 * 2 }>;
}
fn foo<const X: usize>() {
const Y: usize = X * 2;
static Z: (usize, usize) = (X, X);
struct Foo([i32; X]);
}
type Foo<const N: usize> = [i32; N + 1];