Skip to content

Commit 9b6c510

Browse files
bjorn3Mark-Simulacrum
authored andcommitted
Future compatibility warning on cfg_attr on crate_type and crate_name
1 parent c5c9494 commit 9b6c510

6 files changed

+92
-13
lines changed

compiler/rustc_expand/src/config.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,24 @@ impl<'a> StripUnconfigured<'a> {
402402
);
403403
trees.push((bracket_group, Spacing::Alone));
404404
let tokens = Some(LazyTokenStream::new(AttrAnnotatedTokenStream::new(trees)));
405-
self.process_cfg_attr(attr::mk_attr_from_item(item, tokens, attr.style, span))
405+
let attr = attr::mk_attr_from_item(item, tokens, attr.style, span);
406+
if attr.has_name(sym::crate_type) {
407+
self.sess.parse_sess.buffer_lint(
408+
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
409+
attr.span,
410+
ast::CRATE_NODE_ID,
411+
"`crate_type` within an `#![cfg_attr] attribute is deprecated`",
412+
);
413+
}
414+
if attr.has_name(sym::crate_name) {
415+
self.sess.parse_sess.buffer_lint(
416+
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
417+
attr.span,
418+
ast::CRATE_NODE_ID,
419+
"`crate_name` within an `#![cfg_attr] attribute is deprecated`",
420+
);
421+
}
422+
self.process_cfg_attr(attr)
406423
})
407424
.collect()
408425
}

compiler/rustc_lint_defs/src/builtin.rs

+36
Original file line numberDiff line numberDiff line change
@@ -2960,6 +2960,41 @@ declare_lint! {
29602960
"detects large moves or copies",
29612961
}
29622962

2963+
declare_lint! {
2964+
/// The `deprecated_cfg_attr_crate_type_name` lint detects uses of the
2965+
/// `#![cfg_attr(..., crate_type = "...")]` and
2966+
/// `#![cfg_attr(..., crate_name = "...")]` attributes to conditionally
2967+
/// specify the crate type and name in the source code.
2968+
///
2969+
/// ### Example
2970+
///
2971+
/// ```rust
2972+
/// #![cfg_attr(debug_assertions, crate_type = "lib")]
2973+
/// ```
2974+
///
2975+
/// {{produces}}
2976+
///
2977+
///
2978+
/// ### Explanation
2979+
///
2980+
/// The `#![crate_type]` and `#![crate_name]` attributes require a hack in
2981+
/// the compiler to be able to change the used crate type and crate name
2982+
/// after macros have been expanded. Neither attribute works in combination
2983+
/// with Cargo as it explicitly passes `--crate-type` and `--crate-name` on
2984+
/// the commandline. These values must match the value used in the source
2985+
/// code to prevent an error.
2986+
///
2987+
/// To fix the warning use `--crate-type` on the commandline when running
2988+
/// rustc instead of `#![cfg_attr(..., crate_type = "...")]` and
2989+
/// `--crate-name` instead of `#![cfg_attr(..., crate_name = "...")]`.
2990+
pub DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
2991+
Warn,
2992+
"detects usage of `#![cfg_attr(..., crate_type/crate_name = \"...\")]`",
2993+
@future_incompatible = FutureIncompatibleInfo {
2994+
reference: "issue #91632 <https://github.com/rust-lang/rust/issues/91632>",
2995+
};
2996+
}
2997+
29632998
declare_lint_pass! {
29642999
/// Does nothing as a lint pass, but registers some `Lint`s
29653000
/// that are used by other parts of the compiler.
@@ -3056,6 +3091,7 @@ declare_lint_pass! {
30563091
NON_EXHAUSTIVE_OMITTED_PATTERNS,
30573092
TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
30583093
DEREF_INTO_DYN_SUPERTRAIT,
3094+
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
30593095
]
30603096
}
30613097

src/test/ui/cfg/auxiliary/crate-attributes-using-cfg_attr.rs

-6
This file was deleted.

src/test/ui/cfg/crate-attributes-using-cfg_attr.rs

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-fail
2+
// compile-flags:--cfg foo
3+
4+
#![deny(warnings)]
5+
#![cfg_attr(foo, crate_type="bin")]
6+
//~^ERROR `crate_type` within
7+
//~| WARN this was previously accepted
8+
#![cfg_attr(foo, crate_name="bar")]
9+
//~^ERROR `crate_name` within
10+
//~| WARN this was previously accepted
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: `crate_type` within an `#![cfg_attr] attribute is deprecated`
2+
--> $DIR/future-compat-crate-attributes-using-cfg_attr.rs:5:18
3+
|
4+
LL | #![cfg_attr(foo, crate_type="bin")]
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/future-compat-crate-attributes-using-cfg_attr.rs:4:9
9+
|
10+
LL | #![deny(warnings)]
11+
| ^^^^^^^^
12+
= note: `#[deny(deprecated_cfg_attr_crate_type_name)]` implied by `#[deny(warnings)]`
13+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
14+
= note: for more information, see issue #91632 <https://github.com/rust-lang/rust/issues/91632>
15+
16+
error: `crate_name` within an `#![cfg_attr] attribute is deprecated`
17+
--> $DIR/future-compat-crate-attributes-using-cfg_attr.rs:8:18
18+
|
19+
LL | #![cfg_attr(foo, crate_name="bar")]
20+
| ^^^^^^^^^^^^^^^^
21+
|
22+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
23+
= note: for more information, see issue #91632 <https://github.com/rust-lang/rust/issues/91632>
24+
25+
error: aborting due to 2 previous errors
26+

0 commit comments

Comments
 (0)