@@ -2321,54 +2321,99 @@ impl ExprCollector<'_> {
2321
2321
zero_pad,
2322
2322
debug_hex,
2323
2323
} = & placeholder. format_options ;
2324
- let fill = self . alloc_expr_desugared ( Expr :: Literal ( Literal :: Char ( fill. unwrap_or ( ' ' ) ) ) ) ;
2325
2324
2326
- let align = {
2327
- let align = LangItem :: FormatAlignment . ty_rel_path (
2328
- self . db ,
2329
- self . krate ,
2330
- match alignment {
2331
- Some ( FormatAlignment :: Left ) => Name :: new_symbol_root ( sym:: Left . clone ( ) ) ,
2332
- Some ( FormatAlignment :: Right ) => Name :: new_symbol_root ( sym:: Right . clone ( ) ) ,
2333
- Some ( FormatAlignment :: Center ) => Name :: new_symbol_root ( sym:: Center . clone ( ) ) ,
2334
- None => Name :: new_symbol_root ( sym:: Unknown . clone ( ) ) ,
2335
- } ,
2336
- ) ;
2337
- match align {
2338
- Some ( path) => self . alloc_expr_desugared ( Expr :: Path ( path) ) ,
2339
- None => self . missing_expr ( ) ,
2340
- }
2341
- } ;
2342
- // This needs to match `Flag` in library/core/src/fmt/rt.rs.
2343
- let flags: u32 = ( ( sign == Some ( FormatSign :: Plus ) ) as u32 )
2344
- | ( ( ( sign == Some ( FormatSign :: Minus ) ) as u32 ) << 1 )
2345
- | ( ( alternate as u32 ) << 2 )
2346
- | ( ( zero_pad as u32 ) << 3 )
2347
- | ( ( ( debug_hex == Some ( FormatDebugHex :: Lower ) ) as u32 ) << 4 )
2348
- | ( ( ( debug_hex == Some ( FormatDebugHex :: Upper ) ) as u32 ) << 5 ) ;
2349
- let flags = self . alloc_expr_desugared ( Expr :: Literal ( Literal :: Uint (
2350
- flags as u128 ,
2351
- Some ( BuiltinUint :: U32 ) ,
2352
- ) ) ) ;
2353
- let precision = self . make_count ( precision, argmap) ;
2354
- let width = self . make_count ( width, argmap) ;
2325
+ let precision_expr = self . make_count ( precision, argmap) ;
2326
+ let width_expr = self . make_count ( width, argmap) ;
2355
2327
2356
- let format_placeholder_new = {
2357
- let format_placeholder_new = LangItem :: FormatPlaceholder . ty_rel_path (
2358
- self . db ,
2359
- self . krate ,
2360
- Name :: new_symbol_root ( sym:: new. clone ( ) ) ,
2361
- ) ;
2362
- match format_placeholder_new {
2363
- Some ( path) => self . alloc_expr_desugared ( Expr :: Path ( path) ) ,
2364
- None => self . missing_expr ( ) ,
2365
- }
2366
- } ;
2367
-
2368
- self . alloc_expr_desugared ( Expr :: Call {
2369
- callee : format_placeholder_new,
2370
- args : Box :: new ( [ position, fill, align, flags, precision, width] ) ,
2371
- } )
2328
+ if self . krate . workspace_data ( self . db ) . is_atleast_187 ( ) {
2329
+ // These need to match the constants in library/core/src/fmt/rt.rs.
2330
+ let align = match alignment {
2331
+ Some ( FormatAlignment :: Left ) => 0 ,
2332
+ Some ( FormatAlignment :: Right ) => 1 ,
2333
+ Some ( FormatAlignment :: Center ) => 2 ,
2334
+ None => 3 ,
2335
+ } ;
2336
+ // This needs to match `Flag` in library/core/src/fmt/rt.rs.
2337
+ let flags = fill. unwrap_or ( ' ' ) as u32
2338
+ | ( ( sign == Some ( FormatSign :: Plus ) ) as u32 ) << 21
2339
+ | ( ( sign == Some ( FormatSign :: Minus ) ) as u32 ) << 22
2340
+ | ( alternate as u32 ) << 23
2341
+ | ( zero_pad as u32 ) << 24
2342
+ | ( ( debug_hex == Some ( FormatDebugHex :: Lower ) ) as u32 ) << 25
2343
+ | ( ( debug_hex == Some ( FormatDebugHex :: Upper ) ) as u32 ) << 26
2344
+ | ( width. is_some ( ) as u32 ) << 27
2345
+ | ( precision. is_some ( ) as u32 ) << 28
2346
+ | align << 29
2347
+ | 1 << 31 ; // Highest bit always set.
2348
+ let flags = self . alloc_expr_desugared ( Expr :: Literal ( Literal :: Uint (
2349
+ flags as u128 ,
2350
+ Some ( BuiltinUint :: U32 ) ,
2351
+ ) ) ) ;
2352
+
2353
+ let position = RecordLitField {
2354
+ name : Name :: new_symbol_root ( sym:: position. clone ( ) ) ,
2355
+ expr : position,
2356
+ } ;
2357
+ let flags =
2358
+ RecordLitField { name : Name :: new_symbol_root ( sym:: flags. clone ( ) ) , expr : flags } ;
2359
+ let precision = RecordLitField {
2360
+ name : Name :: new_symbol_root ( sym:: precision. clone ( ) ) ,
2361
+ expr : precision_expr,
2362
+ } ;
2363
+ let width = RecordLitField {
2364
+ name : Name :: new_symbol_root ( sym:: width. clone ( ) ) ,
2365
+ expr : width_expr,
2366
+ } ;
2367
+ self . alloc_expr_desugared ( Expr :: RecordLit {
2368
+ path : LangItem :: FormatPlaceholder . path ( self . db , self . krate ) . map ( Box :: new) ,
2369
+ fields : Box :: new ( [ position, flags, precision, width] ) ,
2370
+ spread : None ,
2371
+ } )
2372
+ } else {
2373
+ let format_placeholder_new = {
2374
+ let format_placeholder_new = LangItem :: FormatPlaceholder . ty_rel_path (
2375
+ self . db ,
2376
+ self . krate ,
2377
+ Name :: new_symbol_root ( sym:: new. clone ( ) ) ,
2378
+ ) ;
2379
+ match format_placeholder_new {
2380
+ Some ( path) => self . alloc_expr_desugared ( Expr :: Path ( path) ) ,
2381
+ None => self . missing_expr ( ) ,
2382
+ }
2383
+ } ;
2384
+ // This needs to match `Flag` in library/core/src/fmt/rt.rs.
2385
+ let flags: u32 = ( ( sign == Some ( FormatSign :: Plus ) ) as u32 )
2386
+ | ( ( ( sign == Some ( FormatSign :: Minus ) ) as u32 ) << 1 )
2387
+ | ( ( alternate as u32 ) << 2 )
2388
+ | ( ( zero_pad as u32 ) << 3 )
2389
+ | ( ( ( debug_hex == Some ( FormatDebugHex :: Lower ) ) as u32 ) << 4 )
2390
+ | ( ( ( debug_hex == Some ( FormatDebugHex :: Upper ) ) as u32 ) << 5 ) ;
2391
+ let flags = self . alloc_expr_desugared ( Expr :: Literal ( Literal :: Uint (
2392
+ flags as u128 ,
2393
+ Some ( BuiltinUint :: U32 ) ,
2394
+ ) ) ) ;
2395
+ let fill = self . alloc_expr_desugared ( Expr :: Literal ( Literal :: Char ( fill. unwrap_or ( ' ' ) ) ) ) ;
2396
+ let align = {
2397
+ let align = LangItem :: FormatAlignment . ty_rel_path (
2398
+ self . db ,
2399
+ self . krate ,
2400
+ match alignment {
2401
+ Some ( FormatAlignment :: Left ) => Name :: new_symbol_root ( sym:: Left . clone ( ) ) ,
2402
+ Some ( FormatAlignment :: Right ) => Name :: new_symbol_root ( sym:: Right . clone ( ) ) ,
2403
+ Some ( FormatAlignment :: Center ) => Name :: new_symbol_root ( sym:: Center . clone ( ) ) ,
2404
+ None => Name :: new_symbol_root ( sym:: Unknown . clone ( ) ) ,
2405
+ } ,
2406
+ ) ;
2407
+ match align {
2408
+ Some ( path) => self . alloc_expr_desugared ( Expr :: Path ( path) ) ,
2409
+ None => self . missing_expr ( ) ,
2410
+ }
2411
+ } ;
2412
+ self . alloc_expr_desugared ( Expr :: Call {
2413
+ callee : format_placeholder_new,
2414
+ args : Box :: new ( [ position, fill, align, flags, precision_expr, width_expr] ) ,
2415
+ } )
2416
+ }
2372
2417
}
2373
2418
2374
2419
/// Generate a hir expression for a format_args Count.
0 commit comments