Skip to content

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

Open
rperier wants to merge 1 commit intorust-lang:mainfrom
rperier:provide_diagnostic_on_move_for_smart_pointers
Open

Introduce #[diagnostic::on_move(message)]#150935
rperier wants to merge 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

Comment on lines +1 to +10
#[diagnostic::on_move(
message = Foo,
//~^ERROR expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found expression
label = "Bar",
)]
#[diagnostic::on_move(
message = "Foo"
label = "Bar",
//~^ERROR expected `,`, found `label`
)]
Copy link
Contributor

Choose a reason for hiding this comment

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

This can't be an error; this must be a lint. But you will need #151516 to do so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi. Since this morning, these "cases" are no longer handled at all. The parser does not report anything and I get an empty MetaItemListParser (there are no sub_parsers) in onMoveParser::convert , see #t-compiler/help > #diagnostic::on_move(message) (PR #150935) @ 💬 for the full description. Is it expected ? I mean I cannot emit the lint myself, I have an empty ArgParser context, and the parser reports nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think, I got it. This is due to https://github.com/rust-lang/rust/blob/main/compiler/rustc_attr_parsing/src/parser.rs#L132 . I can of course emit a lint myself when the MetaItemListParser is empty, it's a shame to no longer have the context of the error, we had a more fine grained context with a span before. The span now will be the full diagnostic attribute.

Copy link
Contributor Author

@rperier rperier Jan 28, 2026

Choose a reason for hiding this comment

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

It is possible to replace the entire span https://github.com/rust-lang/rust/blob/main/compiler/rustc_attr_parsing/src/parser.rs#L135 by e.span (or build a span from e) ? so we would have a span on the attr in this case instead of the whole diagnostic attribute.

@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.

@rust-log-analyzer

This comment has been minimized.

@rperier rperier force-pushed the provide_diagnostic_on_move_for_smart_pointers branch 2 times, most recently from c815bc7 to ca385ba Compare February 25, 2026 10:10
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@JonathanBrouwer
Copy link
Contributor

I assume this is still work in progress right? Make sure to mark it as waiting-on-review when it's ready, then I'll take a look

@rperier
Copy link
Contributor Author

rperier commented Feb 27, 2026

Hi, yes, it is still work in progress. I think it should be ready this weekend.

@JonathanBrouwer
Copy link
Contributor

No hurry :) just wanted to verify

@rperier rperier force-pushed the provide_diagnostic_on_move_for_smart_pointers branch from ca385ba to 5d6b5bf Compare March 2, 2026 18:09
@rustbot

This comment has been minimized.

@rperier
Copy link
Contributor Author

rperier commented Mar 2, 2026

I think that's it for now, two remaining tasks:

  • I have to better use FormatArgs
  • Do I add "notes=" to diagnostic::on_move(...) as it is parsed and part of the Directive ? I could emit the notes seperately when these are present. It could be helpful to show an url for example or redirect the user to an api documentation.

@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 2, 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, I think this looks good generally. One important thing missing though is a feature gate; the consensus on Zulip was that this should be unstable for now.

Do I add "notes=" to diagnostic::on_move(...) as it is parsed and part of the Directive ?

Sure, why not? I'd prefer if this attribute was consistent with the others.

Please also add more tests:

  • for when the item is in another crate as the error message generally is different (there's no recommendation to implement Clone, for example).
  • 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

@rperier rperier force-pushed the provide_diagnostic_on_move_for_smart_pointers branch from 5d6b5bf to 81f1be4 Compare March 4, 2026 18:37
@rustbot

This comment has been minimized.

@rperier
Copy link
Contributor Author

rperier commented Mar 4, 2026

Rebased onto main and fixed almost everything, I need to add more tests regarding what you mentioned and for generic params.

EDIT: I have also to update the error message when the string format is unknown. Because currently the help message only mentions {Self}, which is not correct.

@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
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-review Status: Awaiting review from the assignee but also interested parties. 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