Skip to content

Commit 9a2955b

Browse files
authored
Unrolled build for rust-lang#140617
Rollup merge of rust-lang#140617 - Urgau:unsafe_attr-lint-allow, r=jieyouxu Report the `unsafe_attr_outside_unsafe` lint at the closest node This PR have `AstValidation` track a linting node id and then uses it when reporting the `unsafe_attr_outside_unsafe` lint, so that instead of being bound at the crate-root, `#[allow]` of the lint works at any node. Fixes rust-lang#140602 r? `@jieyouxu`
2 parents 1bea580 + f4e1ec1 commit 9a2955b

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct AstValidator<'a> {
8282
/// Used to ban explicit safety on foreign items when the extern block is not marked as unsafe.
8383
extern_mod_safety: Option<Safety>,
8484

85+
lint_node_id: NodeId,
86+
8587
lint_buffer: &'a mut LintBuffer,
8688
}
8789

@@ -826,7 +828,7 @@ fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericPara
826828

827829
impl<'a> Visitor<'a> for AstValidator<'a> {
828830
fn visit_attribute(&mut self, attr: &Attribute) {
829-
validate_attr::check_attr(&self.sess.psess, attr);
831+
validate_attr::check_attr(&self.sess.psess, attr, self.lint_node_id);
830832
}
831833

832834
fn visit_ty(&mut self, ty: &'a Ty) {
@@ -839,6 +841,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
839841
self.has_proc_macro_decls = true;
840842
}
841843

844+
let previous_lint_node_id = mem::replace(&mut self.lint_node_id, item.id);
845+
842846
if let Some(ident) = item.kind.ident()
843847
&& attr::contains_name(&item.attrs, sym::no_mangle)
844848
{
@@ -1128,6 +1132,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11281132
}
11291133
_ => visit::walk_item(self, item),
11301134
}
1135+
1136+
self.lint_node_id = previous_lint_node_id;
11311137
}
11321138

11331139
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
@@ -1694,6 +1700,7 @@ pub fn check_crate(
16941700
outer_impl_trait_span: None,
16951701
disallow_tilde_const: Some(TildeConstReason::Item),
16961702
extern_mod_safety: None,
1703+
lint_node_id: CRATE_NODE_ID,
16971704
lint_buffer: lints,
16981705
};
16991706
visit::walk_crate(&mut validator, krate);

compiler/rustc_expand/src/config.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,12 @@ impl<'a> StripUnconfigured<'a> {
274274
/// is in the original source file. Gives a compiler error if the syntax of
275275
/// the attribute is incorrect.
276276
pub(crate) fn expand_cfg_attr(&self, cfg_attr: &Attribute, recursive: bool) -> Vec<Attribute> {
277-
validate_attr::check_attribute_safety(&self.sess.psess, AttributeSafety::Normal, &cfg_attr);
277+
validate_attr::check_attribute_safety(
278+
&self.sess.psess,
279+
AttributeSafety::Normal,
280+
&cfg_attr,
281+
ast::CRATE_NODE_ID,
282+
);
278283

279284
// A trace attribute left in AST in place of the original `cfg_attr` attribute.
280285
// It can later be used by lints or other diagnostics.

compiler/rustc_expand/src/expand.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,11 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
19831983
let mut span: Option<Span> = None;
19841984
while let Some(attr) = attrs.next() {
19851985
rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
1986-
validate_attr::check_attr(&self.cx.sess.psess, attr);
1986+
validate_attr::check_attr(
1987+
&self.cx.sess.psess,
1988+
attr,
1989+
self.cx.current_expansion.lint_node_id,
1990+
);
19871991

19881992
let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
19891993
span = Some(current_span);

compiler/rustc_parse/src/validate_attr.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use rustc_ast::token::Delimiter;
44
use rustc_ast::tokenstream::DelimSpan;
55
use rustc_ast::{
6-
self as ast, AttrArgs, Attribute, DelimArgs, MetaItem, MetaItemInner, MetaItemKind, Safety,
6+
self as ast, AttrArgs, Attribute, DelimArgs, MetaItem, MetaItemInner, MetaItemKind, NodeId,
7+
Safety,
78
};
89
use rustc_errors::{Applicability, FatalError, PResult};
910
use rustc_feature::{AttributeSafety, AttributeTemplate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
@@ -15,7 +16,7 @@ use rustc_span::{Span, Symbol, sym};
1516

1617
use crate::{errors, parse_in};
1718

18-
pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
19+
pub fn check_attr(psess: &ParseSess, attr: &Attribute, id: NodeId) {
1920
if attr.is_doc_comment() || attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace)
2021
{
2122
return;
@@ -26,7 +27,7 @@ pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
2627

2728
// All non-builtin attributes are considered safe
2829
let safety = attr_info.map(|x| x.safety).unwrap_or(AttributeSafety::Normal);
29-
check_attribute_safety(psess, safety, attr);
30+
check_attribute_safety(psess, safety, attr, id);
3031

3132
// Check input tokens for built-in and key-value attributes.
3233
match attr_info {
@@ -154,7 +155,12 @@ fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaIte
154155
}
155156
}
156157

157-
pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr: &Attribute) {
158+
pub fn check_attribute_safety(
159+
psess: &ParseSess,
160+
safety: AttributeSafety,
161+
attr: &Attribute,
162+
id: NodeId,
163+
) {
158164
let attr_item = attr.get_normal_item();
159165

160166
if let AttributeSafety::Unsafe { unsafe_since } = safety {
@@ -185,7 +191,7 @@ pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr:
185191
psess.buffer_lint(
186192
UNSAFE_ATTR_OUTSIDE_UNSAFE,
187193
path_span,
188-
ast::CRATE_NODE_ID,
194+
id,
189195
BuiltinLintDiag::UnsafeAttrOutsideUnsafe {
190196
attribute_name_span: path_span,
191197
sugg_spans: (diag_span.shrink_to_lo(), diag_span.shrink_to_hi()),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ check-pass
2+
//@ edition: 2021
3+
//
4+
// Anti-regression test for https://github.com/rust-lang/rust/issues/140602
5+
// where the generated warning couldn't be allowed due too being attached to
6+
// the wrong AST node.
7+
8+
#![deny(unsafe_attr_outside_unsafe)]
9+
10+
#[allow(unsafe_attr_outside_unsafe)]
11+
mod generated {
12+
#[no_mangle]
13+
fn _generated_foo() {}
14+
}
15+
16+
fn main() {}

0 commit comments

Comments
 (0)