Skip to content

Commit 8a68891

Browse files
committed
Handle all Rust reserved keywords
1 parent 63e59b1 commit 8a68891

6 files changed

Lines changed: 693 additions & 4 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
22342234
StrCat(GetUnsafeTypeAsString(qual_type), token::kOpenCurlyBracket);
22352235
int i = 0;
22362236
for (const auto *field : record->fields()) {
2237-
StrCat(field->getName(), token::kColon);
2237+
StrCat(GetNamedDeclAsString(field), token::kColon);
22382238
ConvertVarInit(field->getType(), expr->getInit(i++));
22392239
StrCat(token::kComma);
22402240
}

cpp2rust/converter/converter_lib.cpp

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,67 @@
1515
#include "converter/lex.h"
1616
#include "converter/mapper.h"
1717

18-
static std::array<const char *, 5> rust_keywords = {
19-
"fn", "in", "mut", "vec", "ref",
18+
// https://doc.rust-lang.org/reference/keywords.html
19+
static const char *rust_keywords[] = {
20+
// Strict keywords
21+
"as",
22+
"async",
23+
"await",
24+
"break",
25+
"const",
26+
"continue",
27+
"crate",
28+
"dyn",
29+
"else",
30+
"enum",
31+
"extern",
32+
"false",
33+
"fn",
34+
"for",
35+
"if",
36+
"impl",
37+
"in",
38+
"let",
39+
"loop",
40+
"match",
41+
"mod",
42+
"move",
43+
"mut",
44+
"pub",
45+
"ref",
46+
"return",
47+
"self",
48+
"Self",
49+
"static",
50+
"struct",
51+
"super",
52+
"trait",
53+
"true",
54+
"type",
55+
"unsafe",
56+
"use",
57+
"where",
58+
"while",
59+
// Reserved keywords
60+
"abstract",
61+
"become",
62+
"box",
63+
"do",
64+
"final",
65+
"gen",
66+
"macro",
67+
"override",
68+
"priv",
69+
"try",
70+
"typeof",
71+
"unsized",
72+
"virtual",
73+
"yield",
74+
// Weak keywords
75+
"macro_rules",
76+
"raw",
77+
"safe",
78+
"union",
2079
};
2180

2281
namespace cpp2rust {

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ bool ConverterRefCount::VisitInitListExpr(clang::InitListExpr *expr) {
10931093
int i = 0;
10941094
PushConversionKind push(*this, ConversionKind::FullRefCount);
10951095
for (const auto *field : record->fields()) {
1096-
StrCat(field->getName(), token::kColon);
1096+
StrCat(GetNamedDeclAsString(field), token::kColon);
10971097
ConvertVarInit(field->getType(), expr->getInit(i++));
10981098
StrCat(token::kComma);
10991099
}

0 commit comments

Comments
 (0)