Skip to content

Commit

Permalink
Rollup merge of rust-lang#99213 - davidtwco:translation-migrate-passe…
Browse files Browse the repository at this point in the history
…s, r=compiler-errors

migrate some of `rustc_passes::check_attr`'s diagnostics and derive improvements

- Implements `IntoDiagnosticArg` for `char` using its `Debug` implementation and introduces a macro for those types which just delegate the implementation to `ToString`.
- Apply the `#[rustc_lint_diagnostics]` attribute to `LintDiagnosticBuilder::build` so that diagnostic migration lints will trigger for it - some diagnostics in `rustc_privacy` need updated after this since the lints apply to that crate.
- Add support for `MultiSpan` with any of the attributes that work on a `Span` in the diagnostic derive (`SessionDiagnostic` + `LintDiagnostic`). Requires that diagnostic logic generated for these attributes are emitted in the by-move block rather than the by-ref block that they would normally have been generated in.
- Both diagnostic and subdiagnostic derives were missing the ability to add warnings to diagnostics - this is made more difficult by the `warn` attribute already existing, so this name being unavailable for the derives to use. `#[warn_]` is used instead, which requires special-casing so that `{span_,}warn` is called instead of `{span_,}warn_`.
- Migrate half of the `rustc_passes::check_attr` diagnostics to using diagnostic derives and being translatable. I got tired after a while. I modified some diagnostic output for consistency while doing this, nothing too crazy.

r? `@compiler-errors`
  • Loading branch information
matthiaskrgr authored Jul 16, 2022
2 parents 6f8fb91 + 78b19a9 commit 79857a7
Show file tree
Hide file tree
Showing 41 changed files with 1,090 additions and 718 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4239,6 +4239,7 @@ dependencies = [
"rustc_hir",
"rustc_index",
"rustc_lexer",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_session",
Expand Down
151 changes: 151 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/passes.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
-passes-previously-accepted =
this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
-passes-see-issue =
see issue #{$issue} <https://github.com/rust-lang/rust/issues/{$issue}> for more information
passes-outer-crate-level-attr =
crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
passes-inner-crate-level-attr =
crate-level attribute should be in the root module
passes-ignored-attr-with-macro = `#[{$sym}]` is ignored on struct fields, match arms and macro defs
.warn = {-passes-previously-accepted}
.note = {-passes-see-issue(issue: "80564")}
passes-ignored-attr = `#[{$sym}]` is ignored on struct fields and match arms
.warn = {-passes-previously-accepted}
.note = {-passes-see-issue(issue: "80564")}
passes-inline-ignored-function-prototype = `#[inline]` is ignored on function prototypes
passes-inline-ignored-constants = `#[inline]` is ignored on constants
.warn = {-passes-previously-accepted}
.note = {-passes-see-issue(issue: "65833")}
passes-inline-not-fn-or-closure = attribute should be applied to function or closure
.label = not a function or closure
passes-no-coverage-ignored-function-prototype = `#[no_coverage]` is ignored on function prototypes
passes-no-coverage-propagate =
`#[no_coverage]` does not propagate into items and must be applied to the contained functions directly
passes-no-coverage-fn-defn = `#[no_coverage]` may only be applied to function definitions
passes-no-coverage-not-coverable = `#[no_coverage]` must be applied to coverable code
.label = not coverable code
passes-should-be-applied-to-fn = attribute should be applied to a function definition
.label = not a function definition
passes-naked-tracked-caller = cannot use `#[track_caller]` with `#[naked]`
passes-should-be-applied-to-struct-enum = attribute should be applied to a struct or enum
.label = not a struct or enum
passes-should-be-applied-to-trait = attribute should be applied to a trait
.label = not a trait
passes-target-feature-on-statement = {passes-should-be-applied-to-fn}
.warn = {-passes-previously-accepted}
.label = {passes-should-be-applied-to-fn.label}
passes-should-be-applied-to-static = attribute should be applied to a static
.label = not a static
passes-doc-expect-str = doc {$attr_name} attribute expects a string: #[doc({$attr_name} = "a")]
passes-doc-alias-empty = {$attr_str} attribute cannot have empty value
passes-doc-alias-bad-char = {$char_} character isn't allowed in {$attr_str}
passes-doc-alias-start-end = {$attr_str} cannot start or end with ' '
passes-doc-alias-bad-location = {$attr_str} isn't allowed on {$location}
passes-doc-alias-not-an-alias = {$attr_str} is the same as the item's name
passes-doc-alias-duplicated = doc alias is duplicated
.label = first defined here
passes-doc-alias-not-string-literal = `#[doc(alias("a"))]` expects string literals
passes-doc-alias-malformed =
doc alias attribute expects a string `#[doc(alias = "a")]` or a list of strings `#[doc(alias("a", "b"))]`
passes-doc-keyword-empty-mod = `#[doc(keyword = "...")]` should be used on empty modules
passes-doc-keyword-not-mod = `#[doc(keyword = "...")]` should be used on modules
passes-doc-keyword-invalid-ident = `{$doc_keyword}` is not a valid identifier
passes-doc-tuple-variadic-not-first =
`#[doc(tuple_variadic)]` must be used on the first of a set of tuple trait impls with varying arity
passes-doc-keyword-only-impl = `#[doc(keyword = "...")]` should be used on impl blocks
passes-doc-inline-conflict-first = this attribute...
passes-doc-inline-conflict-second = ...conflicts with this attribute
passes-doc-inline-conflict = conflicting doc inlining attributes
.help = remove one of the conflicting attributes
passes-doc-inline-only-use = this attribute can only be applied to a `use` item
.label = only applicable on `use` items
.not-a-use-item-label = not a `use` item
.note = read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#inline-and-no_inline> for more information
passes-doc-attr-not-crate-level =
`#![doc({$attr_name} = "...")]` isn't allowed as a crate-level attribute
passes-attr-crate-level = this attribute can only be applied at the crate level
.suggestion = to apply to the crate, use an inner attribute
.help = to apply to the crate, use an inner attribute
.note = read <https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level> for more information
passes-doc-test-unknown = unknown `doc(test)` attribute `{$path}`
passes-doc-test-takes-list = `#[doc(test(...)]` takes a list of attributes
passes-doc-primitive = `doc(primitive)` should never have been stable
passes-doc-test-unknown-any = unknown `doc` attribute `{$path}`
passes-doc-test-unknown-spotlight = unknown `doc` attribute `{$path}`
.note = `doc(spotlight)` was renamed to `doc(notable_trait)`
.suggestion = use `notable_trait` instead
.no-op-note = `doc(spotlight)` is now a no-op
passes-doc-test-unknown-include = unknown `doc` attribute `{$path}`
.suggestion = use `doc = include_str!` instead
passes-doc-invalid = invalid `doc` attribute
passes-pass-by-value = `pass_by_value` attribute should be applied to a struct, enum or type alias
.label = is not a struct, enum or type alias
passes-allow-incoherent-impl =
`rustc_allow_incoherent_impl` attribute should be applied to impl items.
.label = the only currently supported targets are inherent methods
passes-has-incoherent-inherent-impl =
`rustc_has_incoherent_inherent_impls` attribute should be applied to types or traits.
.label = only adts, extern types and traits are supported
passes-must-use-async =
`must_use` attribute on `async` functions applies to the anonymous `Future` returned by the function, not the value within
.label = this attribute does nothing, the `Future`s returned by async functions are already `must_use`
passes-must-use-no-effect = `#[must_use]` has no effect when applied to {$article} {$target}
passes-must-not-suspend = `must_not_suspend` attribute should be applied to a struct, enum, or trait
.label = is not a struct, enum, or trait
passes-cold = {passes-should-be-applied-to-fn}
.warn = {-passes-previously-accepted}
.label = {passes-should-be-applied-to-fn.label}
passes-link = attribute should be applied to an `extern` block with non-Rust ABI
.warn = {-passes-previously-accepted}
.label = not an `extern` block
9 changes: 9 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/privacy.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ privacy-unnamed-item-is-private = {$kind} is private
privacy-in-public-interface = {$vis_descr} {$kind} `{$descr}` in public interface
.label = can't leak {$vis_descr} {$kind}
.visibility-label = `{$descr}` declared as {$vis_descr}
privacy-from-private-dep-in-public-interface =
{$kind} `{$descr}` from private dependency '{$krate}' in public interface
private-in-public-lint =
{$vis_descr} {$kind} `{$descr}` in public interface (error {$kind ->
[trait] E0445
*[other] E0446
})
1 change: 1 addition & 0 deletions compiler/rustc_error_messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fluent_messages! {
expand => "../locales/en-US/expand.ftl",
lint => "../locales/en-US/lint.ftl",
parser => "../locales/en-US/parser.ftl",
passes => "../locales/en-US/passes.ftl",
privacy => "../locales/en-US/privacy.ftl",
typeck => "../locales/en-US/typeck.ftl",
}
Expand Down
111 changes: 34 additions & 77 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ pub trait IntoDiagnosticArg {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static>;
}

macro_rules! into_diagnostic_arg_using_display {
($( $ty:ty ),+ $(,)?) => {
$(
impl IntoDiagnosticArg for $ty {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
self.to_string().into_diagnostic_arg()
}
}
)+
}
}

into_diagnostic_arg_using_display!(
i8,
u8,
i16,
u16,
i32,
u32,
i64,
u64,
i128,
u128,
std::num::NonZeroU32,
hir::Target,
Edition,
Ident,
);

impl IntoDiagnosticArg for bool {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
if self {
Expand All @@ -50,81 +79,9 @@ impl IntoDiagnosticArg for bool {
}
}

impl IntoDiagnosticArg for i8 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for u8 {
impl IntoDiagnosticArg for char {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for i16 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for u16 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for i32 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for u32 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for i64 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for u64 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for i128 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for u128 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for String {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self))
}
}

impl IntoDiagnosticArg for std::num::NonZeroU32 {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
}
}

impl IntoDiagnosticArg for Edition {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
DiagnosticArgValue::Str(Cow::Owned(self.to_string()))
DiagnosticArgValue::Str(Cow::Owned(format!("{:?}", self)))
}
}

Expand All @@ -134,15 +91,15 @@ impl IntoDiagnosticArg for Symbol {
}
}

impl IntoDiagnosticArg for Ident {
impl<'a> IntoDiagnosticArg for &'a str {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
self.to_string().into_diagnostic_arg()
}
}

impl<'a> IntoDiagnosticArg for &'a str {
impl IntoDiagnosticArg for String {
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
self.to_string().into_diagnostic_arg()
DiagnosticArgValue::Str(Cow::Owned(self))
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_errors/src/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ macro_rules! error_code {
pub struct LintDiagnosticBuilder<'a, G: EmissionGuarantee>(DiagnosticBuilder<'a, G>);

impl<'a, G: EmissionGuarantee> LintDiagnosticBuilder<'a, G> {
#[rustc_lint_diagnostics]
/// Return the inner `DiagnosticBuilder`, first setting the primary message to `msg`.
pub fn build(mut self, msg: impl Into<DiagnosticMessage>) -> DiagnosticBuilder<'a, G> {
self.0.set_primary_message(msg);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_macros/src/diagnostics/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<'a> SessionDiagnosticDerive<'a> {
return DiagnosticDeriveError::ErrorHandled.to_compile_error();
}
(Some(DiagnosticDeriveKind::Lint), _) => {
span_err(span, "only `#[error(..)]` and `#[warn(..)]` are supported")
span_err(span, "only `#[error(..)]` and `#[warning(..)]` are supported")
.help("use the `#[error(...)]` attribute to create a error")
.emit();
return DiagnosticDeriveError::ErrorHandled.to_compile_error();
Expand Down
Loading

0 comments on commit 79857a7

Please sign in to comment.