Skip to content

Commit 79be7a0

Browse files
authored
Rollup merge of #151340 - port_patchable_function_entry, r=JonathanBrouwer
Port `#[patchable_function_entry]` to attr parser This is the last codegen attr (see #151335 and #151336)! Tracking issue: #131229 currently this PR is rebased on #151336 to make CI pass for the last commit to see the actual changes in this PR you can look [here](https://github.com/rust-lang/rust/pull/151340/changes/3e731f7e84301a898a36e46ee5e4845ff9bda98a..55111fb468808b733e97170a841217a67666ac33)
2 parents 43d2006 + a0b3ee2 commit 79be7a0

13 files changed

Lines changed: 412 additions & 357 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,3 +717,100 @@ 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+
cx.expected_integer_literal(name_value_lit.value_span);
791+
continue;
792+
};
793+
794+
let Ok(val) = val.get().try_into() else {
795+
errored = true;
796+
cx.expected_integer_literal_in_range(
797+
name_value_lit.value_span,
798+
u8::MIN as isize,
799+
u8::MAX as isize,
800+
);
801+
continue;
802+
};
803+
804+
*attrib_to_write = Some(val);
805+
}
806+
807+
if errored {
808+
None
809+
} else {
810+
Some(AttributeKind::PatchableFunctionEntry {
811+
prefix: prefix.unwrap_or(0),
812+
entry: entry.unwrap_or(0),
813+
})
814+
}
815+
}
816+
}

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>,
@@ -503,6 +504,18 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
503504
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedIntegerLiteral)
504505
}
505506

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

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

0 commit comments

Comments
 (0)