Skip to content

Detect missing derive on unresolved attribute even when not imported #142487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

estebank
Copy link
Contributor

@estebank estebank commented Jun 14, 2025

When encountering unresolved attributes, ensure the proc-macros for every crate in scope are added to the macro_map so that typos and missing derives are properly detected.

error: cannot find attribute `sede` in this scope
  --> $DIR/missing-derive-3.rs:20:7
   |
LL |     #[sede(untagged)]
   |       ^^^^
   |
help: the derive macros `Deserialize` and `Serialize` accept the similarly named `serde` attribute
   |
LL |     #[serde(untagged)]
   |         +

error: cannot find attribute `serde` in this scope
  --> $DIR/missing-derive-3.rs:14:7
   |
LL |     #[serde(untagged)]
   |       ^^^^^
   |
note: `serde` is imported here, but it is a crate, not an attribute
  --> $DIR/missing-derive-3.rs:4:1
   |
LL | extern crate serde;
   | ^^^^^^^^^^^^^^^^^^^
help: `serde` is an attribute that can be used by the derive macros `Deserialize` and `Serialize`, you might be missing a `derive` attribute
   |
LL + #[derive(Deserialize, Serialize)]
LL | enum B {
   |

Follow up to #134841. Fix #47608.

@rustbot
Copy link
Collaborator

rustbot commented Jun 14, 2025

r? @jieyouxu

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

@rustbot rustbot added 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. labels Jun 14, 2025
@rust-log-analyzer

This comment has been minimized.

@jieyouxu

This comment was marked as off-topic.

@rustbot

This comment was marked as off-topic.

@jieyouxu
Copy link
Member

r? compiler

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

@bors

This comment was marked as resolved.

@compiler-errors
Copy link
Member

This needs a perf run firstly I think

@bors try parent=855e0fe46e68d94e9f6147531b75ac2d488c548e @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 16, 2025
@compiler-errors
Copy link
Member

ugh, that will fail because bors1 doesn't know parents. I meant:

@bors2 try parent=855e0fe46e68d94e9f6147531b75ac2d488c548e @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Jul 16, 2025
Detect missing `derive` on unresolved attribute even when not imported

When encountering unresolved attributes, ensure the proc-macros for every crate in scope are added to the `macro_map` so that typos and missing `derive`s are properly detected.

```
error: cannot find attribute `sede` in this scope
  --> $DIR/missing-derive-3.rs:20:7
   |
LL |     #[sede(untagged)]
   |       ^^^^
   |
help: the derive macros `Deserialize` and `Serialize` accept the similarly named `serde` attribute
   |
LL |     #[serde(untagged)]
   |         +

error: cannot find attribute `serde` in this scope
  --> $DIR/missing-derive-3.rs:14:7
   |
LL |     #[serde(untagged)]
   |       ^^^^^
   |
note: `serde` is imported here, but it is a crate, not an attribute
  --> $DIR/missing-derive-3.rs:4:1
   |
LL | extern crate serde;
   | ^^^^^^^^^^^^^^^^^^^
help: `serde` is an attribute that can be used by the derive macros `Deserialize` and `Serialize`, you might be missing a `derive` attribute
   |
LL + #[derive(Deserialize, Serialize)]
LL | enum B {
   |
```

Follow up to #134841.
@rust-bors
Copy link

rust-bors bot commented Jul 16, 2025

⌛ Trying commit d617a5b with merge 07b2f21

To cancel the try build, run the command @bors2 try cancel.

@bors

This comment was marked as resolved.

@rust-bors
Copy link

rust-bors bot commented Jul 16, 2025

☀️ Try build successful (CI)
Build commit: 07b2f21 (07b2f218801fa85db469fc131c97d08b0a40e9b9, parent: 855e0fe46e68d94e9f6147531b75ac2d488c548e)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (07b2f21): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.4% [0.4%, 0.5%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary -1.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.3% [2.3%, 2.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-6.2% [-6.2%, -6.2%] 1
All ❌✅ (primary) - - 0

Cycles

Results (primary 2.3%, secondary -0.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.3% [2.1%, 2.4%] 2
Regressions ❌
(secondary)
2.3% [2.3%, 2.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.8% [-3.8%, -3.8%] 1
All ❌✅ (primary) 2.3% [2.1%, 2.4%] 2

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 464.604s -> 465.331s (0.16%)
Artifact size: 374.58 MiB -> 374.59 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 16, 2025
@bors

This comment was marked as resolved.

@bors

This comment was marked as resolved.

@estebank estebank force-pushed the serde-attr-5 branch 2 times, most recently from 0d0caca to 3d10f1b Compare August 10, 2025 19:12
@bors

This comment was marked as resolved.

@compiler-errors
Copy link
Member

r? petrochenkov maybe?

@bors
Copy link
Collaborator

bors commented Aug 15, 2025

☔ The latest upstream changes (presumably #145423) made this pull request unmergeable. Please resolve the merge conflicts.

data.macros
.decode(CrateMetadataRef { cdata: self, cstore })
.map(|index| DefId { index, krate })
.collect()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can return an iterator using gen { ... } instead of collecting, see examples in the same file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a neat trick to deal with the lifetimes of the iterator, I didn't know about it. Can't wait for stable generators :)

pub fn all_proc_macro_def_ids(&self) -> Vec<DefId> {
self.iter_crate_data()
.flat_map(|(krate, data)| data.proc_macros_for_crate(krate, self))
.collect()
Copy link
Contributor

Choose a reason for hiding this comment

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

Also can return an iterator.

/// find them for suggestions.
pub(crate) fn register_macros_for_all_crates(&mut self) {
if self.all_crate_macros_already_registered {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: if !things_done { do_things(); things_done = true; } would look slightly better.

@@ -1266,6 +1266,10 @@ pub struct Resolver<'ra, 'tcx> {

mods_with_parse_errors: FxHashSet<DefId>,

/// Whether `Resolver::register_macros_for_all_crates` has been called once already, as we
/// don't need to run it more than once.
all_crate_macros_already_registered: bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
all_crate_macros_already_registered: bool,
all_crate_macros_already_registered: bool = false,

@petrochenkov petrochenkov 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 Aug 15, 2025
```
error: cannot find attribute `sede` in this scope
  --> $DIR/missing-derive-3.rs:20:7
   |
LL |     #[sede(untagged)]
   |       ^^^^
   |
help: the derive macros `Deserialize` and `Serialize` accept the similarly named `serde` attribute
   |
LL |     #[serde(untagged)]
   |         +

error: cannot find attribute `serde` in this scope
  --> $DIR/missing-derive-3.rs:14:7
   |
LL |     #[serde(untagged)]
   |       ^^^^^
   |
note: `serde` is imported here, but it is a crate, not an attribute
  --> $DIR/missing-derive-3.rs:4:1
   |
LL | extern crate serde;
   | ^^^^^^^^^^^^^^^^^^^
help: `serde` is an attribute that can be used by the derive macros `Deserialize` and `Serialize`, you might be missing a `derive` attribute
   |
LL + #[derive(Deserialize, Serialize)]
LL | enum B {
   |
```
@estebank estebank 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 Aug 15, 2025
@estebank
Copy link
Contributor Author

Addressed and squashed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

Poor error message when user forgets derive that has attributes
8 participants