Skip to content

Commit f36627d

Browse files
committed
Port #[patchable_function_entry] to attr parser
1 parent ba2a7d3 commit f36627d

14 files changed

Lines changed: 186 additions & 134 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,3 +717,78 @@ 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+
let mut errored = false;
740+
741+
if meta_item_list.len() < 2 {
742+
cx.expected_list_with_exact_num_args(2, meta_item_list.span);
743+
}
744+
for (i, item) in meta_item_list.mixed().enumerate() {
745+
if i == 2 {
746+
errored = true;
747+
continue;
748+
}
749+
let Some(meta_item) = item.meta_item() else {
750+
cx.expected_name_value(item.span(), None);
751+
continue;
752+
};
753+
754+
let Some(name_value_lit) = meta_item.args().name_value() else {
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) => &mut prefix,
761+
Some(sym::entry_nops) => &mut entry,
762+
_ => {
763+
cx.expected_specific_argument(
764+
meta_item.path().span(),
765+
&[sym::prefix_nops, sym::entry_nops],
766+
);
767+
continue;
768+
}
769+
};
770+
771+
let rustc_ast::LitKind::Int(val, _) = name_value_lit.value_as_lit().kind else {
772+
cx.expected_integer_literal(name_value_lit.value_span);
773+
continue;
774+
};
775+
776+
let Ok(val) = val.get().try_into() else {
777+
cx.expected_integer_literal_in_range(
778+
name_value_lit.value_span,
779+
u8::MIN as isize,
780+
u8::MAX as isize,
781+
);
782+
continue;
783+
};
784+
785+
*attrib_to_write = Some(val);
786+
}
787+
788+
if errored {
789+
None
790+
} else {
791+
Some(AttributeKind::PatchableFunctionEntry { prefix: prefix?, entry: entry? })
792+
}
793+
}
794+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 26 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::{
@@ -219,6 +219,7 @@ attribute_parsers!(
219219
Single<ObjcClassParser>,
220220
Single<ObjcSelectorParser>,
221221
Single<OptimizeParser>,
222+
Single<PatchableFunctionEntryParser>,
222223
Single<PathAttributeParser>,
223224
Single<PatternComplexityLimitParser>,
224225
Single<ProcMacroDeriveParser>,
@@ -492,6 +493,18 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
492493
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedIntegerLiteral)
493494
}
494495

496+
pub(crate) fn expected_integer_literal_in_range(
497+
&self,
498+
span: Span,
499+
lower_bound: isize,
500+
upper_bound: isize,
501+
) -> ErrorGuaranteed {
502+
self.emit_parse_error(
503+
span,
504+
AttributeParseErrorReason::ExpectedIntegerLiteralInRange { lower_bound, upper_bound },
505+
)
506+
}
507+
495508
pub(crate) fn expected_list(&self, span: Span, args: &ArgParser) -> ErrorGuaranteed {
496509
let span = match args {
497510
ArgParser::NoArgs => span,
@@ -512,6 +525,17 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
512525
)
513526
}
514527

528+
pub(crate) fn expected_list_with_exact_num_args(
529+
&self,
530+
args: usize,
531+
span: Span,
532+
) -> ErrorGuaranteed {
533+
self.emit_parse_error(
534+
span,
535+
AttributeParseErrorReason::ExpectedListWithExactNumArgs { args },
536+
)
537+
}
538+
515539
pub(crate) fn expected_list_or_no_args(&self, span: Span) -> ErrorGuaranteed {
516540
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedListOrNoArgs)
517541
}

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,20 @@ 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,
531535
ExpectedListOrNoArgs,
532536
ExpectedListWithNumArgsOrMore {
533537
args: usize,
534538
},
539+
ExpectedListWithExactNumArgs {
540+
args: usize,
541+
},
535542
ExpectedNameValueOrNoArgs,
536543
ExpectedNonEmptyStringLiteral,
537544
UnexpectedLiteral,
@@ -596,6 +603,17 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError<'_> {
596603
AttributeParseErrorReason::ExpectedIntegerLiteral => {
597604
diag.span_label(self.span, "expected an integer literal here");
598605
}
606+
AttributeParseErrorReason::ExpectedIntegerLiteralInRange {
607+
lower_bound,
608+
upper_bound,
609+
} => {
610+
diag.span_label(
611+
self.span,
612+
format!(
613+
"expected an integer literal in the range of {lower_bound}..{upper_bound}"
614+
),
615+
);
616+
}
599617
AttributeParseErrorReason::ExpectedSingleArgument => {
600618
diag.span_label(self.span, "expected a single argument here");
601619
diag.code(E0805);
@@ -612,6 +630,9 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError<'_> {
612630
AttributeParseErrorReason::ExpectedListWithNumArgsOrMore { args } => {
613631
diag.span_label(self.span, format!("expected {args} or more items"));
614632
}
633+
AttributeParseErrorReason::ExpectedListWithExactNumArgs { args } => {
634+
diag.span_label(self.span, format!("expected {args} items"));
635+
}
615636
AttributeParseErrorReason::ExpectedNameValueOrNoArgs => {
616637
diag.span_label(self.span, "didn't expect a list here");
617638
}

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)]
@@ -335,6 +282,10 @@ fn process_builtin_attrs(
335282
AttributeKind::InstructionSet(instruction_set) => {
336283
codegen_fn_attrs.instruction_set = Some(*instruction_set)
337284
}
285+
AttributeKind::PatchableFunctionEntry { prefix, entry } => {
286+
codegen_fn_attrs.patchable_function_entry =
287+
Some(PatchableFunctionEntry::from_prefix_and_entry(*prefix, *entry));
288+
}
338289
_ => {}
339290
}
340291
}
@@ -351,10 +302,6 @@ fn process_builtin_attrs(
351302
sym::rustc_allocator_zeroed => {
352303
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
353304
}
354-
sym::patchable_function_entry => {
355-
codegen_fn_attrs.patchable_function_entry =
356-
parse_patchable_function_entry(tcx, attr);
357-
}
358305
sym::rustc_offload_kernel => {
359306
codegen_fn_attrs.flags |= CodegenFnAttrFlags::OFFLOAD_KERNEL
360307
}

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
@@ -876,6 +876,9 @@ pub enum AttributeKind {
876876
/// Represents `#[rustc_pass_by_value]` (used by the `rustc_pass_by_value` lint).
877877
PassByValue(Span),
878878

879+
/// Represents `#[patchable_function_entry]`
880+
PatchableFunctionEntry { prefix: u8, entry: u8 },
881+
879882
/// Represents `#[path]`
880883
Path(Symbol, Span),
881884

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl AttributeKind {
8787
Optimize(..) => No,
8888
ParenSugar(..) => No,
8989
PassByValue(..) => Yes,
90+
PatchableFunctionEntry { .. } => Yes,
9091
Path(..) => No,
9192
PatternComplexityLimit { .. } => No,
9293
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)