Skip to content

Commit b14916b

Browse files
Rollup merge of rust-lang#151848 - clubby789:rustc-mir-port, r=JonathanBrouwer
Port `rustc_mir` to attribute parser Tracking issue: rust-lang#131229
2 parents 1407149 + b668057 commit b14916b

File tree

13 files changed

+186
-161
lines changed

13 files changed

+186
-161
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4337,11 +4337,11 @@ dependencies = [
43374337
"polonius-engine",
43384338
"regex",
43394339
"rustc_abi",
4340-
"rustc_ast",
43414340
"rustc_data_structures",
43424341
"rustc_errors",
43434342
"rustc_fluent_macro",
43444343
"rustc_graphviz",
4344+
"rustc_hir",
43454345
"rustc_index",
43464346
"rustc_macros",
43474347
"rustc_middle",

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
use std::path::PathBuf;
2+
13
use rustc_ast::{LitIntType, LitKind, MetaItemLit};
2-
use rustc_hir::attrs::RustcLayoutType;
4+
use rustc_hir::attrs::{BorrowckGraphvizFormatKind, RustcLayoutType, RustcMirKind};
35
use rustc_session::errors;
46

57
use super::prelude::*;
@@ -357,7 +359,6 @@ impl<S: Stage> CombineAttributeParser<S> for RustcLayoutParser {
357359

358360
const TEMPLATE: AttributeTemplate =
359361
template!(List: &["abi", "align", "size", "homogenous_aggregate", "debug"]);
360-
361362
fn extend(
362363
cx: &mut AcceptContext<'_, '_, S>,
363364
args: &ArgParser,
@@ -397,6 +398,94 @@ impl<S: Stage> CombineAttributeParser<S> for RustcLayoutParser {
397398
}
398399
}
399400

401+
pub(crate) struct RustcMirParser;
402+
403+
impl<S: Stage> CombineAttributeParser<S> for RustcMirParser {
404+
const PATH: &[rustc_span::Symbol] = &[sym::rustc_mir];
405+
406+
type Item = RustcMirKind;
407+
408+
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcMir(items);
409+
410+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
411+
Allow(Target::Fn),
412+
Allow(Target::Method(MethodKind::Inherent)),
413+
Allow(Target::Method(MethodKind::TraitImpl)),
414+
Allow(Target::Method(MethodKind::Trait { body: false })),
415+
Allow(Target::Method(MethodKind::Trait { body: true })),
416+
]);
417+
418+
const TEMPLATE: AttributeTemplate = template!(List: &["arg1, arg2, ..."]);
419+
420+
fn extend(
421+
cx: &mut AcceptContext<'_, '_, S>,
422+
args: &ArgParser,
423+
) -> impl IntoIterator<Item = Self::Item> {
424+
let Some(list) = args.list() else {
425+
cx.expected_list(cx.attr_span, args);
426+
return ThinVec::new();
427+
};
428+
429+
list.mixed()
430+
.filter_map(|arg| arg.meta_item())
431+
.filter_map(|mi| {
432+
if let Some(ident) = mi.ident() {
433+
match ident.name {
434+
sym::rustc_peek_maybe_init => Some(RustcMirKind::PeekMaybeInit),
435+
sym::rustc_peek_maybe_uninit => Some(RustcMirKind::PeekMaybeUninit),
436+
sym::rustc_peek_liveness => Some(RustcMirKind::PeekLiveness),
437+
sym::stop_after_dataflow => Some(RustcMirKind::StopAfterDataflow),
438+
sym::borrowck_graphviz_postflow => {
439+
let Some(nv) = mi.args().name_value() else {
440+
cx.expected_name_value(
441+
mi.span(),
442+
Some(sym::borrowck_graphviz_postflow),
443+
);
444+
return None;
445+
};
446+
let Some(path) = nv.value_as_str() else {
447+
cx.expected_string_literal(nv.value_span, None);
448+
return None;
449+
};
450+
let path = PathBuf::from(path.to_string());
451+
if path.file_name().is_some() {
452+
Some(RustcMirKind::BorrowckGraphvizPostflow { path })
453+
} else {
454+
cx.expected_filename_literal(nv.value_span);
455+
None
456+
}
457+
}
458+
sym::borrowck_graphviz_format => {
459+
let Some(nv) = mi.args().name_value() else {
460+
cx.expected_name_value(
461+
mi.span(),
462+
Some(sym::borrowck_graphviz_format),
463+
);
464+
return None;
465+
};
466+
let Some(format) = nv.value_as_ident() else {
467+
cx.expected_identifier(nv.value_span);
468+
return None;
469+
};
470+
match format.name {
471+
sym::two_phase => Some(RustcMirKind::BorrowckGraphvizFormat {
472+
format: BorrowckGraphvizFormatKind::TwoPhase,
473+
}),
474+
_ => {
475+
cx.expected_specific_argument(format.span, &[sym::two_phase]);
476+
None
477+
}
478+
}
479+
}
480+
_ => None,
481+
}
482+
} else {
483+
None
484+
}
485+
})
486+
.collect()
487+
}
488+
}
400489
pub(crate) struct RustcNonConstTraitMethodParser;
401490

402491
impl<S: Stage> NoArgsAttributeParser<S> for RustcNonConstTraitMethodParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ use crate::attributes::rustc_internal::{
8181
RustcLayoutScalarValidRangeEndParser, RustcLayoutScalarValidRangeStartParser,
8282
RustcLegacyConstGenericsParser, RustcLintOptDenyFieldAccessParser, RustcLintOptTyParser,
8383
RustcLintQueryInstabilityParser, RustcLintUntrackedQueryInformationParser, RustcMainParser,
84-
RustcMustImplementOneOfParser, RustcNeverReturnsNullPointerParser,
84+
RustcMirParser, RustcMustImplementOneOfParser, RustcNeverReturnsNullPointerParser,
8585
RustcNoImplicitAutorefsParser, RustcNonConstTraitMethodParser, RustcNounwindParser,
8686
RustcObjectLifetimeDefaultParser, RustcOffloadKernelParser, RustcScalableVectorParser,
8787
RustcSimdMonomorphizeLaneLimitParser,
@@ -202,6 +202,7 @@ attribute_parsers!(
202202
Combine<LinkParser>,
203203
Combine<ReprParser>,
204204
Combine<RustcLayoutParser>,
205+
Combine<RustcMirParser>,
205206
Combine<TargetFeatureParser>,
206207
Combine<UnstableFeatureBoundParser>,
207208
// tidy-alphabetical-end
@@ -517,6 +518,11 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
517518
)
518519
}
519520

521+
/// Error that a filename string literal was expected.
522+
pub(crate) fn expected_filename_literal(&self, span: Span) {
523+
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedFilenameLiteral);
524+
}
525+
520526
pub(crate) fn expected_integer_literal(&self, span: Span) -> ErrorGuaranteed {
521527
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedIntegerLiteral)
522528
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ pub(crate) enum AttributeParseErrorReason<'a> {
524524
ExpectedStringLiteral {
525525
byte_string: Option<Span>,
526526
},
527+
ExpectedFilenameLiteral,
527528
ExpectedIntegerLiteral,
528529
ExpectedIntegerLiteralInRange {
529530
lower_bound: isize,
@@ -597,6 +598,9 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError<'_> {
597598
diag.span_label(self.span, "expected a string literal here");
598599
}
599600
}
601+
AttributeParseErrorReason::ExpectedFilenameLiteral => {
602+
diag.span_label(self.span, "expected a filename string literal here");
603+
}
600604
AttributeParseErrorReason::ExpectedIntegerLiteral => {
601605
diag.span_label(self.span, "expected an integer literal here");
602606
}

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,21 @@ pub enum RustcLayoutType {
699699
Debug,
700700
}
701701

702+
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute, PartialEq, Eq)]
703+
pub enum RustcMirKind {
704+
PeekMaybeInit,
705+
PeekMaybeUninit,
706+
PeekLiveness,
707+
StopAfterDataflow,
708+
BorrowckGraphvizPostflow { path: PathBuf },
709+
BorrowckGraphvizFormat { format: BorrowckGraphvizFormatKind },
710+
}
711+
712+
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute, PartialEq, Eq)]
713+
pub enum BorrowckGraphvizFormatKind {
714+
TwoPhase,
715+
}
716+
702717
/// Represents parsed *built-in* inert attributes.
703718
///
704719
/// ## Overview
@@ -1090,6 +1105,9 @@ pub enum AttributeKind {
10901105
/// Represents `#[rustc_main]`.
10911106
RustcMain,
10921107

1108+
/// Represents `#[rustc_mir]`.
1109+
RustcMir(ThinVec<RustcMirKind>),
1110+
10931111
/// Represents `#[rustc_must_implement_one_of]`
10941112
RustcMustImplementOneOf { attr_span: Span, fn_names: ThinVec<Ident> },
10951113

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl AttributeKind {
122122
RustcLintUntrackedQueryInformation => Yes,
123123
RustcMacroTransparency(..) => Yes,
124124
RustcMain => No,
125+
RustcMir(..) => Yes,
125126
RustcMustImplementOneOf { .. } => No,
126127
RustcNeverReturnsNullPointer => Yes,
127128
RustcNoImplicitAutorefs => Yes,

compiler/rustc_hir/src/attrs/pretty_printing.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::num::NonZero;
22
use std::ops::Deref;
3+
use std::path::PathBuf;
34

45
use rustc_abi::Align;
56
use rustc_ast::attr::data_structures::CfgEntry;
@@ -96,7 +97,15 @@ impl<T: PrintAttribute> PrintAttribute for FxIndexMap<T, Span> {
9697
p.word("]");
9798
}
9899
}
100+
impl PrintAttribute for PathBuf {
101+
fn should_render(&self) -> bool {
102+
true
103+
}
99104

105+
fn print_attribute(&self, p: &mut Printer) {
106+
p.word(self.display().to_string());
107+
}
108+
}
100109
macro_rules! print_skip {
101110
($($t: ty),* $(,)?) => {$(
102111
impl PrintAttribute for $t {

compiler/rustc_mir_dataflow/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ edition = "2024"
88
polonius-engine = "0.13.0"
99
regex = "1"
1010
rustc_abi = { path = "../rustc_abi" }
11-
rustc_ast = { path = "../rustc_ast" }
1211
rustc_data_structures = { path = "../rustc_data_structures" }
1312
rustc_errors = { path = "../rustc_errors" }
1413
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1514
rustc_graphviz = { path = "../rustc_graphviz" }
15+
rustc_hir = { path = "../rustc_hir" }
1616
rustc_index = { path = "../rustc_index" }
1717
rustc_macros = { path = "../rustc_macros" }
1818
rustc_middle = { path = "../rustc_middle" }
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
mir_dataflow_duplicate_values_for =
2-
duplicate values for `{$name}`
3-
4-
mir_dataflow_path_must_end_in_filename =
5-
path must end in a filename
6-
71
mir_dataflow_peek_argument_not_a_local =
82
rustc_peek: argument was not a local
93
@@ -19,11 +13,5 @@ mir_dataflow_peek_must_be_not_temporary =
1913
mir_dataflow_peek_must_be_place_or_ref_place =
2014
rustc_peek: argument expression must be either `place` or `&place`
2115
22-
mir_dataflow_requires_an_argument =
23-
`{$name}` requires an argument
24-
2516
mir_dataflow_stop_after_dataflow_ended_compilation =
2617
stop_after_dataflow ended compilation
27-
28-
mir_dataflow_unknown_formatter =
29-
unknown formatter

compiler/rustc_mir_dataflow/src/errors.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,5 @@
11
use rustc_macros::Diagnostic;
2-
use rustc_span::{Span, Symbol};
3-
4-
#[derive(Diagnostic)]
5-
#[diag(mir_dataflow_path_must_end_in_filename)]
6-
pub(crate) struct PathMustEndInFilename {
7-
#[primary_span]
8-
pub span: Span,
9-
}
10-
11-
#[derive(Diagnostic)]
12-
#[diag(mir_dataflow_unknown_formatter)]
13-
pub(crate) struct UnknownFormatter {
14-
#[primary_span]
15-
pub span: Span,
16-
}
17-
18-
#[derive(Diagnostic)]
19-
#[diag(mir_dataflow_duplicate_values_for)]
20-
pub(crate) struct DuplicateValuesFor {
21-
#[primary_span]
22-
pub span: Span,
23-
pub name: Symbol,
24-
}
25-
26-
#[derive(Diagnostic)]
27-
#[diag(mir_dataflow_requires_an_argument)]
28-
pub(crate) struct RequiresAnArgument {
29-
#[primary_span]
30-
pub span: Span,
31-
pub name: Symbol,
32-
}
2+
use rustc_span::Span;
333

344
#[derive(Diagnostic)]
355
#[diag(mir_dataflow_stop_after_dataflow_ended_compilation)]

0 commit comments

Comments
 (0)