From c0e402637eeff7dea29082f983f26be4ef2e317c Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 6 Sep 2023 18:00:30 +0200 Subject: [PATCH] Emit builtin#format_args in builtin format_args expander --- .../macro_expansion_tests/builtin_fn_macro.rs | 39 +++-- .../hir-def/src/macro_expansion_tests/mbe.rs | 6 +- .../macro_expansion_tests/mbe/regression.rs | 12 +- crates/hir-expand/src/builtin_fn_macro.rs | 160 +----------------- .../extract_expressions_from_format_string.rs | 97 +++-------- crates/ide-assists/src/tests/generated.rs | 15 +- .../src/completions/format_string.rs | 22 +-- .../src/syntax_helpers/format_string.rs | 20 +-- crates/ide/src/hover/tests.rs | 20 +++ crates/ide/src/inlay_hints/chaining.rs | 12 +- crates/ide/src/syntax_highlighting/format.rs | 1 + .../ide/src/syntax_highlighting/highlight.rs | 1 + .../test_data/highlight_strings.html | 52 +++--- crates/ide/src/syntax_highlighting/tests.rs | 13 +- crates/test-utils/src/minicore.rs | 17 +- 15 files changed, 146 insertions(+), 341 deletions(-) diff --git a/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs b/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs index a350511c7a1..4aedb22c6bc 100644 --- a/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs +++ b/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs @@ -240,7 +240,7 @@ macro_rules! format_args { } fn main() { - ::core::fmt::Arguments::new_v1(&["", " ", ], &[::core::fmt::ArgumentV1::new(&(arg1(a, b, c)), ::core::fmt::Display::fmt), ::core::fmt::ArgumentV1::new(&(arg2), ::core::fmt::Debug::fmt), ]); + builtin #format_args ("{} {:?}", arg1(a, b, c), arg2); } "##]], ); @@ -258,10 +258,10 @@ macro_rules! format_args { fn main() { format_args!(x = 2); - format_args!(x =); - format_args!(x =, x = 2); - format_args!("{}", x =); - format_args!(=, "{}", x =); + format_args!/*+errors*/(x =); + format_args!/*+errors*/(x =, x = 2); + format_args!/*+errors*/("{}", x =); + format_args!/*+errors*/(=, "{}", x =); format_args!(x = 2, "{}", 5); } "#, @@ -273,12 +273,19 @@ macro_rules! format_args { } fn main() { - /* error: no rule matches input tokens */; - /* error: expected expression */; - /* error: expected expression, expected COMMA */; - /* error: expected expression */::core::fmt::Arguments::new_v1(&["", ], &[::core::fmt::ArgumentV1::new(&(), ::core::fmt::Display::fmt), ]); - /* error: expected expression, expected R_PAREN */; - ::core::fmt::Arguments::new_v1(&["", ], &[::core::fmt::ArgumentV1::new(&(5), ::core::fmt::Display::fmt), ]); + builtin #format_args (x = 2); + /* parse error: expected expression */ +builtin #format_args (x = ); + /* parse error: expected expression */ +/* parse error: expected R_PAREN */ +/* parse error: expected expression, item or let statement */ +builtin #format_args (x = , x = 2); + /* parse error: expected expression */ +builtin #format_args ("{}", x = ); + /* parse error: expected expression */ +/* parse error: expected expression */ +builtin #format_args ( = , "{}", x = ); + builtin #format_args (x = 2, "{}", 5); } "##]], ); @@ -306,7 +313,7 @@ macro_rules! format_args { } fn main() { - ::core::fmt::Arguments::new_v1(&["", " ", ], &[::core::fmt::ArgumentV1::new(&(a::()), ::core::fmt::Display::fmt), ::core::fmt::ArgumentV1::new(&(b), ::core::fmt::Debug::fmt), ]); + builtin #format_args ("{} {:?}", a::(), b); } "##]], ); @@ -339,7 +346,7 @@ macro_rules! format_args { } fn main() { - ::core::fmt::Arguments::new_v1(&[r#""#, r#",mismatch,""#, r#"",""#, r#"""#, ], &[::core::fmt::ArgumentV1::new(&(location_csv_pat(db, &analysis, vfs, &sm, pat_id)), ::core::fmt::Display::fmt), ::core::fmt::ArgumentV1::new(&(mismatch.expected.display(db)), ::core::fmt::Display::fmt), ::core::fmt::ArgumentV1::new(&(mismatch.actual.display(db)), ::core::fmt::Display::fmt), ]); + builtin #format_args (r#"{},mismatch,"{}","{}""#, location_csv_pat(db, &analysis, vfs, &sm, pat_id), mismatch.expected.display(db), mismatch.actual.display(db)); } "##]], ); @@ -373,7 +380,7 @@ macro_rules! format_args { } fn main() { - ::core::fmt::Arguments::new_v1(&["xxx", "y", "zzz", ], &[::core::fmt::ArgumentV1::new(&(2), ::core::fmt::Display::fmt), ::core::fmt::ArgumentV1::new(&(b), ::core::fmt::Debug::fmt), ]); + builtin #format_args (concat!("xxx{}y", "{:?}zzz"), 2, b); } "##]], ); @@ -403,8 +410,8 @@ macro_rules! format_args { fn main() { let _ = - /* error: expected field name or number *//* parse error: expected field name or number */ -::core::fmt::Arguments::new_v1(&["", " ", ], &[::core::fmt::ArgumentV1::new(&(a.), ::core::fmt::Display::fmt), ::core::fmt::ArgumentV1::new(&(), ::core::fmt::Debug::fmt), ]); + /* parse error: expected field name or number */ +builtin #format_args ("{} {:?}", a.); } "##]], ); diff --git a/crates/hir-def/src/macro_expansion_tests/mbe.rs b/crates/hir-def/src/macro_expansion_tests/mbe.rs index 2170cadcf83..d0906213243 100644 --- a/crates/hir-def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir-def/src/macro_expansion_tests/mbe.rs @@ -117,7 +117,7 @@ fn main(foo: ()) { macro_rules! format_args {} fn main(foo: ()) { - /* error: unresolved macro identity */::core::fmt::Arguments::new_v1(&["", " ", " ", ], &[::core::fmt::ArgumentV1::new(&(::core::fmt::Arguments::new_v1(&["", ], &[::core::fmt::ArgumentV1::new(&(0), ::core::fmt::Display::fmt), ])), ::core::fmt::Display::fmt), ::core::fmt::ArgumentV1::new(&(foo), ::core::fmt::Display::fmt), ::core::fmt::ArgumentV1::new(&(identity!(10)), ::core::fmt::Display::fmt), ]) + builtin #format_args ("{} {} {}", format_args!("{}", 0), foo, identity!(10), "bar") } "##]], ); @@ -150,8 +150,8 @@ macro_rules! identity { } fn main(foo: ()) { - // format_args/*+tokenids*/!("{} {} {}"#1,#3 format_args!("{}", 0#10),#12 foo#13,#14 identity!(10#18),#21 "bar"#22) -::core#4294967295::fmt#4294967295::Arguments#4294967295::new_v1#4294967295(�[#4294967295""#4294967295,#4294967295 " "#4294967295,#4294967295 " "#4294967295,#4294967295 ]#4294967295,#4294967295 �[::core#4294967295::fmt#4294967295::ArgumentV1#4294967295::new#4294967295(�(::core#4294967295::fmt#4294967295::Arguments#4294967295::new_v1#4294967295(�[#4294967295""#4294967295,#4294967295 ]#4294967295,#4294967295 �[::core#4294967295::fmt#4294967295::ArgumentV1#4294967295::new#4294967295(�(#42949672950#10)#4294967295,#4294967295 ::core#4294967295::fmt#4294967295::Display#4294967295::fmt#4294967295)#4294967295,#4294967295 ]#4294967295)#4294967295)#4294967295,#4294967295 ::core#4294967295::fmt#4294967295::Display#4294967295::fmt#4294967295)#4294967295,#4294967295 ::core#4294967295::fmt#4294967295::ArgumentV1#4294967295::new#4294967295(�(#4294967295foo#13)#4294967295,#4294967295 ::core#4294967295::fmt#4294967295::Display#4294967295::fmt#4294967295)#4294967295,#4294967295 ::core#4294967295::fmt#4294967295::ArgumentV1#4294967295::new#4294967295(�(#429496729510#18)#4294967295,#4294967295 ::core#4294967295::fmt#4294967295::Display#4294967295::fmt#4294967295)#4294967295,#4294967295 ]#4294967295)#4294967295 + // format_args/*+tokenids*/!("{} {} {}"#1,#2 format_args#3!#4("{}"#6,#7 0#8),#9 foo#10,#11 identity#12!#13(10#15),#16 "bar"#17) +builtin#4294967295 ##4294967295format_args#4294967295 (#0"{} {} {}"#1,#2 format_args#3!#4(#5"{}"#6,#7 0#8)#5,#9 foo#10,#11 identity#12!#13(#1410#15)#14,#16 "bar"#17)#0 } "##]], diff --git a/crates/hir-def/src/macro_expansion_tests/mbe/regression.rs b/crates/hir-def/src/macro_expansion_tests/mbe/regression.rs index 97554f93f1c..b416f45ff20 100644 --- a/crates/hir-def/src/macro_expansion_tests/mbe/regression.rs +++ b/crates/hir-def/src/macro_expansion_tests/mbe/regression.rs @@ -929,8 +929,8 @@ fn main() { macro_rules! format_args {} fn main() { - /* error: expected field name or number *//* parse error: expected field name or number */ -::core::fmt::Arguments::new_v1(&["", ], &[::core::fmt::ArgumentV1::new(&(line.1.), ::core::fmt::Display::fmt), ]); + /* parse error: expected field name or number */ +builtin #format_args ("{}", line.1.); } "##]], @@ -956,19 +956,15 @@ fn main() { macro_rules! format_args {} fn main() { - /* error: expected COMMA, expected R_BRACK, expected COMMA, expected COMMA, expected expression, expected R_PAREN *//* parse error: expected COMMA */ + /* parse error: expected COMMA */ /* parse error: expected R_BRACK */ /* parse error: expected COMMA */ /* parse error: expected COMMA */ /* parse error: expected expression */ /* parse error: expected R_PAREN */ -/* parse error: expected R_PAREN */ /* parse error: expected expression, item or let statement */ /* parse error: expected expression, item or let statement */ -/* parse error: expected expression, item or let statement */ -/* parse error: expected expression, item or let statement */ -/* parse error: expected expression, item or let statement */ -::core::fmt::Arguments::new_v1(&["", ], &[::core::fmt::ArgumentV1::new(&(&[0 2]), ::core::fmt::Display::fmt), ]); +builtin #format_args ("{}", &[0 2]); } "##]], diff --git a/crates/hir-expand/src/builtin_fn_macro.rs b/crates/hir-expand/src/builtin_fn_macro.rs index 00ee5e8b9d0..30b19b6e51b 100644 --- a/crates/hir-expand/src/builtin_fn_macro.rs +++ b/crates/hir-expand/src/builtin_fn_macro.rs @@ -1,13 +1,9 @@ //! Builtin macro -use std::mem; - -use ::tt::Ident; use base_db::{AnchoredPath, Edition, FileId}; use cfg::CfgExpr; use either::Either; use mbe::{parse_exprs_with_sep, parse_to_token_tree, TokenMap}; -use rustc_hash::FxHashMap; use syntax::{ ast::{self, AstToken}, SmolStr, @@ -97,11 +93,11 @@ register_builtin! { (unreachable, Unreachable) => unreachable_expand, (log_syntax, LogSyntax) => log_syntax_expand, (trace_macros, TraceMacros) => trace_macros_expand, - - EAGER: (format_args, FormatArgs) => format_args_expand, (const_format_args, ConstFormatArgs) => format_args_expand, (format_args_nl, FormatArgsNl) => format_args_nl_expand, + + EAGER: (compile_error, CompileError) => compile_error_expand, (concat, Concat) => concat_expand, (concat_idents, ConcatIdents) => concat_idents_expand, @@ -247,151 +243,15 @@ fn format_args_expand_general( _db: &dyn ExpandDatabase, _id: MacroCallId, tt: &tt::Subtree, - end_string: &str, + // FIXME: Make use of this so that mir interpretation works properly + _end_string: &str, ) -> ExpandResult { - let args = parse_exprs_with_sep(tt, ','); - - let expand_error = - ExpandResult::new(tt::Subtree::empty(), mbe::ExpandError::NoMatchingRule.into()); - - let mut key_args = FxHashMap::default(); - let mut args = args.into_iter().filter_map(|mut arg| { - // Remove `key =`. - if matches!(arg.token_trees.get(1), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=') - { - // but not with `==` - if !matches!(arg.token_trees.get(2), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=') - { - let key = arg.token_trees.drain(..2).next().unwrap(); - key_args.insert(key.to_string(), arg); - return None; - } - } - Some(arg) - }).collect::>().into_iter(); - // ^^^^^^^ we need this collect, to enforce the side effect of the filter_map closure (building the `key_args`) - let Some(format_subtree) = args.next() else { - return expand_error; - }; - let format_string = (|| { - let token_tree = format_subtree.token_trees.get(0)?; - match token_tree { - tt::TokenTree::Leaf(l) => match l { - tt::Leaf::Literal(l) => { - if let Some(mut text) = l.text.strip_prefix('r') { - let mut raw_sharps = String::new(); - while let Some(t) = text.strip_prefix('#') { - text = t; - raw_sharps.push('#'); - } - text = - text.strip_suffix(&raw_sharps)?.strip_prefix('"')?.strip_suffix('"')?; - Some((text, l.span, Some(raw_sharps))) - } else { - let text = l.text.strip_prefix('"')?.strip_suffix('"')?; - let span = l.span; - Some((text, span, None)) - } - } - _ => None, - }, - tt::TokenTree::Subtree(_) => None, - } - })(); - let Some((format_string, _format_string_span, raw_sharps)) = format_string else { - return expand_error; - }; - let mut format_iter = format_string.chars().peekable(); - let mut parts = vec![]; - let mut last_part = String::new(); - let mut arg_tts = vec![]; - let mut err = None; - while let Some(c) = format_iter.next() { - // Parsing the format string. See https://doc.rust-lang.org/std/fmt/index.html#syntax for the grammar and more info - match c { - '{' => { - if format_iter.peek() == Some(&'{') { - format_iter.next(); - last_part.push('{'); - continue; - } - let mut argument = String::new(); - while ![Some(&'}'), Some(&':')].contains(&format_iter.peek()) { - argument.push(match format_iter.next() { - Some(c) => c, - None => return expand_error, - }); - } - let format_spec = match format_iter.next().unwrap() { - '}' => "".to_owned(), - ':' => { - let mut s = String::new(); - while let Some(c) = format_iter.next() { - if c == '}' { - break; - } - s.push(c); - } - s - } - _ => unreachable!(), - }; - parts.push(mem::take(&mut last_part)); - let arg_tree = if argument.is_empty() { - match args.next() { - Some(it) => it, - None => { - err = Some(mbe::ExpandError::NoMatchingRule.into()); - tt::Subtree::empty() - } - } - } else if let Some(tree) = key_args.get(&argument) { - tree.clone() - } else { - // FIXME: we should pick the related substring of the `_format_string_span` as the span. You - // can use `.char_indices()` instead of `.char()` for `format_iter` to find the substring interval. - let ident = Ident::new(argument, tt::TokenId::unspecified()); - quote!(#ident) - }; - let formatter = match &*format_spec { - "?" => quote!(::core::fmt::Debug::fmt), - "" => quote!(::core::fmt::Display::fmt), - _ => { - // FIXME: implement the rest and return expand error here - quote!(::core::fmt::Display::fmt) - } - }; - arg_tts.push(quote! { ::core::fmt::ArgumentV1::new(&(#arg_tree), #formatter), }); - } - '}' => { - if format_iter.peek() == Some(&'}') { - format_iter.next(); - last_part.push('}'); - } else { - return expand_error; - } - } - _ => last_part.push(c), - } - } - last_part += end_string; - if !last_part.is_empty() { - parts.push(last_part); - } - let part_tts = parts.into_iter().map(|it| { - let text = if let Some(raw) = &raw_sharps { - format!("r{raw}\"{}\"{raw}", it).into() - } else { - format!("\"{}\"", it).into() - }; - let l = tt::Literal { span: tt::TokenId::unspecified(), text }; - quote!(#l ,) + let pound = quote! {@PUNCT '#'}; + let mut tt = tt.clone(); + tt.delimiter.kind = tt::DelimiterKind::Parenthesis; + return ExpandResult::ok(quote! { + builtin #pound format_args #tt }); - let arg_tts = arg_tts.into_iter().flat_map(|arg| arg.token_trees); - let expanded = quote! { - ::core::fmt::Arguments::new_v1(&[##part_tts], &[##arg_tts]) - }; - ExpandResult { value: expanded, err } } fn asm_expand( @@ -399,8 +259,6 @@ fn asm_expand( _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult { - // FIXME: parse asm here - // We expand all assembly snippets to `format_args!` invocations to get format syntax // highlighting for them. diff --git a/crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs b/crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs index c3d925cb26c..25c4b49c945 100644 --- a/crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs +++ b/crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs @@ -15,26 +15,13 @@ use syntax::{ast, AstNode, AstToken, NodeOrToken, SyntaxKind::COMMA, TextRange}; // Move an expression out of a format string. // // ``` -// macro_rules! format_args { -// ($lit:literal $(tt:tt)*) => { 0 }, -// } -// macro_rules! print { -// ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); -// } -// +// # //- minicore: fmt // fn main() { // print!("{var} {x + 1}$0"); // } // ``` // -> // ``` -// macro_rules! format_args { -// ($lit:literal $(tt:tt)*) => { 0 }, -// } -// macro_rules! print { -// ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); -// } -// // fn main() { // print!("{var} {}"$0, x + 1); // } @@ -47,17 +34,23 @@ pub(crate) fn extract_expressions_from_format_string( let fmt_string = ctx.find_token_at_offset::()?; let tt = fmt_string.syntax().parent().and_then(ast::TokenTree::cast)?; + dbg!(); let expanded_t = ast::String::cast( ctx.sema.descend_into_macros_with_kind_preference(fmt_string.syntax().clone(), 0.into()), )?; + dbg!(); if !is_format_string(&expanded_t) { + dbg!(); return None; } + dbg!(); let (new_fmt, extracted_args) = parse_format_exprs(fmt_string.text()).ok()?; + dbg!(); if extracted_args.is_empty() { return None; } + dbg!(); acc.add( AssistId( @@ -158,37 +151,21 @@ mod tests { use super::*; use crate::tests::check_assist; - const MACRO_DECL: &'static str = r#" -macro_rules! format_args { - ($lit:literal $(tt:tt)*) => { 0 }, -} -macro_rules! print { - ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); -} -"#; - - fn add_macro_decl(s: &'static str) -> String { - MACRO_DECL.to_string() + s - } - #[test] fn multiple_middle_arg() { check_assist( extract_expressions_from_format_string, - &add_macro_decl( - r#" + r#" +//- minicore: fmt fn main() { print!("{} {x + 1:b} {}$0", y + 2, 2); } "#, - ), - &add_macro_decl( - r#" + r#" fn main() { print!("{} {:b} {}"$0, y + 2, x + 1, 2); } "#, - ), ); } @@ -196,20 +173,17 @@ fn main() { fn single_arg() { check_assist( extract_expressions_from_format_string, - &add_macro_decl( - r#" + r#" +//- minicore: fmt fn main() { print!("{obj.value:b}$0",); } "#, - ), - &add_macro_decl( - r#" + r#" fn main() { print!("{:b}"$0, obj.value); } "#, - ), ); } @@ -217,20 +191,17 @@ fn main() { fn multiple_middle_placeholders_arg() { check_assist( extract_expressions_from_format_string, - &add_macro_decl( - r#" + r#" +//- minicore: fmt fn main() { print!("{} {x + 1:b} {} {}$0", y + 2, 2); } "#, - ), - &add_macro_decl( - r#" + r#" fn main() { print!("{} {:b} {} {}"$0, y + 2, x + 1, 2, $1); } "#, - ), ); } @@ -238,20 +209,17 @@ fn main() { fn multiple_trailing_args() { check_assist( extract_expressions_from_format_string, - &add_macro_decl( - r#" + r#" +//- minicore: fmt fn main() { print!("{:b} {x + 1:b} {Struct(1, 2)}$0", 1); } "#, - ), - &add_macro_decl( - r#" + r#" fn main() { print!("{:b} {:b} {}"$0, 1, x + 1, Struct(1, 2)); } "#, - ), ); } @@ -259,20 +227,17 @@ fn main() { fn improper_commas() { check_assist( extract_expressions_from_format_string, - &add_macro_decl( - r#" + r#" +//- minicore: fmt fn main() { print!("{} {x + 1:b} {Struct(1, 2)}$0", 1,); } "#, - ), - &add_macro_decl( - r#" + r#" fn main() { print!("{} {:b} {}"$0, 1, x + 1, Struct(1, 2)); } "#, - ), ); } @@ -280,20 +245,17 @@ fn main() { fn nested_tt() { check_assist( extract_expressions_from_format_string, - &add_macro_decl( - r#" + r#" +//- minicore: fmt fn main() { print!("My name is {} {x$0 + x}", stringify!(Paperino)) } "#, - ), - &add_macro_decl( - r#" + r#" fn main() { print!("My name is {} {}"$0, stringify!(Paperino), x + x) } "#, - ), ); } @@ -301,22 +263,19 @@ fn main() { fn extract_only_expressions() { check_assist( extract_expressions_from_format_string, - &add_macro_decl( - r#" + r#" +//- minicore: fmt fn main() { let var = 1 + 1; print!("foobar {var} {var:?} {x$0 + x}") } "#, - ), - &add_macro_decl( - r#" + r#" fn main() { let var = 1 + 1; print!("foobar {var} {var:?} {}"$0, x + x) } "#, - ), ); } } diff --git a/crates/ide-assists/src/tests/generated.rs b/crates/ide-assists/src/tests/generated.rs index 6eadc3dbcbc..8bd87e5f5db 100644 --- a/crates/ide-assists/src/tests/generated.rs +++ b/crates/ide-assists/src/tests/generated.rs @@ -694,25 +694,12 @@ fn doctest_extract_expressions_from_format_string() { check_doc_test( "extract_expressions_from_format_string", r#####" -macro_rules! format_args { - ($lit:literal $(tt:tt)*) => { 0 }, -} -macro_rules! print { - ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); -} - +//- minicore: fmt fn main() { print!("{var} {x + 1}$0"); } "#####, r#####" -macro_rules! format_args { - ($lit:literal $(tt:tt)*) => { 0 }, -} -macro_rules! print { - ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); -} - fn main() { print!("{var} {}"$0, x + 1); } diff --git a/crates/ide-completion/src/completions/format_string.rs b/crates/ide-completion/src/completions/format_string.rs index 8e904fd605a..cecbe75391d 100644 --- a/crates/ide-completion/src/completions/format_string.rs +++ b/crates/ide-completion/src/completions/format_string.rs @@ -51,9 +51,7 @@ mod tests { fn works_when_wrapped() { check( r#" -macro_rules! format_args { - ($lit:literal $(tt:tt)*) => { 0 }, -} +//- minicore: fmt macro_rules! print { ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); } @@ -70,9 +68,7 @@ fn main() { fn no_completion_without_brace() { check( r#" -macro_rules! format_args { - ($lit:literal $(tt:tt)*) => { 0 }, -} +//- minicore: fmt fn main() { let foobar = 1; format_args!("f$0"); @@ -87,18 +83,13 @@ fn main() { check_edit( "foobar", r#" -macro_rules! format_args { - ($lit:literal $(tt:tt)*) => { 0 }, -} +//- minicore: fmt fn main() { let foobar = 1; format_args!("{f$0"); } "#, r#" -macro_rules! format_args { - ($lit:literal $(tt:tt)*) => { 0 }, -} fn main() { let foobar = 1; format_args!("{foobar"); @@ -108,18 +99,13 @@ fn main() { check_edit( "foobar", r#" -macro_rules! format_args { - ($lit:literal $(tt:tt)*) => { 0 }, -} +//- minicore: fmt fn main() { let foobar = 1; format_args!("{$0"); } "#, r#" -macro_rules! format_args { - ($lit:literal $(tt:tt)*) => { 0 }, -} fn main() { let foobar = 1; format_args!("{foobar"); diff --git a/crates/ide-db/src/syntax_helpers/format_string.rs b/crates/ide-db/src/syntax_helpers/format_string.rs index acf0a67de4a..8302b015dda 100644 --- a/crates/ide-db/src/syntax_helpers/format_string.rs +++ b/crates/ide-db/src/syntax_helpers/format_string.rs @@ -1,10 +1,10 @@ //! Tools to work with format string literals for the `format_args!` family of macros. -use crate::syntax_helpers::node_ext::macro_call_for_string_token; use syntax::{ ast::{self, IsString}, - TextRange, TextSize, + AstNode, AstToken, TextRange, TextSize, }; +// FIXME: This can probably be re-implemented via the HIR? pub fn is_format_string(string: &ast::String) -> bool { // Check if `string` is a format string argument of a macro invocation. // `string` is a string literal, mapped down into the innermost macro expansion. @@ -15,19 +15,9 @@ pub fn is_format_string(string: &ast::String) -> bool { // This setup lets us correctly highlight the components of `concat!("{}", "bla")` format // strings. It still fails for `concat!("{", "}")`, but that is rare. (|| { - let name = macro_call_for_string_token(string)?.path()?.segment()?.name_ref()?; - - if !matches!( - name.text().as_str(), - "format_args" | "format_args_nl" | "const_format_args" | "panic_2015" | "panic_2021" - ) { - return None; - } - - // NB: we match against `panic_2015`/`panic_2021` here because they have a special-cased arm for - // `"{}"`, which otherwise wouldn't get highlighted. - - Some(()) + let lit = string.syntax().parent().and_then(ast::Literal::cast)?; + let fa = lit.syntax().parent().and_then(ast::FormatArgsExpr::cast)?; + (fa.template()? == ast::Expr::Literal(lit)).then_some(|| ()) })() .is_some() } diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index d0f9f7b0e16..81d6db564ff 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -6574,3 +6574,23 @@ fn test() { "#]], ); } + +#[test] +fn format_args_arg() { + check( + r#" +//- minicore: fmt +fn test() { + let foo = 0; + format_args!("{}", foo$0); +} +"#, + expect![[r#" + *foo* + + ```rust + let foo: i32 // size = 4, align = 4 + ``` + "#]], + ); +} diff --git a/crates/ide/src/inlay_hints/chaining.rs b/crates/ide/src/inlay_hints/chaining.rs index b621a8dda7e..8207bec7e89 100644 --- a/crates/ide/src/inlay_hints/chaining.rs +++ b/crates/ide/src/inlay_hints/chaining.rs @@ -474,7 +474,7 @@ fn main() { file_id: FileId( 1, ), - range: 9289..9297, + range: 10739..10747, }, ), tooltip: "", @@ -487,7 +487,7 @@ fn main() { file_id: FileId( 1, ), - range: 9321..9325, + range: 10771..10775, }, ), tooltip: "", @@ -511,7 +511,7 @@ fn main() { file_id: FileId( 1, ), - range: 9289..9297, + range: 10739..10747, }, ), tooltip: "", @@ -524,7 +524,7 @@ fn main() { file_id: FileId( 1, ), - range: 9321..9325, + range: 10771..10775, }, ), tooltip: "", @@ -548,7 +548,7 @@ fn main() { file_id: FileId( 1, ), - range: 9289..9297, + range: 10739..10747, }, ), tooltip: "", @@ -561,7 +561,7 @@ fn main() { file_id: FileId( 1, ), - range: 9321..9325, + range: 10771..10775, }, ), tooltip: "", diff --git a/crates/ide/src/syntax_highlighting/format.rs b/crates/ide/src/syntax_highlighting/format.rs index 2ed57e20130..2ef1315945a 100644 --- a/crates/ide/src/syntax_highlighting/format.rs +++ b/crates/ide/src/syntax_highlighting/format.rs @@ -17,6 +17,7 @@ pub(super) fn highlight_format_string( return; } + // FIXME: Replace this with the HIR info we have now. lex_format_specifiers(string, &mut |piece_range, kind| { if let Some(highlight) = highlight_format_specifier(kind) { stack.add(HlRange { diff --git a/crates/ide/src/syntax_highlighting/highlight.rs b/crates/ide/src/syntax_highlighting/highlight.rs index 8e96bfa01ad..7d00282fc14 100644 --- a/crates/ide/src/syntax_highlighting/highlight.rs +++ b/crates/ide/src/syntax_highlighting/highlight.rs @@ -617,6 +617,7 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight { CONST => SymbolKind::Const, STATIC => SymbolKind::Static, IDENT_PAT => SymbolKind::Local, + FORMAT_ARGS_ARG => SymbolKind::Local, _ => return default.into(), }; diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index 9c5c6d50ea7..64e614cecd2 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -45,17 +45,11 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
macro_rules! println {
     ($($arg:tt)*) => ({
-        $crate::io::_print($crate::format_args_nl!($($arg)*));
+        $crate::io::_print(format_args_nl!($($arg)*));
     })
 }
 #[rustc_builtin_macro]
 #[macro_export]
-macro_rules! format_args {}
-#[rustc_builtin_macro]
-#[macro_export]
-macro_rules! const_format_args {}
-#[rustc_builtin_macro]
-#[macro_export]
 macro_rules! format_args_nl {}
 
 mod panic {
@@ -75,7 +69,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
             $crate::panicking::panic_display(&$arg)
         ),
         ($fmt:expr, $($arg:tt)+) => (
-            $crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+))
+            $crate::panicking::panic_fmt(const_format_args!($fmt, $($arg)+))
         ),
     }
 }
@@ -92,7 +86,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
 
 macro_rules! toho {
     () => ($crate::panic!("not yet implemented"));
-    ($($arg:tt)+) => ($crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
+    ($($arg:tt)+) => ($crate::panic!("not yet implemented: {}", format_args!($($arg)+)));
 }
 
 fn main() {
@@ -114,18 +108,18 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     println!("Hello, {}!", "world");   // => "Hello, world!"
     println!("The number is {}", 1);   // => "The number is 1"
     println!("{:?}", (3, 4));          // => "(3, 4)"
-    println!("{value}", value=4);      // => "4"
+    println!("{value}", value=4);      // => "4"
     println!("{} {}", 1, 2);           // => "1 2"
     println!("{:04}", 42);             // => "0042" with leading zerosV
     println!("{1} {} {0} {}", 1, 2);   // => "2 1 1 2"
-    println!("{argument}", argument = "test");   // => "test"
-    println!("{name} {}", 1, name = 2);          // => "2 1"
-    println!("{a} {c} {b}", a="a", b='b', c=3);  // => "a 3 b"
+    println!("{argument}", argument = "test");   // => "test"
+    println!("{name} {}", 1, name = 2);          // => "2 1"
+    println!("{a} {c} {b}", a="a", b='b', c=3);  // => "a 3 b"
     println!("{{{}}}", 2);                       // => "{2}"
     println!("Hello {:5}!", "x");
     println!("Hello {:1$}!", "x", 5);
     println!("Hello {1:0$}!", 5, "x");
-    println!("Hello {:width$}!", "x", width = 5);
+    println!("Hello {:width$}!", "x", width = 5);
     println!("Hello {:<5}!", "x");
     println!("Hello {:-<5}!", "x");
     println!("Hello {:^5}!", "x");
@@ -140,10 +134,10 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
     println!("Hello {} is {:.*}",    "x", 5, 0.01);
     println!("Hello {} is {2:.*}",   "x", 5, 0.01);
-    println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
-    println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
-    println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
-    println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
+    println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
+    println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
+    println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
+    println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
 
     let _ = "{}"
     let _ = "{{}}";
@@ -167,24 +161,24 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     let _ = c"\u{FF}\xFF"; // valid bytes, valid unicodes
     let backslash = r"\\";
 
-    println!("{\x41}", A = 92);
-    println!("{ничоси}", ничоси = 92);
+    println!("{\x41}", A = 92);
+    println!("{ничоси}", ничоси = 92);
 
     println!("{:x?} {} ", thingy, n2);
-    panic!("{}", 0);
-    panic!("more {}", 1);
-    assert!(true, "{}", 1);
-    assert!(true, "{} asdasd", 1);
-    toho!("{}fmt", 0);
+    panic!("{}", 0);
+    panic!("more {}", 1);
+    assert!(true, "{}", 1);
+    assert!(true, "{} asdasd", 1);
+    toho!("{}fmt", 0);
     let i: u64 = 3;
     let o: u64;
     asm!(
-        "mov {0}, {1}",
-        "add {0}, 5",
+        "mov {0}, {1}",
+        "add {0}, 5",
         out(reg) o,
         in(reg) i,
     );
 
-    format_args!(concat!("{}"), "{}");
-    format_args!("{} {} {} {} {} {}", backslash, format_args!("{}", 0), foo, "bar", toho!(), backslash);
+    format_args!(concat!("{}"), "{}");
+    format_args!("{} {} {} {} {} {}", backslash, format_args!("{}", 0), foo, "bar", toho!(), backslash);
 }
\ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index aacd57af589..542d8992531 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -401,19 +401,14 @@ fn test_string_highlighting() { // thus, we have to copy the macro definition from `std` check_highlighting( r#" +//- minicore: fmt macro_rules! println { ($($arg:tt)*) => ({ - $crate::io::_print($crate::format_args_nl!($($arg)*)); + $crate::io::_print(format_args_nl!($($arg)*)); }) } #[rustc_builtin_macro] #[macro_export] -macro_rules! format_args {} -#[rustc_builtin_macro] -#[macro_export] -macro_rules! const_format_args {} -#[rustc_builtin_macro] -#[macro_export] macro_rules! format_args_nl {} mod panic { @@ -433,7 +428,7 @@ mod panic { $crate::panicking::panic_display(&$arg) ), ($fmt:expr, $($arg:tt)+) => ( - $crate::panicking::panic_fmt($crate::const_format_args!($fmt, $($arg)+)) + $crate::panicking::panic_fmt(const_format_args!($fmt, $($arg)+)) ), } } @@ -450,7 +445,7 @@ macro_rules! concat {} macro_rules! toho { () => ($crate::panic!("not yet implemented")); - ($($arg:tt)+) => ($crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+))); + ($($arg:tt)+) => ($crate::panic!("not yet implemented: {}", format_args!($($arg)+))); } fn main() { diff --git a/crates/test-utils/src/minicore.rs b/crates/test-utils/src/minicore.rs index 0fc95635e66..573f56b003a 100644 --- a/crates/test-utils/src/minicore.rs +++ b/crates/test-utils/src/minicore.rs @@ -1352,8 +1352,6 @@ mod macros { /* compiler built-in */ }; } - - pub(crate) use panic; // endregion:panic // region:fmt @@ -1364,7 +1362,20 @@ mod macros { ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }}; } - pub(crate) use const_format_args; + #[macro_export] + #[rustc_builtin_macro] + macro_rules! format_args { + ($fmt:expr) => {{ /* compiler built-in */ }}; + ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }}; + } + + #[macro_export] + macro_rules! print { + ($($arg:tt)*) => {{ + $crate::io::_print($crate::format_args!($($arg)*)); + }}; + } + // endregion:fmt // region:derive