Skip to content

Semantic highlighting for format macros #10394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
20 tasks done
digama0 opened this issue Sep 30, 2021 · 3 comments · Fixed by #10413
Closed
20 tasks done

Semantic highlighting for format macros #10394

digama0 opened this issue Sep 30, 2021 · 3 comments · Fixed by #10413
Assignees
Labels
A-macro macro expansion C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now

Comments

@digama0
Copy link
Contributor

digama0 commented Sep 30, 2021

The {} in println!("{}", 1) is highlighted specially, which is a very nice feature. However, it only works for some of the format-like macros:

  • asm!
  • assert!
  • assert_eq!
  • assert_ne!
  • debug_assert!
  • debug_assert_eq!
  • debug_assert_ne!
  • eprint!
  • eprintln!
  • format!
  • format_args!
  • format_args_nl!
  • panic! *
  • print!
  • println!
  • todo! *
  • unimplemented! *
  • unreachable!
  • write!
  • writeln!

Arguably, support for asm is more complicated, but it seems close enough to format_args style input in the first argument that I think that applying the usual syntax highlighting will work well enough. Here's a rust snippet containing all the macros for quick testing:

fn main() {
    asm!("mov {}, 0", out(reg) _);
    assert!(true, "{}", 1);
    assert_eq!(1, 2, "{}", 1);
    assert_ne!(1, 2, "{}", 1);
    debug_assert!(true, "{}", 1);
    debug_assert_eq!(1, 2, "{}", 1);
    debug_assert_ne!(1, 2, "{}", 1);
    eprint!("{}", 1);
    eprintln!("{}", 1);
    format!("{}", 1);
    format_args!("{}", 1);
    format_args_nl!("{}", 1);
    panic!("{}", 1);
    print!("{}", 1);
    println!("{}", 1);
    todo!("{}", 1);
    unimplemented!("{}", 1);
    unreachable!("{}", 1);
    write!((), "{}", 1);
    writeln!((), "{}", 1);
}

Tested on rust-analyzer v0.2.760 on VSCode with rustc 1.57.0-nightly (11491938f 2021-09-29). The macros marked with * are recent regressions, which work on stable but not on the latest nightly, presumably due to a change in the implementation of panic! relating to rust 2021.

@lnicola lnicola added C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now labels Sep 30, 2021
@jonas-schievink jonas-schievink self-assigned this Sep 30, 2021
@jonas-schievink jonas-schievink added the A-macro macro expansion label Sep 30, 2021
bors bot added a commit that referenced this issue Sep 30, 2021
10397: fix: fix format string highlighting for `panic!` and `assert!` r=jonas-schievink a=jonas-schievink

part of #10394

bors r+

Co-authored-by: Jonas Schievink <[email protected]>
bors bot added a commit that referenced this issue Sep 30, 2021
10397: fix: fix format string highlighting for `panic!` and `assert!` r=jonas-schievink a=jonas-schievink

part of #10394

bors r+

Co-authored-by: Jonas Schievink <[email protected]>
@jonas-schievink
Copy link
Contributor

#10397 fixes panic!, assert! and debug_assert!

@jonas-schievink
Copy link
Contributor

todo! and unimplemented! both expand to

$crate::panic!("not (yet) implemented: {}", $crate::format_args!($($arg)+))

which we should already handle, so there's another bug in there.

unreachable! on the other hand expands to

$crate::panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)

which is hard to handle correctly, since the actual format string passed to (const_)format_args! differs from the one the user sees

bors bot added a commit that referenced this issue Sep 30, 2021
10400: fix: fix format string highlighting for `todo!` and `unimplemented!` r=jonas-schievink a=jonas-schievink

Part of #10394

These macros require us to see through the `const_format_args!` invocation the panic macros generate, for that we have to add it to our supported built-in macros.

I've also made the macros in the test `#[macro_export]` (turns out they didn't all resolve correctly before this), which changes the output slightly.

bors r+

Co-authored-by: Jonas Schievink <[email protected]>
bors bot added a commit that referenced this issue Sep 30, 2021
10400: fix: fix format string highlighting for `todo!` and `unimplemented!` r=jonas-schievink a=jonas-schievink

Part of #10394

These macros require us to see through the `const_format_args!` invocation the panic macros generate, for that we have to add it to our supported built-in macros.

I've also made the macros in the test `#[macro_export]` (turns out they didn't all resolve correctly before this), which changes the output slightly.

bors r+

Co-authored-by: Jonas Schievink <[email protected]>
bors bot added a commit that referenced this issue Oct 1, 2021
10412: feat: highlight `asm!` as format string r=jonas-schievink a=jonas-schievink

![screenshot-2021-10-01-12:34:31](https://user-images.githubusercontent.com/1786438/135606261-a1cb6caf-0a7f-45f7-9dde-0275370b0889.png)

part of #10394

bors r+

Co-authored-by: Jonas Schievink <[email protected]>
@jonas-schievink
Copy link
Contributor

#10412 highlights asm! as a format string (note that there's also #6031 for actual assembly syntax highlighting)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro macro expansion C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants