Repository navigation
Description
What is this lint about
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. For example:
#![cfg_attr(debug_assertions, crate_type = "lib")]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.
How to fix this warning/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 = "...")].
We are interested to hear about any/all use cases for conditional #![crate_type] and #![crate_name] attributes.
Current status
- Deprecate crate_type and crate_name nested inside #![cfg_attr] #83744 introduces the
deprecated_cfg_attr_crate_type_namelint as warn-by-default - Make forward compatibility lint deprecated_cfg_attr_crate_type_name deny by default #99784 makes the
deprecated_cfg_attr_crate_type_namelint deny-by-default - PR Make deprecated_cfg_attr_crate_type_name a hard error #129670 makes the
deprecated_cfg_attr_crate_type_namelint a hard error
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIdea
Activity
est31 commentedon Jul 26, 2022
I've filed a PR to make this deny-by-default: #99784
Rollup merge of rust-lang#99784 - est31:deny_cfg_attr_crate_type_name…
Rollup merge of rust-lang#99784 - est31:deny_cfg_attr_crate_type_name…
est31 commentedon Mar 21, 2023
Since the grep.app seach conducted in the original PR there have been two new crates using it, despite the lint:
wasmer:
azul:
Both uses were added in 2021, after #83744 was opened and before it was merged. Also both only modify
crate_type. As @bjorn3 has asked above about use cases forcrate_typewithincfg_attr(), it might be interesting to think about alternatives one can suggest to the maintainers of these libraries.Maybe one could extend cargo to support
cargo:rustc-crate-typein build.rs scripts? Alternatively, one could maybe suggest adding a crate that hascrate-typeset in Cargo.toml, and theninclude!()s thelib.rs?bjorn3 commentedon Mar 21, 2023
Wouldn't
cargo build --crate-type cdylibsuffice in both cases?est31 commentedon Mar 21, 2023
It should actually, good point. Maybe the
wasm-packequivalent of it forwasmer, but yes. Also as you've wrote earlier,#![crate_type = "..."]is totally ignored if--crate-typeis present (only then though, if it's not present it's not ignored, but it seems that cargo always sets it?? not 100% sure). Ideally one would interact with downstream though before proposing a PR to remove.ia0 commentedon Jun 14, 2023
This doesn't work for me. My understanding is that
--crate-typeis only arustcflag, you can't use it withcargo build. I would really like it to work withcargo buildthough. Is this planned?bjorn3 commentedon Jun 14, 2023
Right,
--crate-typeis a flag forcargo rustc, notcargo build. My bad.cargo rustcshould work in most cases thatcargo builddoes. The main limitation is that it requires a single target within a single package rather than allowing multiple packages and targets.ia0 commentedon Jun 15, 2023
Oh that's very interesting, I didn't know that. I always assumed
cargo rustcwas just a wrapper around a single invocation ofrustcrequiring you to compile dependencies separately and pass them on the command line. That's good to know, that should solve my issue. I think there's almost no cases wherecargo buildis useful to me then since I always use a single package (I never use workspaces) and always compile for a single target.est31 commentedon Aug 5, 2023
Nice, seems the azul usage has been removed in the meantime: fschutt/azul@6096a1b#diff-2486c782348527d5e8a121166f399ed1449c5960b8079859b573cbc5f8b184c2L3
Now only the usage in wasmer is remaining. It has been reduced to a single attr:
https://github.com/wasmerio/wasmer/blob/ccbe870770604ecb61f2d8e0fa298504381f6784/lib/api/src/lib.rs#L29
I wonder whether it would be possible to enable
cdylibon the api crate unconditionally?nyurik commentedon Sep 5, 2023
Note that the docs still suggest this attribute
bjorn3 commentedon Sep 5, 2023
Using plain
#![crate_name = "..."]and#![crate_type = "..."]is fine. Using them behind a#![cfg_attr(...)]triggers this lint.nyurik commentedon Sep 5, 2023
Is there a recommended way for feature-controlled
crate-type, e.g. with abuild.rs? A crate with optionalcdylibtype would also need to have a feature-gated panic handler and possibly other things, so it would still have to have acdylibfeature in Cargo.toml. So one would have to run this -- a probable source of bugs?RUSTFLAGS='--crate-type cdylib' cargo build --features cdylibP.S. Good point @bjorn3, thx.
bjorn3 commentedon Sep 5, 2023
cargo rustcaccepts--crate-typeto override the crate type. A build script can't change the crate type and#![crate_type]doesn't work as youbl did intend with cargo anyway. Rustc requires that--crate-typeand#![crate_type]match up if both are specified afaik and cargo always passes--crate-type.19 remaining items
[-]Tracking issue for future-incompatible lint `deprecated_cfg_attr_crate_type_name`[/-][+]Tracking issue for future-incompatibility lint `deprecated_cfg_attr_crate_type_name`[/+]Auto merge of rust-lang#129670 - est31:cfg_attr_crate_type_name_error…
est31 commentedon Oct 8, 2024
Closing as #129670 has been merged. It's on the path to being a hard error in the 1.83.0 release of Rust.
est31 commentedon Oct 17, 2024
I've tried to work on the followup refactor to #129670 to remove mutable state from
Sessionbut found that it's already been done in #114622. With that PR, what is written in the explanation section of this issue is obsolete: there is no mutable state inSessionany more. I'm wondering now about further justifications for the change, not from a "should we halt this now" point of view, but from a "how to explain it" view.directory for the incr comp session dir
In #83744 @bjorn3 writes:
general ordering concerns
With this change it's easier to determine the crate name earlier in the compilation, which might be useful for structuring the state. All one needs is to peek into the provided source file, with no other state needed. Obtaining the crate name and crate type are very dependency-free operations now.
bjorn3 commentedon Oct 17, 2024
I think it would now be possible to revert #114622 in a way that doesn't re-introduce the global mutable state in TyCtxt. This would also allow
rust/compiler/rustc_incremental/src/persist/fs.rs
Lines 611 to 612 in 06d261d
rust/compiler/rustc_interface/src/util.rs
Lines 466 to 470 in 06d261d
#![crate_type]correctly at all right now.) Andrust/compiler/rustc_driver_impl/src/lib.rs
Lines 752 to 753 in 06d261d
rust/compiler/rustc_driver_impl/src/lib.rs
Line 766 in 06d261d
fmease commentedon Oct 17, 2024
Incidentally my open PR #127581 does away with recomputing the crate name inside
fs.rs.est31 commentedon Oct 17, 2024
@bjorn3 @fmease thank you!
feat(rln-wasm): bring back wasm support for zerokit
community/lcov2cobertura: remove deprecated lints to fix build