Skip to content

Commit 2956a7d

Browse files
committed
Use usize in format_args!() and have expansion for 16-bit.
1 parent 3a5fb69 commit 2956a7d

File tree

4 files changed

+42
-57
lines changed

4 files changed

+42
-57
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -2129,29 +2129,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21292129
self.arena.alloc(self.expr(sp, hir::ExprKind::Tup(&[])))
21302130
}
21312131

2132-
pub(super) fn expr_u64(&mut self, sp: Span, value: u64) -> hir::Expr<'hir> {
2133-
let lit = self.arena.alloc(hir::Lit {
2134-
span: sp,
2135-
node: ast::LitKind::Int(
2136-
u128::from(value).into(),
2137-
ast::LitIntType::Unsigned(ast::UintTy::U64),
2138-
),
2139-
});
2140-
self.expr(sp, hir::ExprKind::Lit(lit))
2141-
}
2142-
2143-
pub(super) fn expr_u32(&mut self, sp: Span, value: u32) -> hir::Expr<'hir> {
2144-
let lit = self.arena.alloc(hir::Lit {
2145-
span: sp,
2146-
node: ast::LitKind::Int(
2147-
u128::from(value).into(),
2148-
ast::LitIntType::Unsigned(ast::UintTy::U32),
2149-
),
2150-
});
2151-
self.expr(sp, hir::ExprKind::Lit(lit))
2152-
}
2153-
2154-
pub(super) fn expr_usize(&mut self, sp: Span, value: u16) -> hir::Expr<'hir> {
2132+
pub(super) fn expr_usize(&mut self, sp: Span, value: u64) -> hir::Expr<'hir> {
21552133
let lit = self.arena.alloc(hir::Lit {
21562134
span: sp,
21572135
node: ast::LitKind::Int(

compiler/rustc_ast_lowering/src/format.rs

+25-17
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,7 @@ fn expand_format_args<'hir>(
417417
// ```
418418
// Piece::num(4),
419419
// ```
420-
let i = if ctx.tcx.sess.target.pointer_width >= 64 {
421-
ctx.expr_u64(fmt.span, len as u64)
422-
} else {
423-
ctx.expr_u32(fmt.span, len as u32)
424-
};
420+
let i = ctx.expr_usize(macsp, len as u64);
425421
pieces.push(make_piece(ctx, sym::num, i, macsp));
426422

427423
// ```
@@ -434,11 +430,18 @@ fn expand_format_args<'hir>(
434430
// ```
435431
// Piece::num(0xE000_0020_0000_0000),
436432
// ```
437-
// Or, on <64 bit platforms:
433+
// Or, on 32 bit platforms:
438434
// ```
439435
// Piece::num(0xE000_0020),
440436
// Piece::num(0x0000_0000),
441437
// ```
438+
// Or, on 16 bit platforms:
439+
// ```
440+
// Piece::num(0xE000),
441+
// Piece::num(0x0020),
442+
// Piece::num(0x0000),
443+
// Piece::num(0x0000),
444+
// ```
442445

443446
let bits = make_format_spec(p, &mut argmap);
444447

@@ -455,13 +458,22 @@ fn expand_format_args<'hir>(
455458
}
456459

457460
if ctx.tcx.sess.target.pointer_width >= 64 {
458-
let bits = ctx.expr_u64(fmt.span, bits);
461+
let bits = ctx.expr_usize(macsp, bits);
459462
pieces.push(make_piece(ctx, sym::num, bits, macsp));
460-
} else {
461-
let high = ctx.expr_u32(fmt.span, (bits >> 32) as u32);
462-
let low = ctx.expr_u32(fmt.span, bits as u32);
463+
} else if ctx.tcx.sess.target.pointer_width >= 32 {
464+
let high = ctx.expr_usize(macsp, bits >> 32);
465+
let low = ctx.expr_usize(macsp, bits & 0xFFFF_FFFF);
463466
pieces.push(make_piece(ctx, sym::num, high, macsp));
464467
pieces.push(make_piece(ctx, sym::num, low, macsp));
468+
} else {
469+
let w1 = ctx.expr_usize(macsp, bits >> 48);
470+
let w2 = ctx.expr_usize(macsp, bits >> 32 & 0xFFFF);
471+
let w3 = ctx.expr_usize(macsp, bits >> 16 & 0xFFFF);
472+
let w4 = ctx.expr_usize(macsp, bits & 0xFFFF);
473+
pieces.push(make_piece(ctx, sym::num, w1, macsp));
474+
pieces.push(make_piece(ctx, sym::num, w2, macsp));
475+
pieces.push(make_piece(ctx, sym::num, w3, macsp));
476+
pieces.push(make_piece(ctx, sym::num, w4, macsp));
465477
}
466478

467479
implicit_arg_index = (bits & 0x3FF) + 1;
@@ -476,11 +488,7 @@ fn expand_format_args<'hir>(
476488
// ```
477489
// Piece::num(0),
478490
// ```
479-
let zero = if ctx.tcx.sess.target.pointer_width >= 64 {
480-
ctx.expr_u64(fmt.span, 0)
481-
} else {
482-
ctx.expr_u32(fmt.span, 0)
483-
};
491+
let zero = ctx.expr_usize(macsp, 0);
484492
pieces.push(make_piece(ctx, sym::num, zero, macsp));
485493

486494
// ```
@@ -565,7 +573,7 @@ fn expand_format_args<'hir>(
565573
let elements =
566574
ctx.arena.alloc_from_iter(argmap.iter().map(|(&(arg_index, ty), placeholder_span)| {
567575
if let ArgumentType::Constant(c) = ty {
568-
let arg = ctx.arena.alloc(ctx.expr_usize(macsp, c));
576+
let arg = ctx.arena.alloc(ctx.expr_usize(macsp, c.into()));
569577
let arg = ctx.arena.alloc(ctx.expr(
570578
macsp,
571579
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg),
@@ -609,7 +617,7 @@ fn expand_format_args<'hir>(
609617
let args = ctx.arena.alloc_from_iter(argmap.iter().map(
610618
|(&(arg_index, ty), &placeholder_span)| {
611619
if let ArgumentType::Constant(c) = ty {
612-
let arg = ctx.arena.alloc(ctx.expr_usize(macsp, c));
620+
let arg = ctx.arena.alloc(ctx.expr_usize(macsp, c.into()));
613621
let arg = ctx.arena.alloc(ctx.expr(
614622
macsp,
615623
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg),

library/core/src/fmt/mod.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,11 @@ impl<'a> Arguments<'a> {
743743
starts_with_placeholder = true;
744744
}
745745
has_placeholders = true;
746-
#[cfg(not(target_pointer_width = "64"))]
747-
let _ = template.next(); // Skip second half of placeholder.
746+
// Skip remainder of placeholder:
747+
#[cfg(target_pointer_width = "32")]
748+
let _ = template.next();
749+
#[cfg(target_pointer_width = "16")]
750+
let _ = (template.next(), template.next(), template.next());
748751
}
749752
}
750753
}
@@ -1701,9 +1704,17 @@ pub fn write(output: &mut dyn Write, fmt: Arguments<'_>) -> Result {
17011704
// Placeholder piece.
17021705
#[cfg(target_pointer_width = "64")]
17031706
let (high, low) = ((n >> 32) as u32, n as u32);
1704-
#[cfg(not(target_pointer_width = "64"))]
1707+
#[cfg(target_pointer_width = "32")]
17051708
// SAFETY: We can assume the template is valid.
17061709
let (high, low) = (n as u32, unsafe { template.next().i } as u32);
1710+
#[cfg(target_pointer_width = "16")]
1711+
// SAFETY: We can assume the template is valid.
1712+
let (high, low) = unsafe {
1713+
(
1714+
(n as u32) << 16 | template.next().i as u32,
1715+
(template.next().i as u32) << 16 | template.next().i as u32,
1716+
)
1717+
};
17071718
let arg_index = (low & 0x3FF) as usize;
17081719
let mut width = (low >> 10 & 0x3FF) as u16;
17091720
let mut precision = (low >> 20 & 0x3FF) as u16;

library/core/src/fmt/rt.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ impl<'a> Template<'a> {
4343
#[lang = "format_piece"]
4444
#[derive(Copy, Clone)]
4545
pub union Piece {
46-
#[cfg(target_pointer_width = "64")]
47-
pub i: u64,
48-
#[cfg(not(target_pointer_width = "64"))]
49-
pub i: u32,
46+
pub i: usize,
5047
pub p: *const u8,
5148
}
5249

@@ -69,19 +66,10 @@ impl Piece {
6966
Self { p: s as *const str as *const u8 }
7067
}
7168

72-
#[cfg(target_pointer_width = "64")]
7369
#[rustc_promotable]
7470
#[stable(feature = "rust1", since = "1.0.0")]
7571
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
76-
pub const fn num(i: u64) -> Self {
77-
Self { i }
78-
}
79-
80-
#[cfg(not(target_pointer_width = "64"))]
81-
#[rustc_promotable]
82-
#[stable(feature = "rust1", since = "1.0.0")]
83-
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
84-
pub const fn num(i: u32) -> Self {
72+
pub const fn num(i: usize) -> Self {
8573
Self { i }
8674
}
8775
}

0 commit comments

Comments
 (0)