Skip to content

Commit abdac4e

Browse files
authored
Merge pull request #451 from ehuss/const-generics
Syntax: Add support for const generics.
2 parents fe92d59 + 340201e commit abdac4e

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed

RustEnhanced.sublime-syntax

+15-3
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ contexts:
534534
generic-angles-contents:
535535
- include: comments
536536
- include: attribute
537-
- include: type-slice-or-array
538537
- match: '(?=>)'
539538
pop: true
540539
- match: '<'
@@ -544,7 +543,17 @@ contexts:
544543
scope: punctuation.definition.generic.end.rust
545544
pop: true
546545
- include: generic-angles-contents
546+
- include: bool
547+
# byte must be before type-any-identifier since it doesn't know about byte tokens
548+
- include: byte
547549
- include: type-any-identifier
550+
# char must be after type-any-identifier to deal with conflict with lifetimes
551+
- include: char
552+
# Handle negative integers (technically unary negative expression with a literal expression)
553+
- match: '-'
554+
scope: keyword.operator.arithmetic.rust
555+
- include: integers
556+
- include: block
548557
- match: '{{identifier}}'
549558
- match: ':|,'
550559
scope: punctuation.separator.rust
@@ -1501,11 +1510,14 @@ contexts:
15011510
- match: '[:;,]'
15021511
scope: punctuation.separator.rust
15031512

1513+
bool:
1514+
- match: \b(true|false)\b
1515+
scope: constant.language.rust
1516+
15041517
keywords:
15051518
# All keywords. Note in `statements` some of these are superseded by more
15061519
# specific rules.
1507-
- match: \b(true|false)\b
1508-
scope: constant.language.rust
1520+
- include: bool
15091521

15101522
- match: \b(let|const|static)\b
15111523
scope: storage.type.rust

tests/syntax-rust/syntax_test_generics.rs

+94
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,97 @@ fn factory() -> Box<Fn(i32) -> i32> {
350350
// ^^^ storage.type
351351
Box::new(|x| x + 1)
352352
}
353+
354+
// Const generics.
355+
trait Foo<const N: usize> {
356+
// ^^^^^^^^^^^^^^^^ meta.trait meta.generic
357+
// ^ punctuation.definition.generic.begin
358+
// ^^^^^ storage.modifier
359+
// ^ meta.trait meta.generic punctuation.separator
360+
// ^^^^^ storage.type
361+
// ^ punctuation.definition.generic.end
362+
fn method<const M: usize>(&mut self, arr: [[u8; M]; N]);
363+
// ^^^^^^^^^^^^^^^^ meta.generic
364+
// ^ punctuation.definition.generic.begin
365+
// ^^^^^ storage.modifier
366+
// ^ punctuation.separator
367+
// ^^^^^ storage.type
368+
// ^ punctuation.definition.generic.end
369+
}
370+
371+
struct Bar<T, const N: usize> {
372+
// ^^^^^^^^^^^^^^^^^^^ meta.struct meta.generic
373+
// ^ punctuation.definition.generic.begin
374+
// ^ punctuation.separator
375+
// ^^^^^ storage.modifier
376+
// ^ punctuation.separator
377+
// ^^^^^ storage.type
378+
// ^ punctuation.definition.generic.end
379+
inner: [T; N],
380+
}
381+
382+
impl<const N: usize> Foo<N> for Bar<u8, N> {
383+
// ^^^^^^^^^^^^^^^^ meta.impl meta.generic
384+
// ^ punctuation.definition.generic.begin
385+
// ^^^^^ storage.modifier
386+
// ^ punctuation.separator
387+
// ^^^^^ storage.type
388+
// ^ punctuation.definition.generic.end
389+
fn method<const M: usize>(&mut self, arr: [[u8; M]; N]) {}
390+
}
391+
392+
struct Bool<const N: bool>;
393+
// ^^^^^^^^^^^^^^^ meta.struct meta.generic
394+
// ^ punctuation.definition.generic.begin
395+
// ^^^^^ storage.modifier
396+
// ^ punctuation.separator
397+
// ^^^^ storage.type
398+
// ^ punctuation.definition.generic.end
399+
struct Char<const N: char>;
400+
struct Int<const N: i32>;
401+
struct Byte<const N: u8>;
402+
403+
fn function<const N: u16>() {
404+
const fn foo(x: bool) -> usize { 2 }
405+
let x: Bar<i32, 1> = Bar { inner: [1; 1] };
406+
// ^^^^^^^^ meta.function meta.block meta.generic
407+
// ^ meta.function meta.block meta.generic punctuation.definition.generic.begin
408+
// ^^^ meta.function meta.block meta.generic storage.type
409+
// ^ meta.function meta.block meta.generic punctuation.separator
410+
// ^ meta.function meta.block meta.generic constant.numeric.integer.decimal
411+
// ^ meta.function meta.block meta.generic punctuation.definition.generic.end
412+
let y: Bar<i32, { foo(1 > 2) / 2 }> = Bar { inner: [1; 1] };
413+
// ^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function meta.block meta.generic
414+
// ^ punctuation.definition.generic.begin
415+
// ^^^^^^^^^^^^^^^^^^ meta.block
416+
// ^^^ support.function
417+
// ^ meta.group punctuation.section.group.begin
418+
// ^ keyword.operator.comparison
419+
// ^ punctuation.section.group.end
420+
// ^ keyword.operator.arithmetic
421+
// ^ constant.numeric.integer.decimal
422+
// ^ punctuation.section.block.end
423+
// ^ punctuation.definition.generic.end
424+
let b: Bool<true>;
425+
// ^^^^ meta.function meta.block meta.generic constant.language
426+
let c: Char<'∂'>;
427+
// ^^^ meta.function meta.block meta.generic string.quoted.single
428+
// ^ punctuation.definition.string.begin
429+
// ^ punctuation.definition.string.end
430+
let i: Int<-1>;
431+
// ^^^^ meta.function meta.block meta.generic
432+
// ^ keyword.operator.arithmetic
433+
// ^ constant.numeric.integer.decimal
434+
let i: Int<0b1011>;
435+
// ^^^^^^ meta.function meta.block meta.generic constant.numeric.integer.binary
436+
let i: Int<4i32>;
437+
// ^^^^^^ meta.function meta.block meta.generic
438+
// ^ constant.numeric.integer.decimal
439+
// ^^^ storage.type.numeric
440+
let b: Byte<b'a'>;
441+
// ^^^^^^ meta.function meta.block meta.generic
442+
// ^^^^ string.quoted.single.rust
443+
// ^ storage.type.string
444+
// ^ punctuation.definition.string.begin
445+
// ^ punctuation.definition.string.end
446+
}

0 commit comments

Comments
 (0)