diff --git a/compiler/rustc_expand/messages.ftl b/compiler/rustc_expand/messages.ftl index 851d78e0b1051..0c58e9ae12185 100644 --- a/compiler/rustc_expand/messages.ftl +++ b/compiler/rustc_expand/messages.ftl @@ -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 = diff --git a/compiler/rustc_expand/src/config.rs b/compiler/rustc_expand/src/config.rs index 53392f100e148..5a7c58c82ce22 100644 --- a/compiler/rustc_expand/src/config.rs +++ b/compiler/rustc_expand/src/config.rs @@ -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. @@ -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() } } @@ -332,7 +334,7 @@ impl<'a> StripUnconfigured<'a> { &self, cfg_attr: &Attribute, (item, item_span): (ast::AttrItem, Span), - ) -> Attribute { + ) -> Option { // Convert `#[cfg_attr(pred, attr)]` to `#[attr]`. // Use the `#` from `#[cfg_attr(pred, attr)]` in the result `#[attr]`. @@ -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. diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs index 9f9764e060d1b..a9dbc63e4bcbb 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -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 { diff --git a/tests/ui/const-generics/mgca/type-const-within-cfg-attr.rs b/tests/ui/const-generics/mgca/type-const-within-cfg-attr.rs new file mode 100644 index 0000000000000..adb3b45e683a3 --- /dev/null +++ b/tests/ui/const-generics/mgca/type-const-within-cfg-attr.rs @@ -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() {} diff --git a/tests/ui/const-generics/mgca/type-const-within-cfg-attr.stderr b/tests/ui/const-generics/mgca/type-const-within-cfg-attr.stderr new file mode 100644 index 0000000000000..69904a0edd062 --- /dev/null +++ b/tests/ui/const-generics/mgca/type-const-within-cfg-attr.stderr @@ -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 +