Skip to content

Commit 35550d0

Browse files
randomPoisonthedataking
authored andcommitted
refactor: Account for idents in function signatures in reorganize_definitions
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 e1c2970 commit 35550d0

4 files changed

Lines changed: 142 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
_ => {}

c2rust-refactor/tests/snapshots.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ fn test_reorganize_foreign_types() {
459459
.test();
460460
}
461461

462+
#[test]
463+
fn test_reorganize_forward_decl_with_local_definition() {
464+
refactor("reorganize_definitions")
465+
.named("reorganize_forward_decl_with_local_definition.rs")
466+
.test();
467+
}
468+
462469
#[test]
463470
fn test_sink_lets() {
464471
refactor("sink_lets").test();
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#![feature(extern_types)]
2+
#![feature(register_tool)]
3+
#![register_tool(c2rust)]
4+
#![allow(dead_code)]
5+
#![allow(non_camel_case_types)]
6+
7+
pub mod repository {
8+
#[c2rust::header_src = "attrcache.h:1"]
9+
pub mod attrcache_h {
10+
extern "C" {
11+
#[c2rust::src_loc = "1:1"]
12+
pub type git_attr_cache;
13+
}
14+
}
15+
16+
#[c2rust::header_src = "repository.h:2"]
17+
pub mod repository_h {
18+
pub use super::attrcache_h::git_attr_cache;
19+
20+
#[derive(Copy, Clone)]
21+
#[repr(C)]
22+
#[c2rust::src_loc = "2:1"]
23+
pub struct git_repository {
24+
pub attrcache: *mut git_attr_cache,
25+
}
26+
}
27+
28+
use repository_h::git_repository;
29+
}
30+
31+
pub mod attrcache {
32+
#[c2rust::header_src = "attrcache.h:1"]
33+
pub mod attrcache_h {
34+
#[c2rust::src_loc = "1:1"]
35+
pub const GIT_ATTR_CONFIG: i32 = 0;
36+
}
37+
38+
#[c2rust::header_src = "repository.h:2"]
39+
pub mod repository_h {
40+
use super::git_attr_cache;
41+
42+
#[derive(Copy, Clone)]
43+
#[repr(C)]
44+
#[c2rust::src_loc = "2:1"]
45+
pub struct git_repository {
46+
pub attrcache: *mut git_attr_cache,
47+
}
48+
49+
#[c2rust::src_loc = "3:1"]
50+
pub unsafe extern "C" fn git_repository_attr_cache(
51+
repo: *mut git_repository,
52+
) -> *mut git_attr_cache {
53+
(*repo).attrcache
54+
}
55+
}
56+
57+
use repository_h::git_repository;
58+
59+
#[derive(Copy, Clone)]
60+
#[repr(C)]
61+
pub struct git_attr_cache {
62+
pub x: i32,
63+
}
64+
65+
unsafe extern "C" fn attrcache_source_function(
66+
repo: *mut git_repository,
67+
) -> *mut git_attr_cache {
68+
repository_h::git_repository_attr_cache(repo)
69+
}
70+
}
71+
72+
fn main() {}
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)