Skip to content

Commit

Permalink
Future compatibility warning on cfg_attr on crate_type and crate_name
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 authored and Mark-Simulacrum committed Dec 7, 2021
1 parent c5c9494 commit 9b6c510
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 13 deletions.
19 changes: 18 additions & 1 deletion compiler/rustc_expand/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
36 changes: 36 additions & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/rust-lang/rust/issues/91632>",
};
}

declare_lint_pass! {
/// Does nothing as a lint pass, but registers some `Lint`s
/// that are used by other parts of the compiler.
Expand Down Expand Up @@ -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,
]
}

Expand Down
6 changes: 0 additions & 6 deletions src/test/ui/cfg/auxiliary/crate-attributes-using-cfg_attr.rs

This file was deleted.

6 changes: 0 additions & 6 deletions src/test/ui/cfg/crate-attributes-using-cfg_attr.rs

This file was deleted.

12 changes: 12 additions & 0 deletions src/test/ui/cfg/future-compat-crate-attributes-using-cfg_attr.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/91632>

error: aborting due to 2 previous errors

0 comments on commit 9b6c510

Please sign in to comment.