Auto merge of #129670 - est31:cfg_attr_crate_type_name_error, r=Urgau

Make deprecated_cfg_attr_crate_type_name a hard error

Turns the forward compatibility lint added by #83744 into a hard error, so now, while the `#![crate_name]` and `#![crate_type]` attributes are still allowed in raw form, they are now forbidden to be nested inside a `#![cfg_attr()]` attribute.

The following will now be an error:

```Rust
#![cfg_attr(foo, crate_name = "foobar")]
#![cfg_attr(foo, crate_type = "bin")]
```

This code will continue working and is not deprecated:

```Rust
#![crate_name = "foobar"]
#![crate_type = "lib"]
```

The reasoning for this is explained in #83744: it allows us to not have to cfg-expand in order to determine the crate's type and name.

As of filing the PR, exactly two years have passed since #99784 has been merged, which has turned the lint's default warning level into an error, so there has been ample time to move off the now-forbidden syntax.

cc #91632 - tracking issue for the lint
This commit is contained in:
bors 2024-10-06 17:00:02 +00:00
commit 8422e27b27
12 changed files with 60 additions and 118 deletions

View File

@ -27,6 +27,12 @@ expand_collapse_debuginfo_illegal =
expand_count_repetition_misplaced = expand_count_repetition_misplaced =
`count` can not be placed inside the inner-most repetition `count` can not be placed inside the inner-most repetition
expand_crate_name_in_cfg_attr =
`crate_name` within an `#![cfg_attr]` attribute is forbidden
expand_crate_type_in_cfg_attr =
`crate_type` within an `#![cfg_attr]` attribute is forbidden
expand_custom_attribute_panicked = expand_custom_attribute_panicked =
custom attribute panicked custom attribute panicked
.help = message: {$message} .help = message: {$message}

View File

@ -23,8 +23,9 @@ use thin_vec::ThinVec;
use tracing::instrument; use tracing::instrument;
use crate::errors::{ use crate::errors::{
FeatureNotAllowed, FeatureRemoved, FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute, CrateNameInCfgAttr, CrateTypeInCfgAttr, FeatureNotAllowed, FeatureRemoved,
MalformedFeatureAttributeHelp, RemoveExprNotSupported, FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute, MalformedFeatureAttributeHelp,
RemoveExprNotSupported,
}; };
/// A folder that strips out items that do not belong in the current configuration. /// A folder that strips out items that do not belong in the current configuration.
@ -360,20 +361,10 @@ impl<'a> StripUnconfigured<'a> {
item_span, item_span,
); );
if attr.has_name(sym::crate_type) { if attr.has_name(sym::crate_type) {
self.sess.psess.buffer_lint( self.sess.dcx().emit_err(CrateTypeInCfgAttr { span: attr.span });
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
attr.span,
ast::CRATE_NODE_ID,
BuiltinLintDiag::CrateTypeInCfgAttr,
);
} }
if attr.has_name(sym::crate_name) { if attr.has_name(sym::crate_name) {
self.sess.psess.buffer_lint( self.sess.dcx().emit_err(CrateNameInCfgAttr { span: attr.span });
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
attr.span,
ast::CRATE_NODE_ID,
BuiltinLintDiag::CrateNameInCfgAttr,
);
} }
attr attr
} }

View File

@ -467,6 +467,20 @@ pub(crate) struct GlobDelegationOutsideImpls {
pub span: Span, pub span: Span,
} }
#[derive(Diagnostic)]
#[diag(expand_crate_name_in_cfg_attr)]
pub(crate) struct CrateNameInCfgAttr {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(expand_crate_type_in_cfg_attr)]
pub(crate) struct CrateTypeInCfgAttr {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(expand_glob_delegation_traitless_qpath)] #[diag(expand_glob_delegation_traitless_qpath)]
pub(crate) struct GlobDelegationTraitlessQpath { pub(crate) struct GlobDelegationTraitlessQpath {

View File

@ -203,12 +203,6 @@ lint_confusable_identifier_pair = found both `{$existing_sym}` and `{$sym}` as i
.current_use = this identifier can be confused with `{$existing_sym}` .current_use = this identifier can be confused with `{$existing_sym}`
.other_use = other identifier used here .other_use = other identifier used here
lint_crate_name_in_cfg_attr_deprecated =
`crate_name` within an `#![cfg_attr]` attribute is deprecated
lint_crate_type_in_cfg_attr_deprecated =
`crate_type` within an `#![cfg_attr]` attribute is deprecated
lint_cstring_ptr = getting the inner pointer of a temporary `CString` lint_cstring_ptr = getting the inner pointer of a temporary `CString`
.as_ptr_label = this pointer will be invalid .as_ptr_label = this pointer will be invalid
.unwrap_label = this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime .unwrap_label = this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime

View File

@ -400,12 +400,6 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
BuiltinLintDiag::CfgAttrNoAttributes => { BuiltinLintDiag::CfgAttrNoAttributes => {
lints::CfgAttrNoAttributes.decorate_lint(diag); lints::CfgAttrNoAttributes.decorate_lint(diag);
} }
BuiltinLintDiag::CrateTypeInCfgAttr => {
lints::CrateTypeInCfgAttr.decorate_lint(diag);
}
BuiltinLintDiag::CrateNameInCfgAttr => {
lints::CrateNameInCfgAttr.decorate_lint(diag);
}
BuiltinLintDiag::MissingFragmentSpecifier => { BuiltinLintDiag::MissingFragmentSpecifier => {
lints::MissingFragmentSpecifier.decorate_lint(diag); lints::MissingFragmentSpecifier.decorate_lint(diag);
} }

View File

@ -569,6 +569,11 @@ fn register_builtins(store: &mut LintStore) {
"converted into hard error, see RFC #3535 \ "converted into hard error, see RFC #3535 \
<https://rust-lang.github.io/rfcs/3535-constants-in-patterns.html> for more information", <https://rust-lang.github.io/rfcs/3535-constants-in-patterns.html> for more information",
); );
store.register_removed(
"deprecated_cfg_attr_crate_type_name",
"converted into hard error, see issue #91632 \
<https://github.com/rust-lang/rust/issues/91632> for more information",
);
store.register_removed( store.register_removed(
"pointer_structural_match", "pointer_structural_match",
"converted into hard error, see RFC #3535 \ "converted into hard error, see RFC #3535 \

View File

@ -2441,14 +2441,6 @@ pub(crate) struct DuplicateMacroAttribute;
#[diag(lint_cfg_attr_no_attributes)] #[diag(lint_cfg_attr_no_attributes)]
pub(crate) struct CfgAttrNoAttributes; pub(crate) struct CfgAttrNoAttributes;
#[derive(LintDiagnostic)]
#[diag(lint_crate_type_in_cfg_attr_deprecated)]
pub(crate) struct CrateTypeInCfgAttr;
#[derive(LintDiagnostic)]
#[diag(lint_crate_name_in_cfg_attr_deprecated)]
pub(crate) struct CrateNameInCfgAttr;
#[derive(LintDiagnostic)] #[derive(LintDiagnostic)]
#[diag(lint_missing_fragment_specifier)] #[diag(lint_missing_fragment_specifier)]
pub(crate) struct MissingFragmentSpecifier; pub(crate) struct MissingFragmentSpecifier;

View File

@ -34,7 +34,6 @@ declare_lint_pass! {
DEAD_CODE, DEAD_CODE,
DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK, DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK,
DEPRECATED, DEPRECATED,
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
DEPRECATED_IN_FUTURE, DEPRECATED_IN_FUTURE,
DEPRECATED_SAFE_2024, DEPRECATED_SAFE_2024,
DEPRECATED_WHERE_CLAUSE_LOCATION, DEPRECATED_WHERE_CLAUSE_LOCATION,
@ -3144,42 +3143,6 @@ declare_lint! {
"detects large moves or copies", "detects large moves or copies",
} }
declare_lint! {
/// The `deprecated_cfg_attr_crate_type_name` lint detects uses of the
/// `#![cfg_attr(..., crate_type = "...")]` and
/// `#![cfg_attr(..., crate_name = "...")]` attributes to conditionally
/// specify the crate type and name in the source code.
///
/// ### Example
///
/// ```rust,compile_fail
/// #![cfg_attr(debug_assertions, crate_type = "lib")]
/// ```
///
/// {{produces}}
///
///
/// ### Explanation
///
/// The `#![crate_type]` and `#![crate_name]` attributes require a hack in
/// the compiler to be able to change the used crate type and crate name
/// after macros have been expanded. Neither attribute works in combination
/// with Cargo as it explicitly passes `--crate-type` and `--crate-name` on
/// the commandline. These values must match the value used in the source
/// code to prevent an error.
///
/// To fix the warning use `--crate-type` on the commandline when running
/// rustc instead of `#![cfg_attr(..., crate_type = "...")]` and
/// `--crate-name` instead of `#![cfg_attr(..., crate_name = "...")]`.
pub DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
Deny,
"detects usage of `#![cfg_attr(..., crate_type/crate_name = \"...\")]`",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,
reference: "issue #91632 <https://github.com/rust-lang/rust/issues/91632>",
};
}
declare_lint! { declare_lint! {
/// The `unexpected_cfgs` lint detects unexpected conditional compilation conditions. /// The `unexpected_cfgs` lint detects unexpected conditional compilation conditions.
/// ///

View File

@ -720,8 +720,6 @@ pub enum BuiltinLintDiag {
UnnameableTestItems, UnnameableTestItems,
DuplicateMacroAttribute, DuplicateMacroAttribute,
CfgAttrNoAttributes, CfgAttrNoAttributes,
CrateTypeInCfgAttr,
CrateNameInCfgAttr,
MissingFragmentSpecifier, MissingFragmentSpecifier,
MetaVariableStillRepeating(MacroRulesNormalizedIdent), MetaVariableStillRepeating(MacroRulesNormalizedIdent),
MetaVariableWrongOperator, MetaVariableWrongOperator,

View File

@ -3,13 +3,9 @@
#![cfg_attr(foo, crate_type="bin")] #![cfg_attr(foo, crate_type="bin")]
//~^ERROR `crate_type` within //~^ERROR `crate_type` within
//~| WARN this was previously accepted
//~|ERROR `crate_type` within //~|ERROR `crate_type` within
//~| WARN this was previously accepted
#![cfg_attr(foo, crate_name="bar")] #![cfg_attr(foo, crate_name="bar")]
//~^ERROR `crate_name` within //~^ERROR `crate_name` within
//~| WARN this was previously accepted
//~|ERROR `crate_name` within //~|ERROR `crate_name` within
//~| WARN this was previously accepted
fn main() {} fn main() {}

View File

@ -0,0 +1,30 @@
error: `crate_type` within an `#![cfg_attr]` attribute is forbidden
--> $DIR/crate-attributes-using-cfg_attr.rs:4:18
|
LL | #![cfg_attr(foo, crate_type="bin")]
| ^^^^^^^^^^^^^^^^
error: `crate_name` within an `#![cfg_attr]` attribute is forbidden
--> $DIR/crate-attributes-using-cfg_attr.rs:7:18
|
LL | #![cfg_attr(foo, crate_name="bar")]
| ^^^^^^^^^^^^^^^^
error: `crate_type` within an `#![cfg_attr]` attribute is forbidden
--> $DIR/crate-attributes-using-cfg_attr.rs:4:18
|
LL | #![cfg_attr(foo, crate_type="bin")]
| ^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: `crate_name` within an `#![cfg_attr]` attribute is forbidden
--> $DIR/crate-attributes-using-cfg_attr.rs:7:18
|
LL | #![cfg_attr(foo, crate_name="bar")]
| ^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 4 previous errors

View File

@ -1,41 +0,0 @@
error: `crate_type` within an `#![cfg_attr]` attribute is deprecated
--> $DIR/future-compat-crate-attributes-using-cfg_attr.rs:4:18
|
LL | #![cfg_attr(foo, crate_type="bin")]
| ^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #91632 <https://github.com/rust-lang/rust/issues/91632>
= note: `#[deny(deprecated_cfg_attr_crate_type_name)]` on by default
error: `crate_name` within an `#![cfg_attr]` attribute is deprecated
--> $DIR/future-compat-crate-attributes-using-cfg_attr.rs:9:18
|
LL | #![cfg_attr(foo, crate_name="bar")]
| ^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #91632 <https://github.com/rust-lang/rust/issues/91632>
error: `crate_type` within an `#![cfg_attr]` attribute is deprecated
--> $DIR/future-compat-crate-attributes-using-cfg_attr.rs:4:18
|
LL | #![cfg_attr(foo, crate_type="bin")]
| ^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #91632 <https://github.com/rust-lang/rust/issues/91632>
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: `crate_name` within an `#![cfg_attr]` attribute is deprecated
--> $DIR/future-compat-crate-attributes-using-cfg_attr.rs:9:18
|
LL | #![cfg_attr(foo, crate_name="bar")]
| ^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #91632 <https://github.com/rust-lang/rust/issues/91632>
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 4 previous errors