Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions c2rust-refactor/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ macro_rules! match_or_else {
([$e:expr] $($arm_pat:pat => $arm_body:expr),*; $or_else:expr) => {
match $e {
$( $arm_pat => $arm_body, )*
ref x @ _ => $or_else(x),
_ => $or_else,
}
};

([$e:expr] $($arm_pat:pat => $arm_body:expr),*; $or_else_var:ident => $or_else:expr) => {
match $e {
$( $arm_pat => $arm_body, )*
ref $or_else_var @ _ => $or_else,
}
};
}
Expand All @@ -22,11 +29,11 @@ macro_rules! match_or_else {
macro_rules! expect {
([$e:expr] $arm_pat:pat => $arm_body:expr) => {
$crate::match_or_else!([$e] $arm_pat => $arm_body;
|x| panic!("expected {}, got {:?}", stringify!($arm_pat), x))
x => panic!("expected {}, got {:?}", stringify!($arm_pat), x))
};
([$e:expr] $($arm_pat:pat => $arm_body:expr),*) => {
$crate::match_or_else!([$e] $($arm_pat => $arm_body),*;
|x| panic!("expected one of: {}, got {:?}", stringify!($($arm_pat),*), x))
x => panic!("expected one of: {}, got {:?}", stringify!($($arm_pat),*), x))
};
}

Expand All @@ -35,6 +42,28 @@ macro_rules! unpack {
([$e:expr] $enum_:ident :: $variant:ident ( $($arg:ident),* )) => {
let ($($arg,)*) = $crate::expect!([$e] $enum_::$variant($($arg),*) => ($($arg,)*));
};

(
[$e:expr]
$enum_:ident :: $variant:ident ( $($arg:ident),* );
$or_else:expr
) => {
let ($($arg,)*) = $crate::match_or_else!(
[$e] $enum_::$variant($($arg),*) => ($($arg,)*);
$or_else
);
};

(
[$e:expr]
$enum_:ident :: $variant:ident ( $($arg:ident),* );
$or_else_var:ident => $or_else:expr
) => {
let ($($arg,)*) = $crate::match_or_else!(
[$e] $enum_::$variant($($arg),*) => ($($arg,)*);
$or_else_var => $or_else
);
};
}

#[macro_export]
Expand Down
2 changes: 1 addition & 1 deletion c2rust-refactor/src/path_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ where
_ => {}
}

unpack!([&mut t.kind] TyKind::Path(qself, path));
unpack!([&mut t.kind] TyKind::Path(qself, path); return);
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.

Silencing errors isn't really the way to fix issues, let's investigate this.

Any idea why a type would appear as hir::TyKind::Path in the HIR, but as an Rptr in the AST? Maybe an alias? Could you print out which type this is?

Copy link
Copy Markdown
Contributor Author

@Rua Rua May 23, 2026

Choose a reason for hiding this comment

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

I printed out qpath when t.kind isn't TyKind::Path:

if !matches!(t.kind, TyKind::Path(..)) {
    panic!("{qpath:?}");
}

and got:

Resolved(
    None,
    Path {
        span: Span {
            lo: BytePos(129233),
            hi: BytePos(129242),
            ctxt: #57
        },
        res: PrimTy(
            Bool
        ),
        segments: [
            PathSegment {
                ident: bool#57,
                hir_id: HirId {
                    owner: OwnerId {
                        def_id: DefId {
                            krate: crate0,
                            index: DefIndex(
                                50254
                            )
                        }
                    },
                    local_id: 25
                },
                res: PrimTy(
                    Bool
                ),
                args: None,
                infer_args: false
            }
        ]
    }
)

So it's... bool? Not sure how to make sense of that one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Random thought: if the refactorer is making modifications to the AST, what happens to the HIR? Is it modified in parallel? If not, what keeps the two in sync? Could that be the issue here?

let (new_qself, new_path) =
self.handle_qpath(id, qself.clone(), path.clone(), qpath);
*qself = new_qself;
Expand Down
Loading