diff --git a/cc_bindings_from_rs/generate_bindings/BUILD b/cc_bindings_from_rs/generate_bindings/BUILD index f2e2837ea..5f66bd23d 100644 --- a/cc_bindings_from_rs/generate_bindings/BUILD +++ b/cc_bindings_from_rs/generate_bindings/BUILD @@ -58,6 +58,7 @@ crubit_rust_test( rustc_flags = ["-Zallow-features=rustc_private"], deps = [ ":generate_bindings", + ":query_compiler", ":test_helpers", "//cc_bindings_from_rs:run_compiler_test_support", "//cc_bindings_from_rs/generate_bindings/database", diff --git a/cc_bindings_from_rs/generate_bindings/format_type_test.rs b/cc_bindings_from_rs/generate_bindings/format_type_test.rs index d9277e847..80b7d8fe4 100644 --- a/cc_bindings_from_rs/generate_bindings/format_type_test.rs +++ b/cc_bindings_from_rs/generate_bindings/format_type_test.rs @@ -6,8 +6,8 @@ use code_gen_utils::format_cc_includes; use database::TypeLocation; -use generate_bindings::generate_function::get_fn_sig; use proc_macro2::TokenStream; +use query_compiler::liberate_and_deanonymize_late_bound_regions; use quote::quote; use run_compiler_test_support::{find_def_id_by_name, run_compiler_for_testing}; use rustc_middle::ty::{Ty, TyCtxt}; @@ -41,7 +41,14 @@ fn test_ty( } fn get_test_function_ty<'tcx>(tcx: TyCtxt<'tcx>, type_location: TypeLocation) -> Ty<'tcx> { - let sig_mid = get_fn_sig(tcx, find_def_id_by_name(tcx, "test_function").to_def_id()); + let sig_mid = { + let def_id = find_def_id_by_name(tcx, "test_function").to_def_id(); + liberate_and_deanonymize_late_bound_regions( + tcx, + tcx.fn_sig(def_id).instantiate_identity(), + def_id, + ) + }; match type_location { TypeLocation::FnReturn { .. } => sig_mid.output(), TypeLocation::FnParam { .. } => sig_mid.inputs()[0], diff --git a/cc_bindings_from_rs/generate_bindings/generate_function.rs b/cc_bindings_from_rs/generate_bindings/generate_function.rs index be5e1ac92..4ecec630a 100644 --- a/cc_bindings_from_rs/generate_bindings/generate_function.rs +++ b/cc_bindings_from_rs/generate_bindings/generate_function.rs @@ -675,7 +675,11 @@ pub(crate) fn generate_thunk_call<'tcx>( pub(crate) fn fn_arg_idents(tcx: TyCtxt, def_id: DefId) -> Vec> { match tcx.def_kind(def_id) { DefKind::Ctor { .. } => { - vec![None; get_fn_sig(tcx, def_id).inputs().len()] + // Documentation of `skip_binder` says that accessing generic args in the returned value + // is generally incorrect, but specifically points out that it should be okay to use + // it to get the number of arguments of an `FnSig`. + let arg_count = tcx.fn_sig(def_id).skip_binder().inputs().skip_binder().len(); + vec![None; arg_count] } _ => tcx.fn_arg_idents(def_id).iter().cloned().collect_vec(), } @@ -751,12 +755,17 @@ pub fn generate_function<'tcx>( def_id: DefId, ) -> Result> { let tcx = db.tcx(); + + // TODO(b/281542952): Add support for `impl Into` => `T` and similar substitutions. ensure!( !tcx.generics_of(def_id).requires_monomorphization(tcx), "Generic functions are not supported yet (b/259749023)" ); - - let sig_mid = get_fn_sig(tcx, def_id); + let sig_mid = liberate_and_deanonymize_late_bound_regions( + tcx, + tcx.fn_sig(def_id).instantiate_identity(), + def_id, + ); check_fn_sig(&sig_mid)?; let trait_ref = tcx @@ -1076,18 +1085,3 @@ pub fn check_fn_sig(sig: &ty::FnSig) -> Result<()> { Ok(()) } - -/// Returns the rustc_middle and rustc_hir function signatures. -/// -/// In the case of rustc_hir, this returns the `FnDecl`, not the -/// `rustc_hir::FnSig`, because the `FnDecl` type is used for both function -/// pointers and actual functions. This makes it a more useful vocabulary type. -/// `FnDecl` does drop information, but that information is already on the -/// rustc_middle `FnSig`, so there is no loss. -pub fn get_fn_sig<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::FnSig<'tcx> { - liberate_and_deanonymize_late_bound_regions( - tcx, - tcx.fn_sig(def_id).instantiate_identity(), - def_id, - ) -}