Skip to content
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

Add hypothetical lifetime to structure rewrites with empty generic type parameter lists #956

Merged
merged 2 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 15 additions & 13 deletions c2rust-analyze/src/rewrite/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ where
})
}

fn hir_generic_ty_args<'tcx>(ty: &hir::Ty<'tcx>) -> Option<Vec<&'tcx hir::Ty<'tcx>>> {
// Gets the generic type arguments of an HIR type. If the type has no generic
// arguments, an empty vector is returned
fn hir_generic_ty_args<'tcx>(ty: &hir::Ty<'tcx>) -> Vec<&'tcx hir::Ty<'tcx>> {
let args = match ty.kind {
hir::TyKind::Path(hir::QPath::Resolved(
_,
Expand All @@ -140,6 +142,7 @@ fn hir_generic_ty_args<'tcx>(ty: &hir::Ty<'tcx>) -> Option<Vec<&'tcx hir::Ty<'tc
})
.collect()
})
.unwrap_or_default()
aneksteind marked this conversation as resolved.
Show resolved Hide resolved
}

/// Extract arguments from `hir_ty` if it corresponds to the tcx type `ty`. If the two types
Expand Down Expand Up @@ -205,19 +208,18 @@ fn deconstruct_hir_ty<'a, 'tcx>(
}

(&ty::TyKind::Adt(adt_def, substs), &hir::TyKind::Path(..)) => {
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
// such as the allocator `std::alloc::Global` type argument in `Vec`
eprintln!("warning: extra MIR type argument for {adt_def:?}:");
for mir_arg in substs.types().into_iter().skip(type_args.len()) {
eprintln!("\t{:?}", mir_arg)
}
} else if type_args.len() != substs.types().count() {
panic!("mismatched number of type arguments for {adt_def:?} and {hir_ty:?}")
let type_args = hir_generic_ty_args(hir_ty);
if type_args.len() < substs.types().count() {
// this situation occurs when there are hidden type arguments
// such as the allocator `std::alloc::Global` type argument in `Vec`
eprintln!("warning: extra MIR type argument for {adt_def:?}:");
for mir_arg in substs.types().into_iter().skip(type_args.len()) {
eprintln!("\t{:?}", mir_arg)
}
type_args
})
} else if type_args.len() != substs.types().count() {
panic!("mismatched number of type arguments for {adt_def:?} and {hir_ty:?}")
}
Some(type_args)
}
(tk, hir_tk) => {
eprintln!("deconstruct_hir_ty: {tk:?} -- {hir_tk:?} not supported");
Expand Down
11 changes: 11 additions & 0 deletions c2rust-analyze/tests/filecheck/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ struct VecTup<'a> {
bar: *mut Vec<(VecTup<'a>, *mut A<'a>)>,
}

struct Hypo {
h: *mut i32
}

struct HypoWrapper {
hw: *const Hypo
}

// let rd = (*(**ppd).a.pra).rd
// CHECK-DAG: Label { origin: Some(Origin([[REF_D_ORIGIN:[0-9]+]])), origin_params: []{{.*}}}#&'a Data
// CHECK-DAG: Label { origin: None, origin_params: [('d, Origin([[REF_D_ORIGIN]]){{.*}}}#Data
Expand Down Expand Up @@ -79,3 +87,6 @@ unsafe fn _field_access<'d>(ra: &'d mut A<'d>, ppd: *mut *mut Data<'d>) {
// CHECK-DAG: pub rd: &'a Data<'a,'h0,'h1,'h2>,
// CHECK-DAG: pub pra: &'h2 core::cell::Cell<(&'a mut A<'a,'h0,'h1,'h2>)>,
// CHECK-DAG: bar: &'h3 (Vec<(VecTup<'a,'h3,'h4,'h0,'h1,'h2>, &'h4 (A<'a,'h0,'h1,'h2>))>),

// CHECK-DAG: struct HypoWrapper<'h6,'h5>
// CHECK-DAG: hw: &'h6 (Hypo<'h5>)