Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9cd8b2d

Browse files
committedJan 13, 2025··
Implement #[proc_macro_warning] to generate LintId for macro-generated warnings
1 parent a2016aa commit 9cd8b2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+736
-121
lines changed
 

‎compiler/rustc_ast/src/attr/mod.rs

+40-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Functions dealing with attributes and meta items.
22
3-
use std::fmt::Debug;
3+
use std::fmt::{self, Debug, Display};
44
use std::sync::atomic::{AtomicU32, Ordering};
55

66
use rustc_index::bit_set::GrowableBitSet;
@@ -720,6 +720,31 @@ impl MetaItemLit {
720720
}
721721
}
722722

723+
/// An attribute relevant to the expansion of the proc macro harness performed
724+
/// by `rustc_builtin_macros`.
725+
#[derive(PartialEq, Debug)]
726+
pub enum ProcMacroAttr {
727+
/// `#[proc_macro_derive]`
728+
Derive,
729+
/// `#[proc_macro_attribute]`
730+
Attribute,
731+
/// `#[proc_macro]`
732+
Bang,
733+
/// `#[proc_macro_warning]`
734+
Warning,
735+
}
736+
737+
impl Display for ProcMacroAttr {
738+
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
739+
formatter.write_str(match self {
740+
ProcMacroAttr::Derive => "proc_macro_derive",
741+
ProcMacroAttr::Attribute => "proc_macro_attribute",
742+
ProcMacroAttr::Bang => "proc_macro",
743+
ProcMacroAttr::Warning => "proc_macro_warning",
744+
})
745+
}
746+
}
747+
723748
pub trait AttributeExt: Debug {
724749
fn id(&self) -> AttrId;
725750

@@ -774,10 +799,18 @@ pub trait AttributeExt: Debug {
774799
/// * `#[doc(...)]` returns `None`.
775800
fn doc_str(&self) -> Option<Symbol>;
776801

777-
fn is_proc_macro_attr(&self) -> bool {
778-
[sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive]
779-
.iter()
780-
.any(|kind| self.has_name(*kind))
802+
fn proc_macro_attr(&self) -> Option<ProcMacroAttr> {
803+
if self.has_name(sym::proc_macro_derive) {
804+
Some(ProcMacroAttr::Derive)
805+
} else if self.has_name(sym::proc_macro_attribute) {
806+
Some(ProcMacroAttr::Attribute)
807+
} else if self.has_name(sym::proc_macro) {
808+
Some(ProcMacroAttr::Bang)
809+
} else if self.has_name(sym::proc_macro_warning) {
810+
Some(ProcMacroAttr::Warning)
811+
} else {
812+
None
813+
}
781814
}
782815

783816
/// Returns the documentation and its kind if this is a doc comment or a sugared doc comment.
@@ -850,8 +883,8 @@ impl Attribute {
850883
AttributeExt::doc_str(self)
851884
}
852885

853-
pub fn is_proc_macro_attr(&self) -> bool {
854-
AttributeExt::is_proc_macro_attr(self)
886+
pub fn proc_macro_attr(&self) -> Option<ProcMacroAttr> {
887+
AttributeExt::proc_macro_attr(self)
855888
}
856889

857890
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {

‎compiler/rustc_ast_passes/src/ast_validation.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::mem;
2020
use std::ops::{Deref, DerefMut};
2121

2222
use itertools::{Either, Itertools};
23+
use rustc_ast::attr::ProcMacroAttr;
2324
use rustc_ast::ptr::P;
2425
use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, walk_list};
2526
use rustc_ast::*;
@@ -813,7 +814,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
813814
}
814815

815816
fn visit_item(&mut self, item: &'a Item) {
816-
if item.attrs.iter().any(|attr| attr.is_proc_macro_attr()) {
817+
if item.attrs.iter().any(|attr| attr.proc_macro_attr().is_some()) {
817818
self.has_proc_macro_decls = true;
818819
}
819820

@@ -1080,7 +1081,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10801081
self.dcx().emit_err(errors::UnsafeStatic { span: item.span });
10811082
}
10821083

1083-
if expr.is_none() {
1084+
if expr.is_none()
1085+
&& !item
1086+
.attrs
1087+
.iter()
1088+
.any(|attr| attr.proc_macro_attr() == Some(ProcMacroAttr::Warning))
1089+
{
10841090
self.dcx().emit_err(errors::StaticWithoutBody {
10851091
span: item.span,
10861092
replace_span: self.ending_semi_or_hi(item.span),

0 commit comments

Comments
 (0)
Please sign in to comment.