Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4039,7 +4039,6 @@ name = "rustc_incremental"
version = "0.0.0"
dependencies = [
"rand 0.9.2",
"rustc_ast",
"rustc_data_structures",
"rustc_errors",
"rustc_fs_util",
Expand All @@ -4051,7 +4050,6 @@ dependencies = [
"rustc_serialize",
"rustc_session",
"rustc_span",
"thin-vec",
"tracing",
]

Expand Down
236 changes: 230 additions & 6 deletions compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use std::path::PathBuf;

use rustc_ast::{LitIntType, LitKind, MetaItemLit};
use rustc_hir::attrs::{BorrowckGraphvizFormatKind, RustcLayoutType, RustcMirKind};
use rustc_hir::attrs::{
BorrowckGraphvizFormatKind, RustcCleanAttribute, RustcCleanQueries, RustcLayoutType,
RustcMirKind,
};
use rustc_session::errors;
use rustc_span::Symbol;

use super::prelude::*;
use super::util::parse_single_integer;
use crate::session_diagnostics::RustcScalableVectorCountOutOfRange;
use crate::session_diagnostics::{AttributeRequiresOpt, RustcScalableVectorCountOutOfRange};

pub(crate) struct RustcMainParser;

Expand Down Expand Up @@ -234,7 +238,7 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcLintUntrackedQueryInformationPa
pub(crate) struct RustcObjectLifetimeDefaultParser;

impl<S: Stage> SingleAttributeParser<S> for RustcObjectLifetimeDefaultParser {
const PATH: &[rustc_span::Symbol] = &[sym::rustc_object_lifetime_default];
const PATH: &[Symbol] = &[sym::rustc_object_lifetime_default];
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
Expand Down Expand Up @@ -271,7 +275,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcSimdMonomorphizeLaneLimitParser
pub(crate) struct RustcScalableVectorParser;

impl<S: Stage> SingleAttributeParser<S> for RustcScalableVectorParser {
const PATH: &[rustc_span::Symbol] = &[sym::rustc_scalable_vector];
const PATH: &[Symbol] = &[sym::rustc_scalable_vector];
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
Expand Down Expand Up @@ -344,7 +348,7 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcOffloadKernelParser {
pub(crate) struct RustcLayoutParser;

impl<S: Stage> CombineAttributeParser<S> for RustcLayoutParser {
const PATH: &[rustc_span::Symbol] = &[sym::rustc_layout];
const PATH: &[Symbol] = &[sym::rustc_layout];

type Item = RustcLayoutType;

Expand Down Expand Up @@ -401,7 +405,7 @@ impl<S: Stage> CombineAttributeParser<S> for RustcLayoutParser {
pub(crate) struct RustcMirParser;

impl<S: Stage> CombineAttributeParser<S> for RustcMirParser {
const PATH: &[rustc_span::Symbol] = &[sym::rustc_mir];
const PATH: &[Symbol] = &[sym::rustc_mir];

type Item = RustcMirKind;

Expand Down Expand Up @@ -497,3 +501,223 @@ impl<S: Stage> NoArgsAttributeParser<S> for RustcNonConstTraitMethodParser {
]);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcNonConstTraitMethod;
}

pub(crate) struct RustcCleanParser;

impl<S: Stage> CombineAttributeParser<S> for RustcCleanParser {
const PATH: &[Symbol] = &[sym::rustc_clean];

type Item = RustcCleanAttribute;

const CONVERT: ConvertFn<Self::Item> = |items, _| AttributeKind::RustcClean(items);

const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
// tidy-alphabetical-start
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::Const),
Allow(Target::Enum),
Allow(Target::Expression),
Allow(Target::Field),
Allow(Target::Fn),
Allow(Target::ForeignMod),
Allow(Target::Impl { of_trait: false }),
Allow(Target::Impl { of_trait: true }),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: false })),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::TraitImpl)),
Allow(Target::Mod),
Allow(Target::Static),
Allow(Target::Struct),
Allow(Target::Trait),
Allow(Target::TyAlias),
Allow(Target::Union),
// tidy-alphabetical-end
]);

const TEMPLATE: AttributeTemplate =
template!(List: &[r#"cfg = "...", /*opt*/ label = "...", /*opt*/ except = "...""#]);

fn extend(
cx: &mut AcceptContext<'_, '_, S>,
args: &ArgParser,
) -> impl IntoIterator<Item = Self::Item> {
if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
}
let Some(list) = args.list() else {
cx.expected_list(cx.attr_span, args);
return None;
};
let mut except = None;
let mut loaded_from_disk = None;
let mut cfg = None;

for item in list.mixed() {
let Some((value, name)) =
item.meta_item().and_then(|m| Option::zip(m.args().name_value(), m.ident()))
else {
cx.expected_name_value(item.span(), None);
continue;
};
let value_span = value.value_span;
let Some(value) = value.value_as_str() else {
cx.expected_string_literal(value_span, None);
continue;
};
match name.name {
sym::cfg if cfg.is_some() => {
cx.duplicate_key(item.span(), sym::cfg);
}

sym::cfg => {
cfg = Some(value);
}
sym::except if except.is_some() => {
cx.duplicate_key(item.span(), sym::except);
}
sym::except => {
let entries =
value.as_str().split(',').map(|s| Symbol::intern(s.trim())).collect();
except = Some(RustcCleanQueries { entries, span: value_span });
}
sym::loaded_from_disk if loaded_from_disk.is_some() => {
cx.duplicate_key(item.span(), sym::loaded_from_disk);
}
sym::loaded_from_disk => {
let entries =
value.as_str().split(',').map(|s| Symbol::intern(s.trim())).collect();
loaded_from_disk = Some(RustcCleanQueries { entries, span: value_span });
}
_ => {
cx.expected_specific_argument(
name.span,
&[sym::cfg, sym::except, sym::loaded_from_disk],
);
}
}
}
let Some(cfg) = cfg else {
cx.expected_specific_argument(list.span, &[sym::cfg]);
return None;
};

Some(RustcCleanAttribute { span: cx.attr_span, cfg, except, loaded_from_disk })
}
}

pub(crate) struct RustcIfThisChangedParser;

impl<S: Stage> SingleAttributeParser<S> for RustcIfThisChangedParser {
const PATH: &[Symbol] = &[sym::rustc_if_this_changed];

const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;

const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;

const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
// tidy-alphabetical-start
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::Const),
Allow(Target::Enum),
Allow(Target::Expression),
Allow(Target::Field),
Allow(Target::Fn),
Allow(Target::ForeignMod),
Allow(Target::Impl { of_trait: false }),
Allow(Target::Impl { of_trait: true }),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: false })),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::TraitImpl)),
Allow(Target::Mod),
Allow(Target::Static),
Allow(Target::Struct),
Allow(Target::Trait),
Allow(Target::TyAlias),
Allow(Target::Union),
// tidy-alphabetical-end
]);

const TEMPLATE: AttributeTemplate = template!(Word, List: &["DepNode"]);

fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
}
match args {
ArgParser::NoArgs => Some(AttributeKind::RustcIfThisChanged(cx.attr_span, None)),
ArgParser::List(list) => {
let Some(item) = list.single() else {
cx.expected_single_argument(list.span);
return None;
};
let Some(ident) = item.meta_item().and_then(|item| item.ident()) else {
cx.expected_identifier(item.span());
return None;
};
Some(AttributeKind::RustcIfThisChanged(cx.attr_span, Some(ident.name)))
}
ArgParser::NameValue(_) => {
cx.expected_list_or_no_args(cx.inner_span);
None
}
}
}
}

pub(crate) struct RustcThenThisWouldNeedParser;

impl<S: Stage> CombineAttributeParser<S> for RustcThenThisWouldNeedParser {
const PATH: &[Symbol] = &[sym::rustc_then_this_would_need];
type Item = Ident;

const CONVERT: ConvertFn<Self::Item> =
|items, span| AttributeKind::RustcThenThisWouldNeed(span, items);
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
// tidy-alphabetical-start
Allow(Target::AssocConst),
Allow(Target::AssocTy),
Allow(Target::Const),
Allow(Target::Enum),
Allow(Target::Expression),
Allow(Target::Field),
Allow(Target::Fn),
Allow(Target::ForeignMod),
Allow(Target::Impl { of_trait: false }),
Allow(Target::Impl { of_trait: true }),
Allow(Target::Method(MethodKind::Inherent)),
Allow(Target::Method(MethodKind::Trait { body: false })),
Allow(Target::Method(MethodKind::Trait { body: true })),
Allow(Target::Method(MethodKind::TraitImpl)),
Allow(Target::Mod),
Allow(Target::Static),
Allow(Target::Struct),
Allow(Target::Trait),
Allow(Target::TyAlias),
Allow(Target::Union),
// tidy-alphabetical-end
]);

const TEMPLATE: AttributeTemplate = template!(List: &["DepNode"]);

fn extend(
cx: &mut AcceptContext<'_, '_, S>,
args: &ArgParser,
) -> impl IntoIterator<Item = Self::Item> {
if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
}
let Some(item) = args.list().and_then(|l| l.single()) else {
cx.expected_single_argument(cx.inner_span);
return None;
};
let Some(ident) = item.meta_item().and_then(|item| item.ident()) else {
cx.expected_identifier(item.span());
return None;
};
Some(ident)
}
}
3 changes: 3 additions & 0 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@ attribute_parsers!(
Combine<ForceTargetFeatureParser>,
Combine<LinkParser>,
Combine<ReprParser>,
Combine<RustcCleanParser>,
Combine<RustcLayoutParser>,
Combine<RustcMirParser>,
Combine<RustcThenThisWouldNeedParser>,
Combine<TargetFeatureParser>,
Combine<UnstableFeatureBoundParser>,
// tidy-alphabetical-end
Expand Down Expand Up @@ -191,6 +193,7 @@ attribute_parsers!(
Single<RustcAllocatorZeroedVariantParser>,
Single<RustcBuiltinMacroParser>,
Single<RustcForceInlineParser>,
Single<RustcIfThisChangedParser>,
Single<RustcLayoutScalarValidRangeEndParser>,
Single<RustcLayoutScalarValidRangeStartParser>,
Single<RustcLegacyConstGenericsParser>,
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_attr_parsing/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,14 @@ pub(crate) struct RustcScalableVectorCountOutOfRange {
pub n: u128,
}

#[derive(Diagnostic)]
#[diag("attribute requires {$opt} to be enabled")]
pub(crate) struct AttributeRequiresOpt {
#[primary_span]
pub span: Span,
pub opt: &'static str,
}

pub(crate) enum AttributeParseErrorReason<'a> {
ExpectedNoArgs,
ExpectedStringLiteral {
Expand Down
26 changes: 26 additions & 0 deletions compiler/rustc_hir/src/attrs/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,23 @@ pub enum BorrowckGraphvizFormatKind {
TwoPhase,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(HashStable_Generic, Encodable, Decodable, PrintAttribute)]
pub struct RustcCleanAttribute {
pub span: Span,
pub cfg: Symbol,
pub except: Option<RustcCleanQueries>,
pub loaded_from_disk: Option<RustcCleanQueries>,
}

/// Represents the `except=` or `loaded_from_disk=` argument of `#[rustc_clean]`
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(HashStable_Generic, Encodable, Decodable, PrintAttribute)]
pub struct RustcCleanQueries {
pub entries: ThinVec<Symbol>,
pub span: Span,
}

/// Represents parsed *built-in* inert attributes.
///
/// ## Overview
Expand Down Expand Up @@ -1022,6 +1039,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_builtin_macro]`.
RustcBuiltinMacro { builtin_name: Option<Symbol>, helper_attrs: ThinVec<Symbol>, span: Span },

/// Represents `#[rustc_clean]`
RustcClean(ThinVec<RustcCleanAttribute>),

/// Represents `#[rustc_coherence_is_core]`
RustcCoherenceIsCore(Span),

Expand Down Expand Up @@ -1077,6 +1097,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_hidden_type_of_opaques]`
RustcHiddenTypeOfOpaques,

/// Represents `#[rustc_if_this_changed]`
RustcIfThisChanged(Span, Option<Symbol>),

/// Represents `#[rustc_layout]`
RustcLayout(ThinVec<RustcLayoutType>),

Expand Down Expand Up @@ -1178,6 +1201,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_std_internal_symbol]`.
RustcStdInternalSymbol(Span),

/// Represents `#[rustc_then_this_would_need]`
RustcThenThisWouldNeed(Span, ThinVec<Ident>),

/// Represents `#[rustc_unsafe_specialization_marker]`.
RustcUnsafeSpecializationMarker(Span),

Expand Down
Loading
Loading