Skip to content

Commit 2aac34d

Browse files
committed
Assert that both C++ and Rust rules use the same generics
1 parent a6d3427 commit 2aac34d

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

@@ -303,6 +304,26 @@ void ExprRule::dump() const {
303304
}
304305
}
305306

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

308329
void TypeInfo::dump() const {
@@ -339,11 +360,7 @@ std::pair<ExprRules, TypeRules> Load(const std::filesystem::path &path,
339360
LoadIrSrc(exprs, types, dir / "ir_src.json");
340361

341362
for (auto &[name, rule] : exprs) {
342-
if (rule.src.empty()) {
343-
llvm::errs() << name << '\n';
344-
rule.dump();
345-
assert(0 && "Expr rule loaded from IR but has no src");
346-
}
363+
rule.validate(name);
347364
}
348365
for (auto &[name, rule] : types) {
349366
if (rule.src.empty()) {

cpp2rust/converter/translation_rule.h

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

7272
void dump() const;
73+
void validate(const std::string &name) const;
7374
};
7475

7576
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
@@ -1212,19 +1212,16 @@
12121212
"text": " as usize]\n }"
12131213
}
12141214
],
1215-
"generics": {
1216-
"T1": []
1217-
},
12181215
"params": {
12191216
"a0": {
1220-
"type": "&mut Vec<T1>"
1217+
"type": "&mut Vec<u8>"
12211218
},
12221219
"a1": {
12231220
"type": "usize"
12241221
}
12251222
},
12261223
"return_type": {
1227-
"type": "*mut T1",
1224+
"type": "*mut u8",
12281225
"is_unsafe_pointer": true
12291226
}
12301227
},

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)