Skip to content

Commit 4c9dfab

Browse files
committed
Port #[patchable_function_entry] to attr parser
1 parent 3d087e6 commit 4c9dfab

14 files changed

Lines changed: 204 additions & 135 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,3 +717,98 @@ impl<S: Stage> NoArgsAttributeParser<S> for EiiForeignItemParser {
717717
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::ForeignFn)]);
718718
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::EiiForeignItem;
719719
}
720+
721+
pub(crate) struct PatchableFunctionEntryParser;
722+
723+
impl<S: Stage> SingleAttributeParser<S> for PatchableFunctionEntryParser {
724+
const PATH: &[Symbol] = &[sym::patchable_function_entry];
725+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
726+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
727+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
728+
const TEMPLATE: AttributeTemplate = template!(List: &["prefix_nops = m, entry_nops = n"]);
729+
730+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
731+
let Some(meta_item_list) = args.list() else {
732+
cx.expected_list(cx.attr_span, args);
733+
return None;
734+
};
735+
736+
let mut prefix = None;
737+
let mut entry = None;
738+
739+
if meta_item_list.len() == 0 {
740+
cx.expected_list(meta_item_list.span, args);
741+
return None;
742+
}
743+
744+
let mut errored = false;
745+
746+
for item in meta_item_list.mixed() {
747+
let Some(meta_item) = item.meta_item() else {
748+
errored = true;
749+
cx.expected_name_value(item.span(), None);
750+
continue;
751+
};
752+
753+
let Some(name_value_lit) = meta_item.args().name_value() else {
754+
errored = true;
755+
cx.expected_name_value(item.span(), None);
756+
continue;
757+
};
758+
759+
let attrib_to_write = match meta_item.ident().map(|ident| ident.name) {
760+
Some(sym::prefix_nops) => {
761+
// Duplicate prefixes are not allowed
762+
if prefix.is_some() {
763+
errored = true;
764+
cx.duplicate_key(meta_item.path().span(), sym::prefix_nops);
765+
continue;
766+
}
767+
&mut prefix
768+
}
769+
Some(sym::entry_nops) => {
770+
// Duplicate entries are not allowed
771+
if entry.is_some() {
772+
errored = true;
773+
cx.duplicate_key(meta_item.path().span(), sym::entry_nops);
774+
continue;
775+
}
776+
&mut entry
777+
}
778+
_ => {
779+
errored = true;
780+
cx.expected_specific_argument(
781+
meta_item.path().span(),
782+
&[sym::prefix_nops, sym::entry_nops],
783+
);
784+
continue;
785+
}
786+
};
787+
788+
let rustc_ast::LitKind::Int(val, _) = name_value_lit.value_as_lit().kind else {
789+
errored = true;
790+
791+
cx.expected_integer_literal(name_value_lit.value_span);
792+
continue;
793+
};
794+
795+
let Ok(val) = val.get().try_into() else {
796+
errored = true;
797+
cx.expected_integer_literal_in_range(
798+
name_value_lit.value_span,
799+
u8::MIN as isize,
800+
u8::MAX as isize,
801+
);
802+
continue;
803+
};
804+
805+
*attrib_to_write = Some(val);
806+
}
807+
808+
if errored {
809+
None
810+
} else {
811+
Some(AttributeKind::PatchableFunctionEntry { prefix: prefix?, entry: entry? })
812+
}
813+
}
814+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use crate::attributes::cfi_encoding::CfiEncodingParser;
2323
use crate::attributes::codegen_attrs::{
2424
ColdParser, CoverageParser, EiiForeignItemParser, ExportNameParser, ForceTargetFeatureParser,
2525
NakedParser, NoMangleParser, ObjcClassParser, ObjcSelectorParser, OptimizeParser,
26-
RustcPassIndirectlyInNonRusticAbisParser, SanitizeParser, TargetFeatureParser,
27-
ThreadLocalParser, TrackCallerParser, UsedParser,
26+
PatchableFunctionEntryParser, RustcPassIndirectlyInNonRusticAbisParser, SanitizeParser,
27+
TargetFeatureParser, ThreadLocalParser, TrackCallerParser, UsedParser,
2828
};
2929
use crate::attributes::confusables::ConfusablesParser;
3030
use crate::attributes::crate_level::{
@@ -223,6 +223,7 @@ attribute_parsers!(
223223
Single<ObjcClassParser>,
224224
Single<ObjcSelectorParser>,
225225
Single<OptimizeParser>,
226+
Single<PatchableFunctionEntryParser>,
226227
Single<PathAttributeParser>,
227228
Single<PatternComplexityLimitParser>,
228229
Single<ProcMacroDeriveParser>,
@@ -502,6 +503,18 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
502503
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedIntegerLiteral)
503504
}
504505

506+
pub(crate) fn expected_integer_literal_in_range(
507+
&self,
508+
span: Span,
509+
lower_bound: isize,
510+
upper_bound: isize,
511+
) -> ErrorGuaranteed {
512+
self.emit_parse_error(
513+
span,
514+
AttributeParseErrorReason::ExpectedIntegerLiteralInRange { lower_bound, upper_bound },
515+
)
516+
}
517+
505518
pub(crate) fn expected_list(&self, span: Span, args: &ArgParser) -> ErrorGuaranteed {
506519
let span = match args {
507520
ArgParser::NoArgs => span,

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
#![feature(decl_macro)]
8181
#![feature(if_let_guard)]
8282
#![feature(iter_intersperse)]
83+
#![feature(new_range_api)]
8384
#![recursion_limit = "256"]
8485
// tidy-alphabetical-end
8586

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,10 @@ pub(crate) enum AttributeParseErrorReason<'a> {
525525
byte_string: Option<Span>,
526526
},
527527
ExpectedIntegerLiteral,
528+
ExpectedIntegerLiteralInRange {
529+
lower_bound: isize,
530+
upper_bound: isize,
531+
},
528532
ExpectedAtLeastOneArgument,
529533
ExpectedSingleArgument,
530534
ExpectedList,
@@ -596,6 +600,17 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError<'_> {
596600
AttributeParseErrorReason::ExpectedIntegerLiteral => {
597601
diag.span_label(self.span, "expected an integer literal here");
598602
}
603+
AttributeParseErrorReason::ExpectedIntegerLiteralInRange {
604+
lower_bound,
605+
upper_bound,
606+
} => {
607+
diag.span_label(
608+
self.span,
609+
format!(
610+
"expected an integer literal in the range of {lower_bound}..{upper_bound}"
611+
),
612+
);
613+
}
599614
AttributeParseErrorReason::ExpectedSingleArgument => {
600615
diag.span_label(self.span, "expected a single argument here");
601616
diag.code(E0805);

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ codegen_ssa_error_creating_remark_dir = failed to create remark directory: {$err
4848
codegen_ssa_error_writing_def_file =
4949
error writing .DEF file: {$error}
5050
51-
codegen_ssa_expected_name_value_pair = expected name value pair
52-
5351
codegen_ssa_extern_funcs_not_found = some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
5452
5553
codegen_ssa_extract_bundled_libs_archive_member = failed to get data from archive member '{$rlib}': {$error}
@@ -90,9 +88,6 @@ codegen_ssa_incorrect_cgu_reuse_type =
9088
9189
codegen_ssa_insufficient_vs_code_product = VS Code is a different product, and is not sufficient.
9290
93-
codegen_ssa_invalid_literal_value = invalid literal value
94-
.label = value must be an integer between `0` and `255`
95-
9691
codegen_ssa_invalid_monomorphization_basic_float_type = invalid monomorphization of `{$name}` intrinsic: expected basic float type, found `{$ty}`
9792
9893
codegen_ssa_invalid_monomorphization_basic_integer_or_ptr_type = invalid monomorphization of `{$name}` intrinsic: expected basic integer or pointer type, found `{$ty}`
@@ -225,9 +220,6 @@ codegen_ssa_no_natvis_directory = error enumerating natvis directory: {$error}
225220
226221
codegen_ssa_no_saved_object_file = cached cgu {$cgu_name} should have an object file, but doesn't
227222
228-
codegen_ssa_out_of_range_integer = integer value out of range
229-
.label = value must be between `0` and `255`
230-
231223
codegen_ssa_processing_dymutil_failed = processing debug info with `dsymutil` failed: {$status}
232224
.note = {$output}
233225
@@ -357,9 +349,6 @@ codegen_ssa_unable_to_run_dsymutil = unable to run `dsymutil`: {$error}
357349
358350
codegen_ssa_unable_to_write_debugger_visualizer = unable to write debugger visualizer file `{$path}`: {$error}
359351
360-
codegen_ssa_unexpected_parameter_name = unexpected parameter name
361-
.label = expected `{$prefix_nops}` or `{$entry_nops}`
362-
363352
codegen_ssa_unknown_archive_kind =
364353
don't know how to build archive of type: {$kind}
365354

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -47,59 +47,6 @@ fn try_fn_sig<'tcx>(
4747
}
4848
}
4949

50-
// FIXME(jdonszelmann): remove when patchable_function_entry becomes a parsed attr
51-
fn parse_patchable_function_entry(
52-
tcx: TyCtxt<'_>,
53-
attr: &Attribute,
54-
) -> Option<PatchableFunctionEntry> {
55-
attr.meta_item_list().and_then(|l| {
56-
let mut prefix = None;
57-
let mut entry = None;
58-
for item in l {
59-
let Some(meta_item) = item.meta_item() else {
60-
tcx.dcx().emit_err(errors::ExpectedNameValuePair { span: item.span() });
61-
continue;
62-
};
63-
64-
let Some(name_value_lit) = meta_item.name_value_literal() else {
65-
tcx.dcx().emit_err(errors::ExpectedNameValuePair { span: item.span() });
66-
continue;
67-
};
68-
69-
let attrib_to_write = match meta_item.name() {
70-
Some(sym::prefix_nops) => &mut prefix,
71-
Some(sym::entry_nops) => &mut entry,
72-
_ => {
73-
tcx.dcx().emit_err(errors::UnexpectedParameterName {
74-
span: item.span(),
75-
prefix_nops: sym::prefix_nops,
76-
entry_nops: sym::entry_nops,
77-
});
78-
continue;
79-
}
80-
};
81-
82-
let rustc_ast::LitKind::Int(val, _) = name_value_lit.kind else {
83-
tcx.dcx().emit_err(errors::InvalidLiteralValue { span: name_value_lit.span });
84-
continue;
85-
};
86-
87-
let Ok(val) = val.get().try_into() else {
88-
tcx.dcx().emit_err(errors::OutOfRangeInteger { span: name_value_lit.span });
89-
continue;
90-
};
91-
92-
*attrib_to_write = Some(val);
93-
}
94-
95-
if let (None, None) = (prefix, entry) {
96-
tcx.dcx().span_err(attr.span(), "must specify at least one parameter");
97-
}
98-
99-
Some(PatchableFunctionEntry::from_prefix_and_entry(prefix.unwrap_or(0), entry.unwrap_or(0)))
100-
})
101-
}
102-
10350
/// Spans that are collected when processing built-in attributes,
10451
/// that are useful for emitting diagnostics later.
10552
#[derive(Default)]
@@ -347,6 +294,10 @@ fn process_builtin_attrs(
347294
AttributeKind::RustcAllocatorZeroed => {
348295
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
349296
}
297+
AttributeKind::PatchableFunctionEntry { prefix, entry } => {
298+
codegen_fn_attrs.patchable_function_entry =
299+
Some(PatchableFunctionEntry::from_prefix_and_entry(*prefix, *entry));
300+
}
350301
_ => {}
351302
}
352303
}
@@ -357,10 +308,6 @@ fn process_builtin_attrs(
357308

358309
match name {
359310
sym::rustc_nounwind => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND,
360-
sym::patchable_function_entry => {
361-
codegen_fn_attrs.patchable_function_entry =
362-
parse_patchable_function_entry(tcx, attr);
363-
}
364311
sym::rustc_offload_kernel => {
365312
codegen_fn_attrs.flags |= CodegenFnAttrFlags::OFFLOAD_KERNEL
366313
}

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -136,39 +136,6 @@ pub(crate) struct RequiresRustAbi {
136136
pub span: Span,
137137
}
138138

139-
#[derive(Diagnostic)]
140-
#[diag(codegen_ssa_expected_name_value_pair)]
141-
pub(crate) struct ExpectedNameValuePair {
142-
#[primary_span]
143-
pub span: Span,
144-
}
145-
146-
#[derive(Diagnostic)]
147-
#[diag(codegen_ssa_unexpected_parameter_name)]
148-
pub(crate) struct UnexpectedParameterName {
149-
#[primary_span]
150-
#[label]
151-
pub span: Span,
152-
pub prefix_nops: Symbol,
153-
pub entry_nops: Symbol,
154-
}
155-
156-
#[derive(Diagnostic)]
157-
#[diag(codegen_ssa_invalid_literal_value)]
158-
pub(crate) struct InvalidLiteralValue {
159-
#[primary_span]
160-
#[label]
161-
pub span: Span,
162-
}
163-
164-
#[derive(Diagnostic)]
165-
#[diag(codegen_ssa_out_of_range_integer)]
166-
pub(crate) struct OutOfRangeInteger {
167-
#[primary_span]
168-
#[label]
169-
pub span: Span,
170-
}
171-
172139
#[derive(Diagnostic)]
173140
#[diag(codegen_ssa_copy_path_buf)]
174141
pub(crate) struct CopyPathBuf {

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,9 @@ pub enum AttributeKind {
879879
/// Represents `#[rustc_pass_by_value]` (used by the `rustc_pass_by_value` lint).
880880
PassByValue(Span),
881881

882+
/// Represents `#[patchable_function_entry]`
883+
PatchableFunctionEntry { prefix: u8, entry: u8 },
884+
882885
/// Represents `#[path]`
883886
Path(Symbol, Span),
884887

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl AttributeKind {
8888
Optimize(..) => No,
8989
ParenSugar(..) => No,
9090
PassByValue(..) => Yes,
91+
PatchableFunctionEntry { .. } => Yes,
9192
Path(..) => No,
9293
PatternComplexityLimit { .. } => No,
9394
PinV2(..) => Yes,

compiler/rustc_hir/src/attrs/pretty_printing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ macro_rules! print_tup {
171171

172172
print_tup!(A B C D E F G H);
173173
print_skip!(Span, (), ErrorGuaranteed);
174-
print_disp!(u16, u128, usize, bool, NonZero<u32>, Limit);
174+
print_disp!(u8, u16, u128, usize, bool, NonZero<u32>, Limit);
175175
print_debug!(
176176
Symbol,
177177
Ident,

0 commit comments

Comments
 (0)