@@ -81,7 +81,6 @@ pub struct Queries<'comp> {
8181 prepare_outputs : Query < OutputFilenames > ,
8282 global_ctxt : Query < BoxedGlobalCtxt > ,
8383 ongoing_codegen : Query < Box < dyn Any > > ,
84- link : Query < ( ) > ,
8584}
8685
8786impl < ' comp > Queries < ' comp > {
@@ -98,7 +97,6 @@ impl<'comp> Queries<'comp> {
9897 prepare_outputs : Default :: default ( ) ,
9998 global_ctxt : Default :: default ( ) ,
10099 ongoing_codegen : Default :: default ( ) ,
101- link : Default :: default ( ) ,
102100 }
103101 }
104102
@@ -278,35 +276,54 @@ impl<'comp> Queries<'comp> {
278276 } )
279277 }
280278
281- pub fn link ( & self ) -> Result < & Query < ( ) > > {
282- self . link . compute ( || {
283- let sess = self . session ( ) ;
279+ pub fn linker ( self ) -> Result < Linker > {
280+ let dep_graph = self . dep_graph ( ) ?;
281+ let prepare_outputs = self . prepare_outputs ( ) ?;
282+ let ongoing_codegen = self . ongoing_codegen ( ) ?;
284283
285- let ongoing_codegen = self . ongoing_codegen ( ) ?. take ( ) ;
284+ let sess = self . session ( ) . clone ( ) ;
285+ let codegen_backend = self . codegen_backend ( ) . clone ( ) ;
286286
287- self . codegen_backend ( ) . join_codegen_and_link (
288- ongoing_codegen,
289- sess,
290- & * self . dep_graph ( ) ?. peek ( ) ,
291- & * self . prepare_outputs ( ) ?. peek ( ) ,
292- ) . map_err ( |_| ErrorReported ) ?;
293-
294- Ok ( ( ) )
287+ Ok ( Linker {
288+ sess,
289+ dep_graph : dep_graph. take ( ) ,
290+ prepare_outputs : prepare_outputs. take ( ) ,
291+ ongoing_codegen : ongoing_codegen. take ( ) ,
292+ codegen_backend,
295293 } )
296294 }
297295}
298296
297+ pub struct Linker {
298+ sess : Lrc < Session > ,
299+ dep_graph : DepGraph ,
300+ prepare_outputs : OutputFilenames ,
301+ ongoing_codegen : Box < dyn Any > ,
302+ codegen_backend : Lrc < Box < dyn CodegenBackend > > ,
303+ }
304+
305+ impl Linker {
306+ pub fn link ( self ) -> Result < ( ) > {
307+ self . codegen_backend . join_codegen_and_link (
308+ self . ongoing_codegen ,
309+ & self . sess ,
310+ & self . dep_graph ,
311+ & self . prepare_outputs ,
312+ ) . map_err ( |_| ErrorReported )
313+ }
314+ }
315+
299316impl Compiler {
300317 // This method is different to all the other methods in `Compiler` because
301318 // it lacks a `Queries` entry. It's also not currently used. It does serve
302319 // as an example of how `Compiler` can be used, with additional steps added
303320 // between some passes. And see `rustc_driver::run_compiler` for a more
304321 // complex example.
305322 pub fn enter < ' c , F , T > ( & ' c self , f : F ) -> Result < T >
306- where F : for < ' q > FnOnce ( & ' q Queries < ' c > ) -> Result < T >
323+ where F : FnOnce ( Queries < ' c > ) -> Result < T >
307324 {
308325 let queries = Queries :: new ( & self ) ;
309- f ( & queries)
326+ f ( queries)
310327 }
311328
312329 // This method is different to all the other methods in `Compiler` because
@@ -315,13 +332,13 @@ impl Compiler {
315332 // between some passes. And see `rustc_driver::run_compiler` for a more
316333 // complex example.
317334 pub fn compile ( & self ) -> Result < ( ) > {
318- self . enter ( |queries| {
335+ let linker = self . enter ( |queries| {
319336 queries. prepare_outputs ( ) ?;
320337
321338 if self . session ( ) . opts . output_types . contains_key ( & OutputType :: DepInfo )
322339 && self . session ( ) . opts . output_types . len ( ) == 1
323340 {
324- return Ok ( ( ) )
341+ return Ok ( None )
325342 }
326343
327344 queries. global_ctxt ( ) ?;
@@ -334,7 +351,14 @@ impl Compiler {
334351 // Drop GlobalCtxt after starting codegen to free memory.
335352 mem:: drop ( queries. global_ctxt ( ) ?. take ( ) ) ;
336353
337- queries. link ( ) . map ( |_| ( ) )
338- } )
354+ let linker = queries. linker ( ) ?;
355+ Ok ( Some ( linker) )
356+ } ) ?;
357+
358+ if let Some ( linker) = linker {
359+ linker. link ( ) ?
360+ }
361+
362+ Ok ( ( ) )
339363 }
340364}
0 commit comments