Skip to content

Commit 3e58e59

Browse files
committed
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 a7b5bba commit 3e58e59

2 files changed

Lines changed: 63 additions & 15 deletions

File tree

c2rust-refactor/src/transform/reorganize_definitions.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,30 +245,26 @@ impl<'a, 'tcx> Reorganizer<'a, 'tcx> {
245245
fn keep_items(items: &[P<Item>]) -> HashSet<NodeId> {
246246
let mut keep_items = HashSet::new();
247247
let mut used_idents = HashSet::new();
248+
let mut collect_used_idents = |item: &Item| {
249+
visit_nodes(item, |path: &Path| {
250+
if let [segment] = &path.segments[..] {
251+
used_idents.insert(segment.ident);
252+
}
253+
});
254+
};
248255
for item in &items[..] {
249256
match &item.kind {
250257
ItemKind::Fn(box Fn {
251-
body: Some(ref body),
258+
body: Some(_),
252259
..
253260
}) => {
254261
keep_items.insert(item.id);
255-
visit_nodes(&**body, |path: &Path| {
256-
if let [segment] = &path.segments[..] {
257-
used_idents.insert(segment.ident);
258-
}
259-
});
262+
collect_used_idents(item);
260263
}
261264

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

274270
_ => {}
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)