Skip to content

Conversation

@GrigorenkoPV
Copy link
Contributor

@GrigorenkoPV GrigorenkoPV commented Nov 21, 2025

Tracking issue: #149226

This adds support for writing const { ... } as an item in a module. In the current implementation, this is a unique AST item that gets lowered to const _: () = const { ... }; in HIR.

rustfmt support included.

TODO:

@rustbot rustbot added O-SGX Target: SGX S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Nov 21, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@GrigorenkoPV GrigorenkoPV changed the title Const block item const blocks as a mod's item Nov 22, 2025
@rust-log-analyzer

This comment has been minimized.

@GrigorenkoPV GrigorenkoPV changed the title const blocks as a mod's item const blocks as a mod item Nov 23, 2025
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. and removed O-SGX Target: SGX labels Nov 26, 2025
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added T-lang Relevant to the language team and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 29, 2025
@GrigorenkoPV GrigorenkoPV marked this pull request as ready for review November 29, 2025 21:40
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 29, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 29, 2025

Some changes occurred in src/tools/rustfmt

cc @rust-lang/rustfmt

@rustbot
Copy link
Collaborator

rustbot commented Nov 29, 2025

r? @jdonszelmann

rustbot has assigned @jdonszelmann.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@jdonszelmann
Copy link
Contributor

then I think it might be better not to.

@jdonszelmann
Copy link
Contributor

the const block will get its own defid, so with it we effectively allocate two

@jdonszelmann
Copy link
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 9, 2025
@joshtriplett
Copy link
Member

I wonder about maybe doing this a different way, like how we allow omitting -> () on functions, so maybe we could instead allow const _ = …; that has to be unit-typed? That way it'd clearly be an item even in a place where inline const expressions are also valid.

That's not much of an improvement over const _: () = ...;; either way it's a kludge to hide a const block inside another kind of construct.

const { ... } makes perfect sense as an item. const { assert!(...) } is self-explanatory, for instance.

@traviscross traviscross removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Dec 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 10, 2025

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.

@GrigorenkoPV
Copy link
Contributor Author

Changed the expansion to const _: () = { ... };.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 10, 2025
pub enum AllowConstBlockItems {
Yes,
No,
DoesNotMatter,
Copy link
Contributor

Choose a reason for hiding this comment

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

could you just document what does not matter means? e.g. "in the parsing context it doesn't matter so we default to no"

@jdonszelmann
Copy link
Contributor

jdonszelmann commented Dec 11, 2025

r=me after that

@jdonszelmann
Copy link
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 11, 2025
shape
};
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics)?;
let rhs: String = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_kind, rhs_tactics)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

was this change necessary? If not please revert it.

Copy link
Contributor

@ytmimi ytmimi Dec 11, 2025

Choose a reason for hiding this comment

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

Please revert all of these changes, they don't look like they're necessary for const block formatting. It's best not to make refactors like this to subtrees that are maintained outside of r-l/rust

Comment on lines +543 to +558
let context = &self.get_context();
let offset = self.block_indent;
self.push_rewrite(
item.span,
block
.rewrite(
context,
Shape::legacy(
context.budget(offset.block_indent),
offset.block_only(),
),
)
.map(|rhs| {
recover_comment_removed(format!("const {rhs}"), span, context)
}),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you're going to need to call rewrite_block() directly here instead of block.rewrite() so that you can pass along attributes from the Item. I wan to avoid inner attributes getting removed like they were in rust-lang/rustfmt#6158.

Copy link
Contributor

@ytmimi ytmimi Dec 11, 2025

Choose a reason for hiding this comment

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

Can we add a test case similar to the one in rust-lang/rustfmt#6158 that has inner attributes.

Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

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

Need to make some rustfmt changes

View changes since this review

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

Labels

I-lang-radar Items that are on lang's radar and will need eventual work or consideration. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants