Skip to content

Commit 21d3189

Browse files
Move validate_attr to rustc_attr_parsing
1 parent 8e3710e commit 21d3189

File tree

21 files changed

+99
-85
lines changed

21 files changed

+99
-85
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,7 +3459,6 @@ dependencies = [
34593459
"rustc_feature",
34603460
"rustc_fluent_macro",
34613461
"rustc_macros",
3462-
"rustc_parse",
34633462
"rustc_session",
34643463
"rustc_span",
34653464
"rustc_target",
@@ -3490,6 +3489,7 @@ dependencies = [
34903489
"rustc_hir",
34913490
"rustc_lexer",
34923491
"rustc_macros",
3492+
"rustc_parse",
34933493
"rustc_session",
34943494
"rustc_span",
34953495
"thin-vec",
@@ -4355,7 +4355,6 @@ dependencies = [
43554355
"rustc-literal-escaper",
43564356
"rustc_ast",
43574357
"rustc_ast_pretty",
4358-
"rustc_attr_parsing",
43594358
"rustc_data_structures",
43604359
"rustc_errors",
43614360
"rustc_feature",

compiler/rustc_ast_passes/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ rustc_errors = { path = "../rustc_errors" }
1515
rustc_feature = { path = "../rustc_feature" }
1616
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1717
rustc_macros = { path = "../rustc_macros" }
18-
rustc_parse = { path = "../rustc_parse" }
1918
rustc_session = { path = "../rustc_session" }
2019
rustc_span = { path = "../rustc_span" }
2120
rustc_target = { path = "../rustc_target" }

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ use rustc_abi::{CanonAbi, ExternAbi, InterruptKind};
2525
use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, walk_list};
2626
use rustc_ast::*;
2727
use rustc_ast_pretty::pprust::{self, State};
28+
use rustc_attr_parsing::validate_attr;
2829
use rustc_data_structures::fx::FxIndexMap;
2930
use rustc_errors::DiagCtxtHandle;
3031
use rustc_feature::Features;
31-
use rustc_parse::validate_attr;
3232
use rustc_session::Session;
3333
use rustc_session::lint::builtin::{
3434
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, MISSING_UNSAFE_ON_EXTERN,

compiler/rustc_attr_parsing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1414
rustc_hir = { path = "../rustc_hir" }
1515
rustc_lexer = { path = "../rustc_lexer" }
1616
rustc_macros = { path = "../rustc_macros" }
17+
rustc_parse = { path = "../rustc_parse" }
1718
rustc_session = { path = "../rustc_session" }
1819
rustc_span = { path = "../rustc_span" }
1920
thin-vec = "0.2.12"

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,15 @@ attr_parsing_unused_multiple =
170170
171171
-attr_parsing_previously_accepted =
172172
this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
173+
174+
attr_parsing_meta_bad_delim = wrong meta list delimiters
175+
attr_parsing_meta_bad_delim_suggestion = the delimiters should be `(` and `)`
176+
177+
attr_parsing_unsafe_attr_outside_unsafe = unsafe attribute used without unsafe
178+
.label = usage of unsafe attribute
179+
attr_parsing_unsafe_attr_outside_unsafe_suggestion = wrap the attribute in `unsafe(...)`
180+
181+
attr_parsing_invalid_attr_unsafe = `{$name}` is not an unsafe attribute
182+
.label = this is not an unsafe attribute
183+
.suggestion = remove the `unsafe(...)`
184+
.note = extraneous unsafe is not allowed in attributes

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub mod parser;
102102
mod lints;
103103
mod session_diagnostics;
104104
mod target_checking;
105+
pub mod validate_attr;
105106

106107
pub use attributes::cfg::{CFG_TEMPLATE, EvalConfigResult, eval_config_entry, parse_cfg_attr};
107108
pub use attributes::cfg_old::*;

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::num::IntErrorKind;
22

3-
use rustc_ast::{self as ast, AttrStyle};
3+
use rustc_ast::{self as ast, AttrStyle, Path};
44
use rustc_errors::codes::*;
55
use rustc_errors::{
66
Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level,
@@ -737,3 +737,56 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
737737
diag
738738
}
739739
}
740+
741+
#[derive(Diagnostic)]
742+
#[diag(attr_parsing_invalid_attr_unsafe)]
743+
#[note]
744+
pub(crate) struct InvalidAttrUnsafe {
745+
#[primary_span]
746+
#[label]
747+
pub span: Span,
748+
pub name: Path,
749+
}
750+
751+
#[derive(Diagnostic)]
752+
#[diag(attr_parsing_unsafe_attr_outside_unsafe)]
753+
pub(crate) struct UnsafeAttrOutsideUnsafe {
754+
#[primary_span]
755+
#[label]
756+
pub span: Span,
757+
#[subdiagnostic]
758+
pub suggestion: UnsafeAttrOutsideUnsafeSuggestion,
759+
}
760+
761+
#[derive(Subdiagnostic)]
762+
#[multipart_suggestion(
763+
attr_parsing_unsafe_attr_outside_unsafe_suggestion,
764+
applicability = "machine-applicable"
765+
)]
766+
pub(crate) struct UnsafeAttrOutsideUnsafeSuggestion {
767+
#[suggestion_part(code = "unsafe(")]
768+
pub left: Span,
769+
#[suggestion_part(code = ")")]
770+
pub right: Span,
771+
}
772+
773+
#[derive(Diagnostic)]
774+
#[diag(attr_parsing_meta_bad_delim)]
775+
pub(crate) struct MetaBadDelim {
776+
#[primary_span]
777+
pub span: Span,
778+
#[subdiagnostic]
779+
pub sugg: MetaBadDelimSugg,
780+
}
781+
782+
#[derive(Subdiagnostic)]
783+
#[multipart_suggestion(
784+
attr_parsing_meta_bad_delim_suggestion,
785+
applicability = "machine-applicable"
786+
)]
787+
pub(crate) struct MetaBadDelimSugg {
788+
#[suggestion_part(code = "(")]
789+
pub open: Span,
790+
#[suggestion_part(code = ")")]
791+
pub close: Span,
792+
}

compiler/rustc_parse/src/validate_attr.rs renamed to compiler/rustc_attr_parsing/src/validate_attr.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ use rustc_ast::{
88
self as ast, AttrArgs, Attribute, DelimArgs, MetaItem, MetaItemInner, MetaItemKind, NodeId,
99
Path, Safety,
1010
};
11-
use rustc_attr_parsing::{AttributeParser, Late};
1211
use rustc_errors::{Applicability, DiagCtxtHandle, FatalError, PResult};
1312
use rustc_feature::{AttributeSafety, AttributeTemplate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
13+
use rustc_parse::parse_in;
1414
use rustc_session::errors::report_lit_error;
1515
use rustc_session::lint::BuiltinLintDiag;
1616
use rustc_session::lint::builtin::{ILL_FORMED_ATTRIBUTE_INPUT, UNSAFE_ATTR_OUTSIDE_UNSAFE};
1717
use rustc_session::parse::ParseSess;
1818
use rustc_span::{Span, Symbol, sym};
1919

20-
use crate::{errors, parse_in};
20+
use crate::{AttributeParser, Late, session_diagnostics as errors};
2121

2222
pub fn check_attr(psess: &ParseSess, attr: &Attribute, id: NodeId) {
2323
if attr.is_doc_comment() || attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace)
@@ -133,16 +133,6 @@ fn check_meta_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) {
133133
});
134134
}
135135

136-
pub(super) fn check_cfg_attr_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) {
137-
if let Delimiter::Parenthesis = delim {
138-
return;
139-
}
140-
psess.dcx().emit_err(errors::CfgAttrBadDelim {
141-
span: span.entire(),
142-
sugg: errors::MetaBadDelimSugg { open: span.open, close: span.close },
143-
});
144-
}
145-
146136
/// Checks that the given meta-item is compatible with this `AttributeTemplate`.
147137
fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
148138
let is_one_allowed_subword = |items: &[MetaItemInner]| match items {

compiler/rustc_builtin_macros/src/cfg_accessible.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Implementation of the `#[cfg_accessible(path)]` attribute macro.
22
33
use rustc_ast as ast;
4+
use rustc_attr_parsing::validate_attr;
45
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
56
use rustc_feature::AttributeTemplate;
6-
use rustc_parse::validate_attr;
77
use rustc_span::{Span, sym};
88

99
use crate::errors;

compiler/rustc_builtin_macros/src/derive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use rustc_ast as ast;
22
use rustc_ast::{GenericParamKind, ItemKind, MetaItemInner, MetaItemKind, StmtKind};
3+
use rustc_attr_parsing::validate_attr;
34
use rustc_expand::base::{
45
Annotatable, DeriveResolution, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier,
56
};
67
use rustc_feature::AttributeTemplate;
7-
use rustc_parse::validate_attr;
88
use rustc_session::Session;
99
use rustc_span::{ErrorGuaranteed, Ident, Span, sym};
1010

0 commit comments

Comments
 (0)