@@ -15,6 +15,7 @@ use crate::{
1515 } ,
1616 dc_mod,
1717 } ,
18+ def_map:: DefMaps ,
1819 resolution:: { errors:: ResolverError , path_resolver:: PathResolver } ,
1920 scope:: ScopeForest as GenericScopeForest ,
2021 type_check:: TypeCheckError ,
@@ -54,7 +55,7 @@ use crate::{
5455use crate :: {
5556 hir:: {
5657 def_collector:: dc_crate:: { UnresolvedFunctions , UnresolvedTraitImpl } ,
57- def_map:: { CrateDefMap , ModuleData } ,
58+ def_map:: ModuleData ,
5859 } ,
5960 hir_def:: traits:: TraitImpl ,
6061 macros_api:: ItemVisibility ,
@@ -102,7 +103,7 @@ pub struct Elaborator<'context> {
102103
103104 pub ( crate ) interner : & ' context mut NodeInterner ,
104105
105- def_maps : & ' context mut BTreeMap < CrateId , CrateDefMap > ,
106+ def_maps : & ' context mut DefMaps ,
106107
107108 file : FileId ,
108109
@@ -130,8 +131,6 @@ pub struct Elaborator<'context> {
130131 /// to the corresponding trait impl ID.
131132 current_trait_impl : Option < TraitImplId > ,
132133
133- trait_id : Option < TraitId > ,
134-
135134 /// In-resolution names
136135 ///
137136 /// This needs to be a set because we can have multiple in-resolution
@@ -195,22 +194,22 @@ struct FunctionContext {
195194
196195impl < ' context > Elaborator < ' context > {
197196 pub fn new (
198- context : & ' context mut Context ,
197+ interner : & ' context mut NodeInterner ,
198+ def_maps : & ' context mut DefMaps ,
199199 crate_id : CrateId ,
200200 debug_comptime_in_file : Option < FileId > ,
201201 ) -> Self {
202202 Self {
203203 scopes : ScopeForest :: default ( ) ,
204204 errors : Vec :: new ( ) ,
205- interner : & mut context . def_interner ,
206- def_maps : & mut context . def_maps ,
205+ interner,
206+ def_maps,
207207 file : FileId :: dummy ( ) ,
208208 nested_loops : 0 ,
209209 generics : Vec :: new ( ) ,
210210 lambda_stack : Vec :: new ( ) ,
211211 self_type : None ,
212212 current_item : None ,
213- trait_id : None ,
214213 local_module : LocalModuleId :: dummy_id ( ) ,
215214 crate_id,
216215 resolving_ids : BTreeSet :: new ( ) ,
@@ -223,6 +222,19 @@ impl<'context> Elaborator<'context> {
223222 }
224223 }
225224
225+ pub fn from_context (
226+ context : & ' context mut Context ,
227+ crate_id : CrateId ,
228+ debug_comptime_in_file : Option < FileId > ,
229+ ) -> Self {
230+ Self :: new (
231+ & mut context. def_interner ,
232+ & mut context. def_maps ,
233+ crate_id,
234+ debug_comptime_in_file,
235+ )
236+ }
237+
226238 pub fn elaborate (
227239 context : & ' context mut Context ,
228240 crate_id : CrateId ,
@@ -238,7 +250,7 @@ impl<'context> Elaborator<'context> {
238250 items : CollectedItems ,
239251 debug_comptime_in_file : Option < FileId > ,
240252 ) -> Self {
241- let mut this = Self :: new ( context, crate_id, debug_comptime_in_file) ;
253+ let mut this = Self :: from_context ( context, crate_id, debug_comptime_in_file) ;
242254
243255 // Filter out comptime items to execute their functions first if needed.
244256 // This step is why comptime items can only refer to other comptime items
@@ -337,17 +349,12 @@ impl<'context> Elaborator<'context> {
337349 }
338350
339351 fn elaborate_functions ( & mut self , functions : UnresolvedFunctions ) {
340- self . file = functions. file_id ;
341- self . trait_id = functions. trait_id ; // TODO: Resolve?
342- self . self_type = functions. self_type ;
343-
344- for ( local_module, id, _) in functions. functions {
345- self . local_module = local_module;
346- self . recover_generics ( |this| this. elaborate_function ( id) ) ;
352+ for ( _, id, _) in functions. functions {
353+ self . elaborate_function ( id) ;
347354 }
348355
356+ self . generics . clear ( ) ;
349357 self . self_type = None ;
350- self . trait_id = None ;
351358 }
352359
353360 fn introduce_generics_into_scope ( & mut self , all_generics : Vec < ResolvedGeneric > ) {
@@ -365,7 +372,7 @@ impl<'context> Elaborator<'context> {
365372 self . generics = all_generics;
366373 }
367374
368- fn elaborate_function ( & mut self , id : FuncId ) {
375+ pub ( crate ) fn elaborate_function ( & mut self , id : FuncId ) {
369376 let func_meta = self . interner . func_meta . get_mut ( & id) ;
370377 let func_meta =
371378 func_meta. expect ( "FuncMetas should be declared before a function is elaborated" ) ;
@@ -378,11 +385,21 @@ impl<'context> Elaborator<'context> {
378385 FunctionBody :: Resolving => return ,
379386 } ;
380387
388+ let func_meta = func_meta. clone ( ) ;
389+
390+ assert_eq ! (
391+ self . crate_id, func_meta. source_crate,
392+ "Functions in other crates should be already elaborated"
393+ ) ;
394+
395+ self . local_module = func_meta. source_module ;
396+ self . file = func_meta. source_file ;
397+ self . self_type = func_meta. self_type . clone ( ) ;
398+ self . current_trait_impl = func_meta. trait_impl ;
399+
381400 self . scopes . start_function ( ) ;
382401 let old_item = std:: mem:: replace ( & mut self . current_item , Some ( DependencyId :: Function ( id) ) ) ;
383402
384- let func_meta = func_meta. clone ( ) ;
385-
386403 self . trait_bounds = func_meta. trait_constraints . clone ( ) ;
387404 self . function_context . push ( FunctionContext :: default ( ) ) ;
388405
@@ -775,6 +792,8 @@ impl<'context> Elaborator<'context> {
775792 source_crate : self . crate_id ,
776793 source_module : self . local_module ,
777794 function_body : FunctionBody :: Unresolved ( func. kind , body, func. def . span ) ,
795+ self_type : self . self_type . clone ( ) ,
796+ source_file : self . file ,
778797 } ;
779798
780799 self . interner . push_fn_meta ( meta, func_id) ;
@@ -1003,10 +1022,7 @@ impl<'context> Elaborator<'context> {
10031022 self . self_type = None ;
10041023 }
10051024
1006- fn get_module_mut (
1007- def_maps : & mut BTreeMap < CrateId , CrateDefMap > ,
1008- module : ModuleId ,
1009- ) -> & mut ModuleData {
1025+ fn get_module_mut ( def_maps : & mut DefMaps , module : ModuleId ) -> & mut ModuleData {
10101026 let message = "A crate should always be present for a given crate id" ;
10111027 & mut def_maps. get_mut ( & module. krate ) . expect ( message) . modules [ module. local_id . 0 ]
10121028 }
@@ -1528,19 +1544,23 @@ impl<'context> Elaborator<'context> {
15281544 let ( comptime_structs, structs) =
15291545 items. types . into_iter ( ) . partition ( |typ| typ. 1 . struct_def . is_comptime ) ;
15301546
1547+ let ( comptime_globals, globals) =
1548+ items. globals . into_iter ( ) . partition ( |global| global. stmt_def . comptime ) ;
1549+
15311550 let comptime = CollectedItems {
15321551 functions : comptime_function_sets,
15331552 types : comptime_structs,
15341553 type_aliases : BTreeMap :: new ( ) ,
15351554 traits : BTreeMap :: new ( ) ,
15361555 trait_impls : comptime_trait_impls,
1537- globals : Vec :: new ( ) ,
1556+ globals : comptime_globals ,
15381557 impls : rustc_hash:: FxHashMap :: default ( ) ,
15391558 } ;
15401559
15411560 items. functions = function_sets;
15421561 items. trait_impls = trait_impls;
15431562 items. types = structs;
1563+ items. globals = globals;
15441564 ( comptime, items)
15451565 }
15461566
0 commit comments