21
21
//! `ExpnData::call_site` in rustc, [`MacroCallLoc::call_site`] in rust-analyzer.
22
22
use std:: fmt;
23
23
24
- use crate :: { Edition , MacroCallId } ;
24
+ use crate :: Edition ;
25
25
26
26
/// A syntax context describes a hierarchy tracking order of macro definitions.
27
+ #[ cfg( feature = "salsa" ) ]
27
28
#[ derive( Copy , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
28
29
pub struct SyntaxContext (
29
30
salsa:: Id ,
30
31
std:: marker:: PhantomData < & ' static salsa:: plumbing:: interned:: Value < SyntaxContext > > ,
31
32
) ;
32
33
34
+ #[ cfg( feature = "salsa" ) ]
33
35
const _: ( ) = {
36
+ use crate :: MacroCallId ;
34
37
use salsa:: plumbing as zalsa_;
35
38
use salsa:: plumbing:: interned as zalsa_struct_;
36
39
@@ -291,8 +294,6 @@ const _: () = {
291
294
} ;
292
295
293
296
impl SyntaxContext {
294
- const MAX_ID : u32 = salsa:: Id :: MAX_U32 - 1 ;
295
-
296
297
pub fn is_root ( self ) -> bool {
297
298
( SyntaxContext :: MAX_ID - Edition :: LATEST as u32 ) <= self . into_u32 ( )
298
299
&& self . into_u32 ( ) <= ( SyntaxContext :: MAX_ID - Edition :: Edition2015 as u32 )
@@ -308,20 +309,43 @@ impl SyntaxContext {
308
309
/// The root context, which is the parent of all other contexts. All [`FileId`]s have this context.
309
310
pub const fn root ( edition : Edition ) -> Self {
310
311
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)
315
313
}
314
+ }
315
+
316
+ #[ cfg( feature = "salsa" ) ]
317
+ impl SyntaxContext {
318
+ const MAX_ID : u32 = salsa:: Id :: MAX_U32 - 1 ;
316
319
317
- pub fn into_u32 ( self ) -> u32 {
320
+ pub const fn into_u32 ( self ) -> u32 {
318
321
self . 0 . as_u32 ( )
319
322
}
320
323
321
- pub fn from_u32 ( u32 : u32 ) -> Self {
324
+ pub const fn from_u32 ( u32 : u32 ) -> Self {
322
325
Self ( salsa:: Id :: from_u32 ( u32) , std:: marker:: PhantomData )
323
326
}
324
327
}
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
+ }
325
349
326
350
/// A property of a macro expansion that determines how identifiers
327
351
/// produced by that expansion are resolved.
@@ -354,9 +378,9 @@ impl Transparency {
354
378
impl fmt:: Display for SyntaxContext {
355
379
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
356
380
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( ) )
358
382
} else {
359
- write ! ( f, "{}" , self . 0 . as_u32 ( ) )
383
+ write ! ( f, "{}" , self . into_u32 ( ) )
360
384
}
361
385
}
362
386
}
0 commit comments