Skip to content
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

comment inside of empty enum variant payload with explicit discriminant results in invalid syntax after rustfmt run #5686

Closed
LizzyFleckenstein03 opened this issue Feb 7, 2023 · 4 comments · Fixed by #5687
Labels
a-comments bug Panic, non-idempotency, invalid code, etc. good first issue Issues up for grabs, also good candidates for new rustfmt contributors help wanted

Comments

@LizzyFleckenstein03
Copy link

Example code:

#[repr(u8)]
enum MyEnum {
    MyVariant {
        /* Comment */
    } = 0,
}

This compiles fine.

rustfmt 1.5.2-nightly (50d3ba5 2023-02-04) inserts a line:

#[repr(u8)]
enum MyEnum {
    MyVariant {
        /* Comment */
        } =
    } = 0,
}

This is a syntax error:

error: unexpected closing delimiter: `}`
 --> src/main.rs:7:1
  |
3 |     MyVariant {
  |               - this delimiter might not be properly closed...
4 |         /* Comment */
5 |         } =
  |         - ...as it matches this but it has different indentation
6 |     } = 0,
7 | }
  | ^ unexpected closing delimiter
@ytmimi ytmimi added bug Panic, non-idempotency, invalid code, etc. a-comments labels Feb 7, 2023
@calebcartwright
Copy link
Member

Thank you for the report!

For anyone interested in working on this, these types of bugs are almost always caused by an incorrect span derivation that only becomes problematic in the presence of comments. In this case it's an empty struct so I'd suggest taking a look in this general vicinity:

rustfmt/src/items.rs

Lines 1328 to 1332 in 5391847

if fields.is_empty() {
let inner_span = mk_sp(body_lo, span.hi() - BytePos(1));
format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "", "}");
return Some(result);
}

I suspect that the issue is with the construction of that inner span, and the hi value used (the second arg to the mk_sp call) should instead be the hi bytepos from before the last }

@calebcartwright calebcartwright added good first issue Issues up for grabs, also good candidates for new rustfmt contributors help wanted labels Feb 8, 2023
@Swiiz

This comment was marked as off-topic.

@ytmimi

This comment was marked as off-topic.

@ytmimi

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments bug Panic, non-idempotency, invalid code, etc. good first issue Issues up for grabs, also good candidates for new rustfmt contributors help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants