From 8d246b01020edc47c966bc62d9d607c9f480fb07 Mon Sep 17 00:00:00 2001 From: Florian Schmiderer Date: Wed, 26 Jun 2024 19:11:25 +0200 Subject: [PATCH] Updated diagnostic messages --- .../rustc_codegen_ssa/src/codegen_attrs.rs | 52 +++++++++++++------ compiler/rustc_interface/src/tests.rs | 3 +- .../patchable-function-entry-attribute.rs | 8 +-- .../patchable-function-entry-attribute.stderr | 14 ++--- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/codegen_attrs.rs b/compiler/rustc_codegen_ssa/src/codegen_attrs.rs index 84041811bbbf2..bd0c4dc225c05 100644 --- a/compiler/rustc_codegen_ssa/src/codegen_attrs.rs +++ b/compiler/rustc_codegen_ssa/src/codegen_attrs.rs @@ -1,7 +1,6 @@ use rustc_ast::{ast, attr, MetaItemKind, NestedMetaItem}; use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr}; -use rustc_data_structures::packed::Pu128; -use rustc_errors::{codes::*, struct_span_code_err}; +use rustc_errors::{codes::*, struct_span_code_err, DiagMessage, SubdiagMessage}; use rustc_hir as hir; use rustc_hir::def::DefKind; 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; for item in l { 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; }; 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; }; + fn emit_error_with_label( + tcx: TyCtxt<'_>, + span: Span, + error: impl Into, + label: impl Into, + ) { + 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() { sym::prefix_nops => &mut prefix, sym::entry_nops => &mut entry, _ => { - tcx.dcx().span_err( + emit_error_with_label( + tcx, item.span(), - format!( - "Unexpected parameter name. Allowed names: {}, {}", - sym::prefix_nops, - sym::entry_nops - ), + "unexpected parameter name", + format!("expected {} or {}", sym::prefix_nops, sym::entry_nops), ); continue; } }; - let rustc_ast::LitKind::Int(Pu128(val @ 0..=255), _) = name_value_lit.kind - else { - tcx.dcx().span_err( + let rustc_ast::LitKind::Int(val, _) = name_value_lit.kind else { + emit_error_with_label( + tcx, + name_value_lit.span, + "invalid literal value", + "value must be an integer between `0` and `255`", + ); + continue; + }; + + let Ok(val) = val.get().try_into() else { + emit_error_with_label( + tcx, name_value_lit.span, - "Expected integer value between 0 and 255.", + "integer value out of range", + "value must be between `0` and `255`", ); continue; }; - *attrib_to_write = Some(val.try_into().unwrap()); + *attrib_to_write = Some(val); } 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( diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index e23d556ad4610..02322c9b2828f 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -815,7 +815,8 @@ fn test_unstable_options_tracking_hash() { tracked!(panic_in_drop, PanicStrategy::Abort); tracked!( 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!(polonius, Polonius::Legacy); diff --git a/tests/ui/patchable-function-entry/patchable-function-entry-attribute.rs b/tests/ui/patchable-function-entry/patchable-function-entry-attribute.rs index d7231ef416077..1e376c9ff3c1a 100644 --- a/tests/ui/patchable-function-entry/patchable-function-entry-attribute.rs +++ b/tests/ui/patchable-function-entry/patchable-function-entry-attribute.rs @@ -1,17 +1,17 @@ #![feature(patchable_function_entry)] 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() {} -#[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() {} #[patchable_function_entry]//~error: malformed `patchable_function_entry` attribute input 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() {} -#[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() {} diff --git a/tests/ui/patchable-function-entry/patchable-function-entry-attribute.stderr b/tests/ui/patchable-function-entry/patchable-function-entry-attribute.stderr index a270106925f44..d9710c6e6a2f5 100644 --- a/tests/ui/patchable-function-entry/patchable-function-entry-attribute.stderr +++ b/tests/ui/patchable-function-entry/patchable-function-entry-attribute.stderr @@ -4,25 +4,25 @@ error: malformed `patchable_function_entry` attribute input LL | #[patchable_function_entry] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 | 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 | 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 | 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 | LL | #[patchable_function_entry()]