Skip to content

Commit da158c0

Browse files
authored
Rollup merge of #83744 - bjorn3:deprecate_cfg_attr_crate_type_name, r=Mark-Simulacrum
Deprecate crate_type and crate_name nested inside #![cfg_attr] This implements the proposal in #83676 (comment), with a future compatibility lint imposed on usage of crate_type/crate_name inside cfg's. This is a compromise between removing `#![crate_type]` and `#![crate_name]` completely and keeping them as a whole, which requires somewhat of a hack in rustc and is impossible to support by gcc-rust. By only removing `#![crate_type]` and `#![crate_name]` nested inside `#![cfg_attr]` it becomes possible to parse them before a big chunk of the compiler has started. Replaces #83676 ```rust #![crate_type = "lib"] // remains working #![cfg_attr(foo, crate_type = "bin")] // will stop working ``` # Rationale As it currently is it is possible to try to access the stable crate id before it is actually set, which will panic. The fact that the Session contains mutable state beyond debugging things also doesn't completely sit well with me. Especially once parallel rustc becomes the default. I think there is currently also a cyclic dependency where you need to set the stable crate id to be able to load crates, but you need to load crates to expand proc macro attributes that may define #![crate_name] or #![crate_type]. Currently crate level proc macro attributes are unstable or completely unsupported (can't remember which), so this is not a problem, but it may become an issue in the future. Finally if we want to add incremental compilation to macro expansion or even parsing, we need the StableCrateId to be created together with the Session or even earlier as incremental compilation determines the incremental compilation session dir based on the StableCrateId.
2 parents abba5ed + 9b6c510 commit da158c0

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)