Skip to content

Commit

Permalink
Make deprecated_cfg_attr_crate_type_name a hard error
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed Aug 27, 2024
1 parent ab869e0 commit ccaaab0
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 118 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ expand_collapse_debuginfo_illegal =
expand_count_repetition_misplaced =
`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
.help = this was previously accepted but is now an error
expand_crate_type_in_cfg_attr =
`crate_type` within an `#![cfg_attr]` attribute is forbidden
.help = this was previously accepted but is now an error
expand_custom_attribute_panicked =
custom attribute panicked
.help = message: {$message}
Expand Down
19 changes: 5 additions & 14 deletions compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ use thin_vec::ThinVec;
use tracing::instrument;

use crate::errors::{
FeatureNotAllowed, FeatureRemoved, FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute,
MalformedFeatureAttributeHelp, RemoveExprNotSupported,
CrateNameInCfgAttr, CrateTypeInCfgAttr, FeatureNotAllowed, FeatureRemoved,
FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute, MalformedFeatureAttributeHelp,
RemoveExprNotSupported,
};

/// A folder that strips out items that do not belong in the current configuration.
Expand Down Expand Up @@ -358,20 +359,10 @@ impl<'a> StripUnconfigured<'a> {
item_span,
);
if attr.has_name(sym::crate_type) {
self.sess.psess.buffer_lint(
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
attr.span,
ast::CRATE_NODE_ID,
BuiltinLintDiag::CrateTypeInCfgAttr,
);
self.sess.dcx().emit_err(CrateTypeInCfgAttr { span: attr.span });
}
if attr.has_name(sym::crate_name) {
self.sess.psess.buffer_lint(
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
attr.span,
ast::CRATE_NODE_ID,
BuiltinLintDiag::CrateNameInCfgAttr,
);
self.sess.dcx().emit_err(CrateNameInCfgAttr { span: attr.span });
}
attr
}
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,22 @@ pub(crate) struct GlobDelegationOutsideImpls {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(expand_crate_name_in_cfg_attr)]
#[help]
pub(crate) struct CrateNameInCfgAttr {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(expand_crate_type_in_cfg_attr)]
#[help]
pub(crate) struct CrateTypeInCfgAttr {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(expand_glob_delegation_traitless_qpath)]
pub(crate) struct GlobDelegationTraitlessQpath {
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
.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`
.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
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,6 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
BuiltinLintDiag::CfgAttrNoAttributes => {
lints::CfgAttrNoAttributes.decorate_lint(diag);
}
BuiltinLintDiag::CrateTypeInCfgAttr => {
lints::CrateTypeInCfgAttr.decorate_lint(diag);
}
BuiltinLintDiag::CrateNameInCfgAttr => {
lints::CrateNameInCfgAttr.decorate_lint(diag);
}
BuiltinLintDiag::MissingFragmentSpecifier => {
lints::MissingFragmentSpecifier.decorate_lint(diag);
}
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2463,14 +2463,6 @@ pub struct DuplicateMacroAttribute;
#[diag(lint_cfg_attr_no_attributes)]
pub struct CfgAttrNoAttributes;

#[derive(LintDiagnostic)]
#[diag(lint_crate_type_in_cfg_attr_deprecated)]
pub struct CrateTypeInCfgAttr;

#[derive(LintDiagnostic)]
#[diag(lint_crate_name_in_cfg_attr_deprecated)]
pub struct CrateNameInCfgAttr;

#[derive(LintDiagnostic)]
#[diag(lint_missing_fragment_specifier)]
pub struct MissingFragmentSpecifier;
Expand Down
37 changes: 0 additions & 37 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ declare_lint_pass! {
DEAD_CODE,
DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK,
DEPRECATED,
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
DEPRECATED_IN_FUTURE,
DEPRECATED_SAFE_2024,
DEPRECATED_WHERE_CLAUSE_LOCATION,
Expand Down Expand Up @@ -3207,42 +3206,6 @@ declare_lint! {
"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! {
/// The `unexpected_cfgs` lint detects unexpected conditional compilation conditions.
///
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,6 @@ pub enum BuiltinLintDiag {
UnnameableTestItems,
DuplicateMacroAttribute,
CfgAttrNoAttributes,
CrateTypeInCfgAttr,
CrateNameInCfgAttr,
MissingFragmentSpecifier,
MetaVariableStillRepeating(MacroRulesNormalizedIdent),
MetaVariableWrongOperator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@

#![cfg_attr(foo, crate_type="bin")]
//~^ERROR `crate_type` within
//~| WARN this was previously accepted
//~|ERROR `crate_type` within
//~| WARN this was previously accepted
#![cfg_attr(foo, crate_name="bar")]
//~^ERROR `crate_name` within
//~| WARN this was previously accepted
//~|ERROR `crate_name` within
//~| WARN this was previously accepted

fn main() {}
36 changes: 36 additions & 0 deletions tests/ui/cfg/crate-attributes-using-cfg_attr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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")]
| ^^^^^^^^^^^^^^^^
|
= help: this was previously accepted but is now an error

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")]
| ^^^^^^^^^^^^^^^^
|
= help: this was previously accepted but is now an error

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")]
| ^^^^^^^^^^^^^^^^
|
= help: this was previously accepted but is now an error
= 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")]
| ^^^^^^^^^^^^^^^^
|
= help: this was previously accepted but is now an error
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 4 previous errors

41 changes: 0 additions & 41 deletions tests/ui/cfg/future-compat-crate-attributes-using-cfg_attr.stderr

This file was deleted.

0 comments on commit ccaaab0

Please sign in to comment.