Skip to content

refactor: canonicalize moved paths that refer to item left behind - #1928

Open
ahomescu wants to merge 1 commit into
ahomescu/fix_reorganize_definitions/glob_importsfrom
ahomescu/fix_reorganize_definitions/canonicalize_moved_paths
Open

ahomescu wants to merge 1 commit into
ahomescu/fix_reorganize_definitions/glob_importsfrom
ahomescu/fix_reorganize_definitions/canonicalize_moved_paths

Conversation

@ahomescu

Copy link
Copy Markdown
Contributor

Follow-up limitation found while writing the test — FIXED: items moved out
of a header could not rely on bindings that stay behind in the header (e.g.
the kept glob). A bare path like thing that resolved through the glob was
not canonicalized on the way out (is_relative_path treats only
self::/super:: as relative), so the moved item failed to compile with
E0412. move_items now runs canonicalize_moved_decl_paths over every
moved declaration (and its impl block): a single-segment path to a local,
non-moving item def is rewritten to its absolute path via def_qpath.
Paths are left alone when the destination module still binds the ident to
the same def — through a pre-existing import, an import moving along with
the declaration (moved use targets are now recorded in
ModuleInfo::import_targets, mirroring update_module_info_items), or
because the target is defined in the destination module itself — which
keeps the existing snapshots byte-identical. Paths to moved defs are still
rewritten later by update_paths via path_mapping. The
reorganize_glob_import test now uses the bare thing field type and
verifies it becomes crate::defs::thing.


Stack created with GitHub Stacks CLI • Give Feedback 💬

@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 47f0497 to 2cf3f8e Compare July 25, 2026 01:40
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 2cf3f8e to daf6325 Compare July 25, 2026 01:57
@ahomescu
ahomescu changed the base branch from ahomescu/fix_reorganize_definitions/glob_imports to ahomescu/fix_reorganize_definitions/use_comments July 25, 2026 02:07
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from daf6325 to 81140ff Compare July 25, 2026 02:07
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 81140ff to 1d8d52c Compare July 25, 2026 02:11
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch 2 times, most recently from d45e97c to 87a82b0 Compare July 25, 2026 02:35
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 87a82b0 to ff8ac98 Compare July 25, 2026 03:17
@ahomescu
ahomescu changed the base branch from ahomescu/fix_reorganize_definitions/use_comments to ahomescu/fix_reorganize_definitions/glob_imports July 25, 2026 03:26
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from ff8ac98 to 6438594 Compare July 25, 2026 03:26
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 6438594 to 6012d94 Compare July 25, 2026 05:09
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 6012d94 to d20e8ed Compare July 25, 2026 05:33
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from d20e8ed to a4044e4 Compare July 25, 2026 05:41
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from a4044e4 to 252f1c9 Compare July 25, 2026 05:42
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 252f1c9 to 5750c0c Compare July 25, 2026 05:48
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 5750c0c to 4090222 Compare July 25, 2026 05:53
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 4090222 to 0a47a51 Compare July 25, 2026 06:00
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch 2 times, most recently from 2d41b43 to 9885cdf Compare July 30, 2026 00:31
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 9885cdf to 314a319 Compare July 30, 2026 22:54
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch 2 times, most recently from 4055c03 to 9609486 Compare July 30, 2026 22:58
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch 2 times, most recently from cec27ff to b67a574 Compare August 5, 2026 00:10
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch 2 times, most recently from 9cbbba7 to 486931c Compare August 5, 2026 00:22
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 486931c to 4cddd79 Compare August 5, 2026 00:43

@thedataking thedataking left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of stack 1953: qualifying a moved path drops its generic arguments.

cx.hir_map().local_def_id_to_node_id(mod_hir_id) == dest_info.id
});
if !import_covers && !defined_in_dest {
return cx.def_qpath(def_id);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve generic arguments when qualifying a path

def_qpath(def_id) reconstructs the definition's name without the original path's generic arguments. This turns a field type Value<i32> into crate::defs::Value, causing E0107. The destination's glob import makes this a regression: the output before this PR already compiled because Value<i32> remained resolvable there. Please carry the original segment's generic arguments onto the qualified path.

This case uses a generic Rust type; it applies to code that has already been edited after transpilation.

Reproducer

The input compiles. Run c2rust-refactor reorganize_definitions --rewrite-mode alongside -- repro.rs --edition=2021 and compile the resulting repro.new to see E0107.

#![feature(register_tool)]
#![register_tool(c2rust)]
#![allow(dead_code, non_camel_case_types, unused_imports)]

pub mod defs {
    pub struct Value<T> { pub val: T }
}
pub mod user {
    use crate::defs::*;
    #[c2rust::header_src = "/tmp/user.h:1"]
    pub mod user_h {
        use crate::defs::*;
        pub struct config { pub thing: Value<i32> }
    }
    pub fn go(_: user_h::config) {}
}
fn main() {}

@ahomescu
ahomescu removed this pull request from stack #1953 September 9, 2026 01:38
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 4cddd79 to 2b8842b Compare September 9, 2026 01:41
@ahomescu
ahomescu added this pull request to stack #2004 September 9, 2026 01:42
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from 2b8842b to f037a6c Compare September 9, 2026 02:18
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/canonicalize_moved_paths branch from f037a6c to 8f6a4ac Compare September 9, 2026 02:33
@ahomescu
ahomescu removed this pull request from stack #2004 September 17, 2026 00:38
Follow-up limitation found while writing the test — FIXED: items moved out
of a header could not rely on bindings that stay behind in the header (e.g.
the kept glob). A bare path like `thing` that resolved through the glob was
not canonicalized on the way out (`is_relative_path` treats only
`self::`/`super::` as relative), so the moved item failed to compile with
E0412. `move_items` now runs `canonicalize_moved_decl_paths` over every
moved declaration (and its `impl` block): a single-segment path to a local,
non-moving item def is rewritten to its absolute path via `def_qpath`.
Paths are left alone when the destination module still binds the ident to
the same def — through a pre-existing import, an import moving along with
the declaration (moved `use` targets are now recorded in
`ModuleInfo::import_targets`, mirroring `update_module_info_items`), or
because the target is defined in the destination module itself — which
keeps the existing snapshots byte-identical. Paths to moved defs are still
rewritten later by `update_paths` via `path_mapping`. The
`reorganize_glob_import` test now uses the bare `thing` field type and
verifies it becomes `crate::defs::thing`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants