-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
reserved width for unsafe in match arm pattern #7097
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -286,6 +286,15 @@ fn rewrite_match_arm( | |
| .sub_width(7 + label_len, arm.span)? | ||
| .offset_left(pipe_offset, arm.span)? | ||
| } | ||
| ast::ExprKind::Block(block, None) | ||
| if is_unsafe_block(block) | ||
| && context.config.style_edition() >= StyleEdition::Edition2027 => | ||
| { | ||
| // 12 = ` => unsafe {` | ||
| shape | ||
| .sub_width(12, arm.span)? | ||
| .offset_left(pipe_offset, arm.span)? | ||
|
Comment on lines
+293
to
+296
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we know the block is empty, should we: // 13 = ` => unsafe {}`
sub_width(13, arm.span)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes we can |
||
| } | ||
| _ => { | ||
| // 5 = ` => {` | ||
| shape | ||
|
|
@@ -526,9 +535,11 @@ pub(crate) fn rewrite_match_body( | |
| body_shape.width, | ||
| ); | ||
|
|
||
| let enforce_empty_block_width = | ||
| is_empty_block && context.config.style_edition() >= StyleEdition::Edition2027; | ||
| match rewrite { | ||
| Ok(ref body_str) | ||
| if is_block | ||
| if (is_block && !enforce_empty_block_width) | ||
| || (!body_str.contains('\n') | ||
| && unicode_str_width(body_str) <= body_shape.width) => | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // rustfmt-style_edition: 2024 | ||
| // rustfmt-max_width: 80 | ||
|
|
||
| #![feature(gen_blocks, try_blocks)] | ||
|
|
||
| #[derive(Clone, Copy)] | ||
| enum ExampleTypeX { | ||
| VariantAlphaSampleXYZ, | ||
| VariantBetaXYZ, | ||
| VariantABCD, | ||
| VariantABCDE, | ||
| VariantABCDEF, | ||
| VariantABCDEFG, | ||
| VariantABCDEFGH, | ||
| VariantABCDEFGHI, | ||
| VariantABCDEFGHIJ, | ||
| VariantABCDEFGHIJK, | ||
| } | ||
|
|
||
| fn demo(lhs: ExampleTypeX, rhs: ExampleTypeX) { | ||
| //unsafe block | ||
| match (lhs, rhs) { | ||
| ( | ||
| ExampleTypeX::VariantAlphaSampleXYZ, | ||
| ExampleTypeX::VariantBetaXYZ, | ||
| ) => unsafe { | ||
| } | ||
| _ => {} | ||
| } | ||
|
|
||
| match (lhs, rhs) { | ||
| // 1 char below the max_width limit | ||
| (ExampleTypeX::VariantABCD, ExampleTypeX::VariantBetaXYZ) => unsafe {}, | ||
| // everything properly fits on 1 line at exactly the max_width limit | ||
| (ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => unsafe {}, | ||
| // 1 char over the max_width limit | ||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe {}, | ||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()}, | ||
| (ExampleTypeX::VariantABCDEFGHIJKL, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()}, | ||
| (ExampleTypeX::VariantABCDEFGHIJKLMNO, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()}, | ||
| (ExampleTypeX::VariantABCDEFHGHI, ExampleTypeX::VariantBetaXYZ) => 'a: {}, | ||
| _ => {} | ||
| } | ||
|
|
||
| // const block | ||
| match (lhs, rhs) { | ||
| // 1 char below the max_width limit | ||
| (ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => const {}, | ||
| // everything properly fits on 1 line at exactly the max_width limit | ||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => const {}, | ||
| // 1 char over the max_width limit | ||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => const {}, | ||
| _ => {} | ||
| } | ||
|
|
||
| // async block | ||
| match (lhs, rhs) { | ||
| // 1 char below the max_width limit | ||
| (ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => async {}, | ||
| // everything properly fits on 1 line at exactly the max_width limit | ||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => async {}, | ||
| // 1 char over the max_width limit | ||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => async {}, | ||
| _ => {} | ||
| } | ||
|
|
||
| // gen block | ||
| match (lhs, rhs) { | ||
| // 1 char below the max_width limit | ||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => gen {}, | ||
| // everything properly fits on 1 line at exactly the max_width limit | ||
| (ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => gen {}, | ||
| // 1 char over the max_width limit | ||
| (ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => gen {}, | ||
| _ => {} | ||
| } | ||
|
|
||
| // try block | ||
| match (lhs, rhs) { | ||
| //1 char below the max_width limit | ||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => try {}, | ||
| // everything properly fits on 1 line at exactly the max_width limit | ||
| (ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => try {}, | ||
| // 1 char over the max_width limit | ||
| (ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => try {}, | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // rustfmt-style_edition: 2027 | ||
| // rustfmt-max_width: 80 | ||
|
|
||
| #![feature(gen_blocks, try_blocks)] | ||
|
|
||
| #[derive(Clone, Copy)] | ||
| enum ExampleTypeX { | ||
| VariantAlphaSampleXYZ, | ||
| VariantBetaXYZ, | ||
| VariantABCD, | ||
| VariantABCDE, | ||
| VariantABCDEF, | ||
| VariantABCDEFG, | ||
| VariantABCDEFGH, | ||
| VariantABCDEFGHI, | ||
| VariantABCDEFGHIJ, | ||
| VariantABCDEFGHIJK, | ||
| } | ||
|
|
||
| fn demo(lhs: ExampleTypeX, rhs: ExampleTypeX) { | ||
| //unsafe block | ||
| match (lhs, rhs) { | ||
| ( | ||
| ExampleTypeX::VariantAlphaSampleXYZ, | ||
| ExampleTypeX::VariantBetaXYZ, | ||
| ) => unsafe { | ||
| } | ||
| _ => {} | ||
| } | ||
|
|
||
| match (lhs, rhs) { | ||
| // 1 char below the max_width limit | ||
| (ExampleTypeX::VariantABCD, ExampleTypeX::VariantBetaXYZ) => unsafe {}, | ||
| // everything properly fits on 1 line at exactly the max_width limit | ||
| (ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => unsafe {}, | ||
| // 1 char over the max_width limit | ||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe {}, | ||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()}, | ||
| (ExampleTypeX::VariantABCDEFGHIJKL, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()}, | ||
| (ExampleTypeX::VariantABCDEFGHIJKLMNO, ExampleTypeX::VariantBetaXYZ) => unsafe { non_empty_block()}, | ||
| (ExampleTypeX::VariantABCDEFHGHI, ExampleTypeX::VariantBetaXYZ) => 'a: {}, | ||
| _ => {} | ||
| } | ||
|
|
||
| // const block | ||
| match (lhs, rhs) { | ||
| // 1 char below the max_width limit | ||
| (ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => const {}, | ||
| // everything properly fits on 1 line at exactly the max_width limit | ||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => const {}, | ||
| // 1 char over the max_width limit | ||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => const {}, | ||
| _ => {} | ||
| } | ||
|
|
||
| // async block | ||
| match (lhs, rhs) { | ||
| // 1 char below the max_width limit | ||
| (ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => async {}, | ||
| // everything properly fits on 1 line at exactly the max_width limit | ||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => async {}, | ||
| // 1 char over the max_width limit | ||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => async {}, | ||
| _ => {} | ||
| } | ||
|
|
||
| // gen block | ||
| match (lhs, rhs) { | ||
| // 1 char below the max_width limit | ||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => gen {}, | ||
| // everything properly fits on 1 line at exactly the max_width limit | ||
| (ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => gen {}, | ||
| // 1 char over the max_width limit | ||
| (ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => gen {}, | ||
| _ => {} | ||
| } | ||
|
|
||
| // try block | ||
| match (lhs, rhs) { | ||
| //1 char below the max_width limit | ||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => try {}, | ||
| // everything properly fits on 1 line at exactly the max_width limit | ||
| (ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => try {}, | ||
| // 1 char over the max_width limit | ||
| (ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => try {}, | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,102 @@ | ||||||||||||||||||||||||||||||||||
| // rustfmt-style_edition: 2024 | ||||||||||||||||||||||||||||||||||
| // rustfmt-max_width: 80 | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #![feature(gen_blocks, try_blocks)] | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #[derive(Clone, Copy)] | ||||||||||||||||||||||||||||||||||
| enum ExampleTypeX { | ||||||||||||||||||||||||||||||||||
| VariantAlphaSampleXYZ, | ||||||||||||||||||||||||||||||||||
| VariantBetaXYZ, | ||||||||||||||||||||||||||||||||||
| VariantABCD, | ||||||||||||||||||||||||||||||||||
| VariantABCDE, | ||||||||||||||||||||||||||||||||||
| VariantABCDEF, | ||||||||||||||||||||||||||||||||||
| VariantABCDEFG, | ||||||||||||||||||||||||||||||||||
| VariantABCDEFGH, | ||||||||||||||||||||||||||||||||||
| VariantABCDEFGHI, | ||||||||||||||||||||||||||||||||||
| VariantABCDEFGHIJ, | ||||||||||||||||||||||||||||||||||
| VariantABCDEFGHIJK, | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| fn demo(lhs: ExampleTypeX, rhs: ExampleTypeX) { | ||||||||||||||||||||||||||||||||||
| //unsafe block | ||||||||||||||||||||||||||||||||||
| match (lhs, rhs) { | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantAlphaSampleXYZ, ExampleTypeX::VariantBetaXYZ) => unsafe { | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| _ => {} | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| match (lhs, rhs) { | ||||||||||||||||||||||||||||||||||
| // 1 char below the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCD, ExampleTypeX::VariantBetaXYZ) => unsafe {}, | ||||||||||||||||||||||||||||||||||
| // everything properly fits on 1 line at exactly the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => unsafe {}, | ||||||||||||||||||||||||||||||||||
| // 1 char over the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe {}, | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to understand why there's this inconsistency between the Do we need to update the pattern matching for these other kinds of blocks in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pattern matching seems correct we are doing same thing in expr.rs as well and for we are just missing pattern arm for unsafe block, Lines 280 to 295 in 7bc6cd7
lable block works, - |
||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => unsafe { | ||||||||||||||||||||||||||||||||||
| non_empty_block() | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEFGHIJKL, ExampleTypeX::VariantBetaXYZ) => unsafe { | ||||||||||||||||||||||||||||||||||
| non_empty_block() | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| ( | ||||||||||||||||||||||||||||||||||
| ExampleTypeX::VariantABCDEFGHIJKLMNO, | ||||||||||||||||||||||||||||||||||
| ExampleTypeX::VariantBetaXYZ, | ||||||||||||||||||||||||||||||||||
| ) => unsafe { non_empty_block() }, | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEFHGHI, ExampleTypeX::VariantBetaXYZ) => 'a: {} | ||||||||||||||||||||||||||||||||||
| _ => {} | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // const block | ||||||||||||||||||||||||||||||||||
| match (lhs, rhs) { | ||||||||||||||||||||||||||||||||||
| // 1 char below the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => const {}, | ||||||||||||||||||||||||||||||||||
| // everything properly fits on 1 line at exactly the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => const {}, | ||||||||||||||||||||||||||||||||||
| // 1 char over the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => { | ||||||||||||||||||||||||||||||||||
| const {} | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| _ => {} | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // async block | ||||||||||||||||||||||||||||||||||
| match (lhs, rhs) { | ||||||||||||||||||||||||||||||||||
| // 1 char below the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDE, ExampleTypeX::VariantBetaXYZ) => async {}, | ||||||||||||||||||||||||||||||||||
| // everything properly fits on 1 line at exactly the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEF, ExampleTypeX::VariantBetaXYZ) => async {}, | ||||||||||||||||||||||||||||||||||
| // 1 char over the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => { | ||||||||||||||||||||||||||||||||||
| async {} | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| _ => {} | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // gen block | ||||||||||||||||||||||||||||||||||
| match (lhs, rhs) { | ||||||||||||||||||||||||||||||||||
| // 1 char below the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => gen {}, | ||||||||||||||||||||||||||||||||||
| // everything properly fits on 1 line at exactly the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => gen {}, | ||||||||||||||||||||||||||||||||||
| // 1 char over the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => { | ||||||||||||||||||||||||||||||||||
| gen {} | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| _ => {} | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // try block | ||||||||||||||||||||||||||||||||||
| match (lhs, rhs) { | ||||||||||||||||||||||||||||||||||
| //1 char below the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEFG, ExampleTypeX::VariantBetaXYZ) => try {}, | ||||||||||||||||||||||||||||||||||
| // everything properly fits on 1 line at exactly the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEFGH, ExampleTypeX::VariantBetaXYZ) => try {}, | ||||||||||||||||||||||||||||||||||
| // 1 char over the max_width limit | ||||||||||||||||||||||||||||||||||
| (ExampleTypeX::VariantABCDEFGHI, ExampleTypeX::VariantBetaXYZ) => { | ||||||||||||||||||||||||||||||||||
| try {} | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| _ => {} | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| fn main() {} | ||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ever possible to have an unsafe block with a label?
View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'a: unsafe{ 5 } gives compile error - after 'a -> a loop or block is expected.