2121//! `ExpnData::call_site` in rustc, [`MacroCallLoc::call_site`] in rust-analyzer.
2222use std:: fmt;
2323
24- use crate :: { Edition , MacroCallId } ;
24+ use crate :: Edition ;
2525
2626/// A syntax context describes a hierarchy tracking order of macro definitions.
27+ #[ cfg( feature = "salsa" ) ]
2728#[ derive( Copy , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
2829pub struct SyntaxContext (
2930 salsa:: Id ,
3031 std:: marker:: PhantomData < & ' static salsa:: plumbing:: interned:: Value < SyntaxContext > > ,
3132) ;
3233
34+ #[ cfg( feature = "salsa" ) ]
3335const _: ( ) = {
36+ use crate :: MacroCallId ;
3437 use salsa:: plumbing as zalsa_;
3538 use salsa:: plumbing:: interned as zalsa_struct_;
3639
@@ -291,8 +294,6 @@ const _: () = {
291294} ;
292295
293296impl SyntaxContext {
294- const MAX_ID : u32 = salsa:: Id :: MAX_U32 - 1 ;
295-
296297 pub fn is_root ( self ) -> bool {
297298 ( SyntaxContext :: MAX_ID - Edition :: LATEST as u32 ) <= self . into_u32 ( )
298299 && self . into_u32 ( ) <= ( SyntaxContext :: MAX_ID - Edition :: Edition2015 as u32 )
@@ -308,20 +309,43 @@ impl SyntaxContext {
308309 /// The root context, which is the parent of all other contexts. All [`FileId`]s have this context.
309310 pub const fn root ( edition : Edition ) -> Self {
310311 let edition = edition as u32 ;
311- SyntaxContext (
312- salsa:: Id :: from_u32 ( SyntaxContext :: MAX_ID - edition) ,
313- std:: marker:: PhantomData ,
314- )
312+ SyntaxContext :: from_u32 ( SyntaxContext :: MAX_ID - edition)
315313 }
314+ }
315+
316+ #[ cfg( feature = "salsa" ) ]
317+ impl SyntaxContext {
318+ const MAX_ID : u32 = salsa:: Id :: MAX_U32 - 1 ;
316319
317- pub fn into_u32 ( self ) -> u32 {
320+ pub const fn into_u32 ( self ) -> u32 {
318321 self . 0 . as_u32 ( )
319322 }
320323
321- pub fn from_u32 ( u32 : u32 ) -> Self {
324+ pub const fn from_u32 ( u32 : u32 ) -> Self {
322325 Self ( salsa:: Id :: from_u32 ( u32) , std:: marker:: PhantomData )
323326 }
324327}
328+ #[ cfg( not( feature = "salsa" ) ) ]
329+ #[ derive( Copy , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
330+ pub struct SyntaxContext ( u32 ) ;
331+
332+ #[ allow( dead_code) ]
333+ const SALSA_MAX_ID_MIRROR : u32 = u32:: MAX - 0xFF ;
334+ #[ cfg( feature = "salsa" ) ]
335+ const _: ( ) = assert ! ( salsa:: Id :: MAX_U32 == SALSA_MAX_ID_MIRROR ) ;
336+
337+ #[ cfg( not( feature = "salsa" ) ) ]
338+ impl SyntaxContext {
339+ const MAX_ID : u32 = SALSA_MAX_ID_MIRROR - 1 ;
340+
341+ pub const fn into_u32 ( self ) -> u32 {
342+ self . 0
343+ }
344+
345+ pub const fn from_u32 ( u32 : u32 ) -> Self {
346+ Self ( u32)
347+ }
348+ }
325349
326350/// A property of a macro expansion that determines how identifiers
327351/// produced by that expansion are resolved.
@@ -354,9 +378,9 @@ impl Transparency {
354378impl fmt:: Display for SyntaxContext {
355379 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
356380 if self . is_root ( ) {
357- write ! ( f, "ROOT{}" , Edition :: from_u32( SyntaxContext :: MAX_ID - self . 0 . as_u32 ( ) ) . number( ) )
381+ write ! ( f, "ROOT{}" , Edition :: from_u32( SyntaxContext :: MAX_ID - self . into_u32 ( ) ) . number( ) )
358382 } else {
359- write ! ( f, "{}" , self . 0 . as_u32 ( ) )
383+ write ! ( f, "{}" , self . into_u32 ( ) )
360384 }
361385 }
362386}
0 commit comments