Skip to content

Commit 23d40df

Browse files
committed
Make deprecated_cfg_attr_crate_type_name a hard error
1 parent 100fde5 commit 23d40df

11 files changed

+65
-118
lines changed

compiler/rustc_expand/messages.ftl

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ expand_collapse_debuginfo_illegal =
2727
expand_count_repetition_misplaced =
2828
`count` can not be placed inside the inner-most repetition
2929
30+
expand_crate_name_in_cfg_attr =
31+
`crate_name` within an `#![cfg_attr]` attribute is forbidden
32+
.help = this was previously accepted but is now an error
33+
34+
expand_crate_type_in_cfg_attr =
35+
`crate_type` within an `#![cfg_attr]` attribute is forbidden
36+
.help = this was previously accepted but is now an error
37+
3038
expand_custom_attribute_panicked =
3139
custom attribute panicked
3240
.help = message: {$message}

compiler/rustc_expand/src/config.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ use thin_vec::ThinVec;
2121
use tracing::instrument;
2222

2323
use crate::errors::{
24-
FeatureNotAllowed, FeatureRemoved, FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute,
25-
MalformedFeatureAttributeHelp, RemoveExprNotSupported,
24+
CrateNameInCfgAttr, CrateTypeInCfgAttr, FeatureNotAllowed, FeatureRemoved,
25+
FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute, MalformedFeatureAttributeHelp,
26+
RemoveExprNotSupported,
2627
};
2728

2829
/// A folder that strips out items that do not belong in the current configuration.
@@ -358,20 +359,10 @@ impl<'a> StripUnconfigured<'a> {
358359
item_span,
359360
);
360361
if attr.has_name(sym::crate_type) {
361-
self.sess.psess.buffer_lint(
362-
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
363-
attr.span,
364-
ast::CRATE_NODE_ID,
365-
BuiltinLintDiag::CrateTypeInCfgAttr,
366-
);
362+
self.sess.dcx().emit_err(CrateTypeInCfgAttr { span: attr.span });
367363
}
368364
if attr.has_name(sym::crate_name) {
369-
self.sess.psess.buffer_lint(
370-
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
371-
attr.span,
372-
ast::CRATE_NODE_ID,
373-
BuiltinLintDiag::CrateNameInCfgAttr,
374-
);
365+
self.sess.dcx().emit_err(CrateNameInCfgAttr { span: attr.span });
375366
}
376367
attr
377368
}

compiler/rustc_expand/src/errors.rs

+16
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,22 @@ pub(crate) struct GlobDelegationOutsideImpls {
467467
pub span: Span,
468468
}
469469

470+
#[derive(Diagnostic)]
471+
#[diag(expand_crate_name_in_cfg_attr)]
472+
#[help]
473+
pub(crate) struct CrateNameInCfgAttr {
474+
#[primary_span]
475+
pub span: Span,
476+
}
477+
478+
#[derive(Diagnostic)]
479+
#[diag(expand_crate_type_in_cfg_attr)]
480+
#[help]
481+
pub(crate) struct CrateTypeInCfgAttr {
482+
#[primary_span]
483+
pub span: Span,
484+
}
485+
470486
#[derive(Diagnostic)]
471487
#[diag(expand_glob_delegation_traitless_qpath)]
472488
pub(crate) struct GlobDelegationTraitlessQpath {

compiler/rustc_lint/messages.ftl

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

compiler/rustc_lint/src/context/diagnostics.rs

-6
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,6 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
395395
BuiltinLintDiag::CfgAttrNoAttributes => {
396396
lints::CfgAttrNoAttributes.decorate_lint(diag);
397397
}
398-
BuiltinLintDiag::CrateTypeInCfgAttr => {
399-
lints::CrateTypeInCfgAttr.decorate_lint(diag);
400-
}
401-
BuiltinLintDiag::CrateNameInCfgAttr => {
402-
lints::CrateNameInCfgAttr.decorate_lint(diag);
403-
}
404398
BuiltinLintDiag::MissingFragmentSpecifier => {
405399
lints::MissingFragmentSpecifier.decorate_lint(diag);
406400
}

compiler/rustc_lint/src/lints.rs

-8
Original file line numberDiff line numberDiff line change
@@ -2463,14 +2463,6 @@ pub(crate) struct DuplicateMacroAttribute;
24632463
#[diag(lint_cfg_attr_no_attributes)]
24642464
pub(crate) struct CfgAttrNoAttributes;
24652465

2466-
#[derive(LintDiagnostic)]
2467-
#[diag(lint_crate_type_in_cfg_attr_deprecated)]
2468-
pub(crate) struct CrateTypeInCfgAttr;
2469-
2470-
#[derive(LintDiagnostic)]
2471-
#[diag(lint_crate_name_in_cfg_attr_deprecated)]
2472-
pub(crate) struct CrateNameInCfgAttr;
2473-
24742466
#[derive(LintDiagnostic)]
24752467
#[diag(lint_missing_fragment_specifier)]
24762468
pub(crate) struct MissingFragmentSpecifier;

compiler/rustc_lint_defs/src/builtin.rs

-37
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ declare_lint_pass! {
3535
DEAD_CODE,
3636
DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK,
3737
DEPRECATED,
38-
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
3938
DEPRECATED_IN_FUTURE,
4039
DEPRECATED_SAFE_2024,
4140
DEPRECATED_WHERE_CLAUSE_LOCATION,
@@ -3207,42 +3206,6 @@ declare_lint! {
32073206
"detects large moves or copies",
32083207
}
32093208

3210-
declare_lint! {
3211-
/// The `deprecated_cfg_attr_crate_type_name` lint detects uses of the
3212-
/// `#![cfg_attr(..., crate_type = "...")]` and
3213-
/// `#![cfg_attr(..., crate_name = "...")]` attributes to conditionally
3214-
/// specify the crate type and name in the source code.
3215-
///
3216-
/// ### Example
3217-
///
3218-
/// ```rust,compile_fail
3219-
/// #![cfg_attr(debug_assertions, crate_type = "lib")]
3220-
/// ```
3221-
///
3222-
/// {{produces}}
3223-
///
3224-
///
3225-
/// ### Explanation
3226-
///
3227-
/// The `#![crate_type]` and `#![crate_name]` attributes require a hack in
3228-
/// the compiler to be able to change the used crate type and crate name
3229-
/// after macros have been expanded. Neither attribute works in combination
3230-
/// with Cargo as it explicitly passes `--crate-type` and `--crate-name` on
3231-
/// the commandline. These values must match the value used in the source
3232-
/// code to prevent an error.
3233-
///
3234-
/// To fix the warning use `--crate-type` on the commandline when running
3235-
/// rustc instead of `#![cfg_attr(..., crate_type = "...")]` and
3236-
/// `--crate-name` instead of `#![cfg_attr(..., crate_name = "...")]`.
3237-
pub DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
3238-
Deny,
3239-
"detects usage of `#![cfg_attr(..., crate_type/crate_name = \"...\")]`",
3240-
@future_incompatible = FutureIncompatibleInfo {
3241-
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,
3242-
reference: "issue #91632 <https://github.com/rust-lang/rust/issues/91632>",
3243-
};
3244-
}
3245-
32463209
declare_lint! {
32473210
/// The `unexpected_cfgs` lint detects unexpected conditional compilation conditions.
32483211
///

compiler/rustc_lint_defs/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,6 @@ pub enum BuiltinLintDiag {
729729
UnnameableTestItems,
730730
DuplicateMacroAttribute,
731731
CfgAttrNoAttributes,
732-
CrateTypeInCfgAttr,
733-
CrateNameInCfgAttr,
734732
MissingFragmentSpecifier,
735733
MetaVariableStillRepeating(MacroRulesNormalizedIdent),
736734
MetaVariableWrongOperator,

tests/ui/cfg/future-compat-crate-attributes-using-cfg_attr.rs tests/ui/cfg/crate-attributes-using-cfg_attr.rs

-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33

44
#![cfg_attr(foo, crate_type="bin")]
55
//~^ERROR `crate_type` within
6-
//~| WARN this was previously accepted
76
//~|ERROR `crate_type` within
8-
//~| WARN this was previously accepted
97
#![cfg_attr(foo, crate_name="bar")]
108
//~^ERROR `crate_name` within
11-
//~| WARN this was previously accepted
129
//~|ERROR `crate_name` within
13-
//~| WARN this was previously accepted
1410

1511
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: `crate_type` within an `#![cfg_attr]` attribute is forbidden
2+
--> $DIR/crate-attributes-using-cfg_attr.rs:4:18
3+
|
4+
LL | #![cfg_attr(foo, crate_type="bin")]
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= help: this was previously accepted but is now an error
8+
9+
error: `crate_name` within an `#![cfg_attr]` attribute is forbidden
10+
--> $DIR/crate-attributes-using-cfg_attr.rs:7:18
11+
|
12+
LL | #![cfg_attr(foo, crate_name="bar")]
13+
| ^^^^^^^^^^^^^^^^
14+
|
15+
= help: this was previously accepted but is now an error
16+
17+
error: `crate_type` within an `#![cfg_attr]` attribute is forbidden
18+
--> $DIR/crate-attributes-using-cfg_attr.rs:4:18
19+
|
20+
LL | #![cfg_attr(foo, crate_type="bin")]
21+
| ^^^^^^^^^^^^^^^^
22+
|
23+
= help: this was previously accepted but is now an error
24+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
25+
26+
error: `crate_name` within an `#![cfg_attr]` attribute is forbidden
27+
--> $DIR/crate-attributes-using-cfg_attr.rs:7:18
28+
|
29+
LL | #![cfg_attr(foo, crate_name="bar")]
30+
| ^^^^^^^^^^^^^^^^
31+
|
32+
= help: this was previously accepted but is now an error
33+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
34+
35+
error: aborting due to 4 previous errors
36+

tests/ui/cfg/future-compat-crate-attributes-using-cfg_attr.stderr

-41
This file was deleted.

0 commit comments

Comments
 (0)