Skip to content

Commit

Permalink
analyze: fix deconstruct_hir_ty adt/path case (#1023)
Browse files Browse the repository at this point in the history
Previously, type aliases for `Option<*const T>` caused a panic, as
`deconstruct_hir_ty` would match the HIR alias with the MIR type
`Option<T>` and then get confused due to the mismatched number of type
arguments. With this fix, `deconstruct_hir_ty` will no longer match the
HIR alias with the MIR type as the two have different `DefId`s.
  • Loading branch information
spernsteiner authored Sep 8, 2023
2 parents f1c9c44 + 82af6d6 commit 0e9e404
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion c2rust-analyze/src/rewrite/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ fn deconstruct_hir_ty<'a, 'tcx>(
}
}

(&ty::TyKind::Adt(adt_def, substs), &hir::TyKind::Path(..)) => {
(
&ty::TyKind::Adt(adt_def, substs),
&hir::TyKind::Path(hir::QPath::Resolved(_, ref path)),
) if path.res.def_id() == adt_def.did() => {
hir_generic_ty_args(hir_ty).map(|type_args| {
if type_args.len() < substs.types().count() {
// this situation occurs when there are hidden type arguments
Expand Down
1 change: 1 addition & 0 deletions c2rust-analyze/tests/filecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ define_tests! {
statics,
test_attrs,
trivial,
type_alias,
type_annotation_rewrite,
unrewritten_calls,
unrewritten_calls_shim_fail,
Expand Down
11 changes: 11 additions & 0 deletions c2rust-analyze/tests/filecheck/type_alias.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Regression test: previously, type aliases for `Option<*const T>` caused a panic, as
// `deconstruct_hir_ty` would match the HIR alias with the MIR type `Option<T>` and then get
// confused due to the mismatched number of type arguments.
type AliasOption = Option<*const u8>;

// CHECK: struct UseAliasOption<'h0>
struct UseAliasOption {
// FIXME: should be `Option<&'h0 u8>`
// CHECK: x: std::option::Option<&u8>
x: AliasOption,
}

0 comments on commit 0e9e404

Please sign in to comment.