From 274d813cff34576d3fe9d343e73669385214d3c3 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 19 May 2021 21:05:58 +0200 Subject: Fix derive collection after unresolved attribute fallback --- crates/hir_def/src/nameres/collector.rs | 6 +++++- crates/hir_def/src/nameres/tests/macros.rs | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 221a5a556..54e9c8d20 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -367,6 +367,8 @@ impl DefCollector<'_> { /// This improves UX when proc macros are turned off or don't work, and replicates the behavior /// before we supported proc. attribute macros. fn reseed_with_unresolved_attributes(&mut self) -> ReachedFixedPoint { + cov_mark::hit!(unresolved_attribute_fallback); + let mut added_items = false; let unexpanded_macros = std::mem::replace(&mut self.unexpanded_macros, Vec::new()); for directive in &unexpanded_macros { @@ -391,7 +393,9 @@ impl DefCollector<'_> { added_items = true; } } - self.unexpanded_macros = unexpanded_macros; + + // The collection above might add new unresolved macros (eg. derives), so merge the lists. + self.unexpanded_macros.extend(unexpanded_macros); if added_items { // Continue name resolution with the new data. diff --git a/crates/hir_def/src/nameres/tests/macros.rs b/crates/hir_def/src/nameres/tests/macros.rs index c37f915ab..b34ba885d 100644 --- a/crates/hir_def/src/nameres/tests/macros.rs +++ b/crates/hir_def/src/nameres/tests/macros.rs @@ -685,6 +685,27 @@ pub trait Clone {} ); } +#[test] +fn builtin_derive_with_unresolved_attributes_fall_back() { + // Tests that we still resolve derives after ignoring an unresolved attribute. + cov_mark::check!(unresolved_attribute_fallback); + let map = compute_crate_def_map( + r#" + //- /main.rs crate:main deps:core + use core::Clone; + + #[derive(Clone)] + #[unresolved] + struct Foo; + + //- /core.rs crate:core + #[rustc_builtin_macro] + pub macro Clone {} + "#, + ); + assert_eq!(map.modules[map.root].scope.impls().len(), 1); +} + #[test] fn macro_expansion_overflow() { cov_mark::check!(macro_expansion_overflow); @@ -842,7 +863,6 @@ fn collects_derive_helpers() { fn resolve_macro_def() { check( r#" -//- /lib.rs pub macro structs($($i:ident),*) { $(struct $i { field: u32 } )* } -- cgit v1.2.3