Skip to content

Remove (useless) argument of entry_fn query #71648

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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/debuginfo/mod.rs
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ use rustc_codegen_ssa::debuginfo::type_names;
use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext, VariableKind};
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap};
use rustc_index::vec::IndexVec;
use rustc_middle::mir;
use rustc_middle::ty::layout::HasTyCtxt;
@@ -289,7 +289,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
if self.sess().opts.optimize != config::OptLevel::No {
spflags |= DISPFlags::SPFlagOptimized;
}
if let Some((id, _)) = self.tcx.entry_fn(LOCAL_CRATE) {
if let Some((id, _)) = self.tcx.entry_fn(()) {
if id.to_def_id() == def_id {
spflags |= DISPFlags::SPFlagMainSubprogram;
}
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/back/symbol_export.rs
Original file line number Diff line number Diff line change
@@ -178,7 +178,7 @@ fn exported_symbols_provider_local(
.map(|(&def_id, &level)| (ExportedSymbol::NonGeneric(def_id), level))
.collect();

if tcx.entry_fn(LOCAL_CRATE).is_some() {
if tcx.entry_fn(()).is_some() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we give a name (LocalCrate) to the unit struct that implements query::Key? Otherwise, it's not immediately clear to me what this line is doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That seems like a good idea to me. @eddyb do you agree?

Copy link
Member

Choose a reason for hiding this comment

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

That's one way. But this is basically a nulary query.
If we tweaked the macros a bit we could write this instead:

if tcx.entry_fn().is_some() {

Copy link
Contributor

Choose a reason for hiding this comment

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

If we go that route, we should rename the entry_fn query to entry_fn_local_crate. Otherwise the same readability issues apply.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if adding an improved doc comment would suffice? It seems reasonable clear to me that the entry_fn computes, well, the entry function -- I guess the thing that is sort of non-obvious is the overall model where the root crate has an entry function, and it seems like that would be best explained in docs?

In particular, entry_fn_for_local_crate (if we're going to use a long name, I'd prefer to make it read well...) seems to imply that other crates can have entry functions, which isn't really true?

Anyway, a minor thing, but I guess it may become a more wide-spread convention either way.

let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new("main"));

symbols.push((exported_symbol, SymbolExportLevel::C));
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/base.rs
Original file line number Diff line number Diff line change
@@ -392,7 +392,7 @@ pub fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
cx: &'a Bx::CodegenCx,
) -> Option<Bx::Function> {
let (main_def_id, span) = match cx.tcx().entry_fn(LOCAL_CRATE) {
let (main_def_id, span) = match cx.tcx().entry_fn(()) {
Some((def_id, _)) => (def_id, cx.tcx().def_span(def_id)),
None => return None,
};
@@ -407,7 +407,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(

let main_llfn = cx.get_fn_addr(instance);

return cx.tcx().entry_fn(LOCAL_CRATE).map(|(_, et)| {
return cx.tcx().entry_fn(()).map(|(_, et)| {
let use_start_lang_item = EntryFnType::Start != et;
create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, use_start_lang_item)
});
3 changes: 1 addition & 2 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
@@ -784,8 +784,7 @@ fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
sess.time("misc_checking_1", || {
parallel!(
{
entry_point = sess
.time("looking_for_entry_point", || rustc_passes::entry::find_entry_point(tcx));
entry_point = sess.time("looking_for_entry_point", || tcx.entry_fn(()));

sess.time("looking_for_plugin_registrar", || {
plugin::build::find_plugin_registrar(tcx)
2 changes: 1 addition & 1 deletion src/librustc_interface/queries.rs
Original file line number Diff line number Diff line change
@@ -288,7 +288,7 @@ impl<'tcx> Queries<'tcx> {
/// to write compile-fail tests that actually test that compilation succeeds without reporting
/// an error.
fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) {
let def_id = match tcx.entry_fn(LOCAL_CRATE) {
let def_id = match tcx.entry_fn(()) {
Some((def_id, _)) => def_id,
_ => return,
};
2 changes: 1 addition & 1 deletion src/librustc_middle/mir/mono.rs
Original file line number Diff line number Diff line change
@@ -90,7 +90,7 @@ impl<'tcx> MonoItem<'tcx> {

match *self {
MonoItem::Fn(ref instance) => {
let entry_def_id = tcx.entry_fn(LOCAL_CRATE).map(|(id, _)| id);
let entry_def_id = tcx.entry_fn(()).map(|(id, _)| id);
// If this function isn't inlined or otherwise has explicit
// linkage, then we'll be creating a globally shared version.
if self.explicit_linkage(tcx).is_some()
4 changes: 2 additions & 2 deletions src/librustc_middle/query/mod.rs
Original file line number Diff line number Diff line change
@@ -884,9 +884,9 @@ rustc_queries! {
desc { "looking up the foreign modules of a linked crate" }
}

/// Identifies the entry-point (e.g., the `main` function) for a given
/// Identifies the entry-point (e.g., the `main` function) for the current
/// crate, returning `None` if there is no entry point (such as for library crates).
query entry_fn(_: CrateNum) -> Option<(LocalDefId, EntryFnType)> {
query entry_fn(_: ()) -> Option<(LocalDefId, EntryFnType)> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't this query only get executed once in the entire dependency tree? So if a lib crate calls the query, you get None and then all other crates just yield that None?

desc { "looking up the entry function of a crate" }
}
query plugin_registrar_fn(_: CrateNum) -> Option<DefId> {
14 changes: 13 additions & 1 deletion src/librustc_middle/ty/query/keys.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ use crate::traits;
use crate::ty::fast_reject::SimplifiedType;
use crate::ty::subst::{GenericArg, SubstsRef};
use crate::ty::{self, Ty, TyCtxt};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_query_system::query::DefaultCacheSelector;
use rustc_span::symbol::Symbol;
use rustc_span::{Span, DUMMY_SP};
@@ -25,6 +25,18 @@ pub trait Key {
fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
}

impl Key for () {
Copy link
Member

Choose a reason for hiding this comment

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

Wait, this is new? I thought there was precedent for this. I guess we can still go ahead with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I checked, no query have () as argument/key type yet, i think.

type CacheSelector = DefaultCacheSelector;

fn query_crate(&self) -> CrateNum {
LOCAL_CRATE
}

fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
tcx.def_span(DefId::local(CRATE_DEF_INDEX))
}
}

impl<'tcx> Key for ty::InstanceDef<'tcx> {
type CacheSelector = DefaultCacheSelector;

4 changes: 2 additions & 2 deletions src/librustc_mir/monomorphize/collector.rs
Original file line number Diff line number Diff line change
@@ -180,7 +180,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::{par_iter, MTLock, MTRef, ParallelIterator};
use rustc_errors::ErrorReported;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::lang_items::{ExchangeMallocFnLangItem, StartFnLangItem};
use rustc_index::bit_set::GrowableBitSet;
@@ -309,7 +309,7 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec<MonoItem<
let mut roots = Vec::new();

{
let entry_fn = tcx.entry_fn(LOCAL_CRATE);
let entry_fn = tcx.entry_fn(());

debug!("collect_roots: entry_fn = {:?}", entry_fn);

2 changes: 1 addition & 1 deletion src/librustc_passes/dead.rs
Original file line number Diff line number Diff line change
@@ -452,7 +452,7 @@ fn create_and_seed_worklist<'tcx>(
)
.chain(
// Seed entry point
tcx.entry_fn(LOCAL_CRATE).map(|(def_id, _)| tcx.hir().as_local_hir_id(def_id)),
tcx.entry_fn(()).map(|(def_id, _)| tcx.hir().as_local_hir_id(def_id)),
)
.collect::<Vec<_>>();

10 changes: 2 additions & 8 deletions src/librustc_passes/entry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_ast::attr;
use rustc_ast::entry::EntryPointType;
use rustc_errors::struct_span_err;
use rustc_hir::def_id::{CrateNum, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::{HirId, ImplItem, Item, ItemKind, TraitItem};
use rustc_middle::hir::map::Map;
@@ -48,9 +48,7 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> {
}
}

fn entry_fn(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option<(LocalDefId, EntryFnType)> {
assert_eq!(cnum, LOCAL_CRATE);

fn entry_fn(tcx: TyCtxt<'_>, _: ()) -> Option<(LocalDefId, EntryFnType)> {
let any_exe =
tcx.sess.crate_types.borrow().iter().any(|ty| *ty == config::CrateType::Executable);
if !any_exe {
@@ -214,10 +212,6 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
err.emit();
}

pub fn find_entry_point(tcx: TyCtxt<'_>) -> Option<(LocalDefId, EntryFnType)> {
tcx.entry_fn(LOCAL_CRATE)
}

pub fn provide(providers: &mut Providers<'_>) {
*providers = Providers { entry_fn, ..*providers };
}
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
@@ -1450,7 +1450,7 @@ fn check_fn<'a, 'tcx>(

// Check that the main return type implements the termination trait.
if let Some(term_id) = tcx.lang_items().termination() {
if let Some((def_id, EntryFnType::Main)) = tcx.entry_fn(LOCAL_CRATE) {
if let Some((def_id, EntryFnType::Main)) = tcx.entry_fn(()) {
let main_id = hir.as_local_hir_id(def_id);
if main_id == fn_id {
let substs = tcx.mk_substs_trait(declared_ret_ty, &[]);
2 changes: 1 addition & 1 deletion src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
@@ -302,7 +302,7 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: LocalDefId) {
}

fn check_for_entry_fn(tcx: TyCtxt<'_>) {
match tcx.entry_fn(LOCAL_CRATE) {
match tcx.entry_fn(()) {
Some((def_id, EntryFnType::Main)) => check_main_fn_ty(tcx, def_id),
Some((def_id, EntryFnType::Start)) => check_start_fn_ty(tcx, def_id),
_ => {}