diff options
| author | est31 <MTest31@outlook.com> | 2022-06-09 04:46:51 +0200 |
|---|---|---|
| committer | est31 <MTest31@outlook.com> | 2022-06-09 23:21:06 +0200 |
| commit | eb3c611e1d3829e05aec8f9887efa164ea2c6da5 (patch) | |
| tree | ad548a5dc33d62ceb1dd89e273148bf49844873c /compiler | |
| parent | fee3a459dd6aba8e34a5b99f0fbcb4218a1e2401 (diff) | |
| download | rust-eb3c611e1d3829e05aec8f9887efa164ea2c6da5.tar.gz rust-eb3c611e1d3829e05aec8f9887efa164ea2c6da5.zip | |
Never regard macro rules with compile_error! invocations as unused
The very point of compile_error! is to never be reached, and one of the use cases of the macro, currently also listed as examples in the documentation of compile_error, is to create nicer errors for wrong macro invocations. Thus, we shuuld never warn about unused macro arms that contain invocations of compile_error.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_expand/src/lib.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_expand/src/mbe/macro_rules.rs | 36 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/build_reduced_graph.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/macros.rs | 2 |
4 files changed, 39 insertions, 6 deletions
diff --git a/compiler/rustc_expand/src/lib.rs b/compiler/rustc_expand/src/lib.rs index 7043ad54645..86ff110eec1 100644 --- a/compiler/rustc_expand/src/lib.rs +++ b/compiler/rustc_expand/src/lib.rs @@ -1,4 +1,5 @@ #![allow(rustc::potential_query_instability)] +#![feature(array_windows)] #![feature(associated_type_bounds)] #![feature(associated_type_defaults)] #![feature(if_let_guard)] diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs index b86304ba6b1..2a767646eaf 100644 --- a/compiler/rustc_expand/src/mbe/macro_rules.rs +++ b/compiler/rustc_expand/src/mbe/macro_rules.rs @@ -380,7 +380,7 @@ pub fn compile_declarative_macro( features: &Features, def: &ast::Item, edition: Edition, -) -> (SyntaxExtension, Vec<Span>) { +) -> (SyntaxExtension, Vec<(usize, Span)>) { debug!("compile_declarative_macro: {:?}", def); let mk_syn_ext = |expander| { SyntaxExtension::new( @@ -542,8 +542,17 @@ pub fn compile_declarative_macro( // Compute the spans of the macro rules // We only take the span of the lhs here, // so that the spans of created warnings are smaller. + // Also, we are only interested in non-foreign macros. let rule_spans = if def.id != DUMMY_NODE_ID { - lhses.iter().map(|lhs| lhs.span()).collect::<Vec<_>>() + lhses + .iter() + .zip(rhses.iter()) + .enumerate() + // If the rhs contains an invocation like compile_error!, + // don't consider the rule for the unused rule lint. + .filter(|(_idx, (_lhs, rhs))| !has_compile_error_macro(rhs)) + .map(|(idx, (lhs, _rhs))| (idx, lhs.span())) + .collect::<Vec<_>>() } else { Vec::new() }; @@ -651,6 +660,29 @@ fn check_matcher(sess: &ParseSess, def: &ast::Item, matcher: &[mbe::TokenTree]) err == sess.span_diagnostic.err_count() } +fn has_compile_error_macro(rhs: &mbe::TokenTree) -> bool { + match rhs { + mbe::TokenTree::Delimited(_sp, d) => { + let has_compile_error = d.tts.array_windows::<3>().any(|[ident, bang, args]| { + if let mbe::TokenTree::Token(ident) = ident && + let TokenKind::Ident(ident, _) = ident.kind && + ident == sym::compile_error && + let mbe::TokenTree::Token(bang) = bang && + let TokenKind::Not = bang.kind && + let mbe::TokenTree::Delimited(_, del) = args && + del.delim != Delimiter::Invisible + { + true + } else { + false + } + }); + if has_compile_error { true } else { d.tts.iter().any(has_compile_error_macro) } + } + _ => false, + } +} + // `The FirstSets` for a matcher is a mapping from subsequences in the // matcher to the FIRST set for that subsequence. // diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 20d9123e411..f8fa7a0941d 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -1220,12 +1220,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> { ident: Ident, def_id: LocalDefId, node_id: NodeId, - rule_spans: &[Span], + rule_spans: &[(usize, Span)], ) { if !ident.as_str().starts_with('_') { self.r.unused_macros.insert(def_id, (node_id, ident)); - for (rule_i, rule_span) in rule_spans.iter().enumerate() { - self.r.unused_macro_rules.insert((def_id, rule_i), (ident, *rule_span)); + for (rule_i, rule_span) in rule_spans.iter() { + self.r.unused_macro_rules.insert((def_id, *rule_i), (ident, *rule_span)); } } } diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 2e2d3674560..3fb34cdcd9b 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -870,7 +870,7 @@ impl<'a> Resolver<'a> { &mut self, item: &ast::Item, edition: Edition, - ) -> (SyntaxExtension, Vec<Span>) { + ) -> (SyntaxExtension, Vec<(usize, Span)>) { let (mut result, mut rule_spans) = compile_declarative_macro( &self.session, self.session.features_untracked(), |
