-
Notifications
You must be signed in to change notification settings - Fork 315
refactor: reorganize_definitions: fix multi-namespace imports splitting #1925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ahomescu
wants to merge
2
commits into
ahomescu/fix_reorganize_definitions/compare_extern_fn_sigs
from
ahomescu/fix_reorganize_definitions/multi_namespace_import_splitting
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
c2rust-refactor/tests/snapshots/reorganize_split_renamed_import.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #![feature(register_tool)] | ||
| #![register_tool(c2rust)] | ||
| #![allow(non_camel_case_types)] | ||
| #![allow(dead_code)] | ||
| #![allow(unused_imports)] | ||
|
|
||
| pub mod other { | ||
| #[c2rust::header_src = "/home/user/some/workspace/other.h:1"] | ||
| pub mod other_h { | ||
| // This incompatible struct shares the `tick` spelling and is | ||
| // processed first, so the struct in dest.h below is renamed to | ||
| // `tick_1` when both are moved out of their headers. | ||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| #[c2rust::src_loc = "2:0"] | ||
| pub struct tick { | ||
| pub y: f64, | ||
| } | ||
| } | ||
|
|
||
| pub fn other_fn() {} | ||
| } | ||
|
|
||
| pub mod dest { | ||
| #[c2rust::header_src = "/home/user/some/workspace/dest.h:1"] | ||
| pub mod dest_h { | ||
| // A type and a value sharing the `tick` spelling. Both move into | ||
| // `dest`, but the struct is renamed to `tick_1` by the collision | ||
| // with other.h while the static keeps its name, so their new paths | ||
| // differ even though they land in the same module. | ||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| #[c2rust::src_loc = "3:0"] | ||
| pub struct tick { | ||
| pub x: i32, | ||
| } | ||
|
|
||
| extern "C" { | ||
| #[c2rust::src_loc = "9:0"] | ||
| pub static tick: i32; | ||
| } | ||
| } | ||
|
|
||
| pub fn dest_fn() {} | ||
| } | ||
|
|
||
| // The `user` module holds one simple import resolving in both the type and | ||
| // value namespaces. After the reorganization the two targets have different | ||
| // paths (`tick_1` vs `tick`), so a second import must be added for the value | ||
| // namespace: the retained import only covers the renamed type. | ||
| pub mod user { | ||
| use crate::dest::dest_h::tick; | ||
|
|
||
| pub fn make(x: i32) -> tick { | ||
| tick { x } | ||
| } | ||
|
|
||
| pub fn read() -> i32 { | ||
| unsafe { tick } | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
68 changes: 68 additions & 0 deletions
68
...pshots/snapshots__refactor-reorganize_definitions-reorganize_split_renamed_import.rs.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| --- | ||
| source: c2rust-refactor/tests/snapshots.rs | ||
| expression: c2rust-refactor reorganize_definitions --rewrite-mode alongside -- tests/snapshots/reorganize_split_renamed_import.rs --edition 2021 | ||
| --- | ||
| #![feature(register_tool)] | ||
| #![register_tool(c2rust)] | ||
| #![allow(non_camel_case_types)] | ||
| #![allow(dead_code)] | ||
| #![allow(unused_imports)] | ||
|
|
||
| pub mod other { | ||
|
|
||
| // =============== BEGIN other_h ================ | ||
|
|
||
| // This incompatible struct shares the `tick` spelling and is | ||
|
|
||
| // processed first, so the struct in dest.h below is renamed to | ||
|
|
||
| // `tick_1` when both are moved out of their headers. | ||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| pub struct tick { | ||
| pub y: f64, | ||
| } | ||
|
|
||
| pub fn other_fn() {} | ||
| } | ||
|
|
||
| pub mod dest { | ||
| extern "C" { | ||
| pub static tick: i32; | ||
| } | ||
| // =============== BEGIN dest_h ================ | ||
|
|
||
| // A type and a value sharing the `tick` spelling. Both move into | ||
|
|
||
| // `dest`, but the struct is renamed to `tick_1` by the collision | ||
|
|
||
| // with other.h while the static keeps its name, so their new paths | ||
|
|
||
| // differ even though they land in the same module. | ||
| #[derive(Copy, Clone)] | ||
| #[repr(C)] | ||
| pub struct tick_1 { | ||
| pub x: i32, | ||
| } | ||
|
|
||
| pub fn dest_fn() {} | ||
| } | ||
|
|
||
| // The `user` module holds one simple import resolving in both the type and | ||
| // value namespaces. After the reorganization the two targets have different | ||
| // paths (`tick_1` vs `tick`), so a second import must be added for the value | ||
| // namespace: the retained import only covers the renamed type. | ||
| pub mod user { | ||
| use crate::dest::tick; | ||
| use crate::dest::tick_1; | ||
|
|
||
| pub fn make(x: i32) -> crate::dest::tick_1 { | ||
| crate::dest::tick_1 { x } | ||
| } | ||
|
|
||
| pub fn read() -> i32 { | ||
| unsafe { crate::dest::tick } | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Preserve aliases when emitting the second namespace import
This broader split condition now reaches
use_simple_item(path, None::<String>)when the two targets are in the same module but have different names. That generated import ignores the originalasalias and can introduce a name collision.I reproduced this at stack head using
reorganize_split_renamed_import.rs: change the import inusertouse crate::dest::dest_h::tick as alias;, change its type/value uses toalias, and addfn tick() {}touser. The input compiles. The output addsuse crate::dest::tick;alongside that function, producing E0255 (tickdefined multiple times). The earlier transform produces compiling output for this case.The synthesized import should preserve the original binding name, so this case emits
use crate::dest::tick as alias;.