Skip to content

Commit 8d246b0

Browse files
committed
Updated diagnostic messages
1 parent 7c56398 commit 8d246b0

File tree

4 files changed

+49
-28
lines changed

4 files changed

+49
-28
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+36-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc_ast::{ast, attr, MetaItemKind, NestedMetaItem};
22
use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr};
3-
use rustc_data_structures::packed::Pu128;
4-
use rustc_errors::{codes::*, struct_span_code_err};
3+
use rustc_errors::{codes::*, struct_span_code_err, DiagMessage, SubdiagMessage};
54
use rustc_hir as hir;
65
use rustc_hir::def::DefKind;
76
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
@@ -472,45 +471,66 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
472471
let mut entry = None;
473472
for item in l {
474473
let Some(meta_item) = item.meta_item() else {
475-
tcx.dcx().span_err(item.span(), "Expected name value pair.");
474+
tcx.dcx().span_err(item.span(), "expected name value pair");
476475
continue;
477476
};
478477

479478
let Some(name_value_lit) = meta_item.name_value_literal() else {
480-
tcx.dcx().span_err(item.span(), "Expected name value pair.");
479+
tcx.dcx().span_err(item.span(), "expected name value pair");
481480
continue;
482481
};
483482

483+
fn emit_error_with_label(
484+
tcx: TyCtxt<'_>,
485+
span: Span,
486+
error: impl Into<DiagMessage>,
487+
label: impl Into<SubdiagMessage>,
488+
) {
489+
let mut err: rustc_errors::Diag<'_, _> =
490+
tcx.dcx().struct_span_err(span, error);
491+
err.span_label(span, label);
492+
err.emit();
493+
}
494+
484495
let attrib_to_write = match meta_item.name_or_empty() {
485496
sym::prefix_nops => &mut prefix,
486497
sym::entry_nops => &mut entry,
487498
_ => {
488-
tcx.dcx().span_err(
499+
emit_error_with_label(
500+
tcx,
489501
item.span(),
490-
format!(
491-
"Unexpected parameter name. Allowed names: {}, {}",
492-
sym::prefix_nops,
493-
sym::entry_nops
494-
),
502+
"unexpected parameter name",
503+
format!("expected {} or {}", sym::prefix_nops, sym::entry_nops),
495504
);
496505
continue;
497506
}
498507
};
499508

500-
let rustc_ast::LitKind::Int(Pu128(val @ 0..=255), _) = name_value_lit.kind
501-
else {
502-
tcx.dcx().span_err(
509+
let rustc_ast::LitKind::Int(val, _) = name_value_lit.kind else {
510+
emit_error_with_label(
511+
tcx,
512+
name_value_lit.span,
513+
"invalid literal value",
514+
"value must be an integer between `0` and `255`",
515+
);
516+
continue;
517+
};
518+
519+
let Ok(val) = val.get().try_into() else {
520+
emit_error_with_label(
521+
tcx,
503522
name_value_lit.span,
504-
"Expected integer value between 0 and 255.",
523+
"integer value out of range",
524+
"value must be between `0` and `255`",
505525
);
506526
continue;
507527
};
508528

509-
*attrib_to_write = Some(val.try_into().unwrap());
529+
*attrib_to_write = Some(val);
510530
}
511531

512532
if let (None, None) = (prefix, entry) {
513-
tcx.dcx().span_err(attr.span, "Must specify at least one parameter.");
533+
tcx.dcx().span_err(attr.span, "must specify at least one parameter");
514534
}
515535

516536
Some(PatchableFunctionEntry::from_prefix_and_entry(

compiler/rustc_interface/src/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,8 @@ fn test_unstable_options_tracking_hash() {
815815
tracked!(panic_in_drop, PanicStrategy::Abort);
816816
tracked!(
817817
patchable_function_entry,
818-
PatchableFunctionEntry::from_total_and_prefix_nops(10, 5).expect("total >= prefix")
818+
PatchableFunctionEntry::from_total_and_prefix_nops(10, 5)
819+
.expect("total must be greater than or equal to prefix")
819820
);
820821
tracked!(plt, Some(true));
821822
tracked!(polonius, Polonius::Legacy);
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#![feature(patchable_function_entry)]
22
fn main() {}
33

4-
#[patchable_function_entry(prefix_nops = 256, entry_nops = 0)]//~error: Expected integer value between 0 and 255.
4+
#[patchable_function_entry(prefix_nops = 256, entry_nops = 0)]//~error: integer value out of range
55
pub fn too_high_pnops() {}
66

7-
#[patchable_function_entry(prefix_nops = "stringvalue", entry_nops = 0)]//~error: Expected integer value between 0 and 255.
7+
#[patchable_function_entry(prefix_nops = "stringvalue", entry_nops = 0)]//~error: invalid literal value
88
pub fn non_int_nop() {}
99

1010
#[patchable_function_entry]//~error: malformed `patchable_function_entry` attribute input
1111
pub fn malformed_attribute() {}
1212

13-
#[patchable_function_entry(prefix_nops = 10, something = 0)]//~error: Unexpected parameter name. Allowed names: prefix_nops, entry_nops
13+
#[patchable_function_entry(prefix_nops = 10, something = 0)]//~error: unexpected parameter name
1414
pub fn unexpected_parameter_name() {}
1515

16-
#[patchable_function_entry()]//~error: Must specify at least one parameter.
16+
#[patchable_function_entry()]//~error: must specify at least one parameter
1717
pub fn no_parameters_given() {}

tests/ui/patchable-function-entry/patchable-function-entry-attribute.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ error: malformed `patchable_function_entry` attribute input
44
LL | #[patchable_function_entry]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[patchable_function_entry(prefix_nops = m, entry_nops = n)]`
66

7-
error: Expected integer value between 0 and 255.
7+
error: integer value out of range
88
--> $DIR/patchable-function-entry-attribute.rs:4:42
99
|
1010
LL | #[patchable_function_entry(prefix_nops = 256, entry_nops = 0)]
11-
| ^^^
11+
| ^^^ value must be between `0` and `255`
1212

13-
error: Expected integer value between 0 and 255.
13+
error: invalid literal value
1414
--> $DIR/patchable-function-entry-attribute.rs:7:42
1515
|
1616
LL | #[patchable_function_entry(prefix_nops = "stringvalue", entry_nops = 0)]
17-
| ^^^^^^^^^^^^^
17+
| ^^^^^^^^^^^^^ value must be an integer between `0` and `255`
1818

19-
error: Unexpected parameter name. Allowed names: prefix_nops, entry_nops
19+
error: unexpected parameter name
2020
--> $DIR/patchable-function-entry-attribute.rs:13:46
2121
|
2222
LL | #[patchable_function_entry(prefix_nops = 10, something = 0)]
23-
| ^^^^^^^^^^^^^
23+
| ^^^^^^^^^^^^^ expected prefix_nops or entry_nops
2424

25-
error: Must specify at least one parameter.
25+
error: must specify at least one parameter
2626
--> $DIR/patchable-function-entry-attribute.rs:16:1
2727
|
2828
LL | #[patchable_function_entry()]

0 commit comments

Comments
 (0)