Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate crate_type and crate_name nested inside #![cfg_attr] #83744

Merged
merged 1 commit into from
Dec 8, 2021

Conversation

bjorn3
Copy link
Member

@bjorn3 bjorn3 commented Apr 1, 2021

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

#![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.

@bjorn3 bjorn3 added T-lang Relevant to the language team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. labels Apr 1, 2021
@rust-highfive
Copy link
Contributor

r? @matthewjasper

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 1, 2021
@rust-log-analyzer

This comment has been minimized.

@bjorn3 bjorn3 force-pushed the deprecate_cfg_attr_crate_type_name branch from 55d8d5f to 13a7a35 Compare April 1, 2021 10:35
@rust-log-analyzer

This comment has been minimized.

@bjorn3
Copy link
Member Author

bjorn3 commented Apr 1, 2021

Support for this was introduced in #25399.

Part of the rationale was: #25347 (comment)

I'm actually kind of surprised to see this bug. I would have expected #![cfg_attr] to be used in order to enable features based on cargo features, e.g. a cargo feature nightly that enables the use of features such that the same lib can be used in the beta and nightly channels.

This is actually not true. #![crate_name] must match the value passed in using --crate-name by cargo.

$ echo '#![crate_name = "foo"] fn main() {}' | rustc - --crate-name bar
error: `--crate-name` and `#[crate_name]` are required to match, but `bar` != `foo`
 --> <anon>:1:1
  |
1 | #![crate_name = "foo"] fn main() {}
  | ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Surprisingly #![crate_type] is even completely ignored when using --crate-type:

$ echo '#![crate_type = "lib"] fn main() {}' | rustc - --crate-type bin
$ ls
rust_out

I thought it merely had to match, just like the crate name.

@joshtriplett
Copy link
Member

@bjorn3 To confirm, based on your knowledge of the compiler internals, you believe that if we manage to remove this and require that these directives only appear at the top level if at all, we can substantially simplify the logic to handle them in the Rust compiler? Your description that it's "somewhat of a hack in rustc" sounds like there's simplification to be had, and that simplification would be a reasonable motivation to make this change if it turns out no crates use it.

@bjorn3
Copy link
Member Author

bjorn3 commented Apr 1, 2021

Your description that it's "somewhat of a hack in rustc" sounds like there's simplification to be had

Indeed

and that simplification would be a reasonable motivation to make this change if it turns out no crates use it.

IMHO yes. In addition #![cfg_attr(..., crate_type = "...")] can't be supported by rust-gcc.

@joshtriplett
Copy link
Member

The former seems like a sufficient rationale to give this a try. Starting a try run in preparation for a crater run. If the crater run comes back clean, this seems potentially reasonable.

@joshtriplett
Copy link
Member

@bors try

@bors
Copy link
Collaborator

bors commented Apr 2, 2021

⌛ Trying commit 8e4eb5f3283e0d02c4d46d144e6146011d1d4e73 with merge 45d8b6e68beee1c9c1ce92ae258a7e02f8f13aa4...

@bors
Copy link
Collaborator

bors commented Apr 2, 2021

☀️ Try build successful - checks-actions
Build commit: 45d8b6e68beee1c9c1ce92ae258a7e02f8f13aa4 (45d8b6e68beee1c9c1ce92ae258a7e02f8f13aa4)

@bjorn3
Copy link
Member Author

bjorn3 commented Apr 2, 2021

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-83744 created and queued.
🤖 Automatically detected try build 45d8b6e68beee1c9c1ce92ae258a7e02f8f13aa4
⚠️ Try build based on commit 4fa76a4, but latest commit is 8e4eb5f3283e0d02c4d46d144e6146011d1d4e73. Did you forget to make a new try build?
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 2, 2021
@craterbot
Copy link
Collaborator

🚧 Experiment pr-83744 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-83744 is completed!
📊 9 regressed and 17 fixed (154464 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the blacklist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Apr 22, 2021
@bjorn3
Copy link
Member Author

bjorn3 commented Apr 23, 2021

All regressions are spurious. They are either network errors or linkers crashing without any output.

@est31
Copy link
Member

est31 commented Apr 23, 2021

That's really good, although a crater report might not be best suited to turn up cases where these things may occur, as I expect them to be used mainly outside of cargo built codebases.

I've looke for crate_type and crate_name on google's opensource code search, couldn't find anything that could cause a problem.

I've used searchcode.com to look for any repos on gitlab or bitbucket, but no cfg_attr use showed up. crate_type, crate_name.

Using searchcode.com for github turns up too much repetitive stuff for it to be sifted through reasonably in a manual fashion. Same goes for github's builtin search, no way to narrow it down. Anyone know of a better search engine?

@bjorn3
Copy link
Member Author

bjorn3 commented Apr 23, 2021

grep.app shows exactly one usage: https://github.com/dropbox/divans/blob/23459c22f8a63e0a70ef1b968402ae0b3682168f/src/lib.rs#L21 This also enables two feature gates when the same cfg is used and they use cargo so it shouldn't have any effect.

(I believe grep.app doesn't search all of github, but it does search a large portion in my experience)

@est31
Copy link
Member

est31 commented Apr 23, 2021

Interesting, thanks for pointing to grep.app!

I'm a bit familiar with the repo it found, having filed a PR against it in 2018. It has been ignored for 2 years until I closed it. In that same period, nothing else has been commited to the repo's master branch (or any other PR filed/merged). So it's kinda dead I'd say. It might be used internally at dropbox, idk.

So overall the fallout is manageable I think.

@bjorn3
Copy link
Member Author

bjorn3 commented Apr 28, 2021

Can this land as is, or should I turn it into a future compatibility lint?

@Mark-Simulacrum
Copy link
Member

Discussed during lang meeting today -- we agreed this doesn't need FCP given just adding a future compatibility warning. I'll take a look at the impl itself shortly and request changes or indicate approval modulo the rebase.

Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me with the future compat issue filed

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 6, 2021
@bjorn3 bjorn3 force-pushed the deprecate_cfg_attr_crate_type_name branch from 71f5717 to ec3ec98 Compare December 7, 2021 15:38
@bjorn3 bjorn3 force-pushed the deprecate_cfg_attr_crate_type_name branch from ec3ec98 to 8f6a8d6 Compare December 7, 2021 15:50
@bjorn3 bjorn3 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 7, 2021
@Mark-Simulacrum Mark-Simulacrum force-pushed the deprecate_cfg_attr_crate_type_name branch from 8f6a8d6 to 9b6c510 Compare December 7, 2021 16:48
@Mark-Simulacrum
Copy link
Member

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Dec 7, 2021

📌 Commit 9b6c510 has been approved by Mark-Simulacrum

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 7, 2021
@bors bors merged commit da158c0 into rust-lang:master Dec 8, 2021
@rustbot rustbot added this to the 1.59.0 milestone Dec 8, 2021
@bjorn3 bjorn3 deleted the deprecate_cfg_attr_crate_type_name branch December 8, 2021 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.