77
88use std:: iter;
99
10+ use base_db:: salsa:: { self , InternValue } ;
1011use span:: { MacroCallId , Span , SyntaxContextId } ;
1112
12- use crate :: db:: ExpandDatabase ;
13+ use crate :: db:: { ExpandDatabase , InternSyntaxContextQuery } ;
1314
1415#[ derive( Copy , Clone , Hash , PartialEq , Eq ) ]
1516pub struct SyntaxContextData {
@@ -22,6 +23,14 @@ pub struct SyntaxContextData {
2223 pub opaque_and_semitransparent : SyntaxContextId ,
2324}
2425
26+ impl InternValue for SyntaxContextData {
27+ type Key = ( SyntaxContextId , Option < MacroCallId > , Transparency ) ;
28+
29+ fn into_key ( & self ) -> Self :: Key {
30+ ( self . parent , self . outer_expn , self . outer_transparency )
31+ }
32+ }
33+
2534impl std:: fmt:: Debug for SyntaxContextData {
2635 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
2736 f. debug_struct ( "SyntaxContextData" )
@@ -149,38 +158,36 @@ fn apply_mark_internal(
149158 transparency : Transparency ,
150159) -> SyntaxContextId {
151160 let syntax_context_data = db. lookup_intern_syntax_context ( ctxt) ;
152- let mut opaque = handle_self_ref ( ctxt, syntax_context_data. opaque ) ;
153- let mut opaque_and_semitransparent =
154- handle_self_ref ( ctxt, syntax_context_data. opaque_and_semitransparent ) ;
161+ let mut opaque = syntax_context_data. opaque ;
162+ let mut opaque_and_semitransparent = syntax_context_data. opaque_and_semitransparent ;
155163
156164 if transparency >= Transparency :: Opaque {
157165 let parent = opaque;
158- // Unlike rustc, with salsa we can't prefetch the to be allocated ID to create cycles with
159- // salsa when interning, so we use a sentinel value that effectively means the current
160- // syntax context.
161- let new_opaque = SyntaxContextId :: SELF_REF ;
162- opaque = db. intern_syntax_context ( SyntaxContextData {
163- outer_expn : call_id,
164- outer_transparency : transparency,
165- parent,
166- opaque : new_opaque,
167- opaque_and_semitransparent : new_opaque,
168- } ) ;
166+ opaque = salsa:: plumbing:: get_query_table :: < InternSyntaxContextQuery > ( db) . get_or_insert (
167+ ( parent, call_id, transparency) ,
168+ |new_opaque| SyntaxContextData {
169+ outer_expn : call_id,
170+ outer_transparency : transparency,
171+ parent,
172+ opaque : new_opaque,
173+ opaque_and_semitransparent : new_opaque,
174+ } ,
175+ ) ;
169176 }
170177
171178 if transparency >= Transparency :: SemiTransparent {
172179 let parent = opaque_and_semitransparent;
173- // Unlike rustc, with salsa we can't prefetch the to be allocated ID to create cycles with
174- // salsa when interning, so we use a sentinel value that effectively means the current
175- // syntax context.
176- let new_opaque_and_semitransparent = SyntaxContextId :: SELF_REF ;
177- opaque_and_semitransparent = db . intern_syntax_context ( SyntaxContextData {
178- outer_expn : call_id ,
179- outer_transparency : transparency ,
180- parent ,
181- opaque ,
182- opaque_and_semitransparent : new_opaque_and_semitransparent ,
183- } ) ;
180+ opaque_and_semitransparent =
181+ salsa :: plumbing :: get_query_table :: < InternSyntaxContextQuery > ( db ) . get_or_insert (
182+ ( parent , call_id , transparency ) ,
183+ | new_opaque_and_semitransparent| SyntaxContextData {
184+ outer_expn : call_id ,
185+ outer_transparency : transparency ,
186+ parent ,
187+ opaque ,
188+ opaque_and_semitransparent : new_opaque_and_semitransparent ,
189+ } ,
190+ ) ;
184191 }
185192
186193 let parent = ctxt;
@@ -201,20 +208,12 @@ pub trait SyntaxContextExt {
201208 fn marks ( self , db : & dyn ExpandDatabase ) -> Vec < ( Option < MacroCallId > , Transparency ) > ;
202209}
203210
204- #[ inline( always) ]
205- fn handle_self_ref ( p : SyntaxContextId , n : SyntaxContextId ) -> SyntaxContextId {
206- match n {
207- SyntaxContextId :: SELF_REF => p,
208- _ => n,
209- }
210- }
211-
212211impl SyntaxContextExt for SyntaxContextId {
213212 fn normalize_to_macro_rules ( self , db : & dyn ExpandDatabase ) -> Self {
214- handle_self_ref ( self , db. lookup_intern_syntax_context ( self ) . opaque_and_semitransparent )
213+ db. lookup_intern_syntax_context ( self ) . opaque_and_semitransparent
215214 }
216215 fn normalize_to_macros_2_0 ( self , db : & dyn ExpandDatabase ) -> Self {
217- handle_self_ref ( self , db. lookup_intern_syntax_context ( self ) . opaque )
216+ db. lookup_intern_syntax_context ( self ) . opaque
218217 }
219218 fn parent_ctxt ( self , db : & dyn ExpandDatabase ) -> Self {
220219 db. lookup_intern_syntax_context ( self ) . parent
0 commit comments