From 9b6c5109058d0662c9340b0328bcad591458f35d Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Thu, 1 Apr 2021 11:34:39 +0200 Subject: [PATCH] Future compatibility warning on cfg_attr on crate_type and crate_name --- compiler/rustc_expand/src/config.rs | 19 +++++++++- compiler/rustc_lint_defs/src/builtin.rs | 36 +++++++++++++++++++ .../crate-attributes-using-cfg_attr.rs | 6 ---- .../ui/cfg/crate-attributes-using-cfg_attr.rs | 6 ---- ...-compat-crate-attributes-using-cfg_attr.rs | 12 +++++++ ...pat-crate-attributes-using-cfg_attr.stderr | 26 ++++++++++++++ 6 files changed, 92 insertions(+), 13 deletions(-) delete mode 100644 src/test/ui/cfg/auxiliary/crate-attributes-using-cfg_attr.rs delete mode 100644 src/test/ui/cfg/crate-attributes-using-cfg_attr.rs create mode 100644 src/test/ui/cfg/future-compat-crate-attributes-using-cfg_attr.rs create mode 100644 src/test/ui/cfg/future-compat-crate-attributes-using-cfg_attr.stderr diff --git a/compiler/rustc_expand/src/config.rs b/compiler/rustc_expand/src/config.rs index 1b123520961..5221ab4b613 100644 --- a/compiler/rustc_expand/src/config.rs +++ b/compiler/rustc_expand/src/config.rs @@ -402,7 +402,24 @@ impl<'a> StripUnconfigured<'a> { ); trees.push((bracket_group, Spacing::Alone)); let tokens = Some(LazyTokenStream::new(AttrAnnotatedTokenStream::new(trees))); - self.process_cfg_attr(attr::mk_attr_from_item(item, tokens, attr.style, span)) + let attr = attr::mk_attr_from_item(item, tokens, attr.style, span); + if attr.has_name(sym::crate_type) { + self.sess.parse_sess.buffer_lint( + rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME, + attr.span, + ast::CRATE_NODE_ID, + "`crate_type` within an `#![cfg_attr] attribute is deprecated`", + ); + } + if attr.has_name(sym::crate_name) { + self.sess.parse_sess.buffer_lint( + rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME, + attr.span, + ast::CRATE_NODE_ID, + "`crate_name` within an `#![cfg_attr] attribute is deprecated`", + ); + } + self.process_cfg_attr(attr) }) .collect() } diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index c1a53c34b7a..c9294c68a7d 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -2960,6 +2960,41 @@ 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 + /// #![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, + Warn, + "detects usage of `#![cfg_attr(..., crate_type/crate_name = \"...\")]`", + @future_incompatible = FutureIncompatibleInfo { + reference: "issue #91632 ", + }; +} + declare_lint_pass! { /// Does nothing as a lint pass, but registers some `Lint`s /// that are used by other parts of the compiler. @@ -3056,6 +3091,7 @@ declare_lint_pass! { NON_EXHAUSTIVE_OMITTED_PATTERNS, TEXT_DIRECTION_CODEPOINT_IN_COMMENT, DEREF_INTO_DYN_SUPERTRAIT, + DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME, ] } diff --git a/src/test/ui/cfg/auxiliary/crate-attributes-using-cfg_attr.rs b/src/test/ui/cfg/auxiliary/crate-attributes-using-cfg_attr.rs deleted file mode 100644 index 1e0f5d79c0b..00000000000 --- a/src/test/ui/cfg/auxiliary/crate-attributes-using-cfg_attr.rs +++ /dev/null @@ -1,6 +0,0 @@ -// no-prefer-dynamic -// compile-flags: --cfg foo - -#![cfg_attr(foo, crate_type="lib")] - -pub fn foo() {} diff --git a/src/test/ui/cfg/crate-attributes-using-cfg_attr.rs b/src/test/ui/cfg/crate-attributes-using-cfg_attr.rs deleted file mode 100644 index 43b266b778f..00000000000 --- a/src/test/ui/cfg/crate-attributes-using-cfg_attr.rs +++ /dev/null @@ -1,6 +0,0 @@ -// run-pass -// aux-build:crate-attributes-using-cfg_attr.rs - -extern crate crate_attributes_using_cfg_attr; - -pub fn main() {} diff --git a/src/test/ui/cfg/future-compat-crate-attributes-using-cfg_attr.rs b/src/test/ui/cfg/future-compat-crate-attributes-using-cfg_attr.rs new file mode 100644 index 00000000000..ef12b05fab2 --- /dev/null +++ b/src/test/ui/cfg/future-compat-crate-attributes-using-cfg_attr.rs @@ -0,0 +1,12 @@ +// check-fail +// compile-flags:--cfg foo + +#![deny(warnings)] +#![cfg_attr(foo, crate_type="bin")] +//~^ERROR `crate_type` within +//~| WARN this was previously accepted +#![cfg_attr(foo, crate_name="bar")] +//~^ERROR `crate_name` within +//~| WARN this was previously accepted + +fn main() {} diff --git a/src/test/ui/cfg/future-compat-crate-attributes-using-cfg_attr.stderr b/src/test/ui/cfg/future-compat-crate-attributes-using-cfg_attr.stderr new file mode 100644 index 00000000000..5df2eacc96e --- /dev/null +++ b/src/test/ui/cfg/future-compat-crate-attributes-using-cfg_attr.stderr @@ -0,0 +1,26 @@ +error: `crate_type` within an `#![cfg_attr] attribute is deprecated` + --> $DIR/future-compat-crate-attributes-using-cfg_attr.rs:5:18 + | +LL | #![cfg_attr(foo, crate_type="bin")] + | ^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/future-compat-crate-attributes-using-cfg_attr.rs:4:9 + | +LL | #![deny(warnings)] + | ^^^^^^^^ + = note: `#[deny(deprecated_cfg_attr_crate_type_name)]` implied by `#[deny(warnings)]` + = 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 + +error: `crate_name` within an `#![cfg_attr] attribute is deprecated` + --> $DIR/future-compat-crate-attributes-using-cfg_attr.rs:8: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 + +error: aborting due to 2 previous errors +