@@ -72,7 +72,7 @@ pub fn format_cc_ident(db: &BindingsGenerator, ident: &str) -> Result<Ident> {
7272 }
7373}
7474
75- pub fn format_pointer_or_reference_ty_for_cc < ' tcx > (
75+ fn format_pointer_or_reference_ty_for_cc < ' tcx > (
7676 db : & BindingsGenerator < ' tcx > ,
7777 pointee : Ty < ' tcx > ,
7878 mutability : Mutability ,
@@ -91,7 +91,7 @@ pub fn format_pointer_or_reference_ty_for_cc<'tcx>(
9191 Ok ( CcSnippet { prereqs, tokens : quote ! { #tokens #const_qualifier #pointer_sigil } } )
9292}
9393
94- pub fn format_slice_ref_for_cc < ' tcx > (
94+ fn format_slice_ref_for_cc < ' tcx > (
9595 db : & BindingsGenerator < ' tcx > ,
9696 element_ty : Ty < ' tcx > ,
9797 mutability : rustc_middle:: mir:: Mutability ,
@@ -118,11 +118,11 @@ pub fn format_slice_ref_for_cc<'tcx>(
118118}
119119
120120/// Returns a CcSnippet referencing `rs_std::StrRef` and its include path.
121- pub fn format_str_ref_for_cc < ' tcx > ( db : & BindingsGenerator < ' tcx > ) -> CcSnippet < ' tcx > {
121+ fn format_str_ref_for_cc < ' tcx > ( db : & BindingsGenerator < ' tcx > ) -> CcSnippet < ' tcx > {
122122 CcSnippet :: with_include ( quote ! { rs_std:: StrRef } , db. support_header ( "rs_std/str_ref.h" ) )
123123}
124124
125- pub fn format_transparent_pointee_or_reference_for_cc < ' tcx > (
125+ fn format_transparent_pointee_or_reference_for_cc < ' tcx > (
126126 db : & BindingsGenerator < ' tcx > ,
127127 referent_ty : Ty < ' tcx > ,
128128 mutability : rustc_middle:: mir:: Mutability ,
@@ -315,13 +315,15 @@ pub fn format_ty_for_cc<'tcx>(
315315 BridgedType :: Composable ( mut composable) => {
316316 // The existance of crubit_abi_type implies that the type can fully
317317 // composably bridge.
318-
319318 let mut tokens = composable. cpp_type . to_token_stream ( ) ;
320-
321319 if !substs. is_empty ( ) {
322320 let mut generic_types_tokens = Vec :: with_capacity ( substs. len ( ) ) ;
323321 for subst in * substs {
324- let snippet = format_ty_for_cc ( db, subst. expect_ty ( ) , location) ?;
322+ let snippet = format_ty_for_cc (
323+ db,
324+ subst. expect_ty ( ) ,
325+ TypeLocation :: NestedBridgeable ,
326+ ) ?;
325327 generic_types_tokens
326328 . push ( snippet. into_tokens ( & mut composable. prereqs ) ) ;
327329 }
@@ -562,7 +564,7 @@ pub fn format_ret_ty_for_cc<'tcx>(
562564 . with_context ( || format ! ( "Error formatting function return type `{output_ty}`" ) )
563565}
564566
565- pub fn has_elided_region < ' tcx > ( tcx : TyCtxt < ' tcx > , search_ty : ty:: Ty < ' tcx > ) -> bool {
567+ pub ( crate ) fn has_elided_region < ' tcx > ( tcx : TyCtxt < ' tcx > , search_ty : ty:: Ty < ' tcx > ) -> bool {
566568 use core:: ops:: ControlFlow ;
567569 use rustc_middle:: ty:: { Region , TyCtxt , TypeVisitor } ;
568570
@@ -637,7 +639,7 @@ fn try_ty_as_maybe_uninit<'tcx>(
637639}
638640
639641/// Format a supported `repr(transparent)` pointee type
640- pub fn format_transparent_pointee < ' tcx > (
642+ fn format_transparent_pointee < ' tcx > (
641643 db : & BindingsGenerator < ' tcx > ,
642644 ty : Ty < ' tcx > ,
643645) -> Result < TokenStream > {
@@ -762,12 +764,13 @@ pub fn format_ty_for_rs<'tcx>(db: &BindingsGenerator<'tcx>, ty: Ty<'tcx>) -> Res
762764 . map ( |subst| match subst. kind ( ) {
763765 ty:: GenericArgKind :: Type ( ty) => db. format_ty_for_rs ( ty) ,
764766 ty:: GenericArgKind :: Lifetime ( region) => {
765- assert_eq ! (
766- region. kind( ) ,
767- ty:: RegionKind :: ReStatic ,
768- "We should never format types with non-'static regions, as \
769- thunks should first call `replace_all_regions_with_static`. {ty}",
770- ) ;
767+ if !region. is_static ( ) {
768+ panic ! (
769+ "We should never format types with non-'static regions, as \
770+ thunks should first call `replace_all_regions_with_static`. \
771+ Found type: `{ty}`."
772+ ) ;
773+ }
771774 Ok ( quote ! { ' static } )
772775 }
773776 ty:: GenericArgKind :: Const ( _) => {
@@ -850,6 +853,7 @@ pub fn format_region_as_cc_lifetime<'tcx>(
850853 }
851854}
852855
856+ #[ track_caller]
853857pub fn format_region_as_rs_lifetime < ' tcx > (
854858 tcx : TyCtxt < ' tcx > ,
855859 region : ty:: Region < ' tcx > ,
@@ -1014,21 +1018,27 @@ pub fn crubit_abi_type_from_ty<'tcx>(
10141018 }
10151019 // Do we need to confirm that pointee is layout compatible?
10161020 let rust_type = db. format_ty_for_rs ( pointee) ?;
1017- let cpp_type_with_prereqs = db. format_ty_for_cc ( pointee, TypeLocation :: Other ) ?;
1021+ let CcSnippet { tokens : cpp_type, prereqs } =
1022+ db. format_ty_for_cc ( pointee, TypeLocation :: Other ) ?;
10181023
10191024 return Ok ( CrubitAbiTypeWithCcPrereqs {
10201025 crubit_abi_type : CrubitAbiType :: Ptr {
10211026 is_const : mutability. is_not ( ) ,
10221027 is_rust_slice,
10231028 rust_type,
1024- cpp_type : cpp_type_with_prereqs . tokens ,
1029+ cpp_type,
10251030 } ,
1026- prereqs : cpp_type_with_prereqs . prereqs ,
1031+ prereqs,
10271032 } ) ;
10281033 }
1029- ty:: TyKind :: Ref ( _region, _inner, _mutability) => {
1030- // TODO(okabayashi): Support &str and &[T], and possibly other reference types.
1031- bail ! ( "Reference types are not yet supported in bridging" ) ;
1034+ ty:: TyKind :: Ref ( _, _, _) => {
1035+ let rust_type = db. format_ty_for_rs ( ty) ?;
1036+ let CcSnippet { tokens : cpp_type, prereqs } =
1037+ db. format_ty_for_cc ( ty, TypeLocation :: Other ) ?;
1038+ return Ok ( CrubitAbiTypeWithCcPrereqs {
1039+ crubit_abi_type : CrubitAbiType :: Transmute { rust_type, cpp_type } ,
1040+ prereqs,
1041+ } ) ;
10321042 }
10331043 _ => bail ! ( "Unsupported bridge type: {ty:?}" ) ,
10341044 } ) )
@@ -1216,30 +1226,40 @@ pub fn is_bridged_type<'tcx>(
12161226 db : & BindingsGenerator < ' tcx > ,
12171227 ty : Ty < ' tcx > ,
12181228) -> Result < Option < BridgedType < ' tcx > > > {
1219- match ty. kind ( ) {
1220- ty:: TyKind :: Ref ( _, referent, _) if is_bridged_type ( db, * referent) ?. is_some ( ) => {
1221- if let ty:: TyKind :: Adt ( adt, _) = referent. kind ( ) {
1222- if let Some ( BridgedBuiltin :: Option ) = BridgedBuiltin :: new ( db, * adt) {
1223- return Ok ( None ) ;
1229+ // The ABI of bridged types is lifetime-independent, and the Crubit thunks replace all
1230+ // lifetimes with static.
1231+ let ty = crate :: generate_function_thunk:: replace_all_regions_with_static ( db. tcx ( ) , ty) ;
1232+
1233+ match * ty. kind ( ) {
1234+ ty:: TyKind :: Ref ( _, referent, _) => {
1235+ if is_bridged_type ( db, referent) ?. is_some ( ) {
1236+ if let ty:: TyKind :: Adt ( adt, _) = referent. kind ( ) {
1237+ if let Some ( BridgedBuiltin :: Option ) = BridgedBuiltin :: new ( db, * adt) {
1238+ return Ok ( None ) ;
1239+ }
12241240 }
1241+ bail ! (
1242+ "Can't format reference type `{ty}` because the referent is a bridged type. \
1243+ Passing bridged types by reference is not supported."
1244+ )
12251245 }
1226- bail ! (
1227- "Can't format reference type `{ty}` because the referent is a bridged type. \
1228- Passing bridged types by reference is not supported."
1229- )
1246+ Ok ( None )
12301247 }
1231- ty:: TyKind :: RawPtr ( pointee, _) if is_bridged_type ( db, * pointee) ?. is_some ( ) => {
1232- bail ! (
1233- "Can't format pointer type `{ty}` because the pointee is a bridged type. \
1234- Passing bridged types by pointer is not supported."
1235- )
1248+ ty:: TyKind :: RawPtr ( pointee, _) => {
1249+ if is_bridged_type ( db, pointee) ?. is_some ( ) {
1250+ bail ! (
1251+ "Can't format pointer type `{ty}` because the pointee is a bridged type. \
1252+ Passing bridged types by pointer is not supported."
1253+ )
1254+ }
1255+ Ok ( None )
12361256 }
12371257 ty:: TyKind :: Adt ( adt, substs) => {
12381258 if let Some ( bridged_type) = is_manually_annotated_bridged_adt ( db, ty) ? {
12391259 return Ok ( Some ( bridged_type) ) ;
12401260 }
12411261
1242- if let Some ( bridged_builtin) = BridgedBuiltin :: new ( db, * adt) {
1262+ if let Some ( bridged_builtin) = BridgedBuiltin :: new ( db, adt) {
12431263 // The ADT is either a Result or an Option, which are composable bridged types.
12441264 let crubit_abi_type_with_cc_prereqs =
12451265 bridged_builtin. crubit_abi_type ( db, substs) ?;
@@ -1259,7 +1279,7 @@ pub fn is_bridged_type<'tcx>(
12591279 // The ADT does not need to be bridged, but check if it has generic types that
12601280 // need to be bridged e.g. Box<BridgedType> cannot be formated at
12611281 // the moment. If we encounter a type like this we return an error.
1262- for subst in * substs {
1282+ for subst in substs {
12631283 if let Some ( ty) = subst. as_type ( ) {
12641284 if is_bridged_type ( db, ty) ?. is_some ( ) {
12651285 bail ! ( "Can't format ADT as it has a generic type `{ty}` that is a bridged type" ) ;
0 commit comments