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

Prevent ICE when formatting an empty-ish macro arm #5833

Merged
merged 1 commit into from
Jul 19, 2023

Conversation

tdanniels
Copy link
Contributor

@tdanniels tdanniels commented Jul 15, 2023

Fixes #5730

Previously rustfmt was attempting to slice a string with an invalid range (start > end), leading to the ICE.

When formatting a macro transcriber snippet consisting of a lone semicolon, the snippet was being formatted into the empty string, leading the enclosing fn main() {\n} added by format_code_block to be formatted into fn main() {}. However, rustfmt was assuming that the enclosing function string's length had been left unchanged. This was leading to an invalid range being constructed when attempting to trim off the enclosing function.

The fix is to just clamp the range's start to be less than or equal to the range's end, since if end < start there's nothing to iterate over anyway.


Hi rustfmt'ers! This is my first PR to the project. I'm still not too familiar with the codebase, so hopefully I'm doing this right. Any feedback is welcome.

Fixes 5730

Previously rustfmt was attempting to slice a string with an invalid
range (`start > end`), leading to the ICE.

When formatting a macro transcriber snippet consisting of a lone
semicolon, the snippet was being formatted into the empty string,
leading the enclosing `fn main() {\n}` added by `format_code_block` to
be formatted into `fn main() {}`. However, rustfmt was assuming that the
enclosing function string's length had been left unchanged. This was
leading to an invalid range being constructed when attempting to trim
off the enclosing function.

The fix is to just clamp the range's start to be less than or equal
to the range's end, since if `end < start` there's nothing to iterate
over anyway.
Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@calebcartwright calebcartwright added the release-notes Needs an associated changelog entry label Jul 19, 2023
@calebcartwright calebcartwright merged commit b944a32 into rust-lang:master Jul 19, 2023
@tdanniels tdanniels deleted the issue_5730 branch July 19, 2023 02:10
@ytmimi ytmimi removed the release-notes Needs an associated changelog entry label Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE panicked at 'begin <= end (12 <= 11) when slicing fn main() {}''
3 participants