Skip to content

Commit 18388c9

Browse files
committed
Rewrite added diagnostics as translatable
Start messages with lowercase
1 parent 5c5c3c9 commit 18388c9

File tree

6 files changed

+61
-14
lines changed

6 files changed

+61
-14
lines changed

compiler/rustc_resolve/messages.ftl

+10
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,13 @@ resolve_indeterminate =
209209
210210
resolve_module_only =
211211
visibility must resolve to a module
212+
213+
resolve_macro_expected_found =
214+
expected {$expected}, found {$found} `{$macro_path}`
215+
216+
resolve_remove_surrounding_derive =
217+
remove from the surrounding `derive()`
218+
219+
resolve_add_as_non_derive =
220+
add as non-Derive macro
221+
`#[{$macro_path}]`

compiler/rustc_resolve/src/errors.rs

+27
Original file line numberDiff line numberDiff line change
@@ -472,3 +472,30 @@ pub(crate) struct Indeterminate(#[primary_span] pub(crate) Span);
472472
#[derive(Diagnostic)]
473473
#[diag(resolve_module_only)]
474474
pub(crate) struct ModuleOnly(#[primary_span] pub(crate) Span);
475+
476+
#[derive(Diagnostic, Default)]
477+
#[diag(resolve_macro_expected_found)]
478+
pub(crate) struct MacroExpectedFound<'a> {
479+
#[primary_span]
480+
pub(crate) span: Span,
481+
pub(crate) found: &'a str,
482+
pub(crate) expected: &'a str,
483+
pub(crate) macro_path: &'a str,
484+
#[subdiagnostic]
485+
pub(crate) remove_surrounding_derive: Option<RemoveSurroundingDerive>,
486+
#[subdiagnostic]
487+
pub(crate) remove_surrounding_derive_help: Option<RemoveAddAsNonDerive<'a>>,
488+
}
489+
490+
#[derive(Subdiagnostic)]
491+
#[help(resolve_remove_surrounding_derive)]
492+
pub(crate) struct RemoveSurroundingDerive {
493+
#[primary_span]
494+
pub(crate) span: Span,
495+
}
496+
497+
#[derive(Subdiagnostic)]
498+
#[help(resolve_add_as_non_derive)]
499+
pub(crate) struct RemoveAddAsNonDerive<'a> {
500+
pub(crate) macro_path: &'a str,
501+
}

compiler/rustc_resolve/src/macros.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! A bunch of methods and structures more or less related to resolving macros and
22
//! interface provided by `Resolver` to macro expander.
33
4+
use crate::errors::{MacroExpectedFound, RemoveAddAsNonDerive, RemoveSurroundingDerive};
45
use crate::Namespace::*;
56
use crate::{BuiltinMacroState, Determinacy};
67
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
@@ -543,21 +544,30 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
543544
};
544545
if let Some((article, expected)) = unexpected_res {
545546
let path_str = pprust::path_to_string(path);
546-
let msg = format!("expected {}, found {} `{}`", expected, res.descr(), path_str);
547-
let mut err = self.tcx.sess.struct_span_err(path.span, &msg);
548547

549-
err.span_label(path.span, format!("not {} {}", article, expected));
548+
let mut err = MacroExpectedFound {
549+
span: path.span,
550+
expected,
551+
found: res.descr(),
552+
macro_path: &path_str,
553+
..Default::default() // Subdiagnostics default to None
554+
};
550555

551-
// Suggest moving the macro out of the derive() as the macro isn't Derive
556+
// Suggest moving the macro out of the derive() if the macro isn't Derive
552557
if !path.span.from_expansion()
553558
&& kind == MacroKind::Derive
554559
&& ext.macro_kind() != MacroKind::Derive
555560
{
556-
err.span_help(path.span, "remove from the surrounding `derive()`");
557-
err.help(format!("add as non-Derive macro\n`#[{}]`", path_str));
561+
err.remove_surrounding_derive = Some(RemoveSurroundingDerive { span: path.span });
562+
err.remove_surrounding_derive_help =
563+
Some(RemoveAddAsNonDerive { macro_path: &path_str });
558564
}
559565

566+
let mut err = self.tcx.sess.create_err(err);
567+
err.span_label(path.span, format!("not {} {}", article, expected));
568+
560569
err.emit();
570+
561571
return Ok((self.dummy_ext(kind), Res::Err));
562572
}
563573

tests/ui/macros/macro-path-prelude-fail-4.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ error: expected derive macro, found built-in attribute `inline`
44
LL | #[derive(inline)]
55
| ^^^^^^ not a derive macro
66
|
7-
help: Remove from the surrounding `derive()`
7+
help: remove from the surrounding `derive()`
88
--> $DIR/macro-path-prelude-fail-4.rs:1:10
99
|
1010
LL | #[derive(inline)]
1111
| ^^^^^^
12-
= help: Add as non-Derive macro
12+
= help: add as non-Derive macro
1313
`#[inline]`
1414

1515
error: aborting due to previous error

tests/ui/proc-macro/macro-namespace-reserved-2.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ error: expected derive macro, found attribute macro `my_macro_attr`
5858
LL | #[derive(my_macro_attr)]
5959
| ^^^^^^^^^^^^^ not a derive macro
6060
|
61-
help: Remove from the surrounding `derive()`
61+
help: remove from the surrounding `derive()`
6262
--> $DIR/macro-namespace-reserved-2.rs:53:10
6363
|
6464
LL | #[derive(my_macro_attr)]
6565
| ^^^^^^^^^^^^^
66-
= help: Add as non-Derive macro
66+
= help: add as non-Derive macro
6767
`#[my_macro_attr]`
6868

6969
error: can't use a procedural macro from the same crate that defines it
@@ -96,12 +96,12 @@ error: expected derive macro, found macro `crate::my_macro`
9696
LL | #[derive(crate::my_macro)]
9797
| ^^^^^^^^^^^^^^^ not a derive macro
9898
|
99-
help: Remove from the surrounding `derive()`
99+
help: remove from the surrounding `derive()`
100100
--> $DIR/macro-namespace-reserved-2.rs:50:10
101101
|
102102
LL | #[derive(crate::my_macro)]
103103
| ^^^^^^^^^^^^^^^
104-
= help: Add as non-Derive macro
104+
= help: add as non-Derive macro
105105
`#[crate::my_macro]`
106106

107107
error: cannot find macro `my_macro_attr` in this scope

tests/ui/tool-attributes/tool-attributes-misplaced-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ error: expected derive macro, found tool attribute `rustfmt::skip`
44
LL | #[derive(rustfmt::skip)]
55
| ^^^^^^^^^^^^^ not a derive macro
66
|
7-
help: Remove from the surrounding `derive()`
7+
help: remove from the surrounding `derive()`
88
--> $DIR/tool-attributes-misplaced-2.rs:1:10
99
|
1010
LL | #[derive(rustfmt::skip)]
1111
| ^^^^^^^^^^^^^
12-
= help: Add as non-Derive macro
12+
= help: add as non-Derive macro
1313
`#[rustfmt::skip]`
1414

1515
error: expected macro, found tool attribute `rustfmt::skip`

0 commit comments

Comments
 (0)