Skip to content

Fix idempotency issue with raw strings - #6696

Open
shulaoda wants to merge 1 commit into
rust-lang:mainfrom
shulaoda:10-11-fix-idempotency-issue-with-raw-strings
Open

Fix idempotency issue with raw strings#6696
shulaoda wants to merge 1 commit into
rust-lang:mainfrom
shulaoda:10-11-fix-idempotency-issue-with-raw-strings

Conversation

@shulaoda

@shulaoda shulaoda commented Oct 10, 2025

Copy link
Copy Markdown
Contributor

Fixes #6161

CharClasses classifies the closing quote of r"..." as Normal, while "..." and r#"..."# classify theirs as InString. When that quote starts a line, LineClasses fails to mark the line as EndString, so trim_left_preserve_layout treats it as code with zero leading whitespace. The minimum indentation of the macro body is then always zero and every run indents the body one more level.

@shulaoda

Copy link
Copy Markdown
Contributor Author

I’m not very familiar with the codebase, so there might be a better way to handle this.

@jieyouxu jieyouxu added F-impacts-stable-formatted-code Expected formatting impact: affects stable formatted code (caution) S-waiting-on-review Status: awaiting review from the assignee but also interested parties. F-impacts-stable-formatted-but-non-idempotent Expected formatting impact: affects stable "formatted" code, but which is non-idempotent. and removed pr-not-reviewed F-impacts-stable-formatted-code Expected formatting impact: affects stable formatted code (caution) labels Feb 19, 2026
@jieyouxu jieyouxu self-assigned this Jul 25, 2026
Comment thread src/comment.rs
match chr {
'"' => {
if sharps == 0 {
char_kind = FullCodeCharKind::Normal;

@jieyouxu jieyouxu Jul 31, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Question (not necessary for you, but one I had when looking at this diff): do we know or can we guess why we have to override char_kind in this arm? I'm a bit concerned that while this diff is seemingly tiny and fixes #6161, that it regresses another pattern that isn't occurring to me just yet.

View changes since the review

@jieyouxu jieyouxu Jul 31, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like this goes back to 6748042, #2983, which fixes #2642

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The override comes from 6748042, which first special cased r"...". Before that commit it was lexed through LitString, where the closing quote is already InString. The Normal override looks copied from the RawStringSuffix arm, where the last # is Normal.

"a": r"bb
ccc
",
};

@jieyouxu jieyouxu Jul 31, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Discussion (not really for author): okay, so this I am not 100% sure about. Can we actually format raw strings, or strings in general, in user macros? Do we want to?

This raw string here AFAIK are indeed equivalent so...

View changes since the review

@ytmimi ytmimi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Might also be useful to add a unit test to directly test the the CharClasses iterator impl to make sure next returns the expected FullCodeCharKind?

View changes since this review

@shulaoda
shulaoda force-pushed the 10-11-fix-idempotency-issue-with-raw-strings branch from 50a49e7 to cb10a13 Compare August 1, 2026 20:25
@rustbot

rustbot commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

F-impacts-stable-formatted-but-non-idempotent Expected formatting impact: affects stable "formatted" code, but which is non-idempotent. S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perpetually increasing indentation

4 participants