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.

@petrochenkov petrochenkov self-assigned this Apr 27, 2026
@jyn514
Copy link
Copy Markdown
Member Author

jyn514 commented Apr 27, 2026

@nnethercote no, I gave up on that part of it. Now the motivation is just to have a consistent handling of crate-name so that it’s not implicit based on re-parsing the crate root over and over.


pub trait ResolverExpand {
pub trait ResolverExpand<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx>;
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 27, 2026

Choose a reason for hiding this comment

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

I'd add this much earlier, if the required amount of additional lifetime annotations weren't so enormous. The resulting cleanups barely compensate it.
But I guess it needs to happen sooner or later, given the common trend of using tcx earlier.

View changes since the review

Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 27, 2026

Choose a reason for hiding this comment

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

I suspect that we can actually allocate Resolver on tcx arena now, so ExtCtxt will contain pub resolver: &'tcx mut dyn ResolverExpand<'tcx> and the need in the second lifetime will disappear.

(Preferably in a separate PR, creating mega-PRs with combined different changes doesn't make sense if reviewers are consistently available rather than appear once in a few weeks. E.g. this PR currently hides a few meaningful changes among hundreds of added '_s.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it may be possible to make this function return a "short lived" TyCtxt<'_> without being an issue for the current use cases (they don't need to mutate via ResolverExpand after obtaining the TyCtxt<'_>. That would avoid back & forth churn if the lifetimes get merged in the future anyway, and anyone trying to use it more generally later would first need to do the big lifetime commit and not just be able to rely on the lifetime

/// -> expn_data` of their expansion context stored into their span.
pub struct ExtCtxt<'a> {
pub struct ExtCtxt<'a, 'tcx> {
pub sess: &'a Session,
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 27, 2026

Choose a reason for hiding this comment

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

This field is no longer necessary.

View changes since the review

/// Operations on the crate name.
impl<'tcx> TyCtxt<'tcx> {
pub fn local_crate_name(self) -> Symbol {
self.crate_name(LOCAL_CRATE)
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 27, 2026

Choose a reason for hiding this comment

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

This is too trivial and doesn't deserve a separate function, IMO.
The remaining is_build_script can then be moved to one of the existing impl TyCtxt blocks.

View changes since the review

}

pub fn interface_path(&self) -> PathBuf {
debug!("using crate_name={} for interface_path", self.crate_stem);
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 27, 2026

Choose a reason for hiding this comment

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

Nit: could you remove the added debugging statements before merging, they are almost never useful outside of some specific debugging session.

View changes since the review

format!("{pkg_name} build script")
} else {
crate_name.to_string()
ecx.resolver.tcx().crate_name(LOCAL_CRATE).as_str().to_string()
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 27, 2026

Choose a reason for hiding this comment

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

Suggested change
ecx.resolver.tcx().crate_name(LOCAL_CRATE).as_str().to_string()
ecx.resolver.tcx().crate_name(LOCAL_CRATE).to_string()

View changes since the review

validate_crate_name(sess, normalized, span);

CrateName { normalized, unnormalized: Symbol::intern(unnormalized) }
}
Copy link
Copy Markdown
Contributor

@petrochenkov petrochenkov Apr 27, 2026

Choose a reason for hiding this comment

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

The from_(un)normalized are small, private to this file and used only once in another "constructor" for CrateName, I'd rather inline and remove them.

View changes since the review

@petrochenkov petrochenkov 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 Apr 27, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 27, 2026

☔ The latest upstream changes (presumably #155760) made this pull request unmergeable. Please resolve the merge conflicts.

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

7 participants