Skip to content

Commit b16a711

Browse files
authored
Rollup merge of #126405 - He1pa:translate_builtin_macro_diag, r=davidtwco
Migrate some rustc_builtin_macros to SessionDiagnostic <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r​? <reviewer name> --> Part of #100717. pick up abandoned pr: #101935 `@rustbot` label +A-translation
2 parents f0a0f8f + a82f70e commit b16a711

File tree

5 files changed

+76
-23
lines changed

5 files changed

+76
-23
lines changed

compiler/rustc_builtin_macros/messages.ftl

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ builtin_macros_asm_expected_other = expected operand, {$is_global_asm ->
1717
*[false] clobber_abi, options
1818
}, or additional template string
1919
20+
builtin_macros_asm_expected_string_literal = expected string literal
21+
.label = not a string literal
22+
2023
builtin_macros_asm_explicit_register_name = explicit register arguments cannot have names
2124
2225
builtin_macros_asm_mayunwind = asm labels are not allowed with the `may_unwind` option
@@ -25,6 +28,8 @@ builtin_macros_asm_modifier_invalid = asm template modifier must be a single cha
2528
2629
builtin_macros_asm_mutually_exclusive = the `{$opt1}` and `{$opt2}` options are mutually exclusive
2730
31+
builtin_macros_asm_no_matched_argument_name = there is no argument named `{$name}`
32+
2833
builtin_macros_asm_noreturn = asm outputs are not allowed with the `noreturn` option
2934
3035
builtin_macros_asm_opt_already_provided = the `{$symbol}` option was already provided
@@ -228,10 +233,16 @@ builtin_macros_only_one_argument = {$name} takes 1 argument
228233
229234
builtin_macros_proc_macro = `proc-macro` crate types currently cannot export any items other than functions tagged with `#[proc_macro]`, `#[proc_macro_derive]`, or `#[proc_macro_attribute]`
230235
236+
builtin_macros_proc_macro_attribute_only_be_used_on_bare_functions = the `#[{$path}]` attribute may only be used on bare functions
237+
238+
builtin_macros_proc_macro_attribute_only_usable_with_crate_type = the `#[{$path}]` attribute is only usable with crates of the `proc-macro` crate type
239+
231240
builtin_macros_requires_cfg_pattern =
232241
macro requires a cfg-pattern as an argument
233242
.label = cfg-pattern required
234243
244+
builtin_macros_source_uitls_expected_item = expected item, found `{$token}`
245+
235246
builtin_macros_takes_no_arguments = {$name} takes no arguments
236247
237248
builtin_macros_test_bad_fn = {$kind} functions cannot be used for tests

compiler/rustc_builtin_macros/src/asm.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,7 @@ fn parse_clobber_abi<'a>(p: &mut Parser<'a>, args: &mut AsmArgs) -> PResult<'a,
390390
}
391391
Err(opt_lit) => {
392392
let span = opt_lit.map_or(p.token.span, |lit| lit.span);
393-
let mut err = p.dcx().struct_span_err(span, "expected string literal");
394-
err.span_label(span, "not a string literal");
395-
return Err(err);
393+
return Err(p.dcx().create_err(errors::AsmExpectedStringLiteral { span }));
396394
}
397395
};
398396

@@ -639,14 +637,13 @@ fn expand_preparsed_asm(
639637
match args.named_args.get(&Symbol::intern(name)) {
640638
Some(&idx) => Some(idx),
641639
None => {
642-
let msg = format!("there is no argument named `{name}`");
643640
let span = arg.position_span;
644641
ecx.dcx()
645-
.struct_span_err(
646-
template_span
642+
.create_err(errors::AsmNoMatchedArgumentName {
643+
name: name.to_owned(),
644+
span: template_span
647645
.from_inner(InnerSpan::new(span.start, span.end)),
648-
msg,
649-
)
646+
})
650647
.emit();
651648
None
652649
}

compiler/rustc_builtin_macros/src/errors.rs

+40
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,14 @@ pub(crate) struct AsmExpectedComma {
728728
pub(crate) span: Span,
729729
}
730730

731+
#[derive(Diagnostic)]
732+
#[diag(builtin_macros_asm_expected_string_literal)]
733+
pub(crate) struct AsmExpectedStringLiteral {
734+
#[primary_span]
735+
#[label]
736+
pub(crate) span: Span,
737+
}
738+
731739
#[derive(Diagnostic)]
732740
#[diag(builtin_macros_asm_underscore_input)]
733741
pub(crate) struct AsmUnderscoreInput {
@@ -781,6 +789,14 @@ pub(crate) struct AsmNoReturn {
781789
pub(crate) outputs_sp: Vec<Span>,
782790
}
783791

792+
#[derive(Diagnostic)]
793+
#[diag(builtin_macros_asm_no_matched_argument_name)]
794+
pub(crate) struct AsmNoMatchedArgumentName {
795+
pub(crate) name: String,
796+
#[primary_span]
797+
pub(crate) span: Span,
798+
}
799+
784800
#[derive(Diagnostic)]
785801
#[diag(builtin_macros_asm_mayunwind)]
786802
pub(crate) struct AsmMayUnwind {
@@ -872,3 +888,27 @@ pub(crate) struct TakesNoArguments<'a> {
872888
pub span: Span,
873889
pub name: &'a str,
874890
}
891+
892+
#[derive(Diagnostic)]
893+
#[diag(builtin_macros_proc_macro_attribute_only_be_used_on_bare_functions)]
894+
pub(crate) struct AttributeOnlyBeUsedOnBareFunctions<'a> {
895+
#[primary_span]
896+
pub span: Span,
897+
pub path: &'a str,
898+
}
899+
900+
#[derive(Diagnostic)]
901+
#[diag(builtin_macros_proc_macro_attribute_only_usable_with_crate_type)]
902+
pub(crate) struct AttributeOnlyUsableWithCrateType<'a> {
903+
#[primary_span]
904+
pub span: Span,
905+
pub path: &'a str,
906+
}
907+
908+
#[derive(Diagnostic)]
909+
#[diag(builtin_macros_source_uitls_expected_item)]
910+
pub(crate) struct ExpectedItem<'a> {
911+
#[primary_span]
912+
pub span: Span,
913+
pub token: &'a str,
914+
}

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,12 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
214214
};
215215

216216
if !is_fn {
217-
let msg = format!(
218-
"the `#[{}]` attribute may only be used on bare functions",
219-
pprust::path_to_string(&attr.get_normal_item().path),
220-
);
221-
222-
self.dcx.span_err(attr.span, msg);
217+
self.dcx
218+
.create_err(errors::AttributeOnlyBeUsedOnBareFunctions {
219+
span: attr.span,
220+
path: &pprust::path_to_string(&attr.get_normal_item().path),
221+
})
222+
.emit();
223223
return;
224224
}
225225

@@ -228,12 +228,12 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
228228
}
229229

230230
if !self.is_proc_macro_crate {
231-
let msg = format!(
232-
"the `#[{}]` attribute is only usable with crates of the `proc-macro` crate type",
233-
pprust::path_to_string(&attr.get_normal_item().path),
234-
);
235-
236-
self.dcx.span_err(attr.span, msg);
231+
self.dcx
232+
.create_err(errors::AttributeOnlyUsableWithCrateType {
233+
span: attr.span,
234+
path: &pprust::path_to_string(&attr.get_normal_item().path),
235+
})
236+
.emit();
237237
return;
238238
}
239239

compiler/rustc_builtin_macros/src/source_util.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::errors;
12
use crate::util::{
23
check_zero_tts, get_single_str_from_tts, get_single_str_spanned_from_tts, parse_expr,
34
};
@@ -165,9 +166,13 @@ pub(crate) fn expand_include<'cx>(
165166
Ok(Some(item)) => ret.push(item),
166167
Ok(None) => {
167168
if self.p.token != token::Eof {
168-
let token = pprust::token_to_string(&self.p.token);
169-
let msg = format!("expected item, found `{token}`");
170-
self.p.dcx().span_err(self.p.token.span, msg);
169+
self.p
170+
.dcx()
171+
.create_err(errors::ExpectedItem {
172+
span: self.p.token.span,
173+
token: &pprust::token_to_string(&self.p.token),
174+
})
175+
.emit();
171176
}
172177

173178
break;

0 commit comments

Comments
 (0)