Skip to content

Commit 3749f92

Browse files
randomPoisonthedataking
authored andcommitted
Account for used idents in function signatures
Previously only idents used in the function body were accounted for, which led to errors if there was a type referenced in the signature but not the body.
1 parent 4960c84 commit 3749f92

2 files changed

Lines changed: 63 additions & 18 deletions

File tree

c2rust-refactor/src/transform/reorganize_definitions.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -247,30 +247,23 @@ impl<'a, 'tcx> Reorganizer<'a, 'tcx> {
247247
fn keep_items(items: &[P<Item>]) -> HashSet<NodeId> {
248248
let mut keep_items = HashSet::new();
249249
let mut used_idents = HashSet::new();
250+
let mut collect_used_idents = |item: &Item| {
251+
visit_nodes(item, |path: &Path| {
252+
if let [segment] = &path.segments[..] {
253+
used_idents.insert(segment.ident);
254+
}
255+
});
256+
};
250257
for item in &items[..] {
251258
match &item.kind {
252-
ItemKind::Fn(box Fn {
253-
body: Some(ref body),
254-
..
255-
}) => {
259+
ItemKind::Fn(box Fn { body: Some(_), .. }) => {
256260
keep_items.insert(item.id);
257-
visit_nodes(&**body, |path: &Path| {
258-
if let [segment] = &path.segments[..] {
259-
used_idents.insert(segment.ident);
260-
}
261-
});
261+
collect_used_idents(item);
262262
}
263263

264-
ItemKind::Static(_, _, init) if !is_exported(item) => {
264+
ItemKind::Static(_, _, _) if !is_exported(item) => {
265265
keep_items.insert(item.id);
266-
267-
if let Some(init) = init {
268-
visit_nodes(&**init, |path: &Path| {
269-
if let [segment] = &path.segments[..] {
270-
used_idents.insert(segment.ident);
271-
}
272-
});
273-
}
266+
collect_used_idents(item);
274267
}
275268

276269
_ => {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
source: c2rust-refactor/tests/snapshots.rs
3+
assertion_line: 212
4+
expression: c2rust-refactor reorganize_definitions --rewrite-mode alongside -- tests/snapshots/reorganize_forward_decl_with_local_definition.rs --edition 2021
5+
---
6+
#![feature(extern_types)]
7+
#![feature(register_tool)]
8+
#![register_tool(c2rust)]
9+
#![allow(dead_code)]
10+
#![allow(non_camel_case_types)]
11+
12+
pub mod repository {
13+
14+
// =============== BEGIN repository_h ================
15+
#[derive(Copy, Clone)]
16+
#[repr(C)]
17+
pub struct git_repository {
18+
pub attrcache: *mut crate::attrcache::git_attr_cache,
19+
}
20+
}
21+
22+
pub mod attrcache {
23+
24+
// =============== BEGIN attrcache_h ================
25+
pub const GIT_ATTR_CONFIG: i32 = 0;
26+
27+
pub mod repository_h {
28+
use crate::attrcache::git_attr_cache;
29+
30+
pub unsafe extern "C" fn git_repository_attr_cache(
31+
repo: *mut crate::repository::git_repository,
32+
) -> *mut git_attr_cache {
33+
(*repo).attrcache
34+
}
35+
}
36+
37+
use crate::repository::git_repository;
38+
39+
#[derive(Copy, Clone)]
40+
#[repr(C)]
41+
pub struct git_attr_cache {
42+
pub x: i32,
43+
}
44+
45+
unsafe extern "C" fn attrcache_source_function(
46+
repo: *mut crate::repository::git_repository,
47+
) -> *mut git_attr_cache {
48+
repository_h::git_repository_attr_cache(repo)
49+
}
50+
}
51+
52+
fn main() {}

0 commit comments

Comments
 (0)