Skip to content

Commit 1c55d70

Browse files
Use Instance::new_raw less
1 parent 30d1fcb commit 1c55d70

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

compiler/rustc_hir_analysis/src/lib.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ use rustc_middle::query::Providers;
101101
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
102102
use rustc_session::parse::feature_err;
103103
use rustc_span::symbol::sym;
104-
use rustc_span::{ErrorGuaranteed, Span};
104+
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
105105
use rustc_trait_selection::traits;
106106

107107
pub use crate::collect::suggest_impl_trait;
@@ -216,9 +216,15 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
216216
check::maybe_check_static_with_link_section(tcx, item_def_id);
217217
}
218218
DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
219-
let instance = ty::Instance::new_raw(item_def_id.into(), ty::GenericArgs::empty());
220-
let cid = GlobalId { instance, promoted: None };
221219
let typing_env = ty::TypingEnv::fully_monomorphized();
220+
let instance = ty::Instance::expect_resolve(
221+
tcx,
222+
typing_env,
223+
item_def_id.into(),
224+
ty::GenericArgs::empty(),
225+
DUMMY_SP,
226+
);
227+
let cid = GlobalId { instance, promoted: None };
222228
tcx.ensure_ok().eval_to_const_value_raw(typing_env.as_query_input(cid));
223229
}
224230
_ => (),

compiler/rustc_middle/src/mir/interpret/queries.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::assert_matches::debug_assert_matches;
2+
13
use rustc_hir::def::DefKind;
24
use rustc_hir::def_id::DefId;
35
use rustc_session::lint;
@@ -18,14 +20,18 @@ impl<'tcx> TyCtxt<'tcx> {
1820
/// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
1921
#[instrument(skip(self), level = "debug")]
2022
pub fn const_eval_poly(self, def_id: DefId) -> EvalToConstValueResult<'tcx> {
23+
debug_assert_matches!(
24+
self.def_kind(def_id),
25+
DefKind::Const | DefKind::AnonConst | DefKind::InlineConst
26+
);
2127
// In some situations def_id will have generic parameters within scope, but they aren't allowed
2228
// to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
2329
// into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
2430
// encountered.
2531
let args = GenericArgs::identity_for_item(self, def_id);
26-
let instance = ty::Instance::new_raw(def_id, args);
27-
let cid = GlobalId { instance, promoted: None };
2832
let typing_env = ty::TypingEnv::post_analysis(self, def_id);
33+
let instance = ty::Instance::expect_resolve(self, typing_env, def_id, args, DUMMY_SP);
34+
let cid = GlobalId { instance, promoted: None };
2935
self.const_eval_global_id(typing_env, cid, DUMMY_SP)
3036
}
3137

@@ -34,14 +40,18 @@ impl<'tcx> TyCtxt<'tcx> {
3440
/// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
3541
#[instrument(skip(self), level = "debug")]
3642
pub fn const_eval_poly_to_alloc(self, def_id: DefId) -> EvalToAllocationRawResult<'tcx> {
43+
debug_assert_matches!(
44+
self.def_kind(def_id),
45+
DefKind::Const | DefKind::AnonConst | DefKind::InlineConst
46+
);
3747
// In some situations def_id will have generic parameters within scope, but they aren't allowed
3848
// to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
3949
// into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
4050
// encountered.
4151
let args = GenericArgs::identity_for_item(self, def_id);
42-
let instance = ty::Instance::new_raw(def_id, args);
43-
let cid = GlobalId { instance, promoted: None };
4452
let typing_env = ty::TypingEnv::post_analysis(self, def_id);
53+
let instance = ty::Instance::expect_resolve(self, typing_env, def_id, args, DUMMY_SP);
54+
let cid = GlobalId { instance, promoted: None };
4555
let inputs = self.erase_regions(typing_env.as_query_input(cid));
4656
self.eval_to_allocation_raw(inputs)
4757
}
@@ -203,14 +213,24 @@ impl<'tcx> TyCtxtEnsureOk<'tcx> {
203213
/// generic parameter is used within the constant `ErrorHandled::TooGeneric` will be returned.
204214
#[instrument(skip(self), level = "debug")]
205215
pub fn const_eval_poly(self, def_id: DefId) {
216+
debug_assert_matches!(
217+
self.tcx.def_kind(def_id),
218+
DefKind::Const | DefKind::AnonConst | DefKind::InlineConst
219+
);
206220
// In some situations def_id will have generic parameters within scope, but they aren't allowed
207221
// to be used. So we can't use `Instance::mono`, instead we feed unresolved generic parameters
208222
// into `const_eval` which will return `ErrorHandled::TooGeneric` if any of them are
209223
// encountered.
210224
let args = GenericArgs::identity_for_item(self.tcx, def_id);
211-
let instance = ty::Instance::new_raw(def_id, self.tcx.erase_regions(args));
212-
let cid = GlobalId { instance, promoted: None };
213225
let typing_env = ty::TypingEnv::post_analysis(self.tcx, def_id);
226+
let instance = ty::Instance::expect_resolve(
227+
self.tcx,
228+
typing_env,
229+
def_id,
230+
self.tcx.erase_regions(args),
231+
DUMMY_SP,
232+
);
233+
let cid = GlobalId { instance, promoted: None };
214234
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
215235
// improve caching of queries.
216236
let inputs = self.tcx.erase_regions(typing_env.as_query_input(cid));

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,7 @@ rustc_queries! {
24712471
query resolve_instance_raw(
24722472
key: ty::PseudoCanonicalInput<'tcx, (DefId, GenericArgsRef<'tcx>)>
24732473
) -> Result<Option<ty::Instance<'tcx>>, ErrorGuaranteed> {
2474-
desc { "resolving instance `{}`", ty::Instance::new_raw(key.value.0, key.value.1) }
2474+
desc { |tcx| "resolving instance `{}`", tcx.def_path_str_with_args(key.value.0, key.value.1) }
24752475
}
24762476

24772477
query reveal_opaque_types_in_bounds(key: ty::Clauses<'tcx>) -> ty::Clauses<'tcx> {

src/tools/clippy/clippy_lints/src/non_copy_const.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ impl<'tcx> NonCopyConst<'tcx> {
263263
fn is_value_unfrozen_poly(cx: &LateContext<'tcx>, body_id: BodyId, ty: Ty<'tcx>) -> bool {
264264
let def_id = body_id.hir_id.owner.to_def_id();
265265
let args = ty::GenericArgs::identity_for_item(cx.tcx, def_id);
266-
let instance = ty::Instance::new_raw(def_id, args);
266+
let typing_env = ty::TypingEnv::post_analysis(cx.tcx, def_id);
267+
let instance = ty::Instance::expect_resolve(self.tcx,typing_env,def_id, args, DUMMY_SP);
267268
let cid = GlobalId {
268269
instance,
269270
promoted: None,
270271
};
271-
let typing_env = ty::TypingEnv::post_analysis(cx.tcx, def_id);
272272
let result = cx.tcx.const_eval_global_id_for_typeck(typing_env, cid, DUMMY_SP);
273273
Self::is_value_unfrozen_raw(cx, result, ty)
274274
}

0 commit comments

Comments
 (0)