Skip to content

Commit 99743c0

Browse files
committed
Port rustc_mir to attribute parser
1 parent 35a31ba commit 99743c0

13 files changed

Lines changed: 190 additions & 162 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4336,11 +4336,11 @@ dependencies = [
43364336
"polonius-engine",
43374337
"regex",
43384338
"rustc_abi",
4339-
"rustc_ast",
43404339
"rustc_data_structures",
43414340
"rustc_errors",
43424341
"rustc_fluent_macro",
43434342
"rustc_graphviz",
4343+
"rustc_hir",
43444344
"rustc_index",
43454345
"rustc_macros",
43464346
"rustc_middle",

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

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

47
use super::prelude::*;
@@ -329,3 +332,92 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcOffloadKernelParser {
329332
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Fn)]);
330333
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcOffloadKernel;
331334
}
335+
336+
pub(crate) struct RustcMirParser;
337+
338+
impl<S: Stage> CombineAttributeParser<S> for RustcMirParser {
339+
const PATH: &[rustc_span::Symbol] = &[sym::rustc_mir];
340+
341+
type Item = RustcMirKind;
342+
343+
const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcMir(items);
344+
345+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
346+
Allow(Target::Fn),
347+
Allow(Target::Method(MethodKind::Inherent)),
348+
Allow(Target::Method(MethodKind::TraitImpl)),
349+
Allow(Target::Method(MethodKind::Trait { body: false })),
350+
Allow(Target::Method(MethodKind::Trait { body: true })),
351+
]);
352+
353+
const TEMPLATE: AttributeTemplate = template!(List: &["arg1, arg2, ..."]);
354+
355+
fn extend(
356+
cx: &mut AcceptContext<'_, '_, S>,
357+
args: &ArgParser,
358+
) -> impl IntoIterator<Item = Self::Item> {
359+
let Some(list) = args.list() else {
360+
cx.expected_list(cx.attr_span, args);
361+
return ThinVec::new();
362+
};
363+
364+
list.mixed()
365+
.filter_map(|arg| arg.meta_item())
366+
.filter_map(|mi| {
367+
if let Some(ident) = mi.ident() {
368+
match ident.name {
369+
sym::rustc_peek_maybe_init => Some(RustcMirKind::PeekMaybeInit),
370+
sym::rustc_peek_maybe_uninit => Some(RustcMirKind::PeekMaybeUninit),
371+
sym::rustc_peek_liveness => Some(RustcMirKind::PeekLiveness),
372+
sym::stop_after_dataflow => Some(RustcMirKind::StopAfterDataflow),
373+
sym::borrowck_graphviz_postflow => {
374+
let Some(nv) = mi.args().name_value() else {
375+
cx.expected_name_value(
376+
mi.span(),
377+
Some(sym::borrowck_graphviz_postflow),
378+
);
379+
return None;
380+
};
381+
let Some(path) = nv.value_as_str() else {
382+
cx.expected_string_literal(nv.value_span, None);
383+
return None;
384+
};
385+
let path = PathBuf::from(path.to_string());
386+
if path.file_name().is_some() {
387+
Some(RustcMirKind::BorrowckGraphvizPostflow { path })
388+
} else {
389+
cx.expected_filename_literal(nv.value_span);
390+
None
391+
}
392+
}
393+
sym::borrowck_graphviz_format => {
394+
let Some(nv) = mi.args().name_value() else {
395+
cx.expected_name_value(
396+
mi.span(),
397+
Some(sym::borrowck_graphviz_format),
398+
);
399+
return None;
400+
};
401+
let Some(format) = nv.value_as_ident() else {
402+
cx.expected_identifier(nv.value_span);
403+
return None;
404+
};
405+
match format.name {
406+
sym::two_phase => Some(RustcMirKind::BorrowckGraphvizFormat {
407+
format: BorrowckGraphvizFormatKind::TwoPhase,
408+
}),
409+
_ => {
410+
cx.expected_specific_argument(format.span, &[sym::two_phase]);
411+
None
412+
}
413+
}
414+
}
415+
_ => None,
416+
}
417+
} else {
418+
None
419+
}
420+
})
421+
.collect()
422+
}
423+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ use crate::attributes::rustc_internal::{
7878
RustcHasIncoherentInherentImplsParser, RustcLayoutScalarValidRangeEndParser,
7979
RustcLayoutScalarValidRangeStartParser, RustcLegacyConstGenericsParser,
8080
RustcLintOptDenyFieldAccessParser, RustcLintOptTyParser, RustcLintQueryInstabilityParser,
81-
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcMustImplementOneOfParser,
82-
RustcNeverReturnsNullPointerParser, RustcNoImplicitAutorefsParser, RustcNounwindParser,
83-
RustcObjectLifetimeDefaultParser, RustcOffloadKernelParser, RustcScalableVectorParser,
84-
RustcSimdMonomorphizeLaneLimitParser,
81+
RustcLintUntrackedQueryInformationParser, RustcMainParser, RustcMirParser,
82+
RustcMustImplementOneOfParser, RustcNeverReturnsNullPointerParser,
83+
RustcNoImplicitAutorefsParser, RustcNounwindParser, RustcObjectLifetimeDefaultParser,
84+
RustcOffloadKernelParser, RustcScalableVectorParser, RustcSimdMonomorphizeLaneLimitParser,
8585
};
8686
use crate::attributes::semantics::MayDangleParser;
8787
use crate::attributes::stability::{
@@ -198,6 +198,7 @@ attribute_parsers!(
198198
Combine<ForceTargetFeatureParser>,
199199
Combine<LinkParser>,
200200
Combine<ReprParser>,
201+
Combine<RustcMirParser>,
201202
Combine<TargetFeatureParser>,
202203
Combine<UnstableFeatureBoundParser>,
203204
// tidy-alphabetical-end
@@ -510,6 +511,11 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
510511
)
511512
}
512513

514+
/// Error that a filename string literal was expected.
515+
pub(crate) fn expected_filename_literal(&self, span: Span) {
516+
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedFilenameLiteral);
517+
}
518+
513519
pub(crate) fn expected_integer_literal(&self, span: Span) -> ErrorGuaranteed {
514520
self.emit_parse_error(span, AttributeParseErrorReason::ExpectedIntegerLiteral)
515521
}

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
@@ -690,6 +690,21 @@ impl IntoDiagArg for CrateType {
690690
}
691691
}
692692

693+
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute, PartialEq, Eq)]
694+
pub enum RustcMirKind {
695+
PeekMaybeInit,
696+
PeekMaybeUninit,
697+
PeekLiveness,
698+
StopAfterDataflow,
699+
BorrowckGraphvizPostflow { path: PathBuf },
700+
BorrowckGraphvizFormat { format: BorrowckGraphvizFormatKind },
701+
}
702+
703+
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute, PartialEq, Eq)]
704+
pub enum BorrowckGraphvizFormatKind {
705+
TwoPhase,
706+
}
707+
693708
/// Represents parsed *built-in* inert attributes.
694709
///
695710
/// ## Overview
@@ -1075,6 +1090,9 @@ pub enum AttributeKind {
10751090
/// Represents `#[rustc_main]`.
10761091
RustcMain,
10771092

1093+
/// Represents `#[rustc_mir]`.
1094+
RustcMir(ThinVec<RustcMirKind>),
1095+
10781096
/// Represents `#[rustc_must_implement_one_of]`
10791097
RustcMustImplementOneOf { attr_span: Span, fn_names: ThinVec<Ident> },
10801098

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ impl AttributeKind {
120120
RustcLintUntrackedQueryInformation => Yes,
121121
RustcMacroTransparency(..) => Yes,
122122
RustcMain => No,
123+
RustcMir(..) => Yes,
123124
RustcMustImplementOneOf { .. } => No,
124125
RustcNeverReturnsNullPointer => Yes,
125126
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)