Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax: Add support for const generics. #451

Merged
merged 1 commit into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions RustEnhanced.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,6 @@ contexts:
generic-angles-contents:
- include: comments
- include: attribute
- include: type-slice-or-array
- match: '(?=>)'
pop: true
- match: '<'
Expand All @@ -544,7 +543,17 @@ contexts:
scope: punctuation.definition.generic.end.rust
pop: true
- include: generic-angles-contents
- include: bool
# byte must be before type-any-identifier since it doesn't know about byte tokens
- include: byte
- include: type-any-identifier
# char must be after type-any-identifier to deal with conflict with lifetimes
- include: char
# Handle negative integers (technically unary negative expression with a literal expression)
- match: '-'
scope: keyword.operator.arithmetic.rust
- include: integers
- include: block
- match: '{{identifier}}'
- match: ':|,'
scope: punctuation.separator.rust
Expand Down Expand Up @@ -1501,11 +1510,14 @@ contexts:
- match: '[:;,]'
scope: punctuation.separator.rust

bool:
- match: \b(true|false)\b
scope: constant.language.rust

keywords:
# All keywords. Note in `statements` some of these are superseded by more
# specific rules.
- match: \b(true|false)\b
scope: constant.language.rust
- include: bool

- match: \b(let|const|static)\b
scope: storage.type.rust
Expand Down
94 changes: 94 additions & 0 deletions tests/syntax-rust/syntax_test_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,97 @@ fn factory() -> Box<Fn(i32) -> i32> {
// ^^^ storage.type
Box::new(|x| x + 1)
}

// Const generics.
trait Foo<const N: usize> {
// ^^^^^^^^^^^^^^^^ meta.trait meta.generic
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier
// ^ meta.trait meta.generic punctuation.separator
// ^^^^^ storage.type
// ^ punctuation.definition.generic.end
fn method<const M: usize>(&mut self, arr: [[u8; M]; N]);
// ^^^^^^^^^^^^^^^^ meta.generic
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier
// ^ punctuation.separator
// ^^^^^ storage.type
// ^ punctuation.definition.generic.end
}

struct Bar<T, const N: usize> {
// ^^^^^^^^^^^^^^^^^^^ meta.struct meta.generic
// ^ punctuation.definition.generic.begin
// ^ punctuation.separator
// ^^^^^ storage.modifier
// ^ punctuation.separator
// ^^^^^ storage.type
// ^ punctuation.definition.generic.end
inner: [T; N],
}

impl<const N: usize> Foo<N> for Bar<u8, N> {
// ^^^^^^^^^^^^^^^^ meta.impl meta.generic
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier
// ^ punctuation.separator
// ^^^^^ storage.type
// ^ punctuation.definition.generic.end
fn method<const M: usize>(&mut self, arr: [[u8; M]; N]) {}
}

struct Bool<const N: bool>;
// ^^^^^^^^^^^^^^^ meta.struct meta.generic
// ^ punctuation.definition.generic.begin
// ^^^^^ storage.modifier
// ^ punctuation.separator
// ^^^^ storage.type
// ^ punctuation.definition.generic.end
struct Char<const N: char>;
struct Int<const N: i32>;
struct Byte<const N: u8>;

fn function<const N: u16>() {
const fn foo(x: bool) -> usize { 2 }
let x: Bar<i32, 1> = Bar { inner: [1; 1] };
// ^^^^^^^^ meta.function meta.block meta.generic
// ^ meta.function meta.block meta.generic punctuation.definition.generic.begin
// ^^^ meta.function meta.block meta.generic storage.type
// ^ meta.function meta.block meta.generic punctuation.separator
// ^ meta.function meta.block meta.generic constant.numeric.integer.decimal
// ^ meta.function meta.block meta.generic punctuation.definition.generic.end
let y: Bar<i32, { foo(1 > 2) / 2 }> = Bar { inner: [1; 1] };
// ^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function meta.block meta.generic
// ^ punctuation.definition.generic.begin
// ^^^^^^^^^^^^^^^^^^ meta.block
// ^^^ support.function
// ^ meta.group punctuation.section.group.begin
// ^ keyword.operator.comparison
// ^ punctuation.section.group.end
// ^ keyword.operator.arithmetic
// ^ constant.numeric.integer.decimal
// ^ punctuation.section.block.end
// ^ punctuation.definition.generic.end
let b: Bool<true>;
// ^^^^ meta.function meta.block meta.generic constant.language
let c: Char<'∂'>;
// ^^^ meta.function meta.block meta.generic string.quoted.single
// ^ punctuation.definition.string.begin
// ^ punctuation.definition.string.end
let i: Int<-1>;
// ^^^^ meta.function meta.block meta.generic
// ^ keyword.operator.arithmetic
// ^ constant.numeric.integer.decimal
let i: Int<0b1011>;
// ^^^^^^ meta.function meta.block meta.generic constant.numeric.integer.binary
let i: Int<4i32>;
// ^^^^^^ meta.function meta.block meta.generic
// ^ constant.numeric.integer.decimal
// ^^^ storage.type.numeric
let b: Byte<b'a'>;
// ^^^^^^ meta.function meta.block meta.generic
// ^^^^ string.quoted.single.rust
// ^ storage.type.string
// ^ punctuation.definition.string.begin
// ^ punctuation.definition.string.end
}