Skip to content

Commit bab0144

Browse files
Undinyopox
authored andcommitted
GRAM: allow unsafe modifier for module and module declaration items
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
1 parent a5ed759 commit bab0144

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

src/main/grammars/RustParser.bnf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,8 @@ upper ExternCrateItem ::= extern crate (identifier | self) Alias? ';' {
681681
elementTypeFactory = "org.rust.lang.core.stubs.StubImplementationsKt.factory"
682682
}
683683

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

696-
upper ModDeclItem ::= mod identifier ';' {
696+
upper ModDeclItem ::= unsafe? mod identifier ';' {
697697
pin = 'identifier' // make sure `ModDeclItem` goes **after** `ModItem`
698698
name = ""
699699
implements = [ "org.rust.lang.core.psi.ext.RsMandatoryReferenceElement"

src/main/kotlin/org/rust/lang/core/parser/RustParserDefinition.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,6 @@ class RustParserDefinition : ParserDefinition {
104104
/**
105105
* Should be increased after any change of parser rules
106106
*/
107-
const val PARSER_VERSION: Int = LEXER_VERSION + 28
107+
const val PARSER_VERSION: Int = LEXER_VERSION + 29
108108
}
109109
}

src/test/resources/org/rust/lang/core/parser/fixtures/complete/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ mod empty {
1919

2020
pub mod pub_mod {}
2121
pub(crate) mod pub_crate_mod {}
22+
unsafe mod unsafe_crate_mod {} // semantically invalid
23+
pub unsafe mod pub_unsafe_crate_mod {} // semantically invalid
2224

2325
mod mod_decl;
2426
pub mod pub_mod_decl;
2527
pub(crate) mod pub_crate_mod_decl;
28+
unsafe mod unsafe_mod_decl; // semantically invalid
29+
pub unsafe mod pub_unsafe_mod_decl; // semantically invalid

src/test/resources/org/rust/lang/core/parser/fixtures/complete/mod.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,33 @@ FILE
160160
PsiWhiteSpace(' ')
161161
PsiElement({)('{')
162162
PsiElement(})('}')
163+
PsiWhiteSpace('\n')
164+
RsModItemImpl(MOD_ITEM)
165+
PsiElement(unsafe)('unsafe')
166+
PsiWhiteSpace(' ')
167+
PsiElement(mod)('mod')
168+
PsiWhiteSpace(' ')
169+
PsiElement(identifier)('unsafe_crate_mod')
170+
PsiWhiteSpace(' ')
171+
PsiElement({)('{')
172+
PsiElement(})('}')
173+
PsiWhiteSpace(' ')
174+
RsModItemImpl(MOD_ITEM)
175+
PsiComment(<EOL_COMMENT>)('// semantically invalid')
176+
PsiWhiteSpace('\n')
177+
RsVisImpl(VIS)
178+
PsiElement(pub)('pub')
179+
PsiWhiteSpace(' ')
180+
PsiElement(unsafe)('unsafe')
181+
PsiWhiteSpace(' ')
182+
PsiElement(mod)('mod')
183+
PsiWhiteSpace(' ')
184+
PsiElement(identifier)('pub_unsafe_crate_mod')
185+
PsiWhiteSpace(' ')
186+
PsiElement({)('{')
187+
PsiElement(})('}')
188+
PsiWhiteSpace(' ')
189+
PsiComment(<EOL_COMMENT>)('// semantically invalid')
163190
PsiWhiteSpace('\n\n')
164191
RsModDeclItemImpl(MOD_DECL_ITEM)
165192
PsiElement(mod)('mod')
@@ -189,3 +216,26 @@ FILE
189216
PsiWhiteSpace(' ')
190217
PsiElement(identifier)('pub_crate_mod_decl')
191218
PsiElement(;)(';')
219+
PsiWhiteSpace('\n')
220+
RsModDeclItemImpl(MOD_DECL_ITEM)
221+
PsiElement(unsafe)('unsafe')
222+
PsiWhiteSpace(' ')
223+
PsiElement(mod)('mod')
224+
PsiWhiteSpace(' ')
225+
PsiElement(identifier)('unsafe_mod_decl')
226+
PsiElement(;)(';')
227+
PsiWhiteSpace(' ')
228+
RsModDeclItemImpl(MOD_DECL_ITEM)
229+
PsiComment(<EOL_COMMENT>)('// semantically invalid')
230+
PsiWhiteSpace('\n')
231+
RsVisImpl(VIS)
232+
PsiElement(pub)('pub')
233+
PsiWhiteSpace(' ')
234+
PsiElement(unsafe)('unsafe')
235+
PsiWhiteSpace(' ')
236+
PsiElement(mod)('mod')
237+
PsiWhiteSpace(' ')
238+
PsiElement(identifier)('pub_unsafe_mod_decl')
239+
PsiElement(;)(';')
240+
PsiWhiteSpace(' ')
241+
PsiComment(<EOL_COMMENT>)('// semantically invalid')

src/test/resources/org/rust/lang/core/parser/fixtures/partial/fn.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ FILE
105105
PsiElement(})('}')
106106
PsiWhiteSpace('\n\n')
107107
PsiElement(unsafe)('unsafe')
108-
PsiErrorElement:extern, fn, impl or trait expected, got 'const'
108+
PsiErrorElement:extern, fn, impl, mod or trait expected, got 'const'
109109
<empty list>
110110
PsiWhiteSpace(' ')
111111
RsFunctionImpl(FUNCTION)

0 commit comments

Comments
 (0)