Skip to content

Commit 8422e27

Browse files
committed
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
2 parents 373971a + 00ed47b commit 8422e27

12 files changed

+60
-118
lines changed

compiler/rustc_expand/messages.ftl

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ 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+
33+
expand_crate_type_in_cfg_attr =
34+
`crate_type` within an `#![cfg_attr]` attribute is forbidden
35+
3036
expand_custom_attribute_panicked =
3137
custom attribute panicked
3238
.help = message: {$message}

compiler/rustc_expand/src/config.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ use thin_vec::ThinVec;
2323
use tracing::instrument;
2424

2525
use crate::errors::{
26-
FeatureNotAllowed, FeatureRemoved, FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute,
27-
MalformedFeatureAttributeHelp, RemoveExprNotSupported,
26+
CrateNameInCfgAttr, CrateTypeInCfgAttr, FeatureNotAllowed, FeatureRemoved,
27+
FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute, MalformedFeatureAttributeHelp,
28+
RemoveExprNotSupported,
2829
};
2930

3031
/// A folder that strips out items that do not belong in the current configuration.
@@ -360,20 +361,10 @@ impl<'a> StripUnconfigured<'a> {
360361
item_span,
361362
);
362363
if attr.has_name(sym::crate_type) {
363-
self.sess.psess.buffer_lint(
364-
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
365-
attr.span,
366-
ast::CRATE_NODE_ID,
367-
BuiltinLintDiag::CrateTypeInCfgAttr,
368-
);
364+
self.sess.dcx().emit_err(CrateTypeInCfgAttr { span: attr.span });
369365
}
370366
if attr.has_name(sym::crate_name) {
371-
self.sess.psess.buffer_lint(
372-
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
373-
attr.span,
374-
ast::CRATE_NODE_ID,
375-
BuiltinLintDiag::CrateNameInCfgAttr,
376-
);
367+
self.sess.dcx().emit_err(CrateNameInCfgAttr { span: attr.span });
377368
}
378369
attr
379370
}

compiler/rustc_expand/src/errors.rs

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

470+
#[derive(Diagnostic)]
471+
#[diag(expand_crate_name_in_cfg_attr)]
472+
pub(crate) struct CrateNameInCfgAttr {
473+
#[primary_span]
474+
pub span: Span,
475+
}
476+
477+
#[derive(Diagnostic)]
478+
#[diag(expand_crate_type_in_cfg_attr)]
479+
pub(crate) struct CrateTypeInCfgAttr {
480+
#[primary_span]
481+
pub span: Span,
482+
}
483+
470484
#[derive(Diagnostic)]
471485
#[diag(expand_glob_delegation_traitless_qpath)]
472486
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
@@ -400,12 +400,6 @@ pub(super) fn decorate_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &
400400
BuiltinLintDiag::CfgAttrNoAttributes => {
401401
lints::CfgAttrNoAttributes.decorate_lint(diag);
402402
}
403-
BuiltinLintDiag::CrateTypeInCfgAttr => {
404-
lints::CrateTypeInCfgAttr.decorate_lint(diag);
405-
}
406-
BuiltinLintDiag::CrateNameInCfgAttr => {
407-
lints::CrateNameInCfgAttr.decorate_lint(diag);
408-
}
409403
BuiltinLintDiag::MissingFragmentSpecifier => {
410404
lints::MissingFragmentSpecifier.decorate_lint(diag);
411405
}

compiler/rustc_lint/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ fn register_builtins(store: &mut LintStore) {
569569
"converted into hard error, see RFC #3535 \
570570
<https://rust-lang.github.io/rfcs/3535-constants-in-patterns.html> for more information",
571571
);
572+
store.register_removed(
573+
"deprecated_cfg_attr_crate_type_name",
574+
"converted into hard error, see issue #91632 \
575+
<https://github.com/rust-lang/rust/issues/91632> for more information",
576+
);
572577
store.register_removed(
573578
"pointer_structural_match",
574579
"converted into hard error, see RFC #3535 \

compiler/rustc_lint/src/lints.rs

-8
Original file line numberDiff line numberDiff line change
@@ -2441,14 +2441,6 @@ pub(crate) struct DuplicateMacroAttribute;
24412441
#[diag(lint_cfg_attr_no_attributes)]
24422442
pub(crate) struct CfgAttrNoAttributes;
24432443

2444-
#[derive(LintDiagnostic)]
2445-
#[diag(lint_crate_type_in_cfg_attr_deprecated)]
2446-
pub(crate) struct CrateTypeInCfgAttr;
2447-
2448-
#[derive(LintDiagnostic)]
2449-
#[diag(lint_crate_name_in_cfg_attr_deprecated)]
2450-
pub(crate) struct CrateNameInCfgAttr;
2451-
24522444
#[derive(LintDiagnostic)]
24532445
#[diag(lint_missing_fragment_specifier)]
24542446
pub(crate) struct MissingFragmentSpecifier;

compiler/rustc_lint_defs/src/builtin.rs

-37
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ declare_lint_pass! {
3434
DEAD_CODE,
3535
DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK,
3636
DEPRECATED,
37-
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
3837
DEPRECATED_IN_FUTURE,
3938
DEPRECATED_SAFE_2024,
4039
DEPRECATED_WHERE_CLAUSE_LOCATION,
@@ -3144,42 +3143,6 @@ declare_lint! {
31443143
"detects large moves or copies",
31453144
}
31463145

3147-
declare_lint! {
3148-
/// The `deprecated_cfg_attr_crate_type_name` lint detects uses of the
3149-
/// `#![cfg_attr(..., crate_type = "...")]` and
3150-
/// `#![cfg_attr(..., crate_name = "...")]` attributes to conditionally
3151-
/// specify the crate type and name in the source code.
3152-
///
3153-
/// ### Example
3154-
///
3155-
/// ```rust,compile_fail
3156-
/// #![cfg_attr(debug_assertions, crate_type = "lib")]
3157-
/// ```
3158-
///
3159-
/// {{produces}}
3160-
///
3161-
///
3162-
/// ### Explanation
3163-
///
3164-
/// The `#![crate_type]` and `#![crate_name]` attributes require a hack in
3165-
/// the compiler to be able to change the used crate type and crate name
3166-
/// after macros have been expanded. Neither attribute works in combination
3167-
/// with Cargo as it explicitly passes `--crate-type` and `--crate-name` on
3168-
/// the commandline. These values must match the value used in the source
3169-
/// code to prevent an error.
3170-
///
3171-
/// To fix the warning use `--crate-type` on the commandline when running
3172-
/// rustc instead of `#![cfg_attr(..., crate_type = "...")]` and
3173-
/// `--crate-name` instead of `#![cfg_attr(..., crate_name = "...")]`.
3174-
pub DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
3175-
Deny,
3176-
"detects usage of `#![cfg_attr(..., crate_type/crate_name = \"...\")]`",
3177-
@future_incompatible = FutureIncompatibleInfo {
3178-
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,
3179-
reference: "issue #91632 <https://github.com/rust-lang/rust/issues/91632>",
3180-
};
3181-
}
3182-
31833146
declare_lint! {
31843147
/// The `unexpected_cfgs` lint detects unexpected conditional compilation conditions.
31853148
///

compiler/rustc_lint_defs/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,6 @@ pub enum BuiltinLintDiag {
720720
UnnameableTestItems,
721721
DuplicateMacroAttribute,
722722
CfgAttrNoAttributes,
723-
CrateTypeInCfgAttr,
724-
CrateNameInCfgAttr,
725723
MissingFragmentSpecifier,
726724
MetaVariableStillRepeating(MacroRulesNormalizedIdent),
727725
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,30 @@
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+
error: `crate_name` within an `#![cfg_attr]` attribute is forbidden
8+
--> $DIR/crate-attributes-using-cfg_attr.rs:7:18
9+
|
10+
LL | #![cfg_attr(foo, crate_name="bar")]
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: `crate_type` within an `#![cfg_attr]` attribute is forbidden
14+
--> $DIR/crate-attributes-using-cfg_attr.rs:4:18
15+
|
16+
LL | #![cfg_attr(foo, crate_type="bin")]
17+
| ^^^^^^^^^^^^^^^^
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
21+
error: `crate_name` within an `#![cfg_attr]` attribute is forbidden
22+
--> $DIR/crate-attributes-using-cfg_attr.rs:7:18
23+
|
24+
LL | #![cfg_attr(foo, crate_name="bar")]
25+
| ^^^^^^^^^^^^^^^^
26+
|
27+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
28+
29+
error: aborting due to 4 previous errors
30+

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

-41
This file was deleted.

0 commit comments

Comments
 (0)