Skip to content

Commit 3b7a159

Browse files
authored
Rollup merge of #104460 - Nilstrieb:expand-the-translation, r=davidtwco
Migrate parts of `rustc_expand` to session diagnostics This migrates everything but the `mbe` and `proc_macro` modules. It also contains a few cleanups and drive-by/accidental diagnostic improvements which can be seen in the diff for the UI tests.
2 parents b12b836 + 2f9f097 commit 3b7a159

File tree

19 files changed

+640
-236
lines changed

19 files changed

+640
-236
lines changed

compiler/rustc_builtin_macros/src/concat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn expand_concat(
1111
sp: rustc_span::Span,
1212
tts: TokenStream,
1313
) -> Box<dyn base::MacResult + 'static> {
14-
let Some(es) = base::get_exprs_from_tts(cx, sp, tts) else {
14+
let Some(es) = base::get_exprs_from_tts(cx, tts) else {
1515
return DummyResult::any(sp);
1616
};
1717
let mut accumulator = String::new();

compiler/rustc_builtin_macros/src/concat_bytes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub fn expand_concat_bytes(
137137
sp: rustc_span::Span,
138138
tts: TokenStream,
139139
) -> Box<dyn base::MacResult + 'static> {
140-
let Some(es) = base::get_exprs_from_tts(cx, sp, tts) else {
140+
let Some(es) = base::get_exprs_from_tts(cx, tts) else {
141141
return DummyResult::any(sp);
142142
};
143143
let mut accumulator = Vec::new();

compiler/rustc_builtin_macros/src/env.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn expand_env<'cx>(
5252
sp: Span,
5353
tts: TokenStream,
5454
) -> Box<dyn base::MacResult + 'cx> {
55-
let mut exprs = match get_exprs_from_tts(cx, sp, tts) {
55+
let mut exprs = match get_exprs_from_tts(cx, tts) {
5656
Some(exprs) if exprs.is_empty() => {
5757
cx.span_err(sp, "env! takes 1 or 2 arguments");
5858
return DummyResult::any(sp);

compiler/rustc_error_messages/locales/en-US/expand.ftl

+107
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,110 @@ expand_var_still_repeating =
2020
variable '{$ident}' is still repeating at this depth
2121
2222
expand_meta_var_dif_seq_matchers = {$msg}
23+
24+
expand_macro_const_stability =
25+
macros cannot have const stability attributes
26+
.label = invalid const stability attribute
27+
.label2 = const stability attribute affects this macro
28+
29+
expand_macro_body_stability =
30+
macros cannot have body stability attributes
31+
.label = invalid body stability attribute
32+
.label2 = body stability attribute affects this macro
33+
34+
expand_resolve_relative_path =
35+
cannot resolve relative path in non-file source `{$path}`
36+
37+
expand_attr_no_arguments =
38+
attribute must have either one or two arguments
39+
40+
expand_not_a_meta_item =
41+
not a meta item
42+
43+
expand_only_one_word =
44+
must only be one word
45+
46+
expand_cannot_be_name_of_macro =
47+
`{$trait_ident}` cannot be a name of {$macro_type} macro
48+
49+
expand_arg_not_attributes =
50+
second argument must be `attributes`
51+
52+
expand_attributes_wrong_form =
53+
attribute must be of form: `attributes(foo, bar)`
54+
55+
expand_attribute_meta_item =
56+
attribute must be a meta item, not a literal
57+
58+
expand_attribute_single_word =
59+
attribute must only be a single word
60+
61+
expand_helper_attribute_name_invalid =
62+
`{$name}` cannot be a name of derive helper attribute
63+
64+
expand_expected_comma_in_list =
65+
expected token: `,`
66+
67+
expand_only_one_argument =
68+
{$name} takes 1 argument
69+
70+
expand_takes_no_arguments =
71+
{$name} takes no arguments
72+
73+
expand_feature_included_in_edition =
74+
the feature `{$feature}` is included in the Rust {$edition} edition
75+
76+
expand_feature_removed =
77+
feature has been removed
78+
.label = feature has been removed
79+
.reason = {$reason}
80+
81+
expand_feature_not_allowed =
82+
the feature `{$name}` is not in the list of allowed features
83+
84+
expand_recursion_limit_reached =
85+
recursion limit reached while expanding `{$descr}`
86+
.help = consider increasing the recursion limit by adding a `#![recursion_limit = "{$suggested_limit}"]` attribute to your crate (`{$crate_name}`)
87+
88+
expand_malformed_feature_attribute =
89+
malformed `feature` attribute input
90+
.expected = expected just one word
91+
92+
expand_remove_expr_not_supported =
93+
removing an expression is not supported in this position
94+
95+
expand_invalid_cfg_no_parens = `cfg` is not followed by parentheses
96+
expand_invalid_cfg_no_predicate = `cfg` predicate is not specified
97+
expand_invalid_cfg_multiple_predicates = multiple `cfg` predicates are specified
98+
expand_invalid_cfg_predicate_literal = `cfg` predicate key cannot be a literal
99+
expand_invalid_cfg_expected_syntax = expected syntax is
100+
101+
expand_wrong_fragment_kind =
102+
non-{$kind} macro in {$kind} position: {$name}
103+
104+
expand_unsupported_key_value =
105+
key-value macro attributes are not supported
106+
107+
expand_incomplete_parse =
108+
macro expansion ignores token `{$token}` and any following
109+
.label = caused by the macro expansion here
110+
.note = the usage of `{$macro_path}!` is likely invalid in {$kind_name} context
111+
.suggestion_add_semi = you might be missing a semicolon here
112+
113+
expand_remove_node_not_supported =
114+
removing {$descr} is not supported in this position
115+
116+
expand_module_circular =
117+
circular modules: {$modules}
118+
119+
expand_module_in_block =
120+
cannot declare a non-inline module inside a block unless it has a path attribute
121+
.note = maybe `use` the module `{$name}` instead of redeclaring it
122+
123+
expand_module_file_not_found =
124+
file not found for module `{$name}`
125+
.help = to create the module `{$name}`, create file "{$default_path}" or "{$secondary_path}"
126+
127+
expand_module_multiple_candidates =
128+
file for module `{$name}` found at both "{$default_path}" and "{$secondary_path}"
129+
.help = delete or rename one of them to remove the ambiguity

compiler/rustc_errors/src/diagnostic_impls.rs

+6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ impl IntoDiagnosticArg for ast::Path {
152152
}
153153
}
154154

155+
impl IntoDiagnosticArg for &ast::Path {
156+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
157+
DiagnosticArgValue::Str(Cow::Owned(pprust::path_to_string(self)))
158+
}
159+
}
160+
155161
impl IntoDiagnosticArg for ast::token::Token {
156162
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
157163
DiagnosticArgValue::Str(pprust::token_to_string(&self))

compiler/rustc_expand/src/base.rs

+39-51
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
3+
use crate::errors::{
4+
ArgumentNotAttributes, AttrNoArguments, AttributeMetaItem, AttributeSingleWord,
5+
AttributesWrongForm, CannotBeNameOfMacro, ExpectedCommaInList, HelperAttributeNameInvalid,
6+
MacroBodyStability, MacroConstStability, NotAMetaItem, OnlyOneArgument, OnlyOneWord,
7+
ResolveRelativePath, TakesNoArguments,
8+
};
19
use crate::expand::{self, AstFragment, Invocation};
210
use crate::module::DirOwnership;
311

@@ -789,26 +797,16 @@ impl SyntaxExtension {
789797
.unwrap_or_else(|| (None, helper_attrs));
790798
let (stability, const_stability, body_stability) = attr::find_stability(&sess, attrs, span);
791799
if let Some((_, sp)) = const_stability {
792-
sess.parse_sess
793-
.span_diagnostic
794-
.struct_span_err(sp, "macros cannot have const stability attributes")
795-
.span_label(sp, "invalid const stability attribute")
796-
.span_label(
797-
sess.source_map().guess_head_span(span),
798-
"const stability attribute affects this macro",
799-
)
800-
.emit();
800+
sess.emit_err(MacroConstStability {
801+
span: sp,
802+
head_span: sess.source_map().guess_head_span(span),
803+
});
801804
}
802805
if let Some((_, sp)) = body_stability {
803-
sess.parse_sess
804-
.span_diagnostic
805-
.struct_span_err(sp, "macros cannot have body stability attributes")
806-
.span_label(sp, "invalid body stability attribute")
807-
.span_label(
808-
sess.source_map().guess_head_span(span),
809-
"body stability attribute affects this macro",
810-
)
811-
.emit();
806+
sess.emit_err(MacroBodyStability {
807+
span: sp,
808+
head_span: sess.source_map().guess_head_span(span),
809+
});
812810
}
813811

814812
SyntaxExtension {
@@ -1200,13 +1198,11 @@ pub fn resolve_path(
12001198
.expect("attempting to resolve a file path in an external file"),
12011199
FileName::DocTest(path, _) => path,
12021200
other => {
1203-
return Err(parse_sess.span_diagnostic.struct_span_err(
1201+
return Err(ResolveRelativePath {
12041202
span,
1205-
&format!(
1206-
"cannot resolve relative path in non-file source `{}`",
1207-
parse_sess.source_map().filename_for_diagnostics(&other)
1208-
),
1209-
));
1203+
path: parse_sess.source_map().filename_for_diagnostics(&other).to_string(),
1204+
}
1205+
.into_diagnostic(&parse_sess.span_diagnostic));
12101206
}
12111207
};
12121208
result.pop();
@@ -1222,6 +1218,8 @@ pub fn resolve_path(
12221218
/// The returned bool indicates whether an applicable suggestion has already been
12231219
/// added to the diagnostic to avoid emitting multiple suggestions. `Err(None)`
12241220
/// indicates that an ast error was encountered.
1221+
// FIXME(Nilstrieb) Make this function setup translatable
1222+
#[allow(rustc::untranslatable_diagnostic)]
12251223
pub fn expr_to_spanned_string<'a>(
12261224
cx: &'a mut ExtCtxt<'_>,
12271225
expr: P<ast::Expr>,
@@ -1280,9 +1278,9 @@ pub fn expr_to_string(
12801278
/// compilation should call
12811279
/// `cx.parse_sess.span_diagnostic.abort_if_errors()` (this should be
12821280
/// done as rarely as possible).
1283-
pub fn check_zero_tts(cx: &ExtCtxt<'_>, sp: Span, tts: TokenStream, name: &str) {
1281+
pub fn check_zero_tts(cx: &ExtCtxt<'_>, span: Span, tts: TokenStream, name: &str) {
12841282
if !tts.is_empty() {
1285-
cx.span_err(sp, &format!("{} takes no arguments", name));
1283+
cx.emit_err(TakesNoArguments { span, name });
12861284
}
12871285
}
12881286

@@ -1304,31 +1302,27 @@ pub fn parse_expr(p: &mut parser::Parser<'_>) -> Option<P<ast::Expr>> {
13041302
/// expect exactly one string literal, or emit an error and return `None`.
13051303
pub fn get_single_str_from_tts(
13061304
cx: &mut ExtCtxt<'_>,
1307-
sp: Span,
1305+
span: Span,
13081306
tts: TokenStream,
13091307
name: &str,
13101308
) -> Option<Symbol> {
13111309
let mut p = cx.new_parser_from_tts(tts);
13121310
if p.token == token::Eof {
1313-
cx.span_err(sp, &format!("{} takes 1 argument", name));
1311+
cx.emit_err(OnlyOneArgument { span, name });
13141312
return None;
13151313
}
13161314
let ret = parse_expr(&mut p)?;
13171315
let _ = p.eat(&token::Comma);
13181316

13191317
if p.token != token::Eof {
1320-
cx.span_err(sp, &format!("{} takes 1 argument", name));
1318+
cx.emit_err(OnlyOneArgument { span, name });
13211319
}
13221320
expr_to_string(cx, ret, "argument must be a string literal").map(|(s, _)| s)
13231321
}
13241322

13251323
/// Extracts comma-separated expressions from `tts`.
13261324
/// On error, emit it, and return `None`.
1327-
pub fn get_exprs_from_tts(
1328-
cx: &mut ExtCtxt<'_>,
1329-
sp: Span,
1330-
tts: TokenStream,
1331-
) -> Option<Vec<P<ast::Expr>>> {
1325+
pub fn get_exprs_from_tts(cx: &mut ExtCtxt<'_>, tts: TokenStream) -> Option<Vec<P<ast::Expr>>> {
13321326
let mut p = cx.new_parser_from_tts(tts);
13331327
let mut es = Vec::new();
13341328
while p.token != token::Eof {
@@ -1343,7 +1337,7 @@ pub fn get_exprs_from_tts(
13431337
continue;
13441338
}
13451339
if p.token != token::Eof {
1346-
cx.span_err(sp, "expected token: `,`");
1340+
cx.emit_err(ExpectedCommaInList { span: p.token.span });
13471341
return None;
13481342
}
13491343
}
@@ -1353,64 +1347,58 @@ pub fn get_exprs_from_tts(
13531347
pub fn parse_macro_name_and_helper_attrs(
13541348
diag: &rustc_errors::Handler,
13551349
attr: &Attribute,
1356-
descr: &str,
1350+
macro_type: &str,
13571351
) -> Option<(Symbol, Vec<Symbol>)> {
13581352
// Once we've located the `#[proc_macro_derive]` attribute, verify
13591353
// that it's of the form `#[proc_macro_derive(Foo)]` or
13601354
// `#[proc_macro_derive(Foo, attributes(A, ..))]`
13611355
let list = attr.meta_item_list()?;
13621356
if list.len() != 1 && list.len() != 2 {
1363-
diag.span_err(attr.span, "attribute must have either one or two arguments");
1357+
diag.emit_err(AttrNoArguments { span: attr.span });
13641358
return None;
13651359
}
13661360
let Some(trait_attr) = list[0].meta_item() else {
1367-
diag.span_err(list[0].span(), "not a meta item");
1361+
diag.emit_err(NotAMetaItem {span: list[0].span()});
13681362
return None;
13691363
};
13701364
let trait_ident = match trait_attr.ident() {
13711365
Some(trait_ident) if trait_attr.is_word() => trait_ident,
13721366
_ => {
1373-
diag.span_err(trait_attr.span, "must only be one word");
1367+
diag.emit_err(OnlyOneWord { span: trait_attr.span });
13741368
return None;
13751369
}
13761370
};
13771371

13781372
if !trait_ident.name.can_be_raw() {
1379-
diag.span_err(
1380-
trait_attr.span,
1381-
&format!("`{}` cannot be a name of {} macro", trait_ident, descr),
1382-
);
1373+
diag.emit_err(CannotBeNameOfMacro { span: trait_attr.span, trait_ident, macro_type });
13831374
}
13841375

13851376
let attributes_attr = list.get(1);
13861377
let proc_attrs: Vec<_> = if let Some(attr) = attributes_attr {
13871378
if !attr.has_name(sym::attributes) {
1388-
diag.span_err(attr.span(), "second argument must be `attributes`");
1379+
diag.emit_err(ArgumentNotAttributes { span: attr.span() });
13891380
}
13901381
attr.meta_item_list()
13911382
.unwrap_or_else(|| {
1392-
diag.span_err(attr.span(), "attribute must be of form: `attributes(foo, bar)`");
1383+
diag.emit_err(AttributesWrongForm { span: attr.span() });
13931384
&[]
13941385
})
13951386
.iter()
13961387
.filter_map(|attr| {
13971388
let Some(attr) = attr.meta_item() else {
1398-
diag.span_err(attr.span(), "not a meta item");
1389+
diag.emit_err(AttributeMetaItem { span: attr.span() });
13991390
return None;
14001391
};
14011392

14021393
let ident = match attr.ident() {
14031394
Some(ident) if attr.is_word() => ident,
14041395
_ => {
1405-
diag.span_err(attr.span, "must only be one word");
1396+
diag.emit_err(AttributeSingleWord { span: attr.span });
14061397
return None;
14071398
}
14081399
};
14091400
if !ident.name.can_be_raw() {
1410-
diag.span_err(
1411-
attr.span,
1412-
&format!("`{}` cannot be a name of derive helper attribute", ident),
1413-
);
1401+
diag.emit_err(HelperAttributeNameInvalid { span: attr.span, name: ident });
14141402
}
14151403

14161404
Some(ident.name)

0 commit comments

Comments
 (0)