Skip to content

Commit 7e0e9f0

Browse files
committed
Auto merge of rust-lang#137294 - m-ou-se:fmt-args-2025, r=<try>
Experiment: New format_args!() representation // TODO: description
2 parents f280acf + 87ac88f commit 7e0e9f0

23 files changed

+1417
-625
lines changed

Diff for: compiler/rustc_ast/src/format.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ impl FormatArgumentKind {
160160
&Self::Captured(id) => Some(id),
161161
}
162162
}
163+
164+
pub fn is_captured(&self) -> bool {
165+
match self {
166+
Self::Captured(_) => true,
167+
_ => false,
168+
}
169+
}
163170
}
164171

165172
#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
@@ -266,7 +273,7 @@ pub enum FormatAlignment {
266273
#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq)]
267274
pub enum FormatCount {
268275
/// `{:5}` or `{:.5}`
269-
Literal(usize),
276+
Literal(u16),
270277
/// `{:.*}`, `{:.5$}`, or `{:a$}`, etc.
271278
Argument(FormatArgPosition),
272279
}

Diff for: compiler/rustc_ast_lowering/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ ast_lowering_template_modifier = template modifier
175175
176176
ast_lowering_this_not_async = this is not `async`
177177
178+
ast_lowering_too_many_format_arguments =
179+
too many arguments used in format string
180+
178181
ast_lowering_underscore_array_length_unstable =
179182
using `_` for array lengths is unstable
180183

Diff for: compiler/rustc_ast_lowering/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,10 @@ pub(crate) struct UseConstGenericArg {
467467
#[suggestion_part(code = "{other_args}")]
468468
pub call_args: Span,
469469
}
470+
471+
#[derive(Diagnostic)]
472+
#[diag(ast_lowering_too_many_format_arguments)]
473+
pub(crate) struct TooManyFormatArguments {
474+
#[primary_span]
475+
pub span: Span,
476+
}

Diff for: compiler/rustc_ast_lowering/src/expr.rs

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

2132-
pub(super) fn expr_usize(&mut self, sp: Span, value: usize) -> hir::Expr<'hir> {
2132+
pub(super) fn expr_u64(&mut self, sp: Span, value: u64) -> hir::Expr<'hir> {
21332133
let lit = self.arena.alloc(hir::Lit {
21342134
span: sp,
21352135
node: ast::LitKind::Int(
2136-
(value as u128).into(),
2137-
ast::LitIntType::Unsigned(ast::UintTy::Usize),
2136+
u128::from(value).into(),
2137+
ast::LitIntType::Unsigned(ast::UintTy::U64),
21382138
),
21392139
});
21402140
self.expr(sp, hir::ExprKind::Lit(lit))
@@ -2151,8 +2151,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
21512151
self.expr(sp, hir::ExprKind::Lit(lit))
21522152
}
21532153

2154-
pub(super) fn expr_char(&mut self, sp: Span, value: char) -> hir::Expr<'hir> {
2155-
let lit = self.arena.alloc(hir::Lit { span: sp, node: ast::LitKind::Char(value) });
2154+
pub(super) fn expr_usize(&mut self, sp: Span, value: u16) -> hir::Expr<'hir> {
2155+
let lit = self.arena.alloc(hir::Lit {
2156+
span: sp,
2157+
node: ast::LitKind::Int(
2158+
u128::from(value).into(),
2159+
ast::LitIntType::Unsigned(ast::UintTy::Usize),
2160+
),
2161+
});
21562162
self.expr(sp, hir::ExprKind::Lit(lit))
21572163
}
21582164

@@ -2281,6 +2287,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
22812287
self.expr(b.span, hir::ExprKind::Block(b, None))
22822288
}
22832289

2290+
pub(super) fn expr_unsafe_block(
2291+
&mut self,
2292+
span: Span,
2293+
expr: &'hir hir::Expr<'hir>,
2294+
) -> hir::Expr<'hir> {
2295+
let hir_id = self.next_id();
2296+
self.expr_block(self.arena.alloc(hir::Block {
2297+
stmts: &[],
2298+
expr: Some(expr),
2299+
hir_id,
2300+
rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated),
2301+
span,
2302+
targeted_by_break: false,
2303+
}))
2304+
}
2305+
22842306
pub(super) fn expr_array_ref(
22852307
&mut self,
22862308
span: Span,

0 commit comments

Comments
 (0)