-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Make the current crate's name available in Session
#153924
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,12 +33,12 @@ use rustc_parse::lexer::StripTokens; | |
| use rustc_parse::{new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal}; | ||
| use rustc_passes::{abi_test, input_stats, layout_test}; | ||
| use rustc_resolve::{Resolver, ResolverOutputs}; | ||
| use rustc_session::Session; | ||
| use rustc_session::config::{CrateType, Input, OutFileName, OutputFilenames, OutputType}; | ||
| use rustc_session::cstore::Untracked; | ||
| use rustc_session::output::{filename_for_input, invalid_output_for_target}; | ||
| use rustc_session::parse::feature_err; | ||
| use rustc_session::search_paths::PathKind; | ||
| use rustc_session::{CrateName, Session}; | ||
| use rustc_span::{ | ||
| DUMMY_SP, ErrorGuaranteed, ExpnKind, SourceFileHash, SourceFileHashAlgorithm, Span, Symbol, sym, | ||
| }; | ||
|
|
@@ -309,14 +309,13 @@ fn configure_and_expand( | |
| fn print_macro_stats(ecx: &ExtCtxt<'_>) { | ||
| use std::fmt::Write; | ||
|
|
||
| let crate_name = ecx.ecfg.crate_name.as_str(); | ||
| let crate_name = if crate_name == "build_script_build" { | ||
| let crate_name = if ecx.ecfg.crate_name == sym::build_script_build { | ||
| // This is a build script. Get the package name from the environment. | ||
| let pkg_name = | ||
| std::env::var("CARGO_PKG_NAME").unwrap_or_else(|_| "<unknown crate>".to_string()); | ||
| format!("{pkg_name} build script") | ||
| } else { | ||
| crate_name.to_string() | ||
| ecx.ecfg.crate_name.to_string() | ||
| }; | ||
|
|
||
| // No instability because we immediately sort the produced vector. | ||
|
|
@@ -508,7 +507,6 @@ fn generated_output_paths( | |
| tcx: TyCtxt<'_>, | ||
| outputs: &OutputFilenames, | ||
| exact_name: bool, | ||
| crate_name: Symbol, | ||
| ) -> Vec<PathBuf> { | ||
| let sess = tcx.sess; | ||
| let mut out_filenames = Vec::new(); | ||
|
|
@@ -520,7 +518,7 @@ fn generated_output_paths( | |
| // by appending `.rlib`, `.exe`, etc., so we can skip this transformation. | ||
| OutputType::Exe if !exact_name => { | ||
| for crate_type in tcx.crate_types().iter() { | ||
| let p = filename_for_input(sess, *crate_type, crate_name, outputs); | ||
| let p = filename_for_input(sess, *crate_type, outputs); | ||
| out_filenames.push(p.as_path().to_path_buf()); | ||
| } | ||
| } | ||
|
|
@@ -814,11 +812,9 @@ pub fn write_dep_info(tcx: TyCtxt<'_>) { | |
|
|
||
| let sess = tcx.sess; | ||
| let _timer = sess.timer("write_dep_info"); | ||
| let crate_name = tcx.crate_name(LOCAL_CRATE); | ||
|
|
||
| let outputs = tcx.output_filenames(()); | ||
| let output_paths = | ||
| generated_output_paths(tcx, outputs, sess.io.output_file.is_some(), crate_name); | ||
| let output_paths = generated_output_paths(tcx, outputs, sess.io.output_file.is_some()); | ||
|
|
||
| // Ensure the source file isn't accidentally overwritten during compilation. | ||
| if let Some(input_path) = sess.io.input.opt_path() { | ||
|
|
@@ -920,7 +916,8 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>( | |
|
|
||
| let pre_configured_attrs = rustc_expand::config::pre_configure_attrs(sess, &krate.attrs); | ||
|
|
||
| let crate_name = get_crate_name(sess, &pre_configured_attrs); | ||
| load_crate_name(sess, &pre_configured_attrs); | ||
|
|
||
| let crate_types = collect_crate_types( | ||
| sess, | ||
| &compiler.codegen_backend.supported_crate_types(sess), | ||
|
|
@@ -929,15 +926,15 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>( | |
| krate.spans.inner_span, | ||
| ); | ||
| let stable_crate_id = StableCrateId::new( | ||
| crate_name, | ||
| sess.crate_name(), | ||
| crate_types.contains(&CrateType::Executable), | ||
| sess.opts.cg.metadata.clone(), | ||
| sess.cfg_version, | ||
| ); | ||
|
|
||
| let outputs = util::build_output_filenames(&pre_configured_attrs, sess); | ||
| let outputs = util::build_output_filenames(sess); | ||
|
|
||
| let dep_graph = setup_dep_graph(sess, crate_name, stable_crate_id); | ||
| let dep_graph = setup_dep_graph(sess, stable_crate_id); | ||
|
|
||
| let cstore = | ||
| FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _); | ||
|
|
@@ -1001,14 +998,12 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>( | |
| |tcx| { | ||
| let feed = tcx.create_crate_num(stable_crate_id).unwrap(); | ||
| assert_eq!(feed.key(), LOCAL_CRATE); | ||
| feed.crate_name(crate_name); | ||
| feed.crate_name(sess.crate_name()); | ||
|
|
||
| let feed = tcx.feed_unit_query(); | ||
| feed.features_query(tcx.arena.alloc(rustc_expand::config::features( | ||
| tcx.sess, | ||
| &pre_configured_attrs, | ||
| crate_name, | ||
| ))); | ||
| feed.features_query( | ||
| tcx.arena.alloc(rustc_expand::config::features(tcx.sess, &pre_configured_attrs)), | ||
| ); | ||
| feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs)))); | ||
| feed.output_filenames(Arc::new(outputs)); | ||
|
|
||
|
|
@@ -1257,8 +1252,25 @@ pub(crate) fn start_codegen<'tcx>( | |
| (codegen, crate_info, metadata) | ||
| } | ||
|
|
||
| /// Compute and validate the crate name. | ||
| pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> Symbol { | ||
| /// Compute and validate the crate name, then store it on the Session. | ||
| /// | ||
| /// NOTE: this function will panic if called more than once in the same Session. | ||
| #[expect(deprecated, reason = "initial crate name loading")] | ||
| #[track_caller] | ||
| pub fn load_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) { | ||
| let new_name = get_crate_name(sess, krate_attrs); | ||
|
|
||
| // When we have `--print=file-names,crate-name`, we try to load the crate name more than once. | ||
| // Rather than panicking, just allow that as long as we'd use the same name both times. | ||
| if let Some(existing) = sess.crate_name.get() | ||
| && *existing == new_name | ||
| { | ||
| return; | ||
| } | ||
| sess.crate_name.set(new_name).expect("`load_crate_name` called more than once!"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not that big of a fan of modifying global state.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you see an alternative? I document on the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Respecting
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, if
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't think parsing should depend on knowing the crate name in general, I guess occasionally it may be useful for diagnostics (but need examples), but then it can be passed from the outside as well.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My inclination would also have been to figure out how to have TyCtxt available earlier or do the parts that need the crate name later. I'd need to do a dive to get the full picture here, but I'm happy to do that and then do some brainstorming session to see what other options we have to make this all nicer |
||
| } | ||
|
|
||
| fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> CrateName { | ||
| // We validate *all* occurrences of `#![crate_name]`, pick the first find and | ||
| // if a crate name was passed on the command line via `--crate-name` we enforce | ||
| // that they match. | ||
|
|
@@ -1269,11 +1281,7 @@ pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> Symbol | |
| let attr_crate_name = | ||
| parse_crate_name(sess, krate_attrs, ShouldEmit::EarlyFatal { also_emit_lints: true }); | ||
|
|
||
| let validate = |name, span| { | ||
| rustc_session::output::validate_crate_name(sess, name, span); | ||
| name | ||
| }; | ||
|
|
||
| #[expect(deprecated, reason = "sess.crate_name isn't set yet")] | ||
| if let Some(crate_name) = &sess.opts.crate_name { | ||
| let crate_name = Symbol::intern(crate_name); | ||
| if let Some((attr_crate_name, span)) = attr_crate_name | ||
|
|
@@ -1285,11 +1293,11 @@ pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> Symbol | |
| attr_crate_name, | ||
| }); | ||
| } | ||
| return validate(crate_name, None); | ||
| return CrateName::from_normalized(sess, crate_name, None); | ||
| } | ||
|
|
||
| if let Some((crate_name, span)) = attr_crate_name { | ||
| return validate(crate_name, Some(span)); | ||
| return CrateName::from_normalized(sess, crate_name, Some(span)); | ||
| } | ||
|
|
||
| if let Input::File(ref path) = sess.io.input | ||
|
|
@@ -1298,11 +1306,11 @@ pub fn get_crate_name(sess: &Session, krate_attrs: &[ast::Attribute]) -> Symbol | |
| if file_stem.starts_with('-') { | ||
| sess.dcx().emit_err(errors::CrateNameInvalid { crate_name: file_stem }); | ||
| } else { | ||
| return validate(Symbol::intern(&file_stem.replace('-', "_")), None); | ||
| return CrateName::from_unnormalized(sess, file_stem, None); | ||
| } | ||
| } | ||
|
|
||
| sym::rust_out | ||
| CrateName::from_normalized(sess, sym::rust_out, None) | ||
| } | ||
|
|
||
| pub(crate) fn parse_crate_name( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs to stay a query so that it's possible to look up the crate name for other dependent crates.