Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9cd8b2d

Browse files
committed
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

Lines changed: 40 additions & 7 deletions
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

Lines changed: 8 additions & 2 deletions
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),

‎compiler/rustc_builtin_macros/messages.ftl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,13 @@ builtin_macros_proc_macro = `proc-macro` crate types currently cannot export any
272272
273273
builtin_macros_proc_macro_attribute_only_be_used_on_bare_functions = the `#[{$path}]` attribute may only be used on bare functions
274274
275+
builtin_macros_proc_macro_attribute_only_be_used_on_statics = the `#[{$path}]` attribute may only be used on a `static`
276+
275277
builtin_macros_proc_macro_attribute_only_usable_with_crate_type = the `#[{$path}]` attribute is only usable with crates of the `proc-macro` crate type
276278
279+
builtin_macros_proc_macro_lint_id_is_filled_in_by_attribute = a unique LintId value is automatically filled in by `#[proc_macro_warning]`
280+
.suggestion = remove this value
281+
277282
builtin_macros_requires_cfg_pattern =
278283
macro requires a cfg-pattern as an argument
279284
.label = cfg-pattern required

‎compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,22 @@ pub(crate) struct AttributeOnlyBeUsedOnBareFunctions<'a> {
993993
pub path: &'a str,
994994
}
995995

996+
#[derive(Diagnostic)]
997+
#[diag(builtin_macros_proc_macro_attribute_only_be_used_on_statics)]
998+
pub(crate) struct AttributeOnlyBeUsedOnStatics<'a> {
999+
#[primary_span]
1000+
pub span: Span,
1001+
pub path: &'a str,
1002+
}
1003+
1004+
#[derive(Diagnostic)]
1005+
#[diag(builtin_macros_proc_macro_lint_id_is_filled_in_by_attribute)]
1006+
pub(crate) struct LintIdIsFilledInByAttribute {
1007+
#[primary_span]
1008+
#[suggestion(code = "", applicability = "machine-applicable")]
1009+
pub span: Span,
1010+
}
1011+
9961012
#[derive(Diagnostic)]
9971013
#[diag(builtin_macros_proc_macro_attribute_only_usable_with_crate_type)]
9981014
pub(crate) struct AttributeOnlyUsableWithCrateType<'a> {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /