Skip to content

Commit 6e5a666

Browse files
authored
Escape method fragments that happen to be rust keywords (#2359)
1 parent c515919 commit 6e5a666

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

bindgen-tests/tests/expectations/tests/objc_escape.rs

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// bindgen-flags: -- -x objective-c
2+
// bindgen-osx-only
3+
4+
@interface A
5+
-(void)f:(int)arg1 as:(int)arg2;
6+
@end

bindgen/ir/objc.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,17 @@ impl ObjCMethod {
261261
if name.is_empty() {
262262
None
263263
} else {
264-
Some(Ident::new(name, Span::call_site()))
264+
// Try to parse the current name as an identifier. This might fail if the
265+
// name is a keyword so we try to prepend "r#" to it and parse again. If
266+
// this also fails, we panic with the first error.
267+
Some(
268+
syn::parse_str::<Ident>(name)
269+
.or_else(|err| {
270+
syn::parse_str::<Ident>(&format!("r#{}", name))
271+
.map_err(|_| err)
272+
})
273+
.expect("Invalid identifier"),
274+
)
265275
}
266276
})
267277
.collect();

0 commit comments

Comments
 (0)