Skip to content
Merged
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
1 change: 1 addition & 0 deletions cc_bindings_from_rs/generate_bindings/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 9 additions & 2 deletions cc_bindings_from_rs/generate_bindings/format_type_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -41,7 +41,14 @@ fn test_ty<TestFn, Expectation>(
}

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],
Expand Down
30 changes: 12 additions & 18 deletions cc_bindings_from_rs/generate_bindings/generate_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,11 @@ pub(crate) fn generate_thunk_call<'tcx>(
pub(crate) fn fn_arg_idents(tcx: TyCtxt, def_id: DefId) -> Vec<Option<rustc_span::Ident>> {
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(),
}
Expand Down Expand Up @@ -751,12 +755,17 @@ pub fn generate_function<'tcx>(
def_id: DefId,
) -> Result<ApiSnippets<'tcx>> {
let tcx = db.tcx();

// TODO(b/281542952): Add support for `impl Into<T>` => `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
Expand Down Expand Up @@ -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,
)
}
Loading