Skip to content

Commit bb87d3c

Browse files
committed
wip: ui tests for attribute parsing
1 parent be54f6c commit bb87d3c

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +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;
34
use rustc_errors::{codes::*, struct_span_code_err};
45
use rustc_hir as hir;
56
use rustc_hir::def::DefKind;
@@ -489,7 +490,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
489490
tcx.dcx().span_err(
490491
item.span(),
491492
format!(
492-
"Unexpected name. Allowed names: {}, {}",
493+
"Unexpected parameter name. Allowed names: {}, {}",
493494
sym::prefix_nops,
494495
sym::entry_nops
495496
),
@@ -498,23 +499,16 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
498499
}
499500
};
500501

501-
let rustc_ast::LitKind::Int(val, _) = name_value_lit.kind else {
502+
let rustc_ast::LitKind::Int(Pu128(val @ 0..=255), _) = name_value_lit.kind
503+
else {
502504
tcx.dcx().span_err(
503505
name_value_lit.span,
504506
"Expected integer value between 0 and 255.",
505507
);
506508
continue;
507509
};
508510

509-
let Ok(val) = val.get().try_into() else {
510-
tcx.dcx().span_err(
511-
name_value_lit.span,
512-
"Integer value outside range between 0 and 255.",
513-
);
514-
continue;
515-
};
516-
517-
*attrib_to_write = Some(val);
511+
*attrib_to_write = Some(val.try_into().unwrap());
518512
}
519513

520514
if let (None, None) = (prefix, entry) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(patchable_function_entry)]
2+
fn main() {}
3+
4+
#[patchable_function_entry(prefix_nops = 256, entry_nops = 0)]
5+
pub fn too_high_pnops() {}
6+
7+
#[patchable_function_entry(prefix_nops = "stringvalue", entry_nops = 0)]
8+
pub fn non_int_nop() {}
9+
10+
#[patchable_function_entry]
11+
pub fn malformed_attribute() {}
12+
13+
#[patchable_function_entry(prefix_nops = 10, something = 0)]
14+
pub fn unexpected_parameter_name() {}
15+
16+
#[patchable_function_entry()]
17+
pub fn no_parameters_given() {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: malformed `patchable_function_entry` attribute input
2+
--> $DIR/patchable-function-entry-attribute.rs:10:1
3+
|
4+
LL | #[patchable_function_entry]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[patchable_function_entry(prefix_nops = m, entry_nops = n)]`
6+
7+
error: Expected integer value between 0 and 255.
8+
--> $DIR/patchable-function-entry-attribute.rs:4:42
9+
|
10+
LL | #[patchable_function_entry(prefix_nops = 256, entry_nops = 0)]
11+
| ^^^
12+
13+
error: Expected integer value between 0 and 255.
14+
--> $DIR/patchable-function-entry-attribute.rs:7:42
15+
|
16+
LL | #[patchable_function_entry(prefix_nops = "stringvalue", entry_nops = 0)]
17+
| ^^^^^^^^^^^^^
18+
19+
error: Unexpected parameter name. Allowed names: prefix_nops, entry_nops
20+
--> $DIR/patchable-function-entry-attribute.rs:13:46
21+
|
22+
LL | #[patchable_function_entry(prefix_nops = 10, something = 0)]
23+
| ^^^^^^^^^^^^^
24+
25+
error: Must specify at least one parameter.
26+
--> $DIR/patchable-function-entry-attribute.rs:16:1
27+
|
28+
LL | #[patchable_function_entry()]
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
31+
error: aborting due to 5 previous errors
32+

0 commit comments

Comments
 (0)