Skip to content

Commit 331a050

Browse files
authored
Merge pull request rust-lang#3441 from rchaser53/const-generics
implement for const generics
2 parents be5ffc3 + f0c861b commit 331a050

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

src/lists.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,12 @@ pub fn extract_post_comment(
622622
} else {
623623
post_snippet
624624
};
625-
626-
if !post_snippet_trimmed.is_empty() {
625+
// FIXME(#3441): post_snippet includes 'const' now
626+
// it should not include here
627+
let removed_newline_snippet = post_snippet_trimmed.trim();
628+
if !post_snippet_trimmed.is_empty()
629+
&& (removed_newline_snippet.starts_with("//") || removed_newline_snippet.starts_with("/*"))
630+
{
627631
Some(post_snippet_trimmed.to_owned())
628632
} else {
629633
None

src/spanned.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl Spanned for ast::GenericArg {
168168
match *self {
169169
ast::GenericArg::Lifetime(ref lt) => lt.ident.span,
170170
ast::GenericArg::Type(ref ty) => ty.span(),
171-
ast::GenericArg::Const(..) => unreachable!(), // FIXME(#3336)
171+
ast::GenericArg::Const(ref _const) => _const.value.span(),
172172
}
173173
}
174174
}

src/types.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syntax::symbol::keywords;
77

88
use crate::config::lists::*;
99
use crate::config::{IndentStyle, TypeDensity};
10-
use crate::expr::{rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix};
10+
use crate::expr::{format_expr, rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ExprType};
1111
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
1212
use crate::macros::{rewrite_macro, MacroPosition};
1313
use crate::overflow;
@@ -132,6 +132,7 @@ where
132132

133133
#[derive(Debug)]
134134
pub enum SegmentParam<'a> {
135+
Const(&'a ast::AnonConst),
135136
LifeTime(&'a ast::Lifetime),
136137
Type(&'a ast::Ty),
137138
Binding(&'a ast::TypeBinding),
@@ -142,14 +143,15 @@ impl<'a> SegmentParam<'a> {
142143
match arg {
143144
ast::GenericArg::Lifetime(ref lt) => SegmentParam::LifeTime(lt),
144145
ast::GenericArg::Type(ref ty) => SegmentParam::Type(ty),
145-
ast::GenericArg::Const(..) => unreachable!(), // FIXME(#3336)
146+
ast::GenericArg::Const(const_) => SegmentParam::Const(const_),
146147
}
147148
}
148149
}
149150

150151
impl<'a> Spanned for SegmentParam<'a> {
151152
fn span(&self) -> Span {
152153
match *self {
154+
SegmentParam::Const(const_) => const_.value.span,
153155
SegmentParam::LifeTime(lt) => lt.ident.span,
154156
SegmentParam::Type(ty) => ty.span,
155157
SegmentParam::Binding(binding) => binding.span,
@@ -160,6 +162,7 @@ impl<'a> Spanned for SegmentParam<'a> {
160162
impl<'a> Rewrite for SegmentParam<'a> {
161163
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
162164
match *self {
165+
SegmentParam::Const(const_) => const_.rewrite(context, shape),
163166
SegmentParam::LifeTime(lt) => lt.rewrite(context, shape),
164167
SegmentParam::Type(ty) => ty.rewrite(context, shape),
165168
SegmentParam::Binding(binding) => {
@@ -454,7 +457,7 @@ impl Rewrite for ast::GenericArg {
454457
match *self {
455458
ast::GenericArg::Lifetime(ref lt) => lt.rewrite(context, shape),
456459
ast::GenericArg::Type(ref ty) => ty.rewrite(context, shape),
457-
ast::GenericArg::Const(..) => unreachable!(), // FIXME(#3336)
460+
ast::GenericArg::Const(ref const_) => const_.rewrite(context, shape),
458461
}
459462
}
460463
}
@@ -482,6 +485,12 @@ fn rewrite_bounded_lifetime(
482485
}
483486
}
484487

488+
impl Rewrite for ast::AnonConst {
489+
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
490+
format_expr(&self.value, ExprType::SubExpression, context, shape)
491+
}
492+
}
493+
485494
impl Rewrite for ast::Lifetime {
486495
fn rewrite(&self, context: &RewriteContext<'_>, _: Shape) -> Option<String> {
487496
Some(rewrite_ident(context, self.ident).to_owned())
@@ -525,7 +534,16 @@ impl Rewrite for ast::GenericParam {
525534
Some(ref rw) if !rw.is_empty() => result.push_str(&format!("{} ", rw)),
526535
_ => (),
527536
}
528-
result.push_str(rewrite_ident(context, self.ident));
537+
538+
if let syntax::ast::GenericParamKind::Const { ref ty } = &self.kind {
539+
result.push_str("const ");
540+
result.push_str(rewrite_ident(context, self.ident));
541+
result.push_str(": ");
542+
result.push_str(&ty.rewrite(context, shape)?);
543+
} else {
544+
result.push_str(rewrite_ident(context, self.ident));
545+
}
546+
529547
if !self.bounds.is_empty() {
530548
result.push_str(type_bound_colon(context));
531549
result.push_str(&self.bounds.rewrite(context, shape)?)

tests/source/const_generics.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
struct Message {
2+
field2: Vec<
3+
"MessageEntity"
4+
>,
5+
field3: Vec<
6+
1
7+
>,
8+
field4: Vec<
9+
2 , 3
10+
>,
11+
12+
}
13+
14+
struct RectangularArray<T, const WIDTH: usize, const HEIGHT: usize> {
15+
array: [[T; WIDTH]; HEIGHT],
16+
}
17+
18+
fn main() {
19+
const X: usize = 7;
20+
let x: RectangularArray<i32, 2, 4>;
21+
let y: RectangularArray<i32, X, {2
22+
* 2} >;
23+
}
24+
25+
fn foo<const X: usize>() {
26+
const Y: usize = X * 2;
27+
static Z: (usize, usize) = (X, X);
28+
29+
struct Foo([i32; X]);
30+
}
31+
32+
type Foo<const N: usize> = [i32; N + 1];

tests/target/const_generics.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
struct Message {
2+
field2: Vec<"MessageEntity">,
3+
field3: Vec<1>,
4+
field4: Vec<2, 3>,
5+
}
6+
7+
struct RectangularArray<T, const WIDTH: usize, const HEIGHT: usize> {
8+
array: [[T; WIDTH]; HEIGHT],
9+
}
10+
11+
fn main() {
12+
const X: usize = 7;
13+
let x: RectangularArray<i32, 2, 4>;
14+
let y: RectangularArray<i32, X, { 2 * 2 }>;
15+
}
16+
17+
fn foo<const X: usize>() {
18+
const Y: usize = X * 2;
19+
static Z: (usize, usize) = (X, X);
20+
21+
struct Foo([i32; X]);
22+
}
23+
24+
type Foo<const N: usize> = [i32; N + 1];

0 commit comments

Comments
 (0)