Make parsed string literal fields named
This commit is contained in:
parent
54e33bbdec
commit
c6afe82b8a
3 changed files with 23 additions and 9 deletions
|
@ -16,7 +16,7 @@ use smallvec::smallvec;
|
||||||
use {rustc_ast as ast, rustc_parse_format as parse};
|
use {rustc_ast as ast, rustc_parse_format as parse};
|
||||||
|
|
||||||
use crate::errors;
|
use crate::errors;
|
||||||
use crate::util::expr_to_spanned_string;
|
use crate::util::{ExprToSpannedString, expr_to_spanned_string};
|
||||||
|
|
||||||
pub struct AsmArgs {
|
pub struct AsmArgs {
|
||||||
pub templates: Vec<P<ast::Expr>>,
|
pub templates: Vec<P<ast::Expr>>,
|
||||||
|
@ -527,7 +527,11 @@ fn expand_preparsed_asm(
|
||||||
let msg = "asm template must be a string literal";
|
let msg = "asm template must be a string literal";
|
||||||
let template_sp = template_expr.span;
|
let template_sp = template_expr.span;
|
||||||
let template_is_mac_call = matches!(template_expr.kind, ast::ExprKind::MacCall(_));
|
let template_is_mac_call = matches!(template_expr.kind, ast::ExprKind::MacCall(_));
|
||||||
let (template_str, template_style, template_span) = {
|
let ExprToSpannedString {
|
||||||
|
symbol: template_str,
|
||||||
|
style: template_style,
|
||||||
|
span: template_span,
|
||||||
|
} = {
|
||||||
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, template_expr, msg) else {
|
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, template_expr, msg) else {
|
||||||
return ExpandResult::Retry(());
|
return ExpandResult::Retry(());
|
||||||
};
|
};
|
||||||
|
|
|
@ -17,7 +17,7 @@ use rustc_parse_format as parse;
|
||||||
use rustc_span::{BytePos, ErrorGuaranteed, Ident, InnerSpan, Span, Symbol};
|
use rustc_span::{BytePos, ErrorGuaranteed, Ident, InnerSpan, Span, Symbol};
|
||||||
|
|
||||||
use crate::errors;
|
use crate::errors;
|
||||||
use crate::util::expr_to_spanned_string;
|
use crate::util::{ExprToSpannedString, expr_to_spanned_string};
|
||||||
|
|
||||||
// The format_args!() macro is expanded in three steps:
|
// The format_args!() macro is expanded in three steps:
|
||||||
// 1. First, `parse_args` will parse the `(literal, arg, arg, name=arg, name=arg)` syntax,
|
// 1. First, `parse_args` will parse the `(literal, arg, arg, name=arg, name=arg)` syntax,
|
||||||
|
@ -166,13 +166,13 @@ fn make_format_args(
|
||||||
|
|
||||||
let MacroInput { fmtstr: efmt, mut args, is_direct_literal } = input;
|
let MacroInput { fmtstr: efmt, mut args, is_direct_literal } = input;
|
||||||
|
|
||||||
let (fmt_str, fmt_style, fmt_span) = {
|
let ExprToSpannedString { symbol: fmt_str, span: fmt_span, style: fmt_style } = {
|
||||||
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, efmt.clone(), msg) else {
|
let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, efmt.clone(), msg) else {
|
||||||
return ExpandResult::Retry(());
|
return ExpandResult::Retry(());
|
||||||
};
|
};
|
||||||
match mac {
|
match mac {
|
||||||
Ok(mut fmt) if append_newline => {
|
Ok(mut fmt) if append_newline => {
|
||||||
fmt.0 = Symbol::intern(&format!("{}\n", fmt.0));
|
fmt.symbol = Symbol::intern(&format!("{}\n", fmt.symbol));
|
||||||
fmt
|
fmt
|
||||||
}
|
}
|
||||||
Ok(fmt) => fmt,
|
Ok(fmt) => fmt,
|
||||||
|
|
|
@ -57,7 +57,13 @@ pub(crate) fn warn_on_duplicate_attribute(ecx: &ExtCtxt<'_>, item: &Annotatable,
|
||||||
|
|
||||||
/// `Ok` represents successfully retrieving the string literal at the correct
|
/// `Ok` represents successfully retrieving the string literal at the correct
|
||||||
/// position, e.g., `println("abc")`.
|
/// position, e.g., `println("abc")`.
|
||||||
type ExprToSpannedStringResult<'a> = Result<(Symbol, ast::StrStyle, Span), UnexpectedExprKind<'a>>;
|
pub(crate) type ExprToSpannedStringResult<'a> = Result<ExprToSpannedString, UnexpectedExprKind<'a>>;
|
||||||
|
|
||||||
|
pub(crate) struct ExprToSpannedString {
|
||||||
|
pub symbol: Symbol,
|
||||||
|
pub style: ast::StrStyle,
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
/// - `Ok` is returned when the conversion to a string literal is unsuccessful,
|
/// - `Ok` is returned when the conversion to a string literal is unsuccessful,
|
||||||
/// but another type of expression is obtained instead.
|
/// but another type of expression is obtained instead.
|
||||||
|
@ -90,7 +96,11 @@ pub(crate) fn expr_to_spanned_string<'a>(
|
||||||
ExpandResult::Ready(Err(match expr.kind {
|
ExpandResult::Ready(Err(match expr.kind {
|
||||||
ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
|
ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
|
||||||
Ok(ast::LitKind::Str(s, style)) => {
|
Ok(ast::LitKind::Str(s, style)) => {
|
||||||
return ExpandResult::Ready(Ok((s, style, expr.span)));
|
return ExpandResult::Ready(Ok(ExprToSpannedString {
|
||||||
|
symbol: s,
|
||||||
|
style,
|
||||||
|
span: expr.span,
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
Ok(ast::LitKind::ByteStr(..)) => {
|
Ok(ast::LitKind::ByteStr(..)) => {
|
||||||
let mut err = cx.dcx().struct_span_err(expr.span, err_msg);
|
let mut err = cx.dcx().struct_span_err(expr.span, err_msg);
|
||||||
|
@ -128,7 +138,7 @@ pub(crate) fn expr_to_string(
|
||||||
Ok((err, _)) => err.emit(),
|
Ok((err, _)) => err.emit(),
|
||||||
Err(guar) => guar,
|
Err(guar) => guar,
|
||||||
})
|
})
|
||||||
.map(|(symbol, style, _)| (symbol, style))
|
.map(|ExprToSpannedString { symbol, style, .. }| (symbol, style))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,7 +193,7 @@ pub(crate) fn get_single_str_spanned_from_tts(
|
||||||
Ok((err, _)) => err.emit(),
|
Ok((err, _)) => err.emit(),
|
||||||
Err(guar) => guar,
|
Err(guar) => guar,
|
||||||
})
|
})
|
||||||
.map(|(symbol, _style, span)| (symbol, span))
|
.map(|ExprToSpannedString { symbol, span, .. }| (symbol, span))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue