Skip to content

Commit c8d3dee

Browse files
authored
transpile: Update keywords (#1627)
+ Add `await` and `gen` + Remove symbols that are not keywords + Reorder to match the Rust Reference
2 parents f7899f2 + f7b6f80 commit c8d3dee

6 files changed

Lines changed: 380 additions & 10 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

c2rust-transpile/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ tempfile = "3.5.0"
4545
llvm-static = ["c2rust-ast-exporter/llvm-static"]
4646

4747
[dev-dependencies]
48+
fs-err = "3.3.0"
4849
insta = { version = "1.46.3", features = ["glob", "json"] }

c2rust-transpile/src/convert_type.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ pub struct TypeConverter {
2525
extern_crates: CrateSet,
2626
}
2727

28-
pub const RESERVED_NAMES: [&str; 103] = [
28+
pub const RESERVED_NAMES: [&str; 100] = [
2929
// Keywords currently in use
3030
"as",
31+
"async",
32+
"await",
3133
"break",
3234
"const",
3335
"continue",
3436
"crate",
37+
"dyn",
3538
"else",
3639
"enum",
3740
"extern",
@@ -50,8 +53,8 @@ pub const RESERVED_NAMES: [&str; 103] = [
5053
"pub",
5154
"ref",
5255
"return",
53-
"Self",
5456
"self",
57+
"Self",
5558
"static",
5659
"struct",
5760
"super",
@@ -62,27 +65,21 @@ pub const RESERVED_NAMES: [&str; 103] = [
6265
"use",
6366
"where",
6467
"while",
65-
"dyn",
6668
// Keywords reserved for future use
6769
"abstract",
68-
"alignof",
6970
"become",
7071
"box",
7172
"do",
7273
"final",
74+
"gen",
7375
"macro",
74-
"offsetof",
7576
"override",
7677
"priv",
77-
"proc",
78-
"pure",
79-
"sizeof",
78+
"try",
8079
"typeof",
8180
"unsized",
8281
"virtual",
8382
"yield",
84-
"async",
85-
"try",
8683
// Types exported in prelude
8784
"Copy",
8885
"Send",

c2rust-transpile/tests/snapshots.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use std::path::PathBuf;
55
use std::process::Command;
66

77
use c2rust_rust_tools::rustc;
8+
use c2rust_transpile::convert_type::RESERVED_NAMES;
89
use c2rust_transpile::{ReplaceMode, TranspilerConfig};
10+
use itertools::Itertools;
911

1012
fn config() -> TranspilerConfig {
1113
TranspilerConfig {
@@ -135,6 +137,8 @@ fn transpile_snapshot(platform: Option<&str>, c_path: &Path) {
135137

136138
#[test]
137139
fn transpile_all_snapshots() {
140+
generate_keywords_test();
141+
138142
// TODO parallelize these `insta::glob!`s across multiple `#[test]`s
139143
// now that we use `cargo nextest`.
140144

@@ -180,6 +184,25 @@ fn transpile_all_snapshots() {
180184
});
181185
}
182186

187+
fn generate_keywords_test() {
188+
// Common keywords we need to filter out.
189+
let c_keywords = [
190+
"break", "const", "continue", "else", "enum", "extern", "for", "if", "return", "static",
191+
"struct", "while", "do", "typeof", "char",
192+
];
193+
// These don't work yet. We need to fix these.
194+
let broken_rust_keywords = ["await"];
195+
let mut c_code = RESERVED_NAMES
196+
.into_iter()
197+
.filter(|keyword| !c_keywords.contains(keyword))
198+
.filter(|keyword| !broken_rust_keywords.contains(keyword))
199+
.map(|name| format!("void {name}(void) {{}}"))
200+
.join("\n\n");
201+
c_code.push_str("\n");
202+
let c_path = Path::new("tests/snapshots/keywords.c");
203+
fs_err::write(c_path, c_code).unwrap();
204+
}
205+
183206
#[test]
184207
fn check_c_decl_map() {
185208
insta::glob!("c_decls_snapshots/*.c", transpile_with_c_decl_map_snapshot);
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
void as(void) {}
2+
3+
void async(void) {}
4+
5+
void crate(void) {}
6+
7+
void dyn(void) {}
8+
9+
void false(void) {}
10+
11+
void fn(void) {}
12+
13+
void impl(void) {}
14+
15+
void in(void) {}
16+
17+
void let(void) {}
18+
19+
void loop(void) {}
20+
21+
void match(void) {}
22+
23+
void mod(void) {}
24+
25+
void move(void) {}
26+
27+
void mut(void) {}
28+
29+
void pub(void) {}
30+
31+
void ref(void) {}
32+
33+
void self(void) {}
34+
35+
void Self(void) {}
36+
37+
void super(void) {}
38+
39+
void trait(void) {}
40+
41+
void true(void) {}
42+
43+
void type(void) {}
44+
45+
void unsafe(void) {}
46+
47+
void use(void) {}
48+
49+
void where(void) {}
50+
51+
void abstract(void) {}
52+
53+
void become(void) {}
54+
55+
void box(void) {}
56+
57+
void final(void) {}
58+
59+
void gen(void) {}
60+
61+
void macro(void) {}
62+
63+
void override(void) {}
64+
65+
void priv(void) {}
66+
67+
void try(void) {}
68+
69+
void unsized(void) {}
70+
71+
void virtual(void) {}
72+
73+
void yield(void) {}
74+
75+
void Copy(void) {}
76+
77+
void Send(void) {}
78+
79+
void Sized(void) {}
80+
81+
void Sync(void) {}
82+
83+
void Drop(void) {}
84+
85+
void Fn(void) {}
86+
87+
void FnMut(void) {}
88+
89+
void FnOnce(void) {}
90+
91+
void Box(void) {}
92+
93+
void ToOwned(void) {}
94+
95+
void Clone(void) {}
96+
97+
void PartialEq(void) {}
98+
99+
void PartialOrd(void) {}
100+
101+
void Eq(void) {}
102+
103+
void Ord(void) {}
104+
105+
void AsRef(void) {}
106+
107+
void AsMut(void) {}
108+
109+
void Into(void) {}
110+
111+
void From(void) {}
112+
113+
void Default(void) {}
114+
115+
void Iterator(void) {}
116+
117+
void Extend(void) {}
118+
119+
void IntoIterator(void) {}
120+
121+
void DoubleEndedIterator(void) {}
122+
123+
void ExactSizeIterator(void) {}
124+
125+
void Option(void) {}
126+
127+
void Result(void) {}
128+
129+
void SliceConcatExt(void) {}
130+
131+
void String(void) {}
132+
133+
void ToString(void) {}
134+
135+
void Vec(void) {}
136+
137+
void bool(void) {}
138+
139+
void f32(void) {}
140+
141+
void f64(void) {}
142+
143+
void i8(void) {}
144+
145+
void i16(void) {}
146+
147+
void i32(void) {}
148+
149+
void i64(void) {}
150+
151+
void i128(void) {}
152+
153+
void isize(void) {}
154+
155+
void u8(void) {}
156+
157+
void u16(void) {}
158+
159+
void u32(void) {}
160+
161+
void u64(void) {}
162+
163+
void u128(void) {}
164+
165+
void usize(void) {}
166+
167+
void str(void) {}

0 commit comments

Comments
 (0)