Skip to content

Commit

Permalink
Updated diagnostic messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nebulark committed Jun 27, 2024
1 parent 7c56398 commit 8d246b0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
52 changes: 36 additions & 16 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<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() {
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(
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down

0 comments on commit 8d246b0

Please sign in to comment.