Skip to content

Less Instance::new_raw #140518

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion compiler/rustc_codegen_cranelift/src/global_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,

let ty = tcx.typeck(item_id.owner_id).expr_ty(expr);
let instance = match ty.kind() {
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
&ty::FnDef(def_id, args) => Instance::expect_resolve(
tcx,
ty::TypingEnv::fully_monomorphized(),
def_id,
args,
expr.span,
),
_ => span_bug!(op_sp, "asm sym is not a function"),
};
let symbol = tcx.symbol_name(instance);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
intrinsic.name,
);
}
return Err(Instance::new(instance.def_id(), instance.args));
return Err(Instance::new_raw(instance.def_id(), instance.args));
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/src/intrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tc
}

// Fall back to default body
_ => return Err(Instance::new(instance.def_id(), instance.args)),
_ => return Err(Instance::new_raw(instance.def_id(), instance.args)),
};

if !fn_abi.ret.is_ignore() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn make_dummy_instance<'tcx>(tcx: TyCtxt<'tcx>, local_def_id: LocalDefId) -> ty:
let def_id = local_def_id.to_def_id();

// Make a dummy instance that fills in all generics with placeholders.
ty::Instance::new(
ty::Instance::new_raw(
def_id,
ty::GenericArgs::for_item(tcx, def_id, |param, _| {
if let ty::GenericParamDefKind::Lifetime = param.kind {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
_ => {
debug!("unknown intrinsic '{}' -- falling back to default body", name);
// Call the fallback body instead of generating the intrinsic code
return Err(ty::Instance::new(instance.def_id(), instance.args));
return Err(ty::Instance::new_raw(instance.def_id(), instance.args));
}
};

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ pub(crate) fn symbol_name_for_instance_in_crate<'tcx>(
ExportedSymbol::Generic(def_id, args) => {
rustc_symbol_mangling::symbol_name_for_instance_in_crate(
tcx,
Instance::new(def_id, args),
Instance::new_raw(def_id, args),
instantiating_crate,
)
}
Expand Down Expand Up @@ -613,7 +613,7 @@ fn calling_convention_for_symbol<'tcx>(
None
}
ExportedSymbol::NonGeneric(def_id) => Some(Instance::mono(tcx, def_id)),
ExportedSymbol::Generic(def_id, args) => Some(Instance::new(def_id, args)),
ExportedSymbol::Generic(def_id, args) => Some(Instance::new_raw(def_id, args)),
// DropGlue always use the Rust calling convention and thus follow the target's default
// symbol decoration scheme.
ExportedSymbol::DropGlue(..) => None,
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_ssa/src/mir/naked_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ fn inline_to_global_operand<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
);

let instance = match mono_type.kind() {
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
&ty::FnDef(def_id, args) => {
Instance::expect_resolve(cx.tcx(), cx.typing_env(), def_id, args, value.span)
}
_ => bug!("asm sym is not a function"),
};

Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_codegen_ssa/src/mono_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
hir::InlineAsmOperand::SymFn { expr } => {
let ty = cx.tcx().typeck(item_id.owner_id).expr_ty(expr);
let instance = match ty.kind() {
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
&ty::FnDef(def_id, args) => Instance::expect_resolve(
cx.tcx(),
ty::TypingEnv::fully_monomorphized(),
def_id,
args,
expr.span,
),
_ => span_bug!(*op_sp, "asm sym is not a function"),
};

Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ use rustc_middle::query::Providers;
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
use rustc_session::parse::feature_err;
use rustc_span::symbol::sym;
use rustc_span::{ErrorGuaranteed, Span};
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
use rustc_trait_selection::traits;

pub use crate::collect::suggest_impl_trait;
Expand Down Expand Up @@ -216,9 +216,15 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
check::maybe_check_static_with_link_section(tcx, item_def_id);
}
DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty());
let cid = GlobalId { instance, promoted: None };
let typing_env = ty::TypingEnv::fully_monomorphized();
let instance = ty::Instance::expect_resolve(
tcx,
typing_env,
item_def_id.into(),
ty::GenericArgs::empty(),
DUMMY_SP,
);
let cid = GlobalId { instance, promoted: None };
tcx.ensure_ok().eval_to_const_value_raw(typing_env.as_query_input(cid));
}
_ => (),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/foreign_modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl ClashingExternDeclarations {
/// for the item, return its HirId without updating the set.
fn insert(&mut self, tcx: TyCtxt<'_>, fi: hir::ForeignItemId) -> Option<hir::OwnerId> {
let did = fi.owner_id.to_def_id();
let instance = Instance::new(did, ty::List::identity_for_item(tcx, did));
let instance = Instance::new_raw(did, ty::List::identity_for_item(tcx, did));
let name = Symbol::intern(tcx.symbol_name(instance).name);
if let Some(&existing_id) = self.seen_decls.get(&name) {
// Avoid updating the map with the new entry when we do find a collision. We want to
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/middle/exported_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'tcx> ExportedSymbol<'tcx> {
match *self {
ExportedSymbol::NonGeneric(def_id) => tcx.symbol_name(ty::Instance::mono(tcx, def_id)),
ExportedSymbol::Generic(def_id, args) => {
tcx.symbol_name(ty::Instance::new(def_id, args))
tcx.symbol_name(ty::Instance::new_raw(def_id, args))
}
ExportedSymbol::DropGlue(ty) => {
tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty))
Expand Down
32 changes: 26 additions & 6 deletions compiler/rustc_middle/src/mir/interpret/queries.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::assert_matches::debug_assert_matches;

use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_session::lint;
Expand All @@ -18,14 +20,18 @@ impl<'tcx> TyCtxt<'tcx> {
/// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
#[instrument(skip(self), level = "debug")]
pub fn const_eval_poly(self, def_id: DefId) -> EvalToConstValueResult<'tcx> {
debug_assert_matches!(
self.def_kind(def_id),
DefKind::Const | DefKind::AnonConst | DefKind::InlineConst
);
// In some situations def_id will have generic parameters within scope, but they aren't allowed
// to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
// into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
// encountered.
let args = GenericArgs::identity_for_item(self, def_id);
let instance = ty::Instance::new(def_id, args);
let cid = GlobalId { instance, promoted: None };
let typing_env = ty::TypingEnv::post_analysis(self, def_id);
let instance = ty::Instance::expect_resolve(self, typing_env, def_id, args, DUMMY_SP);
let cid = GlobalId { instance, promoted: None };
self.const_eval_global_id(typing_env, cid, DUMMY_SP)
}

Expand All @@ -34,14 +40,18 @@ impl<'tcx> TyCtxt<'tcx> {
/// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
#[instrument(skip(self), level = "debug")]
pub fn const_eval_poly_to_alloc(self, def_id: DefId) -> EvalToAllocationRawResult<'tcx> {
debug_assert_matches!(
self.def_kind(def_id),
DefKind::Const | DefKind::AnonConst | DefKind::InlineConst
);
// In some situations def_id will have generic parameters within scope, but they aren't allowed
// to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
// into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
// encountered.
let args = GenericArgs::identity_for_item(self, def_id);
let instance = ty::Instance::new(def_id, args);
let cid = GlobalId { instance, promoted: None };
let typing_env = ty::TypingEnv::post_analysis(self, def_id);
let instance = ty::Instance::expect_resolve(self, typing_env, def_id, args, DUMMY_SP);
let cid = GlobalId { instance, promoted: None };
let inputs = self.erase_regions(typing_env.as_query_input(cid));
self.eval_to_allocation_raw(inputs)
}
Expand Down Expand Up @@ -203,14 +213,24 @@ impl<'tcx> TyCtxtEnsureOk<'tcx> {
/// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
#[instrument(skip(self), level = "debug")]
pub fn const_eval_poly(self, def_id: DefId) {
debug_assert_matches!(
self.tcx.def_kind(def_id),
DefKind::Const | DefKind::AnonConst | DefKind::InlineConst
);
// In some situations def_id will have generic parameters within scope, but they aren't allowed
// to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
// into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
// encountered.
let args = GenericArgs::identity_for_item(self.tcx, def_id);
let instance = ty::Instance::new(def_id, self.tcx.erase_regions(args));
let cid = GlobalId { instance, promoted: None };
let typing_env = ty::TypingEnv::post_analysis(self.tcx, def_id);
let instance = ty::Instance::expect_resolve(
self.tcx,
typing_env,
def_id,
self.tcx.erase_regions(args),
DUMMY_SP,
);
let cid = GlobalId { instance, promoted: None };
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
// improve caching of queries.
let inputs = self.tcx.erase_regions(typing_env.as_query_input(cid));
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/mono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl<'tcx> fmt::Display for MonoItem<'tcx> {
match *self {
MonoItem::Fn(instance) => write!(f, "fn {instance}"),
MonoItem::Static(def_id) => {
write!(f, "static {}", Instance::new(def_id, GenericArgs::empty()))
write!(f, "static {}", Instance::new_raw(def_id, GenericArgs::empty()))
}
MonoItem::GlobalAsm(..) => write!(f, "global_asm"),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,7 @@ rustc_queries! {
query resolve_instance_raw(
key: ty::PseudoCanonicalInput<'tcx, (DefId, GenericArgsRef<'tcx>)>
) -> Result<Option<ty::Instance<'tcx>>, ErrorGuaranteed> {
desc { "resolving instance `{}`", ty::Instance::new(key.value.0, key.value.1) }
desc { |tcx| "resolving instance `{}`", tcx.def_path_str_with_args(key.value.0, key.value.1) }
}

query reveal_opaque_types_in_bounds(key: ty::Clauses<'tcx>) -> ty::Clauses<'tcx> {
Expand Down
18 changes: 13 additions & 5 deletions compiler/rustc_middle/src/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,15 @@ fn resolve_async_drop_poll<'tcx>(mut cor_ty: Ty<'tcx>) -> Instance<'tcx> {
}

impl<'tcx> Instance<'tcx> {
pub fn new(def_id: DefId, args: GenericArgsRef<'tcx>) -> Instance<'tcx> {
/// Creates a new [`InstanceKind::Item`] from the `def_id` and `args`.
///
/// Note that this item corresponds to the body of `def_id` directly, which
/// likely does not make sense for trait items which need to be resolved to an
/// implementation, and which may not even have a body themselves. Usages of
/// this function should probably use [`Instance::expect_resolve`], or if run
/// in a polymorphic environment or within a lint (that may encounter ambiguity)
/// [`Instance::try_resolve`] instead.
pub fn new_raw(def_id: DefId, args: GenericArgsRef<'tcx>) -> Instance<'tcx> {
assert!(
!args.has_escaping_bound_vars(),
"args of instance {def_id:?} has escaping bound vars: {args:?}"
Expand All @@ -510,7 +518,7 @@ impl<'tcx> Instance<'tcx> {
}
});

Instance::new(def_id, args)
Instance::new_raw(def_id, args)
}

#[inline]
Expand Down Expand Up @@ -603,7 +611,7 @@ impl<'tcx> Instance<'tcx> {
let type_length = type_length(args);
if !tcx.type_length_limit().value_within_limit(type_length) {
let (shrunk, written_to_path) =
shrunk_instance_name(tcx, Instance::new(def_id, args));
shrunk_instance_name(tcx, Instance::new_raw(def_id, args));
let mut path = PathBuf::new();
let was_written = if let Some(path2) = written_to_path {
path = path2;
Expand Down Expand Up @@ -773,7 +781,7 @@ impl<'tcx> Instance<'tcx> {

match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, args),
_ => Instance::new(def_id, args),
_ => Instance::new_raw(def_id, args),
}
}

Expand Down Expand Up @@ -899,7 +907,7 @@ impl<'tcx> Instance<'tcx> {
// This is important for `Iterator`'s combinators, but also useful for
// adding future default methods to `Future`, for instance.
debug_assert!(tcx.defaultness(trait_item_id).has_value());
Some(Instance::new(trait_item_id, rcvr_args))
Some(Instance::new_raw(trait_item_id, rcvr_args))
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ fn visit_instance_use<'tcx>(
// We explicitly skip this otherwise to ensure we get a linker error
// if anyone tries to call this intrinsic and the codegen backend did not
// override the implementation.
let instance = ty::Instance::new(instance.def_id(), instance.args);
let instance = ty::Instance::new_raw(instance.def_id(), instance.args);
if tcx.should_codegen_locally(instance) {
output.push(create_fn_mono_item(tcx, instance, source));
}
Expand Down Expand Up @@ -1502,7 +1502,7 @@ impl<'v> RootCollector<'_, 'v> {
ty::Closure(def_id, args)
| ty::Coroutine(def_id, args)
| ty::CoroutineClosure(def_id, args) => {
Instance::new(def_id, self.tcx.erase_regions(args))
Instance::new_raw(def_id, self.tcx.erase_regions(args))
}
_ => unreachable!(),
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_smir/src/rustc_smir/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'tcx> BodyBuilder<'tcx> {
pub(crate) fn new(tcx: TyCtxt<'tcx>, instance: ty::Instance<'tcx>) -> Self {
let instance = match instance.def {
// To get the fallback body of an intrinsic, we need to convert it to an item.
ty::InstanceKind::Intrinsic(def_id) => ty::Instance::new(def_id, instance.args),
ty::InstanceKind::Intrinsic(def_id) => ty::Instance::new_raw(def_id, instance.args),
_ => instance,
};
BodyBuilder { tcx, instance }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_symbol_mangling/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl SymbolNamesTest<'_> {
// some subset.
for attr in tcx.get_attrs(def_id, SYMBOL_NAME) {
let def_id = def_id.to_def_id();
let instance = Instance::new(
let instance = Instance::new_raw(
def_id,
tcx.erase_regions(GenericArgs::identity_for_item(tcx, def_id)),
);
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ty_utils/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ fn resolve_associated_item<'tcx>(
tcx.ensure_ok().compare_impl_item(leaf_def_item)?;
}

Some(ty::Instance::new(leaf_def.item.def_id, args))
Some(ty::Instance::new_raw(leaf_def.item.def_id, args))
}
traits::ImplSource::Builtin(BuiltinImplSource::Object(_), _) => {
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_args);
Expand Down Expand Up @@ -280,7 +280,7 @@ fn resolve_associated_item<'tcx>(

// Use the default `fn clone_from` from `trait Clone`.
let args = tcx.erase_regions(rcvr_args);
Some(ty::Instance::new(trait_item_id, args))
Some(ty::Instance::new_raw(trait_item_id, args))
}
} else if tcx.is_lang_item(trait_ref.def_id, LangItem::FnPtrTrait) {
if tcx.is_lang_item(trait_item_id, LangItem::FnPtrAddr) {
Expand Down Expand Up @@ -329,7 +329,7 @@ fn resolve_associated_item<'tcx>(
// sync with the built-in trait implementations (since all of the
// implementations return `FnOnce::Output`).
if ty::ClosureKind::FnOnce == args.as_coroutine_closure().kind() {
Some(Instance::new(coroutine_closure_def_id, args))
Some(Instance::new_raw(coroutine_closure_def_id, args))
} else {
Some(Instance {
def: ty::InstanceKind::ConstructCoroutineInClosureShim {
Expand Down Expand Up @@ -362,7 +362,7 @@ fn resolve_associated_item<'tcx>(
args,
})
} else {
Some(Instance::new(coroutine_closure_def_id, args))
Some(Instance::new_raw(coroutine_closure_def_id, args))
}
}
ty::Closure(closure_def_id, args) => {
Expand All @@ -381,7 +381,7 @@ fn resolve_associated_item<'tcx>(
let name = tcx.item_name(trait_item_id);
assert_eq!(name, sym::transmute);
let args = tcx.erase_regions(rcvr_args);
Some(ty::Instance::new(trait_item_id, args))
Some(ty::Instance::new_raw(trait_item_id, args))
} else {
Instance::try_resolve_item_for_coroutine(tcx, trait_item_id, trait_id, rcvr_args)
}
Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/clippy_lints/src/non_copy_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ impl<'tcx> NonCopyConst<'tcx> {
fn is_value_unfrozen_poly(cx: &LateContext<'tcx>, body_id: BodyId, ty: Ty<'tcx>) -> bool {
let def_id = body_id.hir_id.owner.to_def_id();
let args = ty::GenericArgs::identity_for_item(cx.tcx, def_id);
let instance = ty::Instance::new(def_id, args);
let typing_env = ty::TypingEnv::post_analysis(cx.tcx, def_id);
let instance = ty::Instance::expect_resolve(self.tcx,typing_env,def_id, args, DUMMY_SP);
let cid = GlobalId {
instance,
promoted: None,
};
let typing_env = ty::TypingEnv::post_analysis(cx.tcx, def_id);
let result = cx.tcx.const_eval_global_id_for_typeck(typing_env, cid, DUMMY_SP);
Self::is_value_unfrozen_raw(cx, result, ty)
}
Expand Down
Loading
Loading