Fix #2640: Handle hidden crate attributes in fn main wrapping#3041
Open
cobyfrombrooklyn-bot wants to merge 1 commit intorust-lang:masterfrom
Open
Fix #2640: Handle hidden crate attributes in fn main wrapping#3041cobyfrombrooklyn-bot wants to merge 1 commit intorust-lang:masterfrom
cobyfrombrooklyn-bot wants to merge 1 commit intorust-lang:masterfrom
Conversation
The partition_rust_source regex did not account for lines with the hidden line prefix (`# `), so `# #![feature(...)]` was not recognized as a crate-level attribute. This caused wrap_rust_main to place the attribute inside fn main() instead of before it, breaking playground execution for code blocks with hidden feature attributes and no explicit fn main. Updated the HEADER_RE regex to optionally match the `# ` hidden line prefix before crate-level inner attributes. Added unit tests for wrap_rust_main and partition_rust_source covering the hidden prefix case, plus an integration test with a playground code block using a hidden feature attribute.
ehuss
reviewed
Feb 27, 2026
| ( | ||
| (?: | ||
| ^[ \t]*\#!\[.* (?:\r?\n)? | ||
| ^[ \t]*(?:\#\x20)?[ \t]*\#!\[.* (?:\r?\n)? |
Contributor
There was a problem hiding this comment.
Why does this have \x20? This doesn't seem to have the same understanding of hidden lines as the rest of the code does.
Also, this seems to not also update the blank lines rule.
I would expect the following to work:
| Input | Output | Hidden |
|---|---|---|
#!feature(x) |
#!feature(x) |
Not hidden |
##!feature(x) |
#!feature(x) |
Not hidden |
# #![feature(x) |
#!feature(x) |
Hidden |
# #![feature(x)## #![feature(y)] |
#![feature(x)#![feature(y)] |
Hidden |
Perhaps it would be best to extract the hide-lines logic from hide_lines_rust so that there could be an iterator over the rust source lines that indicates if a line is hidden or not, and then these different parts could reuse that logic. rustdoc does something like this here with an enum that represents whether a line is hidden.
Contributor
|
BTW, pull requests that fix an issue should use one of GitHub's keywords in the description (like |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a playground code block contains a hidden crate-level attribute (e.g.
# #![feature(rustc_attrs)]) and no explicitfn main, the attribute gets placed inside the generatedfn main()instead of before it. This causes the playground "play" button to produce compilation errors.As @ehuss identified, the issue is in the regex used by
partition_rust_source— it doesn't account for the#hidden line prefix, so# #![feature(...)]isn't recognized as a crate-level attribute.Fix
Updated the
HEADER_REregex inpartition_rust_sourceto optionally match the#(hash-space) hidden line prefix before crate-level inner attributes (#![...]).Before:
^[ \t]*\#!\[After:
^[ \t]*(?:\#\x20)?[ \t]*\#!\[Test
wrap_rust_main(basic wrapping, hidden feature attr, already-has-main)partition_rust_sourcefor hidden prefix caseplayground_hidden_feature_attr) with a code block using# #![feature(rustc_attrs)]