Skip to content

Commit

Permalink
GRAM: allow unsafe modifier for module and module declaration items
Browse files Browse the repository at this point in the history
Note, `unsafe` is semantically invalid in case of module/module declaration items. The corresponding change is made to improve error message.
See the corresponding change in rustc: rust-lang/rust#75857
  • Loading branch information
Undin authored and yopox committed Feb 27, 2023
1 parent a5ed759 commit bab0144
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/main/grammars/RustParser.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,8 @@ upper ExternCrateItem ::= extern crate (identifier | self) Alias? ';' {
elementTypeFactory = "org.rust.lang.core.stubs.StubImplementationsKt.factory"
}

upper ModItem ::= mod identifier '{' InnerAttr* Items '}' {
pin = 3
upper ModItem ::= unsafe? mod identifier '{' InnerAttr* Items '}' {
pin = 4
name = ""
implements = [ "org.rust.lang.core.psi.ext.RsNameIdentifierOwner"
"org.rust.lang.core.psi.ext.RsItemElement"
Expand All @@ -693,7 +693,7 @@ upper ModItem ::= mod identifier '{' InnerAttr* Items '}' {
elementTypeFactory = "org.rust.lang.core.stubs.StubImplementationsKt.factory"
}

upper ModDeclItem ::= mod identifier ';' {
upper ModDeclItem ::= unsafe? mod identifier ';' {
pin = 'identifier' // make sure `ModDeclItem` goes **after** `ModItem`
name = ""
implements = [ "org.rust.lang.core.psi.ext.RsMandatoryReferenceElement"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ class RustParserDefinition : ParserDefinition {
/**
* Should be increased after any change of parser rules
*/
const val PARSER_VERSION: Int = LEXER_VERSION + 28
const val PARSER_VERSION: Int = LEXER_VERSION + 29
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ mod empty {

pub mod pub_mod {}
pub(crate) mod pub_crate_mod {}
unsafe mod unsafe_crate_mod {} // semantically invalid
pub unsafe mod pub_unsafe_crate_mod {} // semantically invalid

mod mod_decl;
pub mod pub_mod_decl;
pub(crate) mod pub_crate_mod_decl;
unsafe mod unsafe_mod_decl; // semantically invalid
pub unsafe mod pub_unsafe_mod_decl; // semantically invalid
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,33 @@ FILE
PsiWhiteSpace(' ')
PsiElement({)('{')
PsiElement(})('}')
PsiWhiteSpace('\n')
RsModItemImpl(MOD_ITEM)
PsiElement(unsafe)('unsafe')
PsiWhiteSpace(' ')
PsiElement(mod)('mod')
PsiWhiteSpace(' ')
PsiElement(identifier)('unsafe_crate_mod')
PsiWhiteSpace(' ')
PsiElement({)('{')
PsiElement(})('}')
PsiWhiteSpace(' ')
RsModItemImpl(MOD_ITEM)
PsiComment(<EOL_COMMENT>)('// semantically invalid')
PsiWhiteSpace('\n')
RsVisImpl(VIS)
PsiElement(pub)('pub')
PsiWhiteSpace(' ')
PsiElement(unsafe)('unsafe')
PsiWhiteSpace(' ')
PsiElement(mod)('mod')
PsiWhiteSpace(' ')
PsiElement(identifier)('pub_unsafe_crate_mod')
PsiWhiteSpace(' ')
PsiElement({)('{')
PsiElement(})('}')
PsiWhiteSpace(' ')
PsiComment(<EOL_COMMENT>)('// semantically invalid')
PsiWhiteSpace('\n\n')
RsModDeclItemImpl(MOD_DECL_ITEM)
PsiElement(mod)('mod')
Expand Down Expand Up @@ -189,3 +216,26 @@ FILE
PsiWhiteSpace(' ')
PsiElement(identifier)('pub_crate_mod_decl')
PsiElement(;)(';')
PsiWhiteSpace('\n')
RsModDeclItemImpl(MOD_DECL_ITEM)
PsiElement(unsafe)('unsafe')
PsiWhiteSpace(' ')
PsiElement(mod)('mod')
PsiWhiteSpace(' ')
PsiElement(identifier)('unsafe_mod_decl')
PsiElement(;)(';')
PsiWhiteSpace(' ')
RsModDeclItemImpl(MOD_DECL_ITEM)
PsiComment(<EOL_COMMENT>)('// semantically invalid')
PsiWhiteSpace('\n')
RsVisImpl(VIS)
PsiElement(pub)('pub')
PsiWhiteSpace(' ')
PsiElement(unsafe)('unsafe')
PsiWhiteSpace(' ')
PsiElement(mod)('mod')
PsiWhiteSpace(' ')
PsiElement(identifier)('pub_unsafe_mod_decl')
PsiElement(;)(';')
PsiWhiteSpace(' ')
PsiComment(<EOL_COMMENT>)('// semantically invalid')
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ FILE
PsiElement(})('}')
PsiWhiteSpace('\n\n')
PsiElement(unsafe)('unsafe')
PsiErrorElement:extern, fn, impl or trait expected, got 'const'
PsiErrorElement:extern, fn, impl, mod or trait expected, got 'const'
<empty list>
PsiWhiteSpace(' ')
RsFunctionImpl(FUNCTION)
Expand Down

0 comments on commit bab0144

Please sign in to comment.