@@ -150,6 +150,7 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
150150 let nativeCallPrefix = '' ;
151151 let nativeCallWrapperPrefix = '' , nativeCallWrapperSuffix = '' ;
152152 let nativeCallSuffix = '' ;
153+ const anchors = [ ] ;
153154
154155 const semantics = this . methodSemantics ( method , containerType ) ;
155156 if ( semantics . isConstructor && forceStaticConstructor ) {
@@ -193,6 +194,9 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
193194 nativeCallWrapperSuffix += preparedArgument . methodCallWrapperSuffix ;
194195 nativeCallValueAccessors . push ( preparedArgument . accessor ) ;
195196 nativeCallSuffix += preparedArgument . deferredCleanup ;
197+ if ( preparedArgument . requiresAnchoring ) {
198+ anchors . push ( swiftArgumentName ) ;
199+ }
196200 }
197201
198202 let cloneabilityDeprecationWarning = '' ;
@@ -226,11 +230,13 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
226230 visibility = 'internal' ;
227231 }
228232
233+ let anchoringCommand = anchors . map ( a => `try! returnValue.addAnchor(anchor: ${ a } )\n` ) . join ( '\t\t\t\t\t\t' ) ;
229234 let methodDeclarationKeywords = `${ visibility } ${ staticInfix } func` ;
230- let returnCommand = ' return returnValue' ;
235+ let returnCommand = ` ${ anchoringCommand } return returnValue` ;
231236 let returnValueHandlingPrefix = '' ;
232237 let returnValueHandlingSuffix = '' ;
233238 if ( swiftMethodName === 'init' ) {
239+ anchoringCommand = anchoringCommand . replaceAll ( 'returnValue.addAnchor(' , 'self.addAnchor(' )
234240 // it's a constructor
235241 methodDeclarationKeywords = visibility ;
236242 returnCommand = `
@@ -239,6 +245,7 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
239245 Self.instanceCounter += 1
240246 self.instanceNumber = Self.instanceCounter
241247 super.init(conflictAvoidingVariableName: 0)
248+ ${ anchoringCommand }
242249 ` ;
243250
244251 returnValueHandlingPrefix = '/*' ;
@@ -625,7 +632,8 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
625632 methodCallWrapperPrefix : '' ,
626633 methodCallWrapperSuffix : '' ,
627634
628- deferredCleanup : ''
635+ deferredCleanup : '' ,
636+ requiresAnchoring : false
629637 } ;
630638
631639 // this argument is the content of an elided container, like the iteratee of a Vec
@@ -807,6 +815,10 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
807815 }
808816 ` ;
809817
818+ if ( memoryContext && memoryContext . isConstructor && ( argument . type instanceof RustStruct || argument . type instanceof RustTaggedValueEnum || argument . type instanceof RustResult ) ) {
819+ preparedArgument . requiresAnchoring = true ;
820+ }
821+
810822 // the wrapper accesses the variable normally, and introduces a new variable name by which to refer to the value
811823 preparedArgument . accessor = preparedArgument . name ;
812824 }
@@ -1284,6 +1296,11 @@ export interface PreparedArgument {
12841296 * memory deallocation or, ironically, retention beyond the call site
12851297 */
12861298 deferredCleanup : string
1299+
1300+ /**
1301+ * If true (only applicable in constructors), add a `self.addAnchor` call after super.init()
1302+ */
1303+ requiresAnchoring : boolean
12871304}
12881305
12891306export interface PreparedReturnValue {
0 commit comments