Skip to content

Commit

Permalink
macros: use typed identifiers in diag derive
Browse files Browse the repository at this point in the history
Using typed identifiers instead of strings with the Fluent identifier
enables the diagnostic derive to benefit from the compile-time
validation that comes with typed identifiers - use of a non-existent
Fluent identifier will not compile.

Signed-off-by: David Wood <david.wood@huawei.com>
  • Loading branch information
davidtwco committed Jun 24, 2022
1 parent fc96600 commit 99bc979
Show file tree
Hide file tree
Showing 11 changed files with 673 additions and 455 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ pub fn expand_cfg(
}

#[derive(SessionDiagnostic)]
#[error(slug = "builtin-macros-requires-cfg-pattern")]
#[error(builtin_macros::requires_cfg_pattern)]
struct RequiresCfgPattern {
#[primary_span]
#[label]
span: Span,
}

#[derive(SessionDiagnostic)]
#[error(slug = "builtin-macros-expected-one-cfg-pattern")]
#[error(builtin_macros::expected_one_cfg_pattern)]
struct OneCfgPattern {
#[primary_span]
span: Span,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
builtin-macros-requires-cfg-pattern =
builtin_macros-requires-cfg-pattern =
macro requires a cfg-pattern as an argument
.label = cfg-pattern required
builtin-macros-expected-one-cfg-pattern = expected 1 cfg-pattern
builtin_macros-expected-one-cfg-pattern = expected 1 cfg-pattern
460 changes: 284 additions & 176 deletions compiler/rustc_macros/src/diagnostics/diagnostic.rs

Large diffs are not rendered by default.

37 changes: 23 additions & 14 deletions compiler/rustc_macros/src/diagnostics/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ pub(crate) fn _throw_err(
SessionDiagnosticDeriveError::ErrorHandled
}

/// Helper function for printing `syn::Path` - doesn't handle arguments in paths and these are
/// unlikely to come up much in use of the macro.
fn path_to_string(path: &syn::Path) -> String {
let mut out = String::new();
for (i, segment) in path.segments.iter().enumerate() {
if i > 0 || path.leading_colon.is_some() {
out.push_str("::");
}
out.push_str(&segment.ident.to_string());
}
out
}

/// Returns an error diagnostic on span `span` with msg `msg`.
pub(crate) fn span_err(span: impl MultiSpan, msg: &str) -> Diagnostic {
Diagnostic::spanned(span, Level::Error, msg)
Expand All @@ -61,15 +74,13 @@ pub(crate) use throw_span_err;
/// Returns an error diagnostic for an invalid attribute.
pub(crate) fn invalid_attr(attr: &Attribute, meta: &Meta) -> Diagnostic {
let span = attr.span().unwrap();
let name = attr.path.segments.last().unwrap().ident.to_string();
let name = name.as_str();

let path = path_to_string(&attr.path);
match meta {
Meta::Path(_) => span_err(span, &format!("`#[{}]` is not a valid attribute", name)),
Meta::Path(_) => span_err(span, &format!("`#[{}]` is not a valid attribute", path)),
Meta::NameValue(_) => {
span_err(span, &format!("`#[{} = ...]` is not a valid attribute", name))
span_err(span, &format!("`#[{} = ...]` is not a valid attribute", path))
}
Meta::List(_) => span_err(span, &format!("`#[{}(...)]` is not a valid attribute", name)),
Meta::List(_) => span_err(span, &format!("`#[{}(...)]` is not a valid attribute", path)),
}
}

Expand Down Expand Up @@ -101,18 +112,16 @@ pub(crate) fn invalid_nested_attr(attr: &Attribute, nested: &NestedMeta) -> Diag
};

let span = meta.span().unwrap();
let nested_name = meta.path().segments.last().unwrap().ident.to_string();
let nested_name = nested_name.as_str();
let path = path_to_string(meta.path());
match meta {
Meta::NameValue(..) => span_err(
span,
&format!("`#[{}({} = ...)]` is not a valid attribute", name, nested_name),
),
Meta::NameValue(..) => {
span_err(span, &format!("`#[{}({} = ...)]` is not a valid attribute", name, path))
}
Meta::Path(..) => {
span_err(span, &format!("`#[{}({})]` is not a valid attribute", name, nested_name))
span_err(span, &format!("`#[{}({})]` is not a valid attribute", name, path))
}
Meta::List(..) => {
span_err(span, &format!("`#[{}({}(...))]` is not a valid attribute", name, nested_name))
span_err(span, &format!("`#[{}({}(...))]` is not a valid attribute", name, path))
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_macros/src/diagnostics/fluent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
];

#generated

pub mod _subdiag {
pub const note: crate::SubdiagnosticMessage =
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("note"));
pub const help: crate::SubdiagnosticMessage =
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("help"));
pub const label: crate::SubdiagnosticMessage =
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("label"));
pub const suggestion: crate::SubdiagnosticMessage =
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("suggestion"));
}
}
}
.into()
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_macros/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ use synstructure::Structure;
/// # extern crate rust_middle;
/// # use rustc_middle::ty::Ty;
/// #[derive(SessionDiagnostic)]
/// #[error(code = "E0505", slug = "borrowck-move-out-of-borrow")]
/// #[error(borrowck::move_out_of_borrow, code = "E0505")]
/// pub struct MoveOutOfBorrowError<'tcx> {
/// pub name: Ident,
/// pub ty: Ty<'tcx>,
/// #[primary_span]
/// #[label]
/// pub span: Span,
/// #[label = "first-borrow-label"]
/// #[label(borrowck::first_borrow_label)]
/// pub first_borrow_span: Span,
/// #[suggestion(code = "{name}.clone()")]
/// pub clone_sugg: Option<(Span, Applicability)>
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl MultiSugg {
}

#[derive(SessionDiagnostic)]
#[error(slug = "parser-maybe-report-ambiguous-plus")]
#[error(parser::maybe_report_ambiguous_plus)]
struct AmbiguousPlus {
pub sum_ty: String,
#[primary_span]
Expand All @@ -253,7 +253,7 @@ struct AmbiguousPlus {
}

#[derive(SessionDiagnostic)]
#[error(code = "E0178", slug = "parser-maybe-recover-from-bad-type-plus")]
#[error(parser::maybe_recover_from_bad_type_plus, code = "E0178")]
struct BadTypePlus {
pub ty: String,
#[primary_span]
Expand Down Expand Up @@ -287,7 +287,7 @@ pub enum BadTypePlusSub {
}

#[derive(SessionDiagnostic)]
#[error(slug = "parser-maybe-recover-from-bad-qpath-stage-2")]
#[error(parser::maybe_recover_from_bad_qpath_stage_2)]
struct BadQPathStage2 {
#[primary_span]
#[suggestion(applicability = "maybe-incorrect")]
Expand All @@ -296,7 +296,7 @@ struct BadQPathStage2 {
}

#[derive(SessionDiagnostic)]
#[error(slug = "parser-incorrect-semicolon")]
#[error(parser::incorrect_semicolon)]
struct IncorrectSemicolon<'a> {
#[primary_span]
#[suggestion_short(applicability = "machine-applicable")]
Expand All @@ -307,26 +307,26 @@ struct IncorrectSemicolon<'a> {
}

#[derive(SessionDiagnostic)]
#[error(slug = "parser-incorrect-use-of-await")]
#[error(parser::incorrect_use_of_await)]
struct IncorrectUseOfAwait {
#[primary_span]
#[suggestion(message = "parentheses-suggestion", applicability = "machine-applicable")]
#[suggestion(parser::parentheses_suggestion, applicability = "machine-applicable")]
span: Span,
}

#[derive(SessionDiagnostic)]
#[error(slug = "parser-incorrect-use-of-await")]
#[error(parser::incorrect_use_of_await)]
struct IncorrectAwait {
#[primary_span]
span: Span,
#[suggestion(message = "postfix-suggestion", code = "{expr}.await{question_mark}")]
#[suggestion(parser::postfix_suggestion, code = "{expr}.await{question_mark}")]
sugg_span: (Span, Applicability),
expr: String,
question_mark: &'static str,
}

#[derive(SessionDiagnostic)]
#[error(slug = "parser-in-in-typo")]
#[error(parser::in_in_typo)]
struct InInTypo {
#[primary_span]
span: Span,
Expand Down
Loading

0 comments on commit 99bc979

Please sign in to comment.