Skip to content
Closed
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
3 changes: 3 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ expand_trailing_semi_macro = trailing semicolon in macro used in expression posi
.note1 = macro invocations at the end of a block are treated as expressions
.note2 = to ignore the value produced by the macro, add a semicolon after the invocation of `{$name}`

expand_type_const_in_cfg_attr =
`type_const` within an `#[cfg_attr]` attribute is forbidden

expand_unknown_macro_variable = unknown macro variable `{$name}`

expand_unsupported_key_value =
Expand Down
18 changes: 12 additions & 6 deletions compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use tracing::instrument;
use crate::errors::{
CrateNameInCfgAttr, CrateTypeInCfgAttr, FeatureNotAllowed, FeatureRemoved,
FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute, MalformedFeatureAttributeHelp,
RemoveExprNotSupported,
RemoveExprNotSupported, TypeConstInCfgAttr,
};

/// A folder that strips out items that do not belong in the current configuration.
Expand Down Expand Up @@ -319,11 +319,13 @@ impl<'a> StripUnconfigured<'a> {
// `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
let expanded_attrs = expanded_attrs
.into_iter()
.flat_map(|item| self.process_cfg_attr(&self.expand_cfg_attr_item(cfg_attr, item)));
.flat_map(|item| self.expand_cfg_attr_item(cfg_attr, item))
.flat_map(|attr| self.process_cfg_attr(&attr));
iter::once(trace_attr).chain(expanded_attrs).collect()
} else {
let expanded_attrs =
expanded_attrs.into_iter().map(|item| self.expand_cfg_attr_item(cfg_attr, item));
let expanded_attrs = expanded_attrs
.into_iter()
.flat_map(|item| self.expand_cfg_attr_item(cfg_attr, item));
iter::once(trace_attr).chain(expanded_attrs).collect()
}
}
Expand All @@ -332,7 +334,7 @@ impl<'a> StripUnconfigured<'a> {
&self,
cfg_attr: &Attribute,
(item, item_span): (ast::AttrItem, Span),
) -> Attribute {
) -> Option<Attribute> {
// Convert `#[cfg_attr(pred, attr)]` to `#[attr]`.

// Use the `#` from `#[cfg_attr(pred, attr)]` in the result `#[attr]`.
Expand Down Expand Up @@ -388,7 +390,11 @@ impl<'a> StripUnconfigured<'a> {
if attr.has_name(sym::crate_name) {
self.sess.dcx().emit_err(CrateNameInCfgAttr { span: attr.span });
}
attr
if attr.has_name(sym::type_const) {
self.sess.dcx().emit_err(TypeConstInCfgAttr { span: attr.span });
return None;
}
Some(attr)
}

/// Determines if a node with the given attributes should be included in this configuration.
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,13 @@ pub(crate) struct CrateTypeInCfgAttr {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(expand_type_const_in_cfg_attr)]
pub(crate) struct TypeConstInCfgAttr {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(expand_glob_delegation_traitless_qpath)]
pub(crate) struct GlobDelegationTraitlessQpath {
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/const-generics/mgca/type-const-within-cfg-attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//@ needs-rustc-debug-assertions

#![feature(min_generic_const_args)]
#![expect(incomplete_features)]

pub trait Foo {
#[cfg_attr(true, type_const)] //~ ERROR `type_const` within an `#[cfg_attr]` attribute is forbidden
const N: usize;
}

impl Foo for u32 {
#[cfg_attr(true, type_const)] //~ ERROR `type_const` within an `#[cfg_attr]` attribute is forbidden
const N: usize = 99;
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/const-generics/mgca/type-const-within-cfg-attr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: `type_const` within an `#[cfg_attr]` attribute is forbidden
--> $DIR/type-const-within-cfg-attr.rs:7:22
|
LL | #[cfg_attr(true, type_const)]
| ^^^^^^^^^^

error: `type_const` within an `#[cfg_attr]` attribute is forbidden
--> $DIR/type-const-within-cfg-attr.rs:12:22
|
LL | #[cfg_attr(true, type_const)]
| ^^^^^^^^^^

error: aborting due to 2 previous errors

Loading