Skip to content

Introduce #[diagnostic::on_move(message)]#150935

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
rperier:provide_diagnostic_on_move_for_smart_pointers
Mar 20, 2026
Merged

Introduce #[diagnostic::on_move(message)]#150935
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
rperier:provide_diagnostic_on_move_for_smart_pointers

Conversation

@rperier
Copy link
Contributor

@rperier rperier commented Jan 10, 2026

View all comments

cc #149862

This is a first proposal. I have deliberately kept it simpler than diagnostic::on_unimplemented.

Few questions/remarks:

  • Do I need to move the OnMoveDirective logic into a dedicated module perhaps ? let's say into compiler/rustc_borrowck/src/diagnostics/on_move.rs
  • No problems to depend on crates like rustc_ast from the borrowck ?
  • Notes are not supported yet. While message and label are very static , in the sense that they are emitted in the same way from the same place in the borrowck, it is not the case for the notes. It would make the code more complex. But, I can add support for notes if it does make sense.

Suggestions are welcomed !

@rustbot
Copy link
Collaborator

rustbot commented Jan 10, 2026

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 10, 2026
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 10, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 10, 2026

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
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

@rperier
Copy link
Contributor Author

rperier commented Jan 10, 2026

r? @estebank

@rustbot rustbot assigned estebank and unassigned JonathanBrouwer Jan 10, 2026
@mejrs
Copy link
Contributor

mejrs commented Jan 13, 2026

I'm currently working on migrating the existing diagnostic attribute infrastructure, can you do this PR after that? It's going to be quite a mess to resolve that conflict.

Is this meant to be only used by the standard library or also by users? If only by std, I think you should forego the attribute and instead check whether T implements UseCloned and recommend a clone (or .use if on nightly).

@rperier
Copy link
Contributor Author

rperier commented Jan 14, 2026

Yeah I can completly wait until you finished your work and do my PR after that, It was my intention to be honest . Resolving conflicts or rebasing is not an issue for me.

@rperier rperier force-pushed the provide_diagnostic_on_move_for_smart_pointers branch from 9a0162d to 8071a7d Compare January 21, 2026 19:15
@rustbot
Copy link
Collaborator

rustbot commented Jan 21, 2026

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

Changes to the size of AST and/or HIR nodes.

cc @nnethercote

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

@rustbot

This comment has been minimized.

@rperier
Copy link
Contributor Author

rperier commented Jan 21, 2026

I have reworked my code and moved everything to rustc_attr_parsing and rustc_hir/src/attrs/data_structures.rs. Few questions/remarks remain:

  • Perhaps, It would make sense to allow this diagnostic attribute to be used only for nightly, for now, what do you think ?
  • I can wait until some refactoring for attributes are merged upstream, and wait for my PR being merged after that, if it helps, no problems for me

@rperier rperier force-pushed the provide_diagnostic_on_move_for_smart_pointers branch from 8071a7d to 09506a4 Compare January 21, 2026 19:26
@rust-bors

This comment has been minimized.

@rperier rperier force-pushed the provide_diagnostic_on_move_for_smart_pointers branch from 09506a4 to 829651a Compare January 22, 2026 19:23
@rustbot

This comment has been minimized.

@rperier
Copy link
Contributor Author

rperier commented Jan 22, 2026

Rebased onto main, I have also fixed the size of AttributeKind::OnMove

@rperier rperier force-pushed the provide_diagnostic_on_move_for_smart_pointers branch from 829651a to b12aa8b Compare January 28, 2026 14:24
@rustbot

This comment has been minimized.

@rperier
Copy link
Contributor Author

rperier commented Jan 28, 2026

Rebased onto main. I have covered changes related to #151516 .
I assume that changes are still underway in rustc_attr_parsing , but I would be nice to discuss about generic meta item list errors:

  1. Do a generic lint will be emitted by the parser itself in this case ? Instead of the parser of each diagnostic attribute has to do it by itself.. it would be better for code refactoring.
  2. In case this is not the case, it might be helpful to being able to retrieve the span of the error instead of the span of the whole diagnostic attribute (better granularity) . See the previous discussion.

If I can help for this (by proposing a patch for a generic lint, for example), feel free to ask.

@rust-bors

This comment has been minimized.

@rperier rperier force-pushed the provide_diagnostic_on_move_for_smart_pointers branch from b12aa8b to 1efb939 Compare January 30, 2026 17:31
@rustbot

This comment has been minimized.

@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 Mar 6, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 6, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Copy link
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

Diagnostic attributes can't emit errors, you have to emit lints instead in some places.

View changes since this review

@rperier
Copy link
Contributor Author

rperier commented Mar 10, 2026

I will look into it this week. Thanks for your time and your feedbacks.

@rperier
Copy link
Contributor Author

rperier commented Mar 11, 2026

* for when we hit an explicit `Copy` bound, like `fn take_copy<T: Copy>(t: T){}`; then this attribute should do nothing. (or should it?)

View changes since this review

I don't see the point in adding a test case for this. In a case like that either the type passed as argument does not impl the Copy trait, so you will get a trait bound error (not a borrowck error), or the type passed as argument impls the Copy trait, in which case it will be passed by copy and not by value, so it won't be moved. so the borrowck won't complained in such a case.

EDIT: we should already have non regression testing for Copy no ? I think a case like this one is probably already covered.

This might be helpful for smart pointers to explains why they aren't Copy
and what to do instead or just to let the user know that .clone() is very
cheap and can be called without a performance penalty.
@rperier rperier force-pushed the provide_diagnostic_on_move_for_smart_pointers branch from 81f1be4 to 965f1e7 Compare March 11, 2026 15:59
@rustbot
Copy link
Collaborator

rustbot commented Mar 11, 2026

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.

@rperier
Copy link
Contributor Author

rperier commented Mar 11, 2026

Except my last remark regarding the Copy trait, everything should be fixed.

@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 Mar 11, 2026
@rperier rperier requested a review from JonathanBrouwer March 13, 2026 17:20
@JonathanBrouwer
Copy link
Contributor

I'm on holidays atm, will take a look in a few days :)

@rperier
Copy link
Contributor Author

rperier commented Mar 13, 2026

I'm on holidays atm, will take a look in a few days :)

No problem, have nice holidays then 😉

Copy link
Contributor

@JonathanBrouwer JonathanBrouwer left a comment

Choose a reason for hiding this comment

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

I think this PR is fine the way it currently is :)
I checked that all previous conversations are resolved, and fully went through the changes one more time.
Thanks for all the hard work!

@bors r+ rollup

View changes since this review

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 19, 2026

📌 Commit 965f1e7 has been approved by JonathanBrouwer

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 19, 2026
@rperier
Copy link
Contributor Author

rperier commented Mar 20, 2026

Thanks for your time and your feedbacks ! Next step is perhaps update the documentation regarding this diagnostic attribute, what do you think ?

rust-bors bot pushed a commit that referenced this pull request Mar 20, 2026
Rollup of 15 pull requests

Successful merges:

 - #152909 (sess: `-Zbranch-protection` is a target modifier)
 - #153556 (`impl` restriction lowering)
 - #154048 (Don't emit rustdoc `missing_doc_code_examples` lint on impl items)
 - #150935 (Introduce #[diagnostic::on_move(message)])
 - #152973 (remove -Csoft-float)
 - #153862 (Rename `cycle_check` to `find_cycle`)
 - #153992 (bootstrap: Optionally print a backtrace if a command fails)
 - #154019 (two smaller feature cleanups)
 - #154059 (tests: Activate `must_not_suspend` test for `MutexGuard` dropped before `await`)
 - #154075 (Rewrite `query_ensure_result`.)
 - #154082 (Updates derive_where and removes workaround)
 - #154084 (Preserve braces around `self` in use tree pretty printing)
 - #154086 (Insert space after float literal ending with `.` in pretty printer)
 - #154087 (Fix whitespace after fragment specifiers in macro pretty printing)
 - #154109 (tests: Add regression test for async closures involving HRTBs)
@JonathanBrouwer
Copy link
Contributor

@rust-bors rust-bors bot merged commit 734cca0 into rust-lang:main Mar 20, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Mar 20, 2026
Copy link
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

Thanks for your time and your feedbacks ! Next step is perhaps update the documentation regarding this diagnostic attribute, what do you think ?

Adding it to the reference would be lovely https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-diagnostic-tool-attribute-namespace

This is unstable, so it should be in the unstable book, not the reference. See https://doc.rust-lang.org/nightly/unstable-book/language-features/diagnostic-on-const.html for example. It's up to you whether you do this or how much effort you put into this. It'd be nice if it's written in a "referencey" way; that'll be nice for when it gets stabilized.

@JonathanBrouwer #149862 can be closed now.

View changes since this review

/// Allows giving non-const impls custom diagnostic messages if attempted to be used as const
(unstable, diagnostic_on_const, "1.93.0", Some(143874)),
/// Allows giving on-move borrowck custom diagnostic messages for a type
(unstable, diagnostic_on_move, "CURRENT_RUSTC_VERSION", Some(150935)),
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be pointing to a tracking issue (please make one!), not this PR.

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

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants