Skip to content

Commit

Permalink
Reformat mir! macro invocations to use braces.
Browse files Browse the repository at this point in the history
The `mir!` macro has multiple parts:
- An optional return type annotation.
- A sequence of zero or more local declarations.
- A mandatory starting anonymous basic block, which is brace-delimited.
- A sequence of zero of more additional named basic blocks.

Some `mir!` invocations use braces with a "block" style, like so:
```
mir! {
    let _unit: ();
    {
	let non_copy = S(42);
	let ptr = std::ptr::addr_of_mut!(non_copy);
	// Inside `callee`, the first argument and `*ptr` are basically
	// aliasing places!
	Call(_unit = callee(Move(*ptr), ptr), ReturnTo(after_call), UnwindContinue())
    }
    after_call = {
	Return()
    }
}
```
Some invocations use parens with a "block" style, like so:
```
mir!(
    let x: [i32; 2];
    let one: i32;
    {
	x = [42, 43];
	one = 1;
	x = [one, 2];
	RET = Move(x);
	Return()
    }
)
```
And some invocations uses parens with a "tighter" style, like so:
```
mir!({
    SetDiscriminant(*b, 0);
    Return()
})
```
This last style is generally used for cases where just the mandatory
starting basic block is present. Its braces are placed next to the
parens.

This commit changes all `mir!` invocations to use braces with a "block"
style. Why?

- Consistency is good.

- The contents of the invocation is a block of code, so it's odd to use
  parens. They are more normally used for function-like macros.

- Most importantly, the next commit will enable rustfmt for
  `tests/mir-opt/`. rustfmt is more aggressive about formatting macros
  that use parens than macros that use braces. Without this commit's
  changes, rustfmt would break a couple of `mir!` macro invocations that
  use braces within `tests/mir-opt` by inserting an extraneous comma.
  E.g.:
  ```
  mir!(type RET = (i32, bool);, { // extraneous comma after ';'
      RET.0 = 1;
      RET.1 = true;
      Return()
  })
  ```
  Switching those `mir!` invocations to use braces avoids that problem,
  resulting in this, which is nicer to read as well as being valid
  syntax:
  ```
  mir! {
      type RET = (i32, bool);
      {
	  RET.0 = 1;
	  RET.1 = true;
	  Return()
      }
  }
  ```
  • Loading branch information
nnethercote committed Jun 3, 2024
1 parent fe5adb9 commit ae5598a
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions core/src/intrinsics/mir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//!
//! #[custom_mir(dialect = "built")]
//! pub fn simple(x: i32) -> i32 {
//! mir!(
//! mir! {
//! let temp2: i32;
//!
//! {
Expand All @@ -33,7 +33,7 @@
//! RET = temp2;
//! Return()
//! }
//! )
//! }
//! }
//! ```
//!
Expand Down Expand Up @@ -71,7 +71,7 @@
//!
//! #[custom_mir(dialect = "built")]
//! pub fn choose_load(a: &i32, b: &i32, c: bool) -> i32 {
//! mir!(
//! mir! {
//! {
//! match c {
//! true => t,
Expand All @@ -93,20 +93,22 @@
//! RET = *temp;
//! Return()
//! }
//! )
//! }
//! }
//!
//! #[custom_mir(dialect = "built")]
//! fn unwrap_unchecked<T>(opt: Option<T>) -> T {
//! mir!({
//! RET = Move(Field(Variant(opt, 1), 0));
//! Return()
//! })
//! mir! {
//! {
//! RET = Move(Field(Variant(opt, 1), 0));
//! Return()
//! }
//! }
//! }
//!
//! #[custom_mir(dialect = "runtime", phase = "optimized")]
//! fn push_and_pop<T>(v: &mut Vec<T>, value: T) {
//! mir!(
//! mir! {
//! let _unused;
//! let popped;
//!
Expand All @@ -125,19 +127,19 @@
//! ret = {
//! Return()
//! }
//! )
//! }
//! }
//!
//! #[custom_mir(dialect = "runtime", phase = "optimized")]
//! fn annotated_return_type() -> (i32, bool) {
//! mir!(
//! mir! {
//! type RET = (i32, bool);
//! {
//! RET.0 = 1;
//! RET.1 = true;
//! Return()
//! }
//! )
//! }
//! }
//! ```
//!
Expand All @@ -152,7 +154,7 @@
//!
//! #[custom_mir(dialect = "built")]
//! fn borrow_error(should_init: bool) -> i32 {
//! mir!(
//! mir! {
//! let temp: i32;
//!
//! {
Expand All @@ -171,15 +173,15 @@
//! RET = temp;
//! Return()
//! }
//! )
//! }
//! }
//! ```
//!
//! ```text
//! error[E0381]: used binding is possibly-uninitialized
//! --> test.rs:24:13
//! |
//! 8 | / mir!(
//! 8 | / mir! {
//! 9 | | let temp: i32;
//! 10 | |
//! 11 | | {
Expand All @@ -191,7 +193,7 @@
//! | | ^^^^^^^^^^ value used here but it is possibly-uninitialized
//! 25 | | Return()
//! 26 | | }
//! 27 | | )
//! 27 | | }
//! | |_____- binding declared here but left uninitialized
//!
//! error: aborting due to 1 previous error
Expand Down Expand Up @@ -407,18 +409,22 @@ define!(
///
/// #[custom_mir(dialect = "built")]
/// fn unwrap_deref(opt: Option<&i32>) -> i32 {
/// mir!({
/// RET = *Field::<&i32>(Variant(opt, 1), 0);
/// Return()
/// })
/// mir! {
/// {
/// RET = *Field::<&i32>(Variant(opt, 1), 0);
/// Return()
/// }
/// }
/// }
///
/// #[custom_mir(dialect = "built")]
/// fn set(opt: &mut Option<i32>) {
/// mir!({
/// place!(Field(Variant(*opt, 1), 0)) = 5;
/// Return()
/// })
/// mir! {
/// {
/// place!(Field(Variant(*opt, 1), 0)) = 5;
/// Return()
/// }
/// }
/// }
/// ```
fn Field<F>(place: (), field: u32) -> F
Expand Down Expand Up @@ -455,7 +461,7 @@ define!(
/// your MIR into something that is easier to parse in the compiler.
#[rustc_macro_transparency = "transparent"]
pub macro mir {
(
{
$(type RET = $ret_ty:ty ;)?
$(let $local_decl:ident $(: $local_decl_ty:ty)? ;)*
$(debug $dbg_name:ident => $dbg_data:expr ;)*
Expand All @@ -469,7 +475,7 @@ pub macro mir {
$($block:tt)*
}
)*
) => {{
} => {{
// First, we declare all basic blocks.
__internal_declare_basic_blocks!($(
$block_name $(($block_cleanup))?
Expand Down

0 comments on commit ae5598a

Please sign in to comment.