1use std::iter;
4
5use rustc_ast::token::{Delimiter, Token, TokenKind};
6use rustc_ast::tokenstream::{
7 AttrTokenStream, AttrTokenTree, LazyAttrTokenStream, Spacing, TokenTree,
8};
9use rustc_ast::{
10 self as ast, AttrItemKind, AttrKind, AttrStyle, Attribute, DUMMY_NODE_ID, EarlyParsedAttribute,
11 HasAttrs, HasTokens, MetaItem, MetaItemInner, NodeId, NormalAttr,
12};
13use rustc_attr_parsing::parser::AllowExprMetavar;
14use rustc_attr_parsing::{
15 self as attr, AttributeParser, CFG_TEMPLATE, EvalConfigResult, ShouldEmit, eval_config_entry,
16 parse_cfg,
17};
18use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
19use rustc_errors::msg;
20use rustc_feature::{
21 ACCEPTED_LANG_FEATURES, EnabledLangFeature, EnabledLibFeature, Features, REMOVED_LANG_FEATURES,
22 UNSTABLE_LANG_FEATURES,
23};
24use rustc_hir::attrs::AttributeKind;
25use rustc_hir::{
26 Target, {self as hir},
27};
28use rustc_parse::parser::Recovery;
29use rustc_session::Session;
30use rustc_session::parse::feature_err;
31use rustc_span::{DUMMY_SP, STDLIB_STABLE_CRATES, Span, Symbol, sym};
32use tracing::instrument;
33
34use crate::errors::{
35 CrateNameInCfgAttr, CrateTypeInCfgAttr, FeatureNotAllowed, FeatureRemoved,
36 FeatureRemovedReason, InvalidCfg, RemoveExprNotSupported,
37};
38
39pub struct StripUnconfigured<'a> {
41 pub sess: &'a Session,
42 pub features: Option<&'a Features>,
43 pub config_tokens: bool,
47 pub lint_node_id: NodeId,
48}
49
50pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -> Features {
51 let mut features = Features::default();
52
53 if let Some(hir::Attribute::Parsed(AttributeKind::Feature(feature_idents, _))) =
54 AttributeParser::parse_limited(
55 sess,
56 krate_attrs,
57 sym::feature,
58 DUMMY_SP,
59 DUMMY_NODE_ID,
60 Some(&features),
61 )
62 {
63 for feature_ident in feature_idents {
64 if let Some(f) =
66 REMOVED_LANG_FEATURES.iter().find(|f| feature_ident.name == f.feature.name)
67 {
68 let pull_note = if let Some(pull) = f.pull {
69 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("; see <https://github.com/rust-lang/rust/pull/{0}> for more information",
pull))
})format!(
70 "; see <https://github.com/rust-lang/rust/pull/{pull}> for more information",
71 )
72 } else {
73 "".to_owned()
74 };
75 sess.dcx().emit_err(FeatureRemoved {
76 span: feature_ident.span,
77 reason: f.reason.map(|reason| FeatureRemovedReason { reason }),
78 removed_rustc_version: f.feature.since,
79 pull_note,
80 });
81 continue;
82 }
83
84 if let Some(f) = ACCEPTED_LANG_FEATURES.iter().find(|f| feature_ident.name == f.name) {
86 features.set_enabled_lang_feature(EnabledLangFeature {
87 gate_name: feature_ident.name,
88 attr_sp: feature_ident.span,
89 stable_since: Some(Symbol::intern(f.since)),
90 });
91 continue;
92 }
93
94 if let Some(allowed) = sess.opts.unstable_opts.allow_features.as_ref() {
98 if allowed.iter().all(|f| feature_ident.name.as_str() != f) {
99 sess.dcx().emit_err(FeatureNotAllowed {
100 span: feature_ident.span,
101 name: feature_ident.name,
102 });
103 continue;
104 }
105 }
106
107 if UNSTABLE_LANG_FEATURES.iter().find(|f| feature_ident.name == f.name).is_some() {
109 if features.internal(feature_ident.name)
114 && !STDLIB_STABLE_CRATES.contains(&crate_name)
115 {
116 sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
117 }
118
119 features.set_enabled_lang_feature(EnabledLangFeature {
120 gate_name: feature_ident.name,
121 attr_sp: feature_ident.span,
122 stable_since: None,
123 });
124 continue;
125 }
126
127 features.set_enabled_lib_feature(EnabledLibFeature {
130 gate_name: feature_ident.name,
131 attr_sp: feature_ident.span,
132 });
133
134 if features.internal(feature_ident.name) && !STDLIB_STABLE_CRATES.contains(&crate_name)
137 {
138 sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
139 }
140 }
141 }
142
143 features
144}
145
146pub fn pre_configure_attrs(sess: &Session, attrs: &[Attribute]) -> ast::AttrVec {
147 let strip_unconfigured = StripUnconfigured {
148 sess,
149 features: None,
150 config_tokens: false,
151 lint_node_id: ast::CRATE_NODE_ID,
152 };
153 attrs
154 .iter()
155 .flat_map(|attr| strip_unconfigured.process_cfg_attr(attr))
156 .take_while(|attr| {
157 !is_cfg(attr) || strip_unconfigured.cfg_true(attr, ShouldEmit::Nothing).as_bool()
158 })
159 .collect()
160}
161
162pub(crate) fn attr_into_trace(mut attr: Attribute, trace_name: Symbol) -> Attribute {
163 match &mut attr.kind {
164 AttrKind::Normal(normal) => {
165 let NormalAttr { item, tokens } = &mut **normal;
166 item.path.segments[0].ident.name = trace_name;
167 *tokens = Some(LazyAttrTokenStream::new_direct(AttrTokenStream::default()));
169 }
170 AttrKind::DocComment(..) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
171 }
172 attr
173}
174
175#[macro_export]
176macro_rules! configure {
177 ($this:ident, $node:ident) => {
178 match $this.configure($node) {
179 Some(node) => node,
180 None => return Default::default(),
181 }
182 };
183}
184
185impl<'a> StripUnconfigured<'a> {
186 pub fn configure<T: HasAttrs + HasTokens>(&self, mut node: T) -> Option<T> {
187 self.process_cfg_attrs(&mut node);
188 self.in_cfg(node.attrs()).then(|| {
189 self.try_configure_tokens(&mut node);
190 node
191 })
192 }
193
194 fn try_configure_tokens<T: HasTokens>(&self, node: &mut T) {
195 if self.config_tokens {
196 if let Some(Some(tokens)) = node.tokens_mut() {
197 let attr_stream = tokens.to_attr_token_stream();
198 *tokens = LazyAttrTokenStream::new_direct(self.configure_tokens(&attr_stream));
199 }
200 }
201 }
202
203 fn configure_tokens(&self, stream: &AttrTokenStream) -> AttrTokenStream {
208 fn can_skip(stream: &AttrTokenStream) -> bool {
209 stream.0.iter().all(|tree| match tree {
210 AttrTokenTree::AttrsTarget(_) => false,
211 AttrTokenTree::Token(..) => true,
212 AttrTokenTree::Delimited(.., inner) => can_skip(inner),
213 })
214 }
215
216 if can_skip(stream) {
217 return stream.clone();
218 }
219
220 let trees: Vec<_> = stream
221 .0
222 .iter()
223 .filter_map(|tree| match tree.clone() {
224 AttrTokenTree::AttrsTarget(mut target) => {
225 target.attrs.flat_map_in_place(|attr| self.process_cfg_attr(&attr));
227
228 if self.in_cfg(&target.attrs) {
229 target.tokens = LazyAttrTokenStream::new_direct(
230 self.configure_tokens(&target.tokens.to_attr_token_stream()),
231 );
232 Some(AttrTokenTree::AttrsTarget(target))
233 } else {
234 None
237 }
238 }
239 AttrTokenTree::Delimited(sp, spacing, delim, mut inner) => {
240 inner = self.configure_tokens(&inner);
241 Some(AttrTokenTree::Delimited(sp, spacing, delim, inner))
242 }
243 AttrTokenTree::Token(Token { kind, .. }, _) if kind.is_delim() => {
244 {
::core::panicking::panic_fmt(format_args!("Should be `AttrTokenTree::Delimited`, not delim tokens: {0:?}",
tree));
};panic!("Should be `AttrTokenTree::Delimited`, not delim tokens: {:?}", tree);
245 }
246 AttrTokenTree::Token(token, spacing) => Some(AttrTokenTree::Token(token, spacing)),
247 })
248 .collect();
249 AttrTokenStream::new(trees)
250 }
251
252 fn process_cfg_attrs<T: HasAttrs>(&self, node: &mut T) {
259 node.visit_attrs(|attrs| {
260 attrs.flat_map_in_place(|attr| self.process_cfg_attr(&attr));
261 });
262 }
263
264 fn process_cfg_attr(&self, attr: &Attribute) -> Vec<Attribute> {
265 if attr.has_name(sym::cfg_attr) {
266 self.expand_cfg_attr(attr, true)
267 } else {
268 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[attr.clone()]))vec![attr.clone()]
269 }
270 }
271
272 pub(crate) fn expand_cfg_attr(&self, cfg_attr: &Attribute, recursive: bool) -> Vec<Attribute> {
280 let mut trace_attr = cfg_attr.clone();
283 trace_attr.replace_args(AttrItemKind::Parsed(EarlyParsedAttribute::CfgAttrTrace));
284 let trace_attr = attr_into_trace(trace_attr, sym::cfg_attr_trace);
285
286 let Some((cfg_predicate, expanded_attrs)) =
287 rustc_attr_parsing::parse_cfg_attr(cfg_attr, &self.sess, self.features)
288 else {
289 return ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[trace_attr]))vec![trace_attr];
290 };
291
292 if expanded_attrs.is_empty() {
294 self.sess.psess.buffer_lint(
295 rustc_lint_defs::builtin::UNUSED_ATTRIBUTES,
296 cfg_attr.span,
297 ast::CRATE_NODE_ID,
298 crate::errors::CfgAttrNoAttributes,
299 );
300 }
301
302 if !attr::eval_config_entry(self.sess, &cfg_predicate).as_bool() {
303 return ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[trace_attr]))vec![trace_attr];
304 }
305
306 if recursive {
307 let expanded_attrs = expanded_attrs
311 .into_iter()
312 .flat_map(|item| self.process_cfg_attr(&self.expand_cfg_attr_item(cfg_attr, item)));
313 iter::once(trace_attr).chain(expanded_attrs).collect()
314 } else {
315 let expanded_attrs =
316 expanded_attrs.into_iter().map(|item| self.expand_cfg_attr_item(cfg_attr, item));
317 iter::once(trace_attr).chain(expanded_attrs).collect()
318 }
319 }
320
321 fn expand_cfg_attr_item(
322 &self,
323 cfg_attr: &Attribute,
324 (item, item_span): (ast::AttrItem, Span),
325 ) -> Attribute {
326 let mut orig_trees = cfg_attr.token_trees().into_iter();
330 let Some(TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _)) =
331 orig_trees.next()
332 else {
333 {
::core::panicking::panic_fmt(format_args!("Bad tokens for attribute {0:?}",
cfg_attr));
};panic!("Bad tokens for attribute {cfg_attr:?}");
334 };
335
336 let mut trees = if cfg_attr.style == AttrStyle::Inner {
338 let Some(TokenTree::Token(bang_token @ Token { kind: TokenKind::Bang, .. }, _)) =
339 orig_trees.next()
340 else {
341 {
::core::panicking::panic_fmt(format_args!("Bad tokens for attribute {0:?}",
cfg_attr));
};panic!("Bad tokens for attribute {cfg_attr:?}");
342 };
343 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[AttrTokenTree::Token(pound_token, Spacing::Joint),
AttrTokenTree::Token(bang_token, Spacing::JointHidden)]))vec![
344 AttrTokenTree::Token(pound_token, Spacing::Joint),
345 AttrTokenTree::Token(bang_token, Spacing::JointHidden),
346 ]
347 } else {
348 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[AttrTokenTree::Token(pound_token, Spacing::JointHidden)]))vec![AttrTokenTree::Token(pound_token, Spacing::JointHidden)]
349 };
350
351 let Some(TokenTree::Delimited(delim_span, delim_spacing, Delimiter::Bracket, _)) =
353 orig_trees.next()
354 else {
355 {
::core::panicking::panic_fmt(format_args!("Bad tokens for attribute {0:?}",
cfg_attr));
};panic!("Bad tokens for attribute {cfg_attr:?}");
356 };
357 trees.push(AttrTokenTree::Delimited(
358 delim_span,
359 delim_spacing,
360 Delimiter::Bracket,
361 item.tokens
362 .as_ref()
363 .unwrap_or_else(|| {
::core::panicking::panic_fmt(format_args!("Missing tokens for {0:?}",
item));
}panic!("Missing tokens for {item:?}"))
364 .to_attr_token_stream(),
365 ));
366
367 let tokens = Some(LazyAttrTokenStream::new_direct(AttrTokenStream::new(trees)));
368 let attr = ast::attr::mk_attr_from_item(
369 &self.sess.psess.attr_id_generator,
370 item,
371 tokens,
372 cfg_attr.style,
373 item_span,
374 );
375 if attr.has_name(sym::crate_type) {
376 self.sess.dcx().emit_err(CrateTypeInCfgAttr { span: attr.span });
377 }
378 if attr.has_name(sym::crate_name) {
379 self.sess.dcx().emit_err(CrateNameInCfgAttr { span: attr.span });
380 }
381 attr
382 }
383
384 fn in_cfg(&self, attrs: &[Attribute]) -> bool {
386 attrs.iter().all(|attr| {
387 !is_cfg(attr)
388 || self
389 .cfg_true(attr, ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed })
390 .as_bool()
391 })
392 }
393
394 pub(crate) fn cfg_true(&self, attr: &Attribute, emit_errors: ShouldEmit) -> EvalConfigResult {
395 let Some(cfg) = AttributeParser::parse_single(
396 self.sess,
397 attr,
398 attr.span,
399 self.lint_node_id,
400 Target::Crate,
402 self.features,
403 emit_errors,
404 parse_cfg,
405 &CFG_TEMPLATE,
406 AllowExprMetavar::Yes,
407 ) else {
408 return EvalConfigResult::True;
410 };
411
412 eval_config_entry(self.sess, &cfg)
413 }
414
415 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("maybe_emit_expr_attr_err",
"rustc_expand::config", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/config.rs"),
::tracing_core::__macro_support::Option::Some(416u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::config"),
::tracing_core::field::FieldSet::new(&["attr"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&attr)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
if self.features.is_some_and(|features|
!features.stmt_expr_attributes()) &&
!attr.span.allows_unstable(sym::stmt_expr_attributes) {
let mut err =
feature_err(&self.sess, sym::stmt_expr_attributes,
attr.span,
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("attributes on expressions are experimental")));
if attr.is_doc_comment() {
err.help(if attr.style == AttrStyle::Outer {
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`///` is used for outer documentation comments; for a plain comment, use `//`"))
} else {
rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`//!` is used for inner documentation comments; for a plain comment, use `//` by removing the `!` or inserting a space in between them: `// !`"))
});
}
err.emit();
}
}
}
}#[instrument(level = "trace", skip(self))]
417 pub(crate) fn maybe_emit_expr_attr_err(&self, attr: &Attribute) {
418 if self.features.is_some_and(|features| !features.stmt_expr_attributes())
419 && !attr.span.allows_unstable(sym::stmt_expr_attributes)
420 {
421 let mut err = feature_err(
422 &self.sess,
423 sym::stmt_expr_attributes,
424 attr.span,
425 msg!("attributes on expressions are experimental"),
426 );
427
428 if attr.is_doc_comment() {
429 err.help(if attr.style == AttrStyle::Outer {
430 msg!("`///` is used for outer documentation comments; for a plain comment, use `//`")
431 } else {
432 msg!("`//!` is used for inner documentation comments; for a plain comment, use `//` by removing the `!` or inserting a space in between them: `// !`")
433 });
434 }
435
436 err.emit();
437 }
438 }
439
440 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("configure_expr",
"rustc_expand::config", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_expand/src/config.rs"),
::tracing_core::__macro_support::Option::Some(440u32),
::tracing_core::__macro_support::Option::Some("rustc_expand::config"),
::tracing_core::field::FieldSet::new(&["expr",
"method_receiver"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&method_receiver as
&dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
if !method_receiver {
for attr in expr.attrs.iter() {
self.maybe_emit_expr_attr_err(attr);
}
}
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
self.sess.dcx().emit_err(RemoveExprNotSupported {
span: attr.span,
});
}
self.process_cfg_attrs(expr);
self.try_configure_tokens(&mut *expr);
}
}
}#[instrument(level = "trace", skip(self))]
441 pub fn configure_expr(&self, expr: &mut ast::Expr, method_receiver: bool) {
442 if !method_receiver {
443 for attr in expr.attrs.iter() {
444 self.maybe_emit_expr_attr_err(attr);
445 }
446 }
447
448 if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
456 self.sess.dcx().emit_err(RemoveExprNotSupported { span: attr.span });
457 }
458
459 self.process_cfg_attrs(expr);
460 self.try_configure_tokens(&mut *expr);
461 }
462}
463
464pub fn parse_cfg_old<'a>(meta_item: &'a MetaItem, sess: &Session) -> Option<&'a MetaItemInner> {
466 let span = meta_item.span;
467 match meta_item.meta_item_list() {
468 None => {
469 sess.dcx().emit_err(InvalidCfg::NotFollowedByParens { span });
470 None
471 }
472 Some([]) => {
473 sess.dcx().emit_err(InvalidCfg::NoPredicate { span });
474 None
475 }
476 Some([_, .., l]) => {
477 sess.dcx().emit_err(InvalidCfg::MultiplePredicates { span: l.span() });
478 None
479 }
480 Some([single]) => match single.meta_item_or_bool() {
481 Some(meta_item) => Some(meta_item),
482 None => {
483 sess.dcx().emit_err(InvalidCfg::PredicateLiteral { span: single.span() });
484 None
485 }
486 },
487 }
488}
489
490fn is_cfg(attr: &Attribute) -> bool {
491 attr.has_name(sym::cfg)
492}