1
Fork 0

Updated diagnostic messages

This commit is contained in:
Florian Schmiderer 2024-06-26 19:11:25 +02:00
parent 7c56398e91
commit 8d246b0102
4 changed files with 49 additions and 28 deletions

View file

@ -1,7 +1,6 @@
use rustc_ast::{ast, attr, MetaItemKind, NestedMetaItem}; use rustc_ast::{ast, attr, MetaItemKind, NestedMetaItem};
use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr}; use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr};
use rustc_data_structures::packed::Pu128; use rustc_errors::{codes::*, struct_span_code_err, DiagMessage, SubdiagMessage};
use rustc_errors::{codes::*, struct_span_code_err};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
@ -472,45 +471,66 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
let mut entry = None; let mut entry = None;
for item in l { for item in l {
let Some(meta_item) = item.meta_item() else { let Some(meta_item) = item.meta_item() else {
tcx.dcx().span_err(item.span(), "Expected name value pair."); tcx.dcx().span_err(item.span(), "expected name value pair");
continue; continue;
}; };
let Some(name_value_lit) = meta_item.name_value_literal() else { let Some(name_value_lit) = meta_item.name_value_literal() else {
tcx.dcx().span_err(item.span(), "Expected name value pair."); tcx.dcx().span_err(item.span(), "expected name value pair");
continue; continue;
}; };
fn emit_error_with_label(
tcx: TyCtxt<'_>,
span: Span,
error: impl Into<DiagMessage>,
label: impl Into<SubdiagMessage>,
) {
let mut err: rustc_errors::Diag<'_, _> =
tcx.dcx().struct_span_err(span, error);
err.span_label(span, label);
err.emit();
}
let attrib_to_write = match meta_item.name_or_empty() { let attrib_to_write = match meta_item.name_or_empty() {
sym::prefix_nops => &mut prefix, sym::prefix_nops => &mut prefix,
sym::entry_nops => &mut entry, sym::entry_nops => &mut entry,
_ => { _ => {
tcx.dcx().span_err( emit_error_with_label(
tcx,
item.span(), item.span(),
format!( "unexpected parameter name",
"Unexpected parameter name. Allowed names: {}, {}", format!("expected {} or {}", sym::prefix_nops, sym::entry_nops),
sym::prefix_nops,
sym::entry_nops
),
); );
continue; continue;
} }
}; };
let rustc_ast::LitKind::Int(Pu128(val @ 0..=255), _) = name_value_lit.kind let rustc_ast::LitKind::Int(val, _) = name_value_lit.kind else {
else { emit_error_with_label(
tcx.dcx().span_err( tcx,
name_value_lit.span, name_value_lit.span,
"Expected integer value between 0 and 255.", "invalid literal value",
"value must be an integer between `0` and `255`",
); );
continue; continue;
}; };
*attrib_to_write = Some(val.try_into().unwrap()); let Ok(val) = val.get().try_into() else {
emit_error_with_label(
tcx,
name_value_lit.span,
"integer value out of range",
"value must be between `0` and `255`",
);
continue;
};
*attrib_to_write = Some(val);
} }
if let (None, None) = (prefix, entry) { if let (None, None) = (prefix, entry) {
tcx.dcx().span_err(attr.span, "Must specify at least one parameter."); tcx.dcx().span_err(attr.span, "must specify at least one parameter");
} }
Some(PatchableFunctionEntry::from_prefix_and_entry( Some(PatchableFunctionEntry::from_prefix_and_entry(

View file

@ -815,7 +815,8 @@ fn test_unstable_options_tracking_hash() {
tracked!(panic_in_drop, PanicStrategy::Abort); tracked!(panic_in_drop, PanicStrategy::Abort);
tracked!( tracked!(
patchable_function_entry, patchable_function_entry,
PatchableFunctionEntry::from_total_and_prefix_nops(10, 5).expect("total >= prefix") PatchableFunctionEntry::from_total_and_prefix_nops(10, 5)
.expect("total must be greater than or equal to prefix")
); );
tracked!(plt, Some(true)); tracked!(plt, Some(true));
tracked!(polonius, Polonius::Legacy); tracked!(polonius, Polonius::Legacy);

View file

@ -1,17 +1,17 @@
#![feature(patchable_function_entry)] #![feature(patchable_function_entry)]
fn main() {} fn main() {}
#[patchable_function_entry(prefix_nops = 256, entry_nops = 0)]//~error: Expected integer value between 0 and 255. #[patchable_function_entry(prefix_nops = 256, entry_nops = 0)]//~error: integer value out of range
pub fn too_high_pnops() {} pub fn too_high_pnops() {}
#[patchable_function_entry(prefix_nops = "stringvalue", entry_nops = 0)]//~error: Expected integer value between 0 and 255. #[patchable_function_entry(prefix_nops = "stringvalue", entry_nops = 0)]//~error: invalid literal value
pub fn non_int_nop() {} pub fn non_int_nop() {}
#[patchable_function_entry]//~error: malformed `patchable_function_entry` attribute input #[patchable_function_entry]//~error: malformed `patchable_function_entry` attribute input
pub fn malformed_attribute() {} pub fn malformed_attribute() {}
#[patchable_function_entry(prefix_nops = 10, something = 0)]//~error: Unexpected parameter name. Allowed names: prefix_nops, entry_nops #[patchable_function_entry(prefix_nops = 10, something = 0)]//~error: unexpected parameter name
pub fn unexpected_parameter_name() {} pub fn unexpected_parameter_name() {}
#[patchable_function_entry()]//~error: Must specify at least one parameter. #[patchable_function_entry()]//~error: must specify at least one parameter
pub fn no_parameters_given() {} pub fn no_parameters_given() {}

View file

@ -4,25 +4,25 @@ error: malformed `patchable_function_entry` attribute input
LL | #[patchable_function_entry] LL | #[patchable_function_entry]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[patchable_function_entry(prefix_nops = m, entry_nops = n)]` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[patchable_function_entry(prefix_nops = m, entry_nops = n)]`
error: Expected integer value between 0 and 255. error: integer value out of range
--> $DIR/patchable-function-entry-attribute.rs:4:42 --> $DIR/patchable-function-entry-attribute.rs:4:42
| |
LL | #[patchable_function_entry(prefix_nops = 256, entry_nops = 0)] LL | #[patchable_function_entry(prefix_nops = 256, entry_nops = 0)]
| ^^^ | ^^^ value must be between `0` and `255`
error: Expected integer value between 0 and 255. error: invalid literal value
--> $DIR/patchable-function-entry-attribute.rs:7:42 --> $DIR/patchable-function-entry-attribute.rs:7:42
| |
LL | #[patchable_function_entry(prefix_nops = "stringvalue", entry_nops = 0)] LL | #[patchable_function_entry(prefix_nops = "stringvalue", entry_nops = 0)]
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^ value must be an integer between `0` and `255`
error: Unexpected parameter name. Allowed names: prefix_nops, entry_nops error: unexpected parameter name
--> $DIR/patchable-function-entry-attribute.rs:13:46 --> $DIR/patchable-function-entry-attribute.rs:13:46
| |
LL | #[patchable_function_entry(prefix_nops = 10, something = 0)] LL | #[patchable_function_entry(prefix_nops = 10, something = 0)]
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^ expected prefix_nops or entry_nops
error: Must specify at least one parameter. error: must specify at least one parameter
--> $DIR/patchable-function-entry-attribute.rs:16:1 --> $DIR/patchable-function-entry-attribute.rs:16:1
| |
LL | #[patchable_function_entry()] LL | #[patchable_function_entry()]