Skip to content

Commit 2d0a3dc

Browse files
committed
Add feature flags to lint checks
1 parent 9d329c0 commit 2d0a3dc

File tree

5 files changed

+49
-43
lines changed

5 files changed

+49
-43
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ fn validate_generic_param_order(
870870

871871
impl<'a> Visitor<'a> for AstValidator<'a> {
872872
fn visit_attribute(&mut self, attr: &Attribute) {
873-
validate_attr::check_attr(&self.session.psess, attr);
873+
validate_attr::check_attr(&self.features, &self.session.psess, attr);
874874
}
875875

876876
fn visit_ty(&mut self, ty: &'a Ty) {

compiler/rustc_expand/src/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1796,7 +1796,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
17961796
let mut span: Option<Span> = None;
17971797
while let Some(attr) = attrs.next() {
17981798
rustc_ast_passes::feature_gate::check_attribute(attr, self.cx.sess, features);
1799-
validate_attr::check_attr(&self.cx.sess.psess, attr);
1799+
validate_attr::check_attr(features, &self.cx.sess.psess, attr);
18001800

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

compiler/rustc_parse/src/validate_attr.rs

+45-40
Original file line numberDiff line numberDiff line change
@@ -7,65 +7,70 @@ use rustc_ast::tokenstream::DelimSpan;
77
use rustc_ast::MetaItemKind;
88
use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, Attribute, DelimArgs, MetaItem, Safety};
99
use rustc_errors::{Applicability, FatalError, PResult};
10-
use rustc_feature::{AttributeSafety, AttributeTemplate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
10+
use rustc_feature::{
11+
AttributeSafety, AttributeTemplate, BuiltinAttribute, Features, BUILTIN_ATTRIBUTE_MAP,
12+
};
1113
use rustc_session::errors::report_lit_error;
1214
use rustc_session::lint::builtin::{ILL_FORMED_ATTRIBUTE_INPUT, UNSAFE_ATTR_OUTSIDE_UNSAFE};
1315
use rustc_session::lint::BuiltinLintDiag;
1416
use rustc_session::parse::ParseSess;
1517
use rustc_span::{sym, BytePos, Span, Symbol};
1618

17-
pub fn check_attr(psess: &ParseSess, attr: &Attribute) {
19+
pub fn check_attr(features: &Features, psess: &ParseSess, attr: &Attribute) {
1820
if attr.is_doc_comment() {
1921
return;
2022
}
2123

2224
let attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
2325
let attr_item = attr.get_normal_item();
2426

25-
let is_unsafe_attr =
26-
attr_info.map(|attr| attr.safety == AttributeSafety::Unsafe).unwrap_or(false);
27+
let is_unsafe_attr = attr_info.is_some_and(|attr| attr.safety == AttributeSafety::Unsafe);
2728

28-
if is_unsafe_attr {
29-
if let ast::Safety::Default = attr_item.unsafety {
30-
let path_span = attr_item.path.span;
29+
if features.unsafe_attributes {
30+
if is_unsafe_attr {
31+
if let ast::Safety::Default = attr_item.unsafety {
32+
let path_span = attr_item.path.span;
3133

32-
// If the `attr_item`'s span is not from a macro, then just suggest
33-
// wrapping it in `unsafe(...)`. Otherwise, we suggest putting the
34-
// `unsafe(`, `)` right after and right before the opening and closing
35-
// square bracket respectively.
36-
let diag_span = if attr_item.span().can_be_used_for_suggestions() {
37-
attr_item.span()
38-
} else {
39-
attr.span.with_lo(attr.span.lo() + BytePos(2)).with_hi(attr.span.hi() - BytePos(1))
40-
};
34+
// If the `attr_item`'s span is not from a macro, then just suggest
35+
// wrapping it in `unsafe(...)`. Otherwise, we suggest putting the
36+
// `unsafe(`, `)` right after and right before the opening and closing
37+
// square bracket respectively.
38+
let diag_span = if attr_item.span().can_be_used_for_suggestions() {
39+
attr_item.span()
40+
} else {
41+
attr.span
42+
.with_lo(attr.span.lo() + BytePos(2))
43+
.with_hi(attr.span.hi() - BytePos(1))
44+
};
4145

42-
if attr.span.at_least_rust_2024() {
43-
psess.dcx.emit_err(errors::UnsafeAttrOutsideUnsafe {
44-
span: path_span,
45-
suggestion: errors::UnsafeAttrOutsideUnsafeSuggestion {
46-
left: diag_span.shrink_to_lo(),
47-
right: diag_span.shrink_to_hi(),
48-
},
46+
if attr.span.at_least_rust_2024() {
47+
psess.dcx.emit_err(errors::UnsafeAttrOutsideUnsafe {
48+
span: path_span,
49+
suggestion: errors::UnsafeAttrOutsideUnsafeSuggestion {
50+
left: diag_span.shrink_to_lo(),
51+
right: diag_span.shrink_to_hi(),
52+
},
53+
});
54+
} else {
55+
psess.buffer_lint(
56+
UNSAFE_ATTR_OUTSIDE_UNSAFE,
57+
path_span,
58+
ast::CRATE_NODE_ID,
59+
BuiltinLintDiag::UnsafeAttrOutsideUnsafe {
60+
attribute_name_span: path_span,
61+
sugg_spans: (diag_span.shrink_to_lo(), diag_span.shrink_to_hi()),
62+
},
63+
);
64+
}
65+
}
66+
} else {
67+
if let Safety::Unsafe(unsafe_span) = attr_item.unsafety {
68+
psess.dcx.emit_err(errors::InvalidAttrUnsafe {
69+
span: unsafe_span,
70+
name: attr_item.path.clone(),
4971
});
50-
} else {
51-
psess.buffer_lint(
52-
UNSAFE_ATTR_OUTSIDE_UNSAFE,
53-
path_span,
54-
ast::CRATE_NODE_ID,
55-
BuiltinLintDiag::UnsafeAttrOutsideUnsafe {
56-
attribute_name_span: path_span,
57-
sugg_spans: (diag_span.shrink_to_lo(), diag_span.shrink_to_hi()),
58-
},
59-
);
6072
}
6173
}
62-
} else {
63-
if let Safety::Unsafe(unsafe_span) = attr_item.unsafety {
64-
psess.dcx.emit_err(errors::InvalidAttrUnsafe {
65-
span: unsafe_span,
66-
name: attr_item.path.clone(),
67-
});
68-
}
6974
}
7075

7176
// Check input tokens for built-in and key-value attributes.

tests/ui/rust-2024/unsafe-attributes/in_2024_compatibility.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![deny(rust_2024_compatibility)]
2+
#![feature(unsafe_attributes)]
23

34
#[no_mangle]
45
//~^ ERROR: unsafe attribute used without unsafe

tests/ui/rust-2024/unsafe-attributes/in_2024_compatibility.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unsafe attribute used without unsafe
2-
--> $DIR/in_2024_compatibility.rs:3:3
2+
--> $DIR/in_2024_compatibility.rs:4:3
33
|
44
LL | #[no_mangle]
55
| ^^^^^^^^^ usage of unsafe attribute

0 commit comments

Comments
 (0)