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

[Rust] Add + use<> syntax #4139

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 26 additions & 10 deletions Rust/Rust.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ contexts:
types:
# matches stray identifiers as a types because a type is expected
- include: comments
- include: impl-types
- include: return-type
- match: '&'
scope: keyword.operator.rust
Expand All @@ -590,6 +589,7 @@ contexts:
- include: pattern-param
- include: dyn-types
- include: lifetime
- include: impl-trait
- match: (?={{identifier}}::)
push: type-path
- match: (?=::)
Expand Down Expand Up @@ -710,15 +710,6 @@ contexts:
- match: '\b(?:r#)?_*{{type_identifier}}'
scope: storage.type.rust

impl-types:
- match: '\bimpl\b'
scope: storage.type.impl.rust
push:
- include: comments
- include: impl-generic
- match: '(?=\S)'
pop: true

dyn-types:
- match: '\bdyn\b(?=\s*(?:\(|{{lifetime}}|{{identifier}}))'
scope: storage.type.trait.rust
Expand Down Expand Up @@ -803,6 +794,31 @@ contexts:
- match: '[-+%/*]'
scope: keyword.operator.arithmetic.rust

impl-trait:
- match: '\bimpl\b'
scope: storage.type.impl.rust
push:
- match: '\buse\b'
scope: keyword.other.rust
push:
- match: '<'
scope: punctuation.definition.generic.begin.rust
set:
- match: '>'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to add a meta scope to generics.

Suggested change
- match: '>'
- match: '>'
- meta_scope: meta.generic.rust

scope: punctuation.definition.generic.end.rust
pop: true
- include: lifetime
- match: ','
scope: punctuation.separator.rust
- match: \b(Self)\b
scope: keyword.other.rust
- match: '{{identifier}}'
Comment on lines +813 to +815
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self is already defined as storage type in no-path-type-names context and identifiers should also receive valuable scope names. Types are currently unscoped as other identifiers are.

I'd therefore suggest to include ordinary identifiers context, which would also - even if maybe not required - handle fully qualified identifiers.

Suggested change
- match: \b(Self)\b
scope: keyword.other.rust
- match: '{{identifier}}'
- include: identifiers

Copy link
Collaborator

@FichteFoll FichteFoll Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's not be 100% accurate either, I'd rather include types here since that is meant to be used in situations where types are expected. It matches lifetimes as well as stray identifiers and commonly known stdlib types. Alternatively, you could refactor the types context a bit and only include the relevant things for UseBoundGenericArg here, which are lifetimes and (pathed) identifiers (with a type scope).

Commas still need to be matched separately, though.

Copy link
Collaborator

@FichteFoll FichteFoll Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, reading the spec properly, pathed identifiers aren't supported and it is also unlikely for built-in names to be used since this is supposed to refer to generics and a generic with a built-in name would shadow it.

In that case, please instead simply include all-generic-type-names.

- match: '(?=\S)'
pop: true
- include: types
- match: '(?=\S)'
pop: true

##[ STRUCTS ]###############################################################

struct-identifier:
Expand Down
90 changes: 89 additions & 1 deletion Rust/tests/syntax_test_generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,96 @@ fn function<const N: u16>() {
// ^^^ storage.type.numeric
let b: Byte<b'a'>;
// ^^^^^^ meta.function meta.block meta.generic
// ^^^^ string.quoted.single.rust
// ^^^^ string.quoted.single
// ^ storage.type.string
// ^ punctuation.definition.string.begin
// ^ punctuation.definition.string.end
}

fn impl_trait_return_use_bound<'a>() -> impl for<'b> Trait1<Item = impl Trait2<'a> + use<'a>> {}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function meta.function.return-type
// ^^ punctuation.separator
// ^^^^ storage.type.impl
// ^^^ keyword.other
// ^ meta.generic punctuation.definition.generic.begin
// ^ meta.generic punctuation.definition.generic.end
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.generic
// ^ meta.generic punctuation.definition.generic.begin
// ^ meta.generic keyword.operator
// ^^^^ meta.generic storage.type.impl
// ^^^^ meta.generic meta.generic
// ^ punctuation.definition.generic.begin
// ^^ storage.modifier.lifetime
// ^ punctuation.definition.generic.end
// ^ keyword.operator
// ^^^ keyword.other
// ^^^^ meta.generic
// ^ punctuation.definition.generic.begin
// ^^ storage.modifier.lifetime
// ^ punctuation.definition.generic.end
// ^ punctuation.definition.generic.end

fn impl_trait_use<'a, foo>() -> impl Trait1 + use<'a, Self, foo> {}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function meta.function.return-type
// ^ keyword.operator
// ^^^ keyword.other
// ^ punctuation.definition.generic.begin
// ^^ storage.modifier.lifetime
// ^ punctuation.separator
// ^^^^ keyword.other
// ^ punctuation.separator
// ^ punctuation.definition.generic.end

fn impl_trait_return1<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function meta.function.return-type
// ^^ punctuation.separator
// ^^^^ storage.type.impl
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.generic
// ^ punctuation.definition.generic.begin
// ^ keyword.operator
// ^^ storage.modifier.lifetime
// ^^ storage.type
// ^ punctuation.separator
// ^ keyword.operator
// ^^^^ storage.type.impl
// ^^^^^ support.type
// ^ keyword.operator
// ^^ storage.modifier.lifetime
// ^ punctuation.definition.generic.end
fn impl_trait_return2() -> impl Debug + 'a {}
// ^^^^^^^^^^^^^^^^^^ meta.function meta.function.return-type
// ^^^^ storage.type.impl
// ^ keyword.operator
// ^^ storage.modifier.lifetime

fn impl_trait_param(x: impl FnOnce(&[u8]) -> &[u8]) {}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function meta.function.parameters
// ^^^^ storage.type.impl
// ^^^^^^ support.type
// ^ meta.group punctuation.section.group.begin
// ^ meta.group keyword.operator
// ^ meta.group punctuation.section.group.begin
// ^^ meta.group storage.type
// ^^ meta.group punctuation.section.group.end
// ^^^^^^^^ meta.function meta.function.parameters meta.function.return-type
// ^^ punctuation.separator
// ^ meta.function.parameters keyword.operator
// ^ meta.function.parameters punctuation.section.group.begin
// ^^ meta.function.parameters storage.type
// ^ meta.function.parameters punctuation.section.group.end


fn impl_trait_with_plus() -> impl Iterator<Item = hir::GenericParam<'hir>> + Captures<'a> + Captures<'s> {}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function meta.function.return-type
// ^^^^ storage.type.impl
// ^^^^^^^^ support.type
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.generic
// ^ keyword.operator
// ^^^^ storage.modifier.lifetime
// ^ keyword.operator
// ^^^^ meta.generic
// ^^ storage.modifier.lifetime
// ^ keyword.operator
// ^^^^ meta.generic
// ^^ storage.modifier.lifetime

Loading