Skip to content

Refactor crate name handling; pass a TyCtxt into macro expansion#155844

Open
jyn514 wants to merge 13 commits intorust-lang:mainfrom
jyn514:crate-name-refactors
Open

Refactor crate name handling; pass a TyCtxt into macro expansion#155844
jyn514 wants to merge 13 commits intorust-lang:mainfrom
jyn514:crate-name-refactors

Conversation

@jyn514
Copy link
Copy Markdown
Member

@jyn514 jyn514 commented Apr 26, 2026

Modified version of #153924 that doesn't try to change any dataflow, just clarify the existing behavior.

  • The first two commits are couple of crate_name cleanups #155835, which has already been approved.
  • The next five commits pass a TyCtxt into macro expansion, and make some easy cleanups that are possible as a result. b84658a should be the only commit that changes behavior (in diagnostics), I kept it intentionally small.
  • The next three commits are minor cleanups related to crate name handling.
  • b59aeac introduces a new CrateName struct that documents the difference between the filestem and crate name. Previously the compiler confused the two (unsure if this was intentional, the code was quite complicated). I have preserved the behavior even though I'm not sure it's correct; a bunch of run-make tests depend on it.
  • Now that all the hard bits are taken care of, d0d1967 lints on opts.crate_name and mops up a last couple places that shouldn't have been using it.
  • The last commit removes the temptation to use filestem() instead of CrateName.

I promise that I tried very hard not to change any behavior in this PR. Here is a transcript of some existing behavior on nightly:

$ cat x-y.rs
fn main() {}
$ rustc x-y.rs --print=file-names --crate-type=lib --crate-type=bin --crate-type=cdylib --crate-type=staticlib --crate-type=cdylib --crate-type=proc-macro  --print=crate-name
libx_y.rlib
x-y
libx_y.dylib
libx_y.a
libx_y.dylib
x_y
$ echo "fn main() {}" | rustc - --print=file-names --crate-type=bin --print=crate-name
rust_out
rust_out

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 26, 2026

Some changes occurred in compiler/rustc_builtin_macros/src/autodiff.rs

cc @ZuseZ4

Some changes occurred in check-cfg diagnostics

cc @Urgau

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Apr 26, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 26, 2026

r? @eholk

rustbot has assigned @eholk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 19 candidates

@jyn514
Copy link
Copy Markdown
Member Author

jyn514 commented Apr 26, 2026

r? @oli-obk cc @petrochenkov @nnethercote

@rustbot rustbot assigned oli-obk and unassigned eholk Apr 26, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 26, 2026

oli-obk is not on the review rotation at the moment.
They may take a while to respond.

@rust-log-analyzer

This comment has been minimized.

jyn514 added 4 commits April 26, 2026 23:18
Using it is almost always a mistake. Get rid of the temptation.

Rustdoc has to keep using it because it doesn't have a real crate name
when parsing markdown files.
@jyn514 jyn514 force-pushed the crate-name-refactors branch from a3fd25e to 8cbe665 Compare April 26, 2026 21:18
Comment on lines +222 to +224
let exe_path = tcx.output_filenames(()).path(OutputType::Exe);
let exe_stem = exe_path.filestem().unwrap().to_string_lossy();
config.args.insert(0, exe_stem.to_string());
Copy link
Copy Markdown
Member Author

@jyn514 jyn514 Apr 26, 2026

Choose a reason for hiding this comment

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

@saethlin already approved these changes in #153924 (comment).

View changes since the review

@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
 Documenting rustc_interface v0.0.0 (/checkout/compiler/rustc_interface)
warning: unresolved link to `TyCtxt::filestem`
    --> compiler/rustc_interface/src/passes.rs:1300:66
     |
1300 |     /// If you don't want the crate name to be normalized, use [`TyCtxt::filestem`].
     |                                                                  ^^^^^^^^^^^^^^^^ the struct `TyCtxt` has no field or associated item named `filestem`
     |
     = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default

 Documenting rustc_borrowck v0.0.0 (/checkout/compiler/rustc_borrowck)
 Documenting rustc_passes v0.0.0 (/checkout/compiler/rustc_passes)
 Documenting rustc_privacy v0.0.0 (/checkout/compiler/rustc_privacy)
error: `rustc_interface` (lib doc) generated 1 warning
error: warnings are denied by `build.warnings` configuration
warning: build failed, waiting for other jobs to finish...
Bootstrap failed while executing `doc compiler --stage 1`
Command `/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo doc -Zwarnings --target x86_64-unknown-linux-gnu -Zbinary-dep-depinfo -j 4 -Zroot-dir=/checkout --locked --color=always --profile=release --features 'llvm rustc' --manifest-path /checkout/compiler/rustc/Cargo.toml -Zskip-rustdoc-fingerprint --no-deps -Zrustdoc-map -p rustc-main -p rustc_abi -p rustc_arena -p rustc_ast -p rustc_ast_ir -p rustc_ast_lowering -p rustc_ast_passes -p rustc_ast_pretty -p rustc_attr_parsing -p rustc_baked_icu_data -p rustc_borrowck -p rustc_builtin_macros -p rustc_codegen_llvm -p rustc_codegen_ssa -p rustc_const_eval -p rustc_data_structures -p rustc_driver -p rustc_driver_impl -p rustc_error_codes -p rustc_error_messages -p rustc_errors -p rustc_expand -p rustc_feature -p rustc_fs_util -p rustc_graphviz -p rustc_hashes -p rustc_hir -p rustc_hir_analysis -p rustc_hir_id -p rustc_hir_pretty -p rustc_hir_typeck -p rustc_incremental -p rustc_index -p rustc_index_macros -p rustc_infer -p rustc_interface -p rustc_lexer -p rustc_lint -p rustc_lint_defs -p rustc_llvm -p rustc_log -p rustc_macros -p rustc_metadata -p rustc_middle -p rustc_mir_build -p rustc_mir_dataflow -p rustc_mir_transform -p rustc_monomorphize -p rustc_next_trait_solver -p rustc_parse -p rustc_parse_format -p rustc_passes -p rustc_pattern_analysis -p rustc_privacy -p rustc_proc_macro -p rustc_public -p rustc_public_bridge -p rustc_query_impl -p rustc_resolve -p rustc_sanitizers -p rustc_serialize -p rustc_session -p rustc_span -p rustc_symbol_mangling -p rustc_target -p rustc_thread_pool -p rustc_trait_selection -p rustc_traits -p rustc_transmute -p rustc_ty_utils -p rustc_type_ir -p rustc_type_ir_macros -p rustc_windows_rc [workdir=/checkout]` failed with exit code 101
Created at: src/bootstrap/src/core/build_steps/doc.rs:916:25
Executed at: src/bootstrap/src/core/build_steps/doc.rs:977:26

--- BACKTRACE vvv
   0: <bootstrap::utils::exec::DeferredCommand>::finish_process
             at /checkout/src/bootstrap/src/utils/exec.rs:939:17
   1: <bootstrap::utils::exec::DeferredCommand>::wait_for_output::<&bootstrap::utils::exec::ExecutionContext>
             at /checkout/src/bootstrap/src/utils/exec.rs:831:21
   2: <bootstrap::utils::exec::ExecutionContext>::run
             at /checkout/src/bootstrap/src/utils/exec.rs:741:45
   3: <bootstrap::utils::exec::BootstrapCommand>::run::<&bootstrap::core::builder::Builder>
             at /checkout/src/bootstrap/src/utils/exec.rs:339:27
   4: <bootstrap::core::build_steps::doc::Rustc as bootstrap::core::builder::Step>::run
             at /checkout/src/bootstrap/src/core/build_steps/doc.rs:977:26
   5: <bootstrap::core::builder::Builder>::ensure::<bootstrap::core::build_steps::doc::Rustc>
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1579:36
   6: <bootstrap::core::build_steps::doc::Rustc as bootstrap::core::builder::Step>::make_run
             at /checkout/src/bootstrap/src/core/build_steps/doc.rs:886:21
   7: <bootstrap::core::builder::StepDescription>::maybe_run
             at /checkout/src/bootstrap/src/core/builder/mod.rs:476:13
   8: bootstrap::core::builder::cli_paths::match_paths_to_steps_and_run
             at /checkout/src/bootstrap/src/core/builder/cli_paths.rs:232:18
   9: <bootstrap::core::builder::Builder>::run_step_descriptions
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1122:9
  10: <bootstrap::core::builder::Builder>::execute_cli
             at /checkout/src/bootstrap/src/core/builder/mod.rs:1101:14
  11: <bootstrap::Build>::build
             at /checkout/src/bootstrap/src/lib.rs:803:25
  12: bootstrap::main
             at /checkout/src/bootstrap/src/bin/main.rs:130:11
  13: <fn() as core::ops::function::FnOnce<()>>::call_once
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library/core/src/ops/function.rs:250:5
  14: std::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library/std/src/sys/backtrace.rs:166:18
  15: std::rt::lang_start::<()>::{closure#0}
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library/std/src/rt.rs:206:18
  16: <&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe as core::ops::function::FnOnce<()>>::call_once
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library/core/src/ops/function.rs:287:21
  17: std::panicking::catch_unwind::do_call::<&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe, i32>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library/std/src/panicking.rs:581:40
  18: std::panicking::catch_unwind::<i32, &dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library/std/src/panicking.rs:544:19
  19: std::panic::catch_unwind::<&dyn core::ops::function::Fn<(), Output = i32> + core::marker::Sync + core::panic::unwind_safe::RefUnwindSafe, i32>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library/std/src/panic.rs:359:14
  20: std::rt::lang_start_internal::{closure#0}
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library/std/src/rt.rs:175:24
  21: std::panicking::catch_unwind::do_call::<std::rt::lang_start_internal::{closure#0}, isize>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library/std/src/panicking.rs:581:40
---
  28: __libc_start_main
  29: _start


Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:01:46
  local time: Sun Apr 26 22:07:20 UTC 2026
  network time: Sun, 26 Apr 2026 22:07:20 GMT
##[error]Process completed with exit code 1.
##[group]Run echo "disk usage:"

@nnethercote
Copy link
Copy Markdown
Contributor

No complaints from me, but one question about the motivation. #153924 said:

the long-term goal for this is to be able to further split up the crate graph by removing dependencies on TyCtxt that only need the crate name, such as in rustc_attr_parsing.

Some of the early commits (involving ExtCtxt) increase the use of TyCtxt. Is it a use-tcx-more-in-some-places-now-to-use-it-less-in-others-later situation? I can't picture it in my head.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants