Skip to content

Commit 39c1b06

Browse files
committed
Make some invalid codegen attr errors structured/translatable
1 parent d88ffcd commit 39c1b06

File tree

4 files changed

+172
-93
lines changed

4 files changed

+172
-93
lines changed

compiler/rustc_codegen_ssa/messages.ftl

+30
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ codegen_ssa_atomic_compare_exchange = Atomic compare-exchange intrinsic missing
1818
1919
codegen_ssa_autodiff_without_lto = using the autodiff feature requires using fat-lto
2020
21+
codegen_ssa_bare_instruction_set = `#[instruction_set]` requires an argument
22+
2123
codegen_ssa_binary_output_to_tty = option `-o` or `--emit` is used to write binary output type `{$shorthand}` to stdout, but stdout is a tty
2224
2325
codegen_ssa_cgu_not_recorded =
@@ -52,6 +54,10 @@ codegen_ssa_error_creating_remark_dir = failed to create remark directory: {$err
5254
codegen_ssa_error_writing_def_file =
5355
Error writing .DEF file: {$error}
5456
57+
codegen_ssa_expected_name_value_pair = expected name value pair
58+
59+
codegen_ssa_expected_one_argument = expected one argument
60+
5561
codegen_ssa_expected_used_symbol = expected `used`, `used(compiler)` or `used(linker)`
5662
5763
codegen_ssa_extern_funcs_not_found = some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
@@ -88,9 +94,17 @@ codegen_ssa_incorrect_cgu_reuse_type =
8894
8995
codegen_ssa_insufficient_vs_code_product = VS Code is a different product, and is not sufficient.
9096
97+
codegen_ssa_invalid_argument = invalid argument
98+
.help = valid inline arguments are `always` and `never`
99+
100+
codegen_ssa_invalid_instruction_set = invalid instruction set specified
101+
91102
codegen_ssa_invalid_link_ordinal_nargs = incorrect number of arguments to `#[link_ordinal]`
92103
.note = the attribute requires exactly one argument
93104
105+
codegen_ssa_invalid_literal_value = invalid literal value
106+
.label = value must be an integer between `0` and `255`
107+
94108
codegen_ssa_invalid_monomorphization_basic_float_type = invalid monomorphization of `{$name}` intrinsic: expected basic float type, found `{$ty}`
95109
96110
codegen_ssa_invalid_monomorphization_basic_integer_type = invalid monomorphization of `{$name}` intrinsic: expected basic integer type, found `{$ty}`
@@ -170,6 +184,8 @@ codegen_ssa_invalid_monomorphization_vector_argument = invalid monomorphization
170184
codegen_ssa_invalid_no_sanitize = invalid argument for `no_sanitize`
171185
.note = expected one of: `address`, `cfi`, `hwaddress`, `kcfi`, `memory`, `memtag`, `shadow-call-stack`, or `thread`
172186
187+
codegen_ssa_invalid_repr_align_attr = invalid `repr(align)` attribute: {$msg}
188+
173189
codegen_ssa_invalid_windows_subsystem = invalid windows subsystem `{$subsystem}`, only `windows` and `console` are allowed
174190
175191
codegen_ssa_ld64_unimplemented_modifier = `as-needed` modifier not implemented yet for ld64
@@ -216,6 +232,8 @@ codegen_ssa_msvc_missing_linker = the msvc targets depend on the msvc linker but
216232
217233
codegen_ssa_multiple_external_func_decl = multiple declarations of external function `{$function}` from library `{$library_name}` have different calling conventions
218234
235+
codegen_ssa_multiple_instruction_set = cannot specify more than one instruction set
236+
219237
codegen_ssa_multiple_main_functions = entry symbol `main` declared multiple times
220238
.help = did you use `#[no_mangle]` on `fn main`? Use `#![no_main]` to suppress the usual Rust-generated entry point
221239
@@ -228,13 +246,20 @@ codegen_ssa_no_natvis_directory = error enumerating natvis directory: {$error}
228246
229247
codegen_ssa_no_saved_object_file = cached cgu {$cgu_name} should have an object file, but doesn't
230248
249+
codegen_ssa_null_on_export = `export_name` may not contain null characters
250+
251+
codegen_ssa_out_of_range_integer = integer value out of range
252+
.label = value must be between `0` and `255`
253+
231254
codegen_ssa_processing_dymutil_failed = processing debug info with `dsymutil` failed: {$status}
232255
.note = {$output}
233256
234257
codegen_ssa_read_file = failed to read file: {$message}
235258
236259
codegen_ssa_repair_vs_build_tools = the Visual Studio build tools may need to be repaired using the Visual Studio installer
237260
261+
codegen_ssa_requires_rust_abi = `#[track_caller]` requires Rust ABI
262+
238263
codegen_ssa_rlib_archive_build_failure = failed to build archive from rlib at `{$path}`: {$error}
239264
240265
codegen_ssa_rlib_incompatible_dependency_formats = `{$ty1}` and `{$ty2}` do not have equivalent dependency formats (`{$list1}` vs `{$list2}`)
@@ -355,6 +380,9 @@ codegen_ssa_unable_to_run_dsymutil = unable to run `dsymutil`: {$error}
355380
356381
codegen_ssa_unable_to_write_debugger_visualizer = Unable to write debugger visualizer file `{$path}`: {$error}
357382
383+
codegen_ssa_unexpected_parameter_name = unexpected parameter name
384+
.label = expected `{$prefix_nops}` or `{$entry_nops}`
385+
358386
codegen_ssa_unknown_archive_kind =
359387
Don't know how to build archive of type: {$kind}
360388
@@ -366,6 +394,8 @@ codegen_ssa_unknown_reuse_kind = unknown cgu-reuse-kind `{$kind}` specified
366394
367395
codegen_ssa_unsupported_arch = unsupported arch `{$arch}` for os `{$os}`
368396
397+
codegen_ssa_unsupported_instruction_set = target does not support `#[instruction_set]`
398+
369399
codegen_ssa_unsupported_link_self_contained = option `-C link-self-contained` is not supported on this target
370400
371401
codegen_ssa_use_cargo_directive = use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib)

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+29-92
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use rustc_ast::expand::autodiff_attrs::{
88
use rustc_ast::{MetaItem, MetaItemInner, attr};
99
use rustc_attr_parsing::{InlineAttr, InstructionSetAttr, OptimizeAttr};
1010
use rustc_data_structures::fx::FxHashMap;
11-
use rustc_errors::codes::*;
12-
use rustc_errors::{DiagMessage, SubdiagMessage, struct_span_code_err};
1311
use rustc_hir::def::DefKind;
1412
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
1513
use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
@@ -222,13 +220,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
222220
&& let Some(fn_sig) = fn_sig()
223221
&& fn_sig.skip_binder().abi() != ExternAbi::Rust
224222
{
225-
struct_span_code_err!(
226-
tcx.dcx(),
227-
attr.span,
228-
E0737,
229-
"`#[track_caller]` requires Rust ABI"
230-
)
231-
.emit();
223+
tcx.dcx().emit_err(errors::RequiresRustAbi { span: attr.span });
232224
}
233225
if is_closure
234226
&& !tcx.features().closure_track_caller()
@@ -249,13 +241,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
249241
if s.as_str().contains('\0') {
250242
// `#[export_name = ...]` will be converted to a null-terminated string,
251243
// so it may not contain any null characters.
252-
struct_span_code_err!(
253-
tcx.dcx(),
254-
attr.span,
255-
E0648,
256-
"`export_name` may not contain null characters"
257-
)
258-
.emit();
244+
tcx.dcx().emit_err(errors::NullOnExport { span: attr.span });
259245
}
260246
codegen_fn_attrs.export_name = Some(s);
261247
mixed_export_name_no_mangle_lint_state.track_export_name(attr.span);
@@ -379,13 +365,9 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
379365
match segments.as_slice() {
380366
[sym::arm, sym::a32] | [sym::arm, sym::t32] => {
381367
if !tcx.sess.target.has_thumb_interworking {
382-
struct_span_code_err!(
383-
tcx.dcx(),
384-
attr.span,
385-
E0779,
386-
"target does not support `#[instruction_set]`"
387-
)
388-
.emit();
368+
tcx.dcx().emit_err(errors::UnsuportedInstructionSet {
369+
span: attr.span,
370+
});
389371
None
390372
} else if segments[1] == sym::a32 {
391373
Some(InstructionSetAttr::ArmA32)
@@ -396,35 +378,19 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
396378
}
397379
}
398380
_ => {
399-
struct_span_code_err!(
400-
tcx.dcx(),
401-
attr.span,
402-
E0779,
403-
"invalid instruction set specified",
404-
)
405-
.emit();
381+
tcx.dcx().emit_err(errors::InvalidInstructionSet {
382+
span: attr.span,
383+
});
406384
None
407385
}
408386
}
409387
}
410388
[] => {
411-
struct_span_code_err!(
412-
tcx.dcx(),
413-
attr.span,
414-
E0778,
415-
"`#[instruction_set]` requires an argument"
416-
)
417-
.emit();
389+
tcx.dcx().emit_err(errors::BareInstructionSet { span: attr.span });
418390
None
419391
}
420392
_ => {
421-
struct_span_code_err!(
422-
tcx.dcx(),
423-
attr.span,
424-
E0779,
425-
"cannot specify more than one instruction set"
426-
)
427-
.emit();
393+
tcx.dcx().emit_err(errors::MultipleInstructionSet { span: attr.span });
428394
None
429395
}
430396
})
@@ -436,14 +402,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
436402
{
437403
rustc_attr_parsing::parse_alignment(&literal.kind)
438404
.map_err(|msg| {
439-
struct_span_code_err!(
440-
tcx.dcx(),
441-
literal.span,
442-
E0589,
443-
"invalid `repr(align)` attribute: {}",
444-
msg
445-
)
446-
.emit();
405+
tcx.dcx()
406+
.emit_err(errors::InvalidReprAlignAttr { span: literal.span, msg });
447407
})
448408
.ok()
449409
} else {
@@ -456,58 +416,38 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
456416
let mut entry = None;
457417
for item in l {
458418
let Some(meta_item) = item.meta_item() else {
459-
tcx.dcx().span_err(item.span(), "expected name value pair");
419+
tcx.dcx().emit_err(errors::ExpectedNameValuePair { span: item.span() });
460420
continue;
461421
};
462422

463423
let Some(name_value_lit) = meta_item.name_value_literal() else {
464-
tcx.dcx().span_err(item.span(), "expected name value pair");
424+
tcx.dcx().emit_err(errors::ExpectedNameValuePair { span: item.span() });
465425
continue;
466426
};
467427

468-
fn emit_error_with_label(
469-
tcx: TyCtxt<'_>,
470-
span: Span,
471-
error: impl Into<DiagMessage>,
472-
label: impl Into<SubdiagMessage>,
473-
) {
474-
let mut err: rustc_errors::Diag<'_, _> =
475-
tcx.dcx().struct_span_err(span, error);
476-
err.span_label(span, label);
477-
err.emit();
478-
}
479-
480428
let attrib_to_write = match meta_item.name_or_empty() {
481429
sym::prefix_nops => &mut prefix,
482430
sym::entry_nops => &mut entry,
483431
_ => {
484-
emit_error_with_label(
485-
tcx,
486-
item.span(),
487-
"unexpected parameter name",
488-
format!("expected {} or {}", sym::prefix_nops, sym::entry_nops),
489-
);
432+
tcx.dcx().emit_err(errors::UnexpectedParameterName {
433+
span: item.span(),
434+
prefix_nops: sym::prefix_nops,
435+
entry_nops: sym::entry_nops,
436+
});
490437
continue;
491438
}
492439
};
493440

494441
let rustc_ast::LitKind::Int(val, _) = name_value_lit.kind else {
495-
emit_error_with_label(
496-
tcx,
497-
name_value_lit.span,
498-
"invalid literal value",
499-
"value must be an integer between `0` and `255`",
500-
);
442+
tcx.dcx().emit_err(errors::InvalidLiteralValue {
443+
span: name_value_lit.span,
444+
});
501445
continue;
502446
};
503447

504448
let Ok(val) = val.get().try_into() else {
505-
emit_error_with_label(
506-
tcx,
507-
name_value_lit.span,
508-
"integer value out of range",
509-
"value must be between `0` and `255`",
510-
);
449+
tcx.dcx()
450+
.emit_err(errors::OutOfRangeInteger { span: name_value_lit.span });
511451
continue;
512452
};
513453

@@ -540,16 +480,14 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
540480
} else if let Some(ref items) = attr.meta_item_list() {
541481
inline_span = Some(attr.span);
542482
if items.len() != 1 {
543-
struct_span_code_err!(tcx.dcx(), attr.span, E0534, "expected one argument").emit();
483+
tcx.dcx().emit_err(errors::ExpectedOneArgument { span: attr.span });
544484
InlineAttr::None
545485
} else if list_contains_name(items, sym::always) {
546486
InlineAttr::Always
547487
} else if list_contains_name(items, sym::never) {
548488
InlineAttr::Never
549489
} else {
550-
struct_span_code_err!(tcx.dcx(), items[0].span(), E0535, "invalid argument")
551-
.with_help("valid inline arguments are `always` and `never`")
552-
.emit();
490+
tcx.dcx().emit_err(errors::InvalidArgument { span: items[0].span() });
553491

554492
InlineAttr::None
555493
}
@@ -583,14 +521,13 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
583521
if !attr.has_name(sym::optimize) {
584522
return ia;
585523
}
586-
let err = |sp, s| struct_span_code_err!(tcx.dcx(), sp, E0722, "{}", s).emit();
587524
if attr.is_word() {
588-
err(attr.span, "expected one argument");
525+
tcx.dcx().emit_err(errors::ExpectedOneArgumentOptimize { span: attr.span });
589526
ia
590527
} else if let Some(ref items) = attr.meta_item_list() {
591528
inline_span = Some(attr.span);
592529
if items.len() != 1 {
593-
err(attr.span, "expected one argument");
530+
tcx.dcx().emit_err(errors::ExpectedOneArgumentOptimize { span: attr.span });
594531
OptimizeAttr::Default
595532
} else if list_contains_name(items, sym::size) {
596533
OptimizeAttr::Size
@@ -599,7 +536,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
599536
} else if list_contains_name(items, sym::none) {
600537
OptimizeAttr::DoNotOptimize
601538
} else {
602-
err(items[0].span(), "invalid argument");
539+
tcx.dcx().emit_err(errors::InvalidArgumentOptimize { span: items[0].span() });
603540
OptimizeAttr::Default
604541
}
605542
} else {

0 commit comments

Comments
 (0)