Skip to content

Commit 17c5895

Browse files
Move validate_attr to rustc_attr_parsing
1 parent 6d091b2 commit 17c5895

File tree

21 files changed

+99
-84
lines changed

21 files changed

+99
-84
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,7 +3458,6 @@ dependencies = [
34583458
"rustc_feature",
34593459
"rustc_fluent_macro",
34603460
"rustc_macros",
3461-
"rustc_parse",
34623461
"rustc_session",
34633462
"rustc_span",
34643463
"rustc_target",
@@ -3489,6 +3488,7 @@ dependencies = [
34893488
"rustc_hir",
34903489
"rustc_lexer",
34913490
"rustc_macros",
3491+
"rustc_parse",
34923492
"rustc_session",
34933493
"rustc_span",
34943494
"thin-vec",
@@ -4344,7 +4344,6 @@ dependencies = [
43444344
"rustc-literal-escaper",
43454345
"rustc_ast",
43464346
"rustc_ast_pretty",
4347-
"rustc_attr_parsing",
43484347
"rustc_data_structures",
43494348
"rustc_errors",
43504349
"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
@@ -26,10 +26,10 @@ use rustc_ast::ptr::P;
2626
use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, walk_list};
2727
use rustc_ast::*;
2828
use rustc_ast_pretty::pprust::{self, State};
29+
use rustc_attr_parsing::validate_attr;
2930
use rustc_data_structures::fx::FxIndexMap;
3031
use rustc_errors::DiagCtxtHandle;
3132
use rustc_feature::Features;
32-
use rustc_parse::validate_attr;
3333
use rustc_session::Session;
3434
use rustc_session::lint::builtin::{
3535
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
@@ -161,3 +161,15 @@ attr_parsing_unused_multiple =
161161
162162
-attr_parsing_previously_accepted =
163163
this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
164+
165+
attr_parsing_meta_bad_delim = wrong meta list delimiters
166+
attr_parsing_meta_bad_delim_suggestion = the delimiters should be `(` and `)`
167+
168+
attr_parsing_unsafe_attr_outside_unsafe = unsafe attribute used without unsafe
169+
.label = usage of unsafe attribute
170+
attr_parsing_unsafe_attr_outside_unsafe_suggestion = wrap the attribute in `unsafe(...)`
171+
172+
attr_parsing_invalid_attr_unsafe = `{$name}` is not an unsafe attribute
173+
.label = this is not an unsafe attribute
174+
.suggestion = remove the `unsafe(...)`
175+
.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
@@ -89,6 +89,7 @@ pub(crate) mod context;
8989
mod lints;
9090
pub mod parser;
9191
mod session_diagnostics;
92+
pub mod validate_attr;
9293

9394
pub use attributes::cfg::{CFG_TEMPLATE, EvalConfigResult, eval_config_entry, parse_cfg_attr};
9495
pub use attributes::cfg_old::*;

compiler/rustc_attr_parsing/src/session_diagnostics.rs

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

33
use rustc_ast as ast;
4+
use rustc_ast::Path;
45
use rustc_errors::codes::*;
56
use rustc_errors::{
67
Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level,
@@ -705,3 +706,56 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
705706
diag
706707
}
707708
}
709+
710+
#[derive(Diagnostic)]
711+
#[diag(attr_parsing_invalid_attr_unsafe)]
712+
#[note]
713+
pub(crate) struct InvalidAttrUnsafe {
714+
#[primary_span]
715+
#[label]
716+
pub span: Span,
717+
pub name: Path,
718+
}
719+
720+
#[derive(Diagnostic)]
721+
#[diag(attr_parsing_unsafe_attr_outside_unsafe)]
722+
pub(crate) struct UnsafeAttrOutsideUnsafe {
723+
#[primary_span]
724+
#[label]
725+
pub span: Span,
726+
#[subdiagnostic]
727+
pub suggestion: UnsafeAttrOutsideUnsafeSuggestion,
728+
}
729+
730+
#[derive(Subdiagnostic)]
731+
#[multipart_suggestion(
732+
attr_parsing_unsafe_attr_outside_unsafe_suggestion,
733+
applicability = "machine-applicable"
734+
)]
735+
pub(crate) struct UnsafeAttrOutsideUnsafeSuggestion {
736+
#[suggestion_part(code = "unsafe(")]
737+
pub left: Span,
738+
#[suggestion_part(code = ")")]
739+
pub right: Span,
740+
}
741+
742+
#[derive(Diagnostic)]
743+
#[diag(attr_parsing_meta_bad_delim)]
744+
pub(crate) struct MetaBadDelim {
745+
#[primary_span]
746+
pub span: Span,
747+
#[subdiagnostic]
748+
pub sugg: MetaBadDelimSugg,
749+
}
750+
751+
#[derive(Subdiagnostic)]
752+
#[multipart_suggestion(
753+
attr_parsing_meta_bad_delim_suggestion,
754+
applicability = "machine-applicable"
755+
)]
756+
pub(crate) struct MetaBadDelimSugg {
757+
#[suggestion_part(code = "(")]
758+
pub open: Span,
759+
#[suggestion_part(code = ")")]
760+
pub close: Span,
761+
}

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)