Skip to content

Commit c6afe82

Browse files
Make parsed string literal fields named
1 parent 54e33bb commit c6afe82

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

Diff for: compiler/rustc_builtin_macros/src/asm.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use smallvec::smallvec;
1616
use {rustc_ast as ast, rustc_parse_format as parse};
1717

1818
use crate::errors;
19-
use crate::util::expr_to_spanned_string;
19+
use crate::util::{ExprToSpannedString, expr_to_spanned_string};
2020

2121
pub struct AsmArgs {
2222
pub templates: Vec<P<ast::Expr>>,
@@ -527,7 +527,11 @@ fn expand_preparsed_asm(
527527
let msg = "asm template must be a string literal";
528528
let template_sp = template_expr.span;
529529
let template_is_mac_call = matches!(template_expr.kind, ast::ExprKind::MacCall(_));
530-
let (template_str, template_style, template_span) = {
530+
let ExprToSpannedString {
531+
symbol: template_str,
532+
style: template_style,
533+
span: template_span,
534+
} = {
531535
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, template_expr, msg) else {
532536
return ExpandResult::Retry(());
533537
};

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_parse_format as parse;
1717
use rustc_span::{BytePos, ErrorGuaranteed, Ident, InnerSpan, Span, Symbol};
1818

1919
use crate::errors;
20-
use crate::util::expr_to_spanned_string;
20+
use crate::util::{ExprToSpannedString, expr_to_spanned_string};
2121

2222
// The format_args!() macro is expanded in three steps:
2323
// 1. First, `parse_args` will parse the `(literal, arg, arg, name=arg, name=arg)` syntax,
@@ -166,13 +166,13 @@ fn make_format_args(
166166

167167
let MacroInput { fmtstr: efmt, mut args, is_direct_literal } = input;
168168

169-
let (fmt_str, fmt_style, fmt_span) = {
169+
let ExprToSpannedString { symbol: fmt_str, span: fmt_span, style: fmt_style } = {
170170
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, efmt.clone(), msg) else {
171171
return ExpandResult::Retry(());
172172
};
173173
match mac {
174174
Ok(mut fmt) if append_newline => {
175-
fmt.0 = Symbol::intern(&format!("{}\n", fmt.0));
175+
fmt.symbol = Symbol::intern(&format!("{}\n", fmt.symbol));
176176
fmt
177177
}
178178
Ok(fmt) => fmt,

Diff for: compiler/rustc_builtin_macros/src/util.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ pub(crate) fn warn_on_duplicate_attribute(ecx: &ExtCtxt<'_>, item: &Annotatable,
5757

5858
/// `Ok` represents successfully retrieving the string literal at the correct
5959
/// position, e.g., `println("abc")`.
60-
type ExprToSpannedStringResult<'a> = Result<(Symbol, ast::StrStyle, Span), UnexpectedExprKind<'a>>;
60+
pub(crate) type ExprToSpannedStringResult<'a> = Result<ExprToSpannedString, UnexpectedExprKind<'a>>;
61+
62+
pub(crate) struct ExprToSpannedString {
63+
pub symbol: Symbol,
64+
pub style: ast::StrStyle,
65+
pub span: Span,
66+
}
6167

6268
/// - `Ok` is returned when the conversion to a string literal is unsuccessful,
6369
/// but another type of expression is obtained instead.
@@ -90,7 +96,11 @@ pub(crate) fn expr_to_spanned_string<'a>(
9096
ExpandResult::Ready(Err(match expr.kind {
9197
ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
9298
Ok(ast::LitKind::Str(s, style)) => {
93-
return ExpandResult::Ready(Ok((s, style, expr.span)));
99+
return ExpandResult::Ready(Ok(ExprToSpannedString {
100+
symbol: s,
101+
style,
102+
span: expr.span,
103+
}));
94104
}
95105
Ok(ast::LitKind::ByteStr(..)) => {
96106
let mut err = cx.dcx().struct_span_err(expr.span, err_msg);
@@ -128,7 +138,7 @@ pub(crate) fn expr_to_string(
128138
Ok((err, _)) => err.emit(),
129139
Err(guar) => guar,
130140
})
131-
.map(|(symbol, style, _)| (symbol, style))
141+
.map(|ExprToSpannedString { symbol, style, .. }| (symbol, style))
132142
})
133143
}
134144

@@ -183,7 +193,7 @@ pub(crate) fn get_single_str_spanned_from_tts(
183193
Ok((err, _)) => err.emit(),
184194
Err(guar) => guar,
185195
})
186-
.map(|(symbol, _style, span)| (symbol, span))
196+
.map(|ExprToSpannedString { symbol, span, .. }| (symbol, span))
187197
})
188198
}
189199

0 commit comments

Comments
 (0)