Skip to content

Commit 224387a

Browse files
bors[bot]dtolnay
andauthored
Merge #6606
6606: Parse unsafe extern block r=lnicola a=dtolnay `unsafe extern` block is parsed successfully by rustc, which means it is usable in attribute macro input. https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6f805556f176d082d87255957f16b5f6 ```rust #[cfg(parse)] unsafe extern "C++" { fn demo(); } ``` ```diff [email protected] - [email protected] + [email protected] [email protected] [email protected] "#" [email protected] "[" [email protected] [email protected] [email protected] [email protected] "cfg" [email protected] [email protected] "(" [email protected] "parse" [email protected] ")" [email protected] "]" [email protected] "\n" [email protected] "unsafe" [email protected] " " [email protected] [email protected] "extern" [email protected] " " [email protected] "\"C++\"" - [email protected] " " - [email protected] - [email protected] "{" - [email protected] "\n " - [email protected] - [email protected] "fn" - [email protected] " " - [email protected] - [email protected] "demo" - [email protected] - [email protected] "(" - [email protected] ")" - [email protected] ";" - [email protected] "\n" - [email protected] "}" + [email protected] " " + [email protected] + [email protected] "{" + [email protected] "\n " + [email protected] + [email protected] "fn" + [email protected] " " + [email protected] + [email protected] "demo" + [email protected] + [email protected] "(" + [email protected] ")" + [email protected] ";" + [email protected] "\n" + [email protected] "}" ``` This is of interest for https://github.com/dtolnay/cxx. Co-authored-by: David Tolnay <[email protected]>
2 parents cadf0e9 + 8a11da4 commit 224387a

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

crates/parser/src/grammar/items.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
112112
has_mods = true;
113113
}
114114

115-
if p.at(T![extern]) {
115+
if p.at(T![extern]) && p.nth(1) != T!['{'] && (p.nth(1) != STRING || p.nth(2) != T!['{']) {
116116
has_mods = true;
117117
abi(p);
118118
}
@@ -181,6 +181,14 @@ pub(super) fn maybe_item(p: &mut Parser, m: Marker) -> Result<(), Marker> {
181181
T![type] => {
182182
type_alias(p, m);
183183
}
184+
185+
// unsafe extern "C" {}
186+
T![extern] => {
187+
abi(p);
188+
extern_item_list(p);
189+
m.complete(p, EXTERN_BLOCK);
190+
}
191+
184192
_ => {
185193
if !has_visibility && !has_mods {
186194
return Err(m);

crates/syntax/test_data/parser/ok/0068_item_modifiers.rast

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SOURCE_FILE@0..304
1+
SOURCE_FILE@0..328
22
33
44
@@ -215,4 +215,16 @@ [email protected]
215215
216216
217217
218-
218+
219+
220+
221+
222+
223+
224+
225+
226+
227+
228+
229+
230+

crates/syntax/test_data/parser/ok/0068_item_modifiers.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ unsafe auto trait T {}
1414
unsafe impl Foo {}
1515
default impl Foo {}
1616
unsafe default impl Foo {}
17+
18+
unsafe extern "C++" {}

0 commit comments

Comments
 (0)