Skip to content

Commit 967ff9b

Browse files
committed
Detect missing derive on unresolved attribute even when not imported
``` 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 { | ```
1 parent 321a89b commit 967ff9b

File tree

7 files changed

+56
-26
lines changed

7 files changed

+56
-26
lines changed

compiler/rustc_metadata/src/creader.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate, ExternCrateS
3232
use rustc_session::lint::{self, BuiltinLintDiag};
3333
use rustc_session::output::validate_crate_name;
3434
use rustc_session::search_paths::PathKind;
35+
use rustc_span::def_id::DefId;
3536
use rustc_span::edition::Edition;
3637
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
3738
use rustc_target::spec::{PanicStrategy, Target};
@@ -275,6 +276,12 @@ impl CStore {
275276
.filter_map(|(cnum, data)| data.as_deref().map(|data| (cnum, data)))
276277
}
277278

279+
pub fn all_proc_macro_def_ids(&self) -> Vec<DefId> {
280+
self.iter_crate_data()
281+
.flat_map(|(krate, data)| data.proc_macros_for_crate(krate, self))
282+
.collect()
283+
}
284+
278285
fn push_dependencies_in_postorder(&self, deps: &mut IndexSet<CrateNum>, cnum: CrateNum) {
279286
if !deps.contains(&cnum) {
280287
let data = self.get_crate_data(cnum);

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,6 +2014,16 @@ impl CrateMetadata {
20142014
self.root.is_proc_macro_crate()
20152015
}
20162016

2017+
pub(crate) fn proc_macros_for_crate(&self, krate: CrateNum, cstore: &CStore) -> Vec<DefId> {
2018+
let Some(data) = self.root.proc_macro_data.as_ref() else {
2019+
return vec![];
2020+
};
2021+
data.macros
2022+
.decode(CrateMetadataRef { cdata: self, cstore })
2023+
.map(|index| DefId { index, krate })
2024+
.collect()
2025+
}
2026+
20172027
pub(crate) fn name(&self) -> Symbol {
20182028
self.root.header.name
20192029
}

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
210210
}
211211
}
212212

213+
/// Add every proc macro accessible from the current crate to the `macro_map` so diagnostics can
214+
/// find them for suggestions.
215+
pub(crate) fn register_macros_for_all_crates(&mut self) {
216+
let def_ids = self.cstore().all_proc_macro_def_ids();
217+
for def_id in def_ids {
218+
self.get_macro_by_def_id(def_id);
219+
}
220+
}
221+
213222
pub(crate) fn build_reduced_graph(
214223
&mut self,
215224
fragment: &AstFragment,

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,32 +1480,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14801480
krate: &Crate,
14811481
sugg_span: Option<Span>,
14821482
) {
1483-
// Bring imported but unused `derive` macros into `macro_map` so we ensure they can be used
1484-
// for suggestions.
1485-
self.visit_scopes(
1486-
ScopeSet::Macro(MacroKind::Derive),
1487-
&parent_scope,
1488-
ident.span.ctxt(),
1489-
|this, scope, _use_prelude, _ctxt| {
1490-
let Scope::Module(m, _) = scope else {
1491-
return None;
1492-
};
1493-
for (_, resolution) in this.resolutions(m).borrow().iter() {
1494-
let Some(binding) = resolution.borrow().best_binding() else {
1495-
continue;
1496-
};
1497-
let Res::Def(DefKind::Macro(MacroKind::Derive | MacroKind::Attr), def_id) =
1498-
binding.res()
1499-
else {
1500-
continue;
1501-
};
1502-
// By doing this all *imported* macros get added to the `macro_map` even if they
1503-
// are *unused*, which makes the later suggestions find them and work.
1504-
let _ = this.get_macro_by_def_id(def_id);
1505-
}
1506-
None::<()>
1507-
},
1508-
);
1483+
// Bring all unused `derive` macros into `macro_map` so we ensure they can be used for
1484+
// suggestions.
1485+
self.register_macros_for_all_crates();
15091486

15101487
let is_expected = &|res: Res| res.macro_kind() == Some(macro_kind);
15111488
let suggestion = self.early_lookup_typo_candidate(

tests/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,12 @@ error: cannot find attribute `error` in this scope
559559
|
560560
LL | #[error(no_crate_example, code = E0123)]
561561
| ^^^^^
562+
|
563+
help: `error` is an attribute that can be used by the derive macro `Error`, you might be missing a `derive` attribute
564+
|
565+
LL + #[derive(Error)]
566+
LL | struct ErrorAttribute {}
567+
|
562568

563569
error: cannot find attribute `warn_` in this scope
564570
--> $DIR/diagnostic-derive.rs:590:3

tests/ui/macros/missing-derive-3.stderr

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error: cannot find attribute `sede` in this scope
33
|
44
LL | #[sede(untagged)]
55
| ^^^^
6+
|
7+
help: the derive macros `Deserialize` and `Serialize` accept the similarly named `serde` attribute
8+
|
9+
LL | #[serde(untagged)]
10+
| +
611

712
error: cannot find attribute `serde` in this scope
813
--> $DIR/missing-derive-3.rs:14:7
@@ -15,6 +20,11 @@ note: `serde` is imported here, but it is a crate, not an attribute
1520
|
1621
LL | extern crate serde;
1722
| ^^^^^^^^^^^^^^^^^^^
23+
help: `serde` is an attribute that can be used by the derive macros `Deserialize` and `Serialize`, you might be missing a `derive` attribute
24+
|
25+
LL + #[derive(Deserialize, Serialize)]
26+
LL | enum B {
27+
|
1828

1929
error: cannot find attribute `serde` in this scope
2030
--> $DIR/missing-derive-3.rs:6:3
@@ -27,6 +37,11 @@ note: `serde` is imported here, but it is a crate, not an attribute
2737
|
2838
LL | extern crate serde;
2939
| ^^^^^^^^^^^^^^^^^^^
40+
help: `serde` is an attribute that can be used by the derive macros `Deserialize` and `Serialize`, you might be missing a `derive` attribute
41+
|
42+
LL + #[derive(Deserialize, Serialize)]
43+
LL | enum A {
44+
|
3045

3146
error: aborting due to 3 previous errors
3247

tests/ui/proc-macro/derive-helper-legacy-spurious.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ error: cannot find attribute `empty_helper` in this scope
99
|
1010
LL | #[empty_helper]
1111
| ^^^^^^^^^^^^
12+
|
13+
help: `empty_helper` is an attribute that can be used by the derive macro `Empty`, you might be missing a `derive` attribute
14+
|
15+
LL + #[derive(Empty)]
16+
LL | struct Foo {}
17+
|
1218

1319
error: aborting due to 2 previous errors
1420

0 commit comments

Comments
 (0)