about summary refs log tree commit diff
path: root/compiler/rustc_builtin_macros/src
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2024-12-31 04:15:40 +0000
committerMichael Goulet <michael@errs.io>2024-12-31 04:55:10 +0000
commitc6afe82b8a3255145ba0eeeb49f8c590e38f38e2 (patch)
treea4d963d532d40161d55db3b2a355c1544269a132 /compiler/rustc_builtin_macros/src
parent54e33bbdeca62508a71c0e445f1d1c82eb0b48c3 (diff)
downloadrust-c6afe82b8a3255145ba0eeeb49f8c590e38f38e2.tar.gz
rust-c6afe82b8a3255145ba0eeeb49f8c590e38f38e2.zip
Make parsed string literal fields named
Diffstat (limited to 'compiler/rustc_builtin_macros/src')
-rw-r--r--compiler/rustc_builtin_macros/src/asm.rs8
-rw-r--r--compiler/rustc_builtin_macros/src/format.rs6
-rw-r--r--compiler/rustc_builtin_macros/src/util.rs18
3 files changed, 23 insertions, 9 deletions
diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs
index 6ae697d4030..238cc14ff0b 100644
--- a/compiler/rustc_builtin_macros/src/asm.rs
+++ b/compiler/rustc_builtin_macros/src/asm.rs
@@ -16,7 +16,7 @@ use smallvec::smallvec;
 use {rustc_ast as ast, rustc_parse_format as parse};
 
 use crate::errors;
-use crate::util::expr_to_spanned_string;
+use crate::util::{ExprToSpannedString, expr_to_spanned_string};
 
 pub struct AsmArgs {
     pub templates: Vec<P<ast::Expr>>,
@@ -527,7 +527,11 @@ fn expand_preparsed_asm(
         let msg = "asm template must be a string literal";
         let template_sp = template_expr.span;
         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 {
                 return ExpandResult::Retry(());
             };
diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs
index 528eb7725f5..5b3f08948a9 100644
--- a/compiler/rustc_builtin_macros/src/format.rs
+++ b/compiler/rustc_builtin_macros/src/format.rs
@@ -17,7 +17,7 @@ use rustc_parse_format as parse;
 use rustc_span::{BytePos, ErrorGuaranteed, Ident, InnerSpan, Span, Symbol};
 
 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:
 //  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 (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 {
             return ExpandResult::Retry(());
         };
         match mac {
             Ok(mut fmt) if append_newline => {
-                fmt.0 = Symbol::intern(&format!("{}\n", fmt.0));
+                fmt.symbol = Symbol::intern(&format!("{}\n", fmt.symbol));
                 fmt
             }
             Ok(fmt) => fmt,
diff --git a/compiler/rustc_builtin_macros/src/util.rs b/compiler/rustc_builtin_macros/src/util.rs
index be12d21a800..9162e94eddb 100644
--- a/compiler/rustc_builtin_macros/src/util.rs
+++ b/compiler/rustc_builtin_macros/src/util.rs
@@ -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
 /// 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,
 /// 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 {
         ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
             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(..)) => {
                 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(),
             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(),
             Err(guar) => guar,
         })
-        .map(|(symbol, _style, span)| (symbol, span))
+        .map(|ExprToSpannedString { symbol, span, .. }| (symbol, span))
     })
 }