Skip to content

Commit 79ffcde

Browse files
committed
Assert that both C++ and Rust rules use the same generics
1 parent 1ac73cc commit 79ffcde

6 files changed

Lines changed: 27 additions & 15 deletions

File tree

cpp2rust/converter/translation_rule.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <llvm/Support/MemoryBuffer.h>
88

99
#include <algorithm>
10+
#include <format>
1011
#include <string>
1112
#include <vector>
1213

@@ -299,6 +300,26 @@ void ExprRule::dump() const {
299300
}
300301
}
301302

303+
void ExprRule::validate(const std::string &name) const {
304+
if (src.empty()) {
305+
llvm::errs() << name << '\n';
306+
dump();
307+
assert(0 && "Expr rule loaded from IR but has no src");
308+
}
309+
310+
// Check that both source and target use the same generics.
311+
for (unsigned i = 0; i < generics.size(); ++i) {
312+
auto placeholder = std::format("T{}", i + 1);
313+
if (src.find(placeholder) == std::string::npos) {
314+
llvm::errs() << name << '\n';
315+
dump();
316+
llvm::errs() << "generic " << placeholder
317+
<< " declared but missing from src: " << src << '\n';
318+
assert(0 && "Expr rule declares a generic absent from its src");
319+
}
320+
}
321+
}
322+
302323
void GenericFragment::dump() const { log() << " generic: " << n << '\n'; }
303324

304325
void TypeInfo::dump() const {
@@ -335,11 +356,7 @@ std::pair<ExprRules, TypeRules> Load(const std::filesystem::path &path,
335356
LoadIrSrc(exprs, types, dir / "ir_src.json");
336357

337358
for (auto &[name, rule] : exprs) {
338-
if (rule.src.empty()) {
339-
llvm::errs() << name << '\n';
340-
rule.dump();
341-
assert(0 && "Expr rule loaded from IR but has no src");
342-
}
359+
rule.validate(name);
343360
}
344361
for (auto &[name, rule] : types) {
345362
if (rule.src.empty()) {

cpp2rust/converter/translation_rule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct ExprRule {
6969
bool multi_statement = false;
7070

7171
void dump() const;
72+
void validate(const std::string &name) const;
7273
};
7374

7475
struct TypeRule {

rules/algorithm/ir_unsafe.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,6 @@
419419
}
420420
}
421421
],
422-
"generics": {
423-
"T1": []
424-
},
425422
"multi_statement": true,
426423
"params": {
427424
"a0": {

rules/algorithm/tgt_unsafe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ unsafe fn f12<T1: Clone>(a0: *mut T1, a1: *mut T1, a2: T1) {
110110
}
111111
}
112112

113-
unsafe fn f13<T1>(a0: *const u8, a1: *const u8, a2: &mut ::std::fs::File) -> ::std::fs::File {
113+
unsafe fn f13(a0: *const u8, a1: *const u8, a2: &mut ::std::fs::File) -> ::std::fs::File {
114114
let __start = a0 as *const u8;
115115
let __end = a1 as *const u8;
116116
let __len = __end.offset_from(__start) as usize;

rules/string/ir_unsafe.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,19 +1210,16 @@
12101210
"text": " as usize]\n }"
12111211
}
12121212
],
1213-
"generics": {
1214-
"T1": []
1215-
},
12161213
"params": {
12171214
"a0": {
1218-
"type": "&mut Vec<T1>"
1215+
"type": "&mut Vec<u8>"
12191216
},
12201217
"a1": {
12211218
"type": "usize"
12221219
}
12231220
},
12241221
"return_type": {
1225-
"type": "*mut T1",
1222+
"type": "*mut u8",
12261223
"is_unsafe_pointer": true
12271224
}
12281225
},

rules/string/tgt_unsafe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ unsafe fn f25(a0: &mut Vec<u8>) {
146146
a0.shrink_to_fit()
147147
}
148148

149-
unsafe fn f26<T1>(a0: &mut Vec<T1>, a1: usize) -> *mut T1 {
149+
unsafe fn f26(a0: &mut Vec<u8>, a1: usize) -> *mut u8 {
150150
if a1 as usize >= a0.len() - 1 {
151151
panic!("out of bounds access")
152152
} else {

0 commit comments

Comments
 (0)