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

+2-2
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

+2-2
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

+14-23
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

+10-2
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

+6-7
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

+1-1
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

+2-2
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

+2-2
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

+1-1
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

+2-1
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(..)

src/librustc_mir/monomorphize/partitioning.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ fn mono_item_visibility(
314314
};
315315

316316
let def_id = match instance.def {
317-
InstanceDef::Item(def_id, _) | InstanceDef::DropGlue(def_id, Some(_)) => def_id,
317+
InstanceDef::Item(def) => def.did,
318+
InstanceDef::DropGlue(def_id, Some(_)) => def_id,
318319

319320
// These are all compiler glue and such, never exported, always hidden.
320321
InstanceDef::VtableShim(..)
@@ -704,7 +705,7 @@ fn characteristic_def_id_of_mono_item<'tcx>(
704705
match mono_item {
705706
MonoItem::Fn(instance) => {
706707
let def_id = match instance.def {
707-
ty::InstanceDef::Item(def_id, _) => def_id,
708+
ty::InstanceDef::Item(def) => def.did,
708709
ty::InstanceDef::VtableShim(..)
709710
| ty::InstanceDef::ReifyShim(..)
710711
| ty::InstanceDef::FnPtrShim(..)

src/librustc_mir/transform/check_consts/validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,8 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
537537
let instance = Instance::resolve(self.tcx, self.param_env, def_id, substs);
538538
debug!("Resolving ({:?}) -> {:?}", def_id, instance);
539539
if let Ok(Some(func)) = instance {
540-
if let InstanceDef::Item(def_id, _) = func.def {
541-
if is_const_fn(self.tcx, def_id) {
540+
if let InstanceDef::Item(def) = func.def {
541+
if is_const_fn(self.tcx, def.did) {
542542
return;
543543
}
544544
}

src/librustc_mir/transform/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,17 @@ pub struct MirSource<'tcx> {
116116

117117
impl<'tcx> MirSource<'tcx> {
118118
pub fn item(def_id: DefId) -> Self {
119-
MirSource { instance: InstanceDef::Item(def_id, None), promoted: None }
119+
MirSource { instance: InstanceDef::Item(ty::WithOptParam::dummy(def_id)), promoted: None }
120120
}
121121

122122
#[inline]
123-
pub fn def_id(&self) -> DefId {
123+
pub fn def_id(self) -> DefId {
124124
self.instance.def_id()
125125
}
126126

127127
#[inline]
128-
pub fn with_opt_param(&self, tcx: TyCtxt<'tcx>) -> ty::WithOptParam<DefId> {
129-
self.instance.with_opt_param(tcx)
128+
pub fn with_opt_param(self) -> ty::WithOptParam<DefId> {
129+
self.instance.with_opt_param()
130130
}
131131
}
132132

@@ -264,7 +264,7 @@ fn mir_const(tcx: TyCtxt<'_>, def: ty::WithOptParam<DefId>) -> Steal<Body<'_>> {
264264
run_passes(
265265
tcx,
266266
&mut body,
267-
InstanceDef::Item(def.did.to_def_id(), def.param_did),
267+
InstanceDef::Item(def.to_global()),
268268
None,
269269
MirPhase::Const,
270270
&[&[
@@ -299,7 +299,7 @@ fn mir_validated(
299299
run_passes(
300300
tcx,
301301
&mut body,
302-
InstanceDef::Item(def.did.to_def_id(), def.param_did),
302+
InstanceDef::Item(def.to_global()),
303303
None,
304304
MirPhase::Validated,
305305
&[&[
@@ -365,7 +365,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(
365365
run_passes(
366366
tcx,
367367
body,
368-
InstanceDef::Item(def_id.to_def_id(), None),
368+
InstanceDef::Item(ty::WithOptParam::dummy(def_id.to_def_id())),
369369
promoted,
370370
MirPhase::DropElab,
371371
&[post_borrowck_cleanup],
@@ -429,7 +429,7 @@ fn run_optimization_passes<'tcx>(
429429
run_passes(
430430
tcx,
431431
body,
432-
InstanceDef::Item(def_id.to_def_id(), None),
432+
InstanceDef::Item(ty::WithOptParam::dummy(def_id.to_def_id())),
433433
promoted,
434434
MirPhase::Optimized,
435435
&[

src/librustc_mir/transform/promote_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> {
6060
return;
6161
}
6262

63-
let def = src.with_opt_param(tcx);
63+
let def = src.with_opt_param();
6464

6565
let mut rpo = traversal::reverse_postorder(body);
6666
let ccx = ConstCx::new(tcx, def.did.expect_local(), body);

src/librustc_mir/util/pretty.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ pub fn write_mir_pretty<'tcx>(
235235

236236
let mut first = true;
237237
for def_id in dump_mir_def_ids(tcx, single) {
238-
let body = &tcx.optimized_mir(tcx.with_opt_param(def_id));
238+
let def = ty::WithOptParam::dummy(def_id);
239+
let body = &tcx.optimized_mir(def);
239240

240241
if first {
241242
first = false;
@@ -246,10 +247,9 @@ pub fn write_mir_pretty<'tcx>(
246247

247248
write_mir_fn(tcx, MirSource::item(def_id), body, &mut |_, _| Ok(()), w)?;
248249

249-
for (i, body) in tcx.promoted_mir(tcx.with_opt_param(def_id)).iter_enumerated() {
250+
for (i, body) in tcx.promoted_mir(def).iter_enumerated() {
250251
writeln!(w)?;
251-
let src =
252-
MirSource { instance: ty::InstanceDef::Item(def_id, None), promoted: Some(i) };
252+
let src = MirSource { instance: ty::InstanceDef::Item(def), promoted: Some(i) };
253253
write_mir_fn(tcx, src, body, &mut |_, _| Ok(()), w)?;
254254
}
255255
}

src/librustc_mir_build/hair/pattern/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
802802
// defined, not where it is declared. The difference is significant for associated
803803
// constants.
804804
let mir_structural_match_violation =
805-
self.tcx.mir_const_qualif(instance.with_opt_param(self.tcx)).custom_eq;
805+
self.tcx.mir_const_qualif(instance.def.with_opt_param()).custom_eq;
806806
debug!("mir_structural_match_violation({:?}) -> {}", qpath, mir_structural_match_violation);
807807

808808
match self.tcx.const_eval_instance(param_env_reveal_all, instance, Some(span)) {

0 commit comments

Comments
 (0)