Skip to content

Commit a87dda3

Browse files
authored
Rollup merge of rust-lang#114670 - compiler-errors:issue-114660, r=cjgillot
Don't use `type_of` to determine if item has intrinsic shim When we're calling `resolve_instance` on an inline const, we were previously looking at the `type_of` for that const, seeing that it was an `extern "intrinsic"` fn def, and treating it as if we were computing the instance of that intrinsic itself. This is incorrect. Instead, we should be using the def-id of the item we're computing to determine if it's an intrinsic. Fixes rust-lang#114660
2 parents bbc1109 + d8e3986 commit a87dda3

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

compiler/rustc_ty_utils/src/instance.rs

+30-35
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_errors::ErrorGuaranteed;
2+
use rustc_hir::def::DefKind;
23
use rustc_hir::def_id::DefId;
34
use rustc_infer::infer::TyCtxtInferExt;
45
use rustc_middle::query::Providers;
@@ -15,54 +16,48 @@ fn resolve_instance<'tcx>(
1516
tcx: TyCtxt<'tcx>,
1617
key: ty::ParamEnvAnd<'tcx, (DefId, GenericArgsRef<'tcx>)>,
1718
) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> {
18-
let (param_env, (def, args)) = key.into_parts();
19+
let (param_env, (def_id, args)) = key.into_parts();
1920

20-
let result = if let Some(trait_def_id) = tcx.trait_of_item(def) {
21+
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
2122
debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
2223
resolve_associated_item(
2324
tcx,
24-
def,
25+
def_id,
2526
param_env,
2627
trait_def_id,
2728
tcx.normalize_erasing_regions(param_env, args),
2829
)
2930
} else {
30-
let ty = tcx.type_of(def);
31-
let item_type = tcx.subst_and_normalize_erasing_regions(args, param_env, ty);
31+
let def = if matches!(tcx.def_kind(def_id), DefKind::Fn) && tcx.is_intrinsic(def_id) {
32+
debug!(" => intrinsic");
33+
ty::InstanceDef::Intrinsic(def_id)
34+
} else if Some(def_id) == tcx.lang_items().drop_in_place_fn() {
35+
let ty = args.type_at(0);
3236

33-
let def = match *item_type.kind() {
34-
ty::FnDef(def_id, ..) if tcx.is_intrinsic(def_id) => {
35-
debug!(" => intrinsic");
36-
ty::InstanceDef::Intrinsic(def)
37-
}
38-
ty::FnDef(def_id, args) if Some(def_id) == tcx.lang_items().drop_in_place_fn() => {
39-
let ty = args.type_at(0);
40-
41-
if ty.needs_drop(tcx, param_env) {
42-
debug!(" => nontrivial drop glue");
43-
match *ty.kind() {
44-
ty::Closure(..)
45-
| ty::Generator(..)
46-
| ty::Tuple(..)
47-
| ty::Adt(..)
48-
| ty::Dynamic(..)
49-
| ty::Array(..)
50-
| ty::Slice(..) => {}
51-
// Drop shims can only be built from ADTs.
52-
_ => return Ok(None),
53-
}
54-
55-
ty::InstanceDef::DropGlue(def_id, Some(ty))
56-
} else {
57-
debug!(" => trivial drop glue");
58-
ty::InstanceDef::DropGlue(def_id, None)
37+
if ty.needs_drop(tcx, param_env) {
38+
debug!(" => nontrivial drop glue");
39+
match *ty.kind() {
40+
ty::Closure(..)
41+
| ty::Generator(..)
42+
| ty::Tuple(..)
43+
| ty::Adt(..)
44+
| ty::Dynamic(..)
45+
| ty::Array(..)
46+
| ty::Slice(..) => {}
47+
// Drop shims can only be built from ADTs.
48+
_ => return Ok(None),
5949
}
50+
51+
ty::InstanceDef::DropGlue(def_id, Some(ty))
52+
} else {
53+
debug!(" => trivial drop glue");
54+
ty::InstanceDef::DropGlue(def_id, None)
6055
}
61-
_ => {
62-
debug!(" => free item");
63-
ty::InstanceDef::Item(def)
64-
}
56+
} else {
57+
debug!(" => free item");
58+
ty::InstanceDef::Item(def_id)
6559
};
60+
6661
Ok(Some(Instance { def, args }))
6762
};
6863
debug!("inner_resolve_instance: result={:?}", result);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// check-pass
2+
// issue: 114660
3+
4+
#![feature(inline_const)]
5+
6+
fn main() {
7+
const { core::mem::transmute::<u8, u8> };
8+
// Don't resolve the instance of this inline constant to be an intrinsic,
9+
// even if the type of the constant is `extern "intrinsic" fn(u8) -> u8`.
10+
}

0 commit comments

Comments
 (0)