Skip to content

Commit ae7828a

Browse files
committed
who told you this was unsound!
1 parent 2ada8e3 commit ae7828a

File tree

17 files changed

+63
-63
lines changed

17 files changed

+63
-63
lines changed

src/librustc_codegen_ssa/back/symbol_export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ fn exported_symbols_provider_local(
249249
}
250250

251251
match *mono_item {
252-
MonoItem::Fn(Instance { def: InstanceDef::Item(def_id, _), substs }) => {
252+
MonoItem::Fn(Instance { def: InstanceDef::Item(def), substs }) => {
253253
if substs.non_erasable_generics().next().is_some() {
254-
let symbol = ExportedSymbol::Generic(def_id, substs);
254+
let symbol = ExportedSymbol::Generic(def.did, substs);
255255
symbols.push((symbol, SymbolExportLevel::Rust));
256256
}
257257
}

src/librustc_middle/mir/mono.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ impl<'tcx> CodegenUnit<'tcx> {
346346
// instances into account. The others don't matter for
347347
// the codegen tests and can even make item order
348348
// unstable.
349-
InstanceDef::Item(def_id, _) => {
350-
def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id))
349+
InstanceDef::Item(def) => {
350+
def.did.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id))
351351
}
352352
InstanceDef::VtableShim(..)
353353
| InstanceDef::ReifyShim(..)

src/librustc_middle/ty/instance.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ impl<'tcx> Instance<'tcx> {
160160
self.substs.non_erasable_generics().next()?;
161161

162162
match self.def {
163-
InstanceDef::Item(def_id, _) => tcx
164-
.upstream_monomorphizations_for(def_id)
163+
InstanceDef::Item(def) => tcx
164+
.upstream_monomorphizations_for(def.did)
165165
.and_then(|monos| monos.get(&self.substs).cloned()),
166166
InstanceDef::DropGlue(_, Some(_)) => tcx.upstream_drop_glue_for(self.substs),
167167
_ => None,
@@ -173,8 +173,8 @@ impl<'tcx> InstanceDef<'tcx> {
173173
#[inline]
174174
pub fn def_id(&self) -> DefId {
175175
match *self {
176-
InstanceDef::Item(def_id, _)
177-
| InstanceDef::VtableShim(def_id)
176+
InstanceDef::Item(def) => def.did,
177+
InstanceDef::VtableShim(def_id)
178178
| InstanceDef::ReifyShim(def_id)
179179
| InstanceDef::FnPtrShim(def_id, _)
180180
| InstanceDef::Virtual(def_id, _)
@@ -186,12 +186,8 @@ impl<'tcx> InstanceDef<'tcx> {
186186
}
187187

188188
#[inline]
189-
pub fn with_opt_param(&self, tcx: TyCtxt<'tcx>) -> ty::WithOptParam<DefId> {
190-
ty::WithOptParam {
191-
did: self.def_id(),
192-
param_did: if let InstanceDef::Item(_, param_did) = *self { param_did } else { None }
193-
.or_else(|| tcx.const_param_of(self.def_id())),
194-
}
189+
pub fn with_opt_param(self) -> ty::WithOptParam<DefId> {
190+
if let InstanceDef::Item(def) = self { def } else { ty::WithOptParam::dummy(self.def_id()) }
195191
}
196192

197193
#[inline]
@@ -207,7 +203,7 @@ impl<'tcx> InstanceDef<'tcx> {
207203
pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
208204
use rustc_hir::definitions::DefPathData;
209205
let def_id = match *self {
210-
ty::InstanceDef::Item(def_id, _) => def_id,
206+
ty::InstanceDef::Item(def) => def.did,
211207
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
212208
_ => return true,
213209
};
@@ -253,8 +249,8 @@ impl<'tcx> InstanceDef<'tcx> {
253249

254250
pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool {
255251
match *self {
256-
InstanceDef::Item(def_id, _) => {
257-
tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
252+
InstanceDef::Item(def) => {
253+
tcx.codegen_fn_attrs(def.did).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
258254
}
259255
_ => false,
260256
}
@@ -271,7 +267,7 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
271267
})?;
272268

273269
match self.def {
274-
InstanceDef::Item(_, _) => Ok(()),
270+
InstanceDef::Item(_) => Ok(()),
275271
InstanceDef::VtableShim(_) => write!(f, " - shim(vtable)"),
276272
InstanceDef::ReifyShim(_) => write!(f, " - shim(reify)"),
277273
InstanceDef::Intrinsic(_) => write!(f, " - intrinsic"),
@@ -292,7 +288,7 @@ impl<'tcx> Instance<'tcx> {
292288
did,
293289
substs
294290
);
295-
Instance { def: InstanceDef::Item(did, None), substs }
291+
Instance { def: InstanceDef::Item(ty::WithOptParam::dummy(did)), substs }
296292
}
297293

298294
pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
@@ -304,11 +300,6 @@ impl<'tcx> Instance<'tcx> {
304300
self.def.def_id()
305301
}
306302

307-
#[inline]
308-
pub fn with_opt_param(&self, tcx: TyCtxt<'tcx>) -> ty::WithOptParam<DefId> {
309-
self.def.with_opt_param(tcx)
310-
}
311-
312303
/// Identical to `resolve`, but may also take an optional `param_def_id` for
313304
/// generic const arguments.
314305
pub fn resolve_const_arg(
@@ -378,9 +369,9 @@ impl<'tcx> Instance<'tcx> {
378369
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
379370
Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| {
380371
match resolved.def {
381-
InstanceDef::Item(def_id, _) if resolved.def.requires_caller_location(tcx) => {
372+
InstanceDef::Item(def) if resolved.def.requires_caller_location(tcx) => {
382373
debug!(" => fn pointer created for function with #[track_caller]");
383-
resolved.def = InstanceDef::ReifyShim(def_id);
374+
resolved.def = InstanceDef::ReifyShim(def.did);
384375
}
385376
InstanceDef::Virtual(def_id, _) => {
386377
debug!(" => fn pointer created for virtual call");
@@ -476,7 +467,7 @@ impl<'tcx> Instance<'tcx> {
476467
| InstanceDef::DropGlue(..)
477468
// FIXME(#69925): `FnPtrShim` should be in the other branch.
478469
| InstanceDef::FnPtrShim(..)
479-
| InstanceDef::Item(_, _)
470+
| InstanceDef::Item(_)
480471
| InstanceDef::Intrinsic(..)
481472
| InstanceDef::ReifyShim(..)
482473
| InstanceDef::Virtual(..)

src/librustc_middle/ty/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,17 +1583,25 @@ pub struct WithOptParam<T> {
15831583
pub did: T,
15841584
/// The `DefId` of the corresponding generic paramter in case `did` is
15851585
/// a const argument.
1586+
///
1587+
/// If `param_did` is `Some` it must be equal to `tcx.const_param_of`,
1588+
/// and may otherwise cause panics or problems.
15861589
pub param_did: Option<DefId>,
15871590
}
15881591

15891592
impl<'tcx> TyCtxt<'tcx> {
15901593
#[inline(always)]
15911594
pub fn with_opt_param<T: IntoQueryParam<DefId> + Copy>(self, did: T) -> WithOptParam<T> {
1592-
WithOptParam { did, param_did: self.const_param_of(did) }
1595+
WithOptParam { did, param_did: None }
15931596
}
15941597
}
15951598

15961599
impl<T: IntoQueryParam<DefId>> WithOptParam<T> {
1600+
/// Wraps the given `DefId` and sets `param_did` to none.
1601+
pub fn dummy(did: T) -> WithOptParam<T> {
1602+
WithOptParam { did, param_did: None }
1603+
}
1604+
15971605
pub fn ty_def_id(self) -> DefId {
15981606
self.param_did.unwrap_or(self.did.into_query_param())
15991607
}
@@ -2790,7 +2798,7 @@ impl<'tcx> TyCtxt<'tcx> {
27902798
/// Returns the possibly-auto-generated MIR of a `(DefId, Subst)` pair.
27912799
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> {
27922800
match instance {
2793-
ty::InstanceDef::Item(_, _) => self.optimized_mir(instance.with_opt_param(self)),
2801+
ty::InstanceDef::Item(def) => self.optimized_mir(def),
27942802
ty::InstanceDef::VtableShim(..)
27952803
| ty::InstanceDef::ReifyShim(..)
27962804
| ty::InstanceDef::Intrinsic(..)

src/librustc_middle/ty/structural_impls.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::InstanceDef<'a> {
672672
type Lifted = ty::InstanceDef<'tcx>;
673673
fn lift_to_tcx(&self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
674674
match *self {
675-
ty::InstanceDef::Item(did, param_did) => Some(ty::InstanceDef::Item(did, param_did)),
675+
ty::InstanceDef::Item(did) => Some(ty::InstanceDef::Item(did)),
676676
ty::InstanceDef::VtableShim(def_id) => Some(ty::InstanceDef::VtableShim(def_id)),
677677
ty::InstanceDef::ReifyShim(def_id) => Some(ty::InstanceDef::ReifyShim(def_id)),
678678
ty::InstanceDef::Intrinsic(def_id) => Some(ty::InstanceDef::Intrinsic(def_id)),
@@ -844,7 +844,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
844844
Self {
845845
substs: self.substs.fold_with(folder),
846846
def: match self.def {
847-
Item(did, param_did) => Item(did.fold_with(folder), param_did.fold_with(folder)),
847+
Item(def) => Item(def.fold_with(folder)),
848848
VtableShim(did) => VtableShim(did.fold_with(folder)),
849849
ReifyShim(did) => ReifyShim(did.fold_with(folder)),
850850
Intrinsic(did) => Intrinsic(did.fold_with(folder)),
@@ -863,11 +863,10 @@ impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
863863
use crate::ty::InstanceDef::*;
864864
self.substs.visit_with(visitor)
865865
|| match self.def {
866-
Item(did, _)
867-
| VtableShim(did)
868-
| ReifyShim(did)
869-
| Intrinsic(did)
870-
| Virtual(did, _) => did.visit_with(visitor),
866+
Item(def) => def.visit_with(visitor),
867+
VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
868+
did.visit_with(visitor)
869+
}
871870
FnPtrShim(did, ty) | CloneShim(did, ty) => {
872871
did.visit_with(visitor) || ty.visit_with(visitor)
873872
}

src/librustc_mir/const_eval/eval_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ pub fn const_eval_raw_provider<'tcx>(
288288
}
289289

290290
let cid = key.value;
291-
let def = cid.instance.with_opt_param(tcx);
291+
let def = cid.instance.def.with_opt_param();
292292

293293
if let Some(def) = def.as_local() {
294294
if tcx.has_typeck_tables(def.did) {

src/librustc_mir/const_eval/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
191191
debug!("find_mir_or_eval_fn: {:?}", instance);
192192

193193
// Only check non-glue functions
194-
if let ty::InstanceDef::Item(def_id, _) = instance.def {
194+
if let ty::InstanceDef::Item(def) = instance.def {
195195
// Execution might have wandered off into other crates, so we cannot do a stability-
196196
// sensitive check here. But we can at least rule out functions that are not const
197197
// at all.
198-
if ecx.tcx.is_const_fn_raw(def_id) {
198+
if ecx.tcx.is_const_fn_raw(def.did) {
199199
// If this function is a `const fn` then under certain circumstances we
200200
// can evaluate call via the query system, thus memoizing all future calls.
201201
if ecx.try_eval_const_fn_call(instance, ret, args)? {

src/librustc_mir/interpret/eval_context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
400400
promoted: Option<mir::Promoted>,
401401
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
402402
// do not continue if typeck errors occurred (can only occur in local crate)
403-
let def = instance.with_opt_param(*self.tcx);
403+
let def = instance.with_opt_param();
404404
if let Some(def) = def.as_local() {
405405
if self.tcx.has_typeck_tables(def.did) {
406406
if let Some(error_reported) = self.tcx.typeck_tables_of(def).tainted_by_errors {
@@ -413,7 +413,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
413413
return Ok(&self.tcx.promoted_mir(def)[promoted]);
414414
}
415415
match instance {
416-
ty::InstanceDef::Item(_, _) => {
416+
ty::InstanceDef::Item(_) => {
417417
if self.tcx.is_mir_available(def.did) {
418418
Ok(self.tcx.optimized_mir(def))
419419
} else {

src/librustc_mir/interpret/terminator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
257257
| ty::InstanceDef::FnPtrShim(..)
258258
| ty::InstanceDef::DropGlue(..)
259259
| ty::InstanceDef::CloneShim(..)
260-
| ty::InstanceDef::Item(_, _) => {
260+
| ty::InstanceDef::Item(_) => {
261261
// We need MIR for this fn
262262
let body = match M::find_mir_or_eval_fn(self, instance, args, ret, unwind)? {
263263
Some(body) => body,

src/librustc_mir/monomorphize/collector.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,8 @@ fn visit_instance_use<'tcx>(
768768
// need a mono item.
769769
fn should_monomorphize_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) -> bool {
770770
let def_id = match instance.def {
771-
ty::InstanceDef::Item(def_id, _) | ty::InstanceDef::DropGlue(def_id, Some(_)) => def_id,
771+
ty::InstanceDef::Item(def) => def.did,
772+
ty::InstanceDef::DropGlue(def_id, Some(_)) => def_id,
772773

773774
ty::InstanceDef::VtableShim(..)
774775
| ty::InstanceDef::ReifyShim(..)

0 commit comments

Comments
 (0)