Skip to content

Commit 553ad86

Browse files
authored
Rollup merge of rust-lang#99347 - compiler-errors:opaque-type-key-local-def-id, r=oli-obk
Use `LocalDefId` in `OpaqueTypeKey` Addresses a `// FIXME(oli-obk): make this a LocalDefId` r? `@oli-obk`
2 parents 98a8844 + 136f017 commit 553ad86

File tree

15 files changed

+51
-53
lines changed

15 files changed

+51
-53
lines changed

compiler/rustc_borrowck/src/nll.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! The entry point of the NLL borrow checker.
22
33
use rustc_data_structures::vec_map::VecMap;
4-
use rustc_hir::def_id::DefId;
4+
use rustc_hir::def_id::LocalDefId;
55
use rustc_index::vec::IndexVec;
66
use rustc_infer::infer::InferCtxt;
77
use rustc_middle::mir::{create_dump_file, dump_enabled, dump_mir, PassWhere};
@@ -44,7 +44,7 @@ pub type PoloniusOutput = Output<RustcFacts>;
4444
/// closure requirements to propagate, and any generated errors.
4545
pub(crate) struct NllOutput<'tcx> {
4646
pub regioncx: RegionInferenceContext<'tcx>,
47-
pub opaque_type_values: VecMap<DefId, OpaqueHiddenType<'tcx>>,
47+
pub opaque_type_values: VecMap<LocalDefId, OpaqueHiddenType<'tcx>>,
4848
pub polonius_input: Option<Box<AllFacts>>,
4949
pub polonius_output: Option<Rc<PoloniusOutput>>,
5050
pub opt_closure_req: Option<ClosureRegionRequirements<'tcx>>,
@@ -373,7 +373,7 @@ pub(super) fn dump_annotation<'a, 'tcx>(
373373
body: &Body<'tcx>,
374374
regioncx: &RegionInferenceContext<'tcx>,
375375
closure_region_requirements: &Option<ClosureRegionRequirements<'_>>,
376-
opaque_type_values: &VecMap<DefId, OpaqueHiddenType<'tcx>>,
376+
opaque_type_values: &VecMap<LocalDefId, OpaqueHiddenType<'tcx>>,
377377
errors: &mut crate::error::BorrowckErrors<'tcx>,
378378
) {
379379
let tcx = infcx.tcx;

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_data_structures::fx::FxHashMap;
22
use rustc_data_structures::vec_map::VecMap;
3-
use rustc_hir::def_id::DefId;
3+
use rustc_hir::def_id::LocalDefId;
44
use rustc_hir::OpaqueTyOrigin;
55
use rustc_infer::infer::error_reporting::unexpected_hidden_region_diagnostic;
66
use rustc_infer::infer::InferCtxt;
@@ -63,8 +63,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
6363
&self,
6464
infcx: &InferCtxt<'_, 'tcx>,
6565
opaque_ty_decls: VecMap<OpaqueTypeKey<'tcx>, (OpaqueHiddenType<'tcx>, OpaqueTyOrigin)>,
66-
) -> VecMap<DefId, OpaqueHiddenType<'tcx>> {
67-
let mut result: VecMap<DefId, OpaqueHiddenType<'tcx>> = VecMap::new();
66+
) -> VecMap<LocalDefId, OpaqueHiddenType<'tcx>> {
67+
let mut result: VecMap<LocalDefId, OpaqueHiddenType<'tcx>> = VecMap::new();
6868
for (opaque_type_key, (concrete_type, origin)) in opaque_ty_decls {
6969
let substs = opaque_type_key.substs;
7070
debug!(?concrete_type, ?substs);
@@ -235,7 +235,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
235235
// lifetimes with 'static and remapping only those used in the
236236
// `impl Trait` return type, resulting in the parameters
237237
// shifting.
238-
let id_substs = InternalSubsts::identity_for_item(self.tcx, def_id);
238+
let id_substs = InternalSubsts::identity_for_item(self.tcx, def_id.to_def_id());
239239
debug!(?id_substs);
240240
let map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>> =
241241
substs.iter().enumerate().map(|(index, subst)| (subst, id_substs[index])).collect();
@@ -268,7 +268,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
268268
// This logic duplicates most of `check_opaque_meets_bounds`.
269269
// FIXME(oli-obk): Also do region checks here and then consider removing `check_opaque_meets_bounds` entirely.
270270
let param_env = self.tcx.param_env(def_id);
271-
let body_id = self.tcx.local_def_id_to_hir_id(def_id.as_local().unwrap());
271+
let body_id = self.tcx.local_def_id_to_hir_id(def_id);
272272
self.tcx.infer_ctxt().enter(move |infcx| {
273273
// Require the hidden type to be well-formed with only the generics of the opaque type.
274274
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
@@ -296,7 +296,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
296296
infcx
297297
.report_mismatched_types(
298298
&ObligationCause::misc(instantiated_ty.span, body_id),
299-
self.tcx.mk_opaque(def_id, id_substs),
299+
self.tcx.mk_opaque(def_id.to_def_id(), id_substs),
300300
definition_ty,
301301
err,
302302
)
@@ -423,7 +423,7 @@ fn check_opaque_type_parameter_valid(
423423
struct ReverseMapper<'tcx> {
424424
tcx: TyCtxt<'tcx>,
425425

426-
opaque_type_def_id: DefId,
426+
opaque_type_def_id: LocalDefId,
427427
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
428428
map_missing_regions_to_empty: bool,
429429

@@ -437,7 +437,7 @@ struct ReverseMapper<'tcx> {
437437
impl<'tcx> ReverseMapper<'tcx> {
438438
fn new(
439439
tcx: TyCtxt<'tcx>,
440-
opaque_type_def_id: DefId,
440+
opaque_type_def_id: LocalDefId,
441441
map: FxHashMap<GenericArg<'tcx>, GenericArg<'tcx>>,
442442
hidden_ty: Ty<'tcx>,
443443
span: Span,

compiler/rustc_infer/src/infer/canonical/query_response.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
153153
.opaque_type_storage
154154
.take_opaque_types()
155155
.into_iter()
156-
.map(|(k, v)| (self.tcx.mk_opaque(k.def_id, k.substs), v.hidden_type.ty))
156+
.map(|(k, v)| (self.tcx.mk_opaque(k.def_id.to_def_id(), k.substs), v.hidden_type.ty))
157157
.collect()
158158
}
159159

compiler/rustc_infer/src/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
938938
#[instrument(skip(self), level = "debug")]
939939
pub fn member_constraint(
940940
&self,
941-
opaque_type_def_id: DefId,
941+
opaque_type_def_id: LocalDefId,
942942
definition_span: Span,
943943
hidden_ty: Ty<'tcx>,
944944
region: ty::Region<'tcx>,

compiler/rustc_infer/src/infer/opaque_types.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
5151
return InferOk { value: ty, obligations: vec![] };
5252
}
5353
let mut obligations = vec![];
54-
let replace_opaque_type = |def_id| self.opaque_type_origin(def_id, span).is_some();
54+
let replace_opaque_type = |def_id: DefId| {
55+
def_id
56+
.as_local()
57+
.map_or(false, |def_id| self.opaque_type_origin(def_id, span).is_some())
58+
};
5559
let value = ty.fold_with(&mut ty::fold::BottomUpFolder {
5660
tcx: self.tcx,
5761
lt_op: |lt| lt,
@@ -96,6 +100,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
96100
let (a, b) = if a_is_expected { (a, b) } else { (b, a) };
97101
let process = |a: Ty<'tcx>, b: Ty<'tcx>| match *a.kind() {
98102
ty::Opaque(def_id, substs) if def_id.is_local() => {
103+
let def_id = def_id.expect_local();
99104
let origin = if self.defining_use_anchor.is_some() {
100105
// Check that this is `impl Trait` type is
101106
// declared by `parent_def_id` -- i.e., one whose
@@ -141,7 +146,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
141146
// no one encounters it in practice.
142147
// It does occur however in `fn fut() -> impl Future<Output = i32> { async { 42 } }`,
143148
// where it is of no concern, so we only check for TAITs.
144-
if let Some(OpaqueTyOrigin::TyAlias) = self.opaque_type_origin(did2, cause.span)
149+
if let Some(OpaqueTyOrigin::TyAlias) =
150+
did2.as_local().and_then(|did2| self.opaque_type_origin(did2, cause.span))
145151
{
146152
self.tcx
147153
.sess
@@ -399,8 +405,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
399405
}
400406

401407
#[instrument(skip(self), level = "trace")]
402-
pub fn opaque_type_origin(&self, opaque_def_id: DefId, span: Span) -> Option<OpaqueTyOrigin> {
403-
let def_id = opaque_def_id.as_local()?;
408+
pub fn opaque_type_origin(&self, def_id: LocalDefId, span: Span) -> Option<OpaqueTyOrigin> {
404409
let opaque_hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
405410
let parent_def_id = self.defining_use_anchor?;
406411
let item_kind = &self.tcx.hir().expect_item(def_id).kind;
@@ -409,7 +414,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
409414
span_bug!(
410415
span,
411416
"weird opaque type: {:#?}, {:#?}",
412-
opaque_def_id,
417+
def_id,
413418
item_kind
414419
)
415420
};
@@ -428,12 +433,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
428433
}
429434

430435
#[instrument(skip(self), level = "trace")]
431-
fn opaque_ty_origin_unchecked(&self, opaque_def_id: DefId, span: Span) -> OpaqueTyOrigin {
432-
let def_id = opaque_def_id.as_local().unwrap();
436+
fn opaque_ty_origin_unchecked(&self, def_id: LocalDefId, span: Span) -> OpaqueTyOrigin {
433437
let origin = match self.tcx.hir().expect_item(def_id).kind {
434438
hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => origin,
435439
ref itemkind => {
436-
span_bug!(span, "weird opaque type: {:?}, {:#?}", opaque_def_id, itemkind)
440+
span_bug!(span, "weird opaque type: {:?}, {:#?}", def_id, itemkind)
437441
}
438442
};
439443
trace!(?origin);
@@ -557,7 +561,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
557561
obligations = self.at(&cause, param_env).eq(prev, hidden_ty)?.obligations;
558562
}
559563

560-
let item_bounds = tcx.bound_explicit_item_bounds(def_id);
564+
let item_bounds = tcx.bound_explicit_item_bounds(def_id.to_def_id());
561565

562566
for predicate in item_bounds.transpose_iter().map(|e| e.map_bound(|(p, _)| *p)) {
563567
debug!(?predicate);
@@ -579,7 +583,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
579583
}
580584
// Replace all other mentions of the same opaque type with the hidden type,
581585
// as the bounds must hold on the hidden type after all.
582-
ty::Opaque(def_id2, substs2) if def_id == def_id2 && substs == substs2 => {
586+
ty::Opaque(def_id2, substs2)
587+
if def_id.to_def_id() == def_id2 && substs == substs2 =>
588+
{
583589
hidden_ty
584590
}
585591
_ => ty,

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::intern::Interned;
1212
use rustc_data_structures::sync::Lrc;
1313
use rustc_data_structures::undo_log::UndoLogs;
1414
use rustc_data_structures::unify as ut;
15-
use rustc_hir::def_id::DefId;
15+
use rustc_hir::def_id::LocalDefId;
1616
use rustc_index::vec::IndexVec;
1717
use rustc_middle::infer::unify_key::{RegionVidKey, UnifiedRegion};
1818
use rustc_middle::ty::ReStatic;
@@ -533,7 +533,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
533533

534534
pub fn member_constraint(
535535
&mut self,
536-
opaque_type_def_id: DefId,
536+
opaque_type_def_id: LocalDefId,
537537
definition_span: Span,
538538
hidden_ty: Ty<'tcx>,
539539
member_region: ty::Region<'tcx>,

compiler/rustc_middle/src/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod unify_key;
44
use crate::ty::Region;
55
use crate::ty::Ty;
66
use rustc_data_structures::sync::Lrc;
7-
use rustc_hir::def_id::DefId;
7+
use rustc_hir::def_id::LocalDefId;
88
use rustc_span::Span;
99

1010
/// Requires that `region` must be equal to one of the regions in `choice_regions`.
@@ -16,7 +16,7 @@ use rustc_span::Span;
1616
#[derive(Debug, Clone, HashStable, TypeFoldable, TypeVisitable, Lift)]
1717
pub struct MemberConstraint<'tcx> {
1818
/// The `DefId` of the opaque type causing this constraint: used for error reporting.
19-
pub opaque_type_def_id: DefId,
19+
pub opaque_type_def_id: LocalDefId,
2020

2121
/// The span where the hidden type was instantiated.
2222
pub definition_span: Span,

compiler/rustc_middle/src/mir/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub struct BorrowCheckResult<'tcx> {
235235
/// All the opaque types that are restricted to concrete types
236236
/// by this function. Unlike the value in `TypeckResults`, this has
237237
/// unerased regions.
238-
pub concrete_opaque_types: VecMap<DefId, OpaqueHiddenType<'tcx>>,
238+
pub concrete_opaque_types: VecMap<LocalDefId, OpaqueHiddenType<'tcx>>,
239239
pub closure_requirements: Option<ClosureRegionRequirements<'tcx>>,
240240
pub used_mut_upvars: SmallVec<[Field; 8]>,
241241
pub tainted_by_errors: Option<ErrorGuaranteed>,

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ pub struct TypeckResults<'tcx> {
542542
/// even if they are only set in dead code (which doesn't show up in MIR).
543543
/// For type-alias-impl-trait, this map is only used to prevent query cycles,
544544
/// so the hidden types are all `None`.
545-
pub concrete_opaque_types: VecMap<DefId, Option<Ty<'tcx>>>,
545+
pub concrete_opaque_types: VecMap<LocalDefId, Option<Ty<'tcx>>>,
546546

547547
/// Tracks the minimum captures required for a closure;
548548
/// see `MinCaptureInformationMap` for more details.

compiler/rustc_middle/src/ty/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1108,8 +1108,7 @@ impl<'tcx> InstantiatedPredicates<'tcx> {
11081108
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, TyEncodable, TyDecodable, Lift)]
11091109
#[derive(TypeFoldable, TypeVisitable)]
11101110
pub struct OpaqueTypeKey<'tcx> {
1111-
// FIXME(oli-obk): make this a LocalDefId
1112-
pub def_id: DefId,
1111+
pub def_id: LocalDefId,
11131112
pub substs: SubstsRef<'tcx>,
11141113
}
11151114

compiler/rustc_middle/src/ty/sty.rs

-7
Original file line numberDiff line numberDiff line change
@@ -1707,13 +1707,6 @@ impl<'tcx> Ty<'tcx> {
17071707
}
17081708
}
17091709

1710-
pub fn expect_opaque_type(self) -> ty::OpaqueTypeKey<'tcx> {
1711-
match *self.kind() {
1712-
Opaque(def_id, substs) => ty::OpaqueTypeKey { def_id, substs },
1713-
_ => bug!("`expect_opaque_type` called on non-opaque type: {}", self),
1714-
}
1715-
}
1716-
17171710
pub fn simd_size_and_type(self, tcx: TyCtxt<'tcx>) -> (u64, Ty<'tcx>) {
17181711
match self.kind() {
17191712
Adt(def, substs) => {

compiler/rustc_typeck/src/check/compare_method.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ pub fn check_type_bounds<'tcx>(
15131513
value.hidden_type.span,
15141514
tcx.hir().local_def_id_to_hir_id(impl_ty.def_id.expect_local()),
15151515
),
1516-
tcx.mk_opaque(key.def_id, key.substs),
1516+
tcx.mk_opaque(key.def_id.to_def_id(), key.substs),
15171517
value.hidden_type.ty,
15181518
TypeError::Mismatch,
15191519
)

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
763763
// src/test/ui/impl-trait/hidden-type-is-opaque-2.rs for examples that hit this path.
764764
if formal_ret.has_infer_types() {
765765
for ty in ret_ty.walk() {
766-
if let ty::subst::GenericArgKind::Type(ty) = ty.unpack() {
767-
if let ty::Opaque(def_id, _) = *ty.kind() {
768-
if self.infcx.opaque_type_origin(def_id, DUMMY_SP).is_some() {
769-
return None;
770-
}
771-
}
766+
if let ty::subst::GenericArgKind::Type(ty) = ty.unpack()
767+
&& let ty::Opaque(def_id, _) = *ty.kind()
768+
&& let Some(def_id) = def_id.as_local()
769+
&& self.infcx.opaque_type_origin(def_id, DUMMY_SP).is_some() {
770+
return None;
772771
}
773772
}
774773
}

compiler/rustc_typeck/src/check/writeback.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use crate::check::FnCtxt;
66

7+
use hir::def_id::LocalDefId;
78
use rustc_data_structures::stable_map::FxHashMap;
89
use rustc_errors::ErrorGuaranteed;
910
use rustc_hir as hir;
@@ -509,13 +510,13 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
509510
hir::OpaqueTyOrigin::FnReturn(_) | hir::OpaqueTyOrigin::AsyncFn(_) => {
510511
let ty = self.resolve(decl.hidden_type.ty, &decl.hidden_type.span);
511512
struct RecursionChecker {
512-
def_id: DefId,
513+
def_id: LocalDefId,
513514
}
514515
impl<'tcx> ty::TypeVisitor<'tcx> for RecursionChecker {
515516
type BreakTy = ();
516517
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
517518
if let ty::Opaque(def_id, _) = *t.kind() {
518-
if def_id == self.def_id {
519+
if def_id == self.def_id.to_def_id() {
519520
return ControlFlow::Break(());
520521
}
521522
}

compiler/rustc_typeck/src/collect/type_of.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
342342
let concrete_ty = tcx
343343
.mir_borrowck(owner)
344344
.concrete_opaque_types
345-
.get(&def_id.to_def_id())
345+
.get(&def_id)
346346
.copied()
347347
.map(|concrete| concrete.ty)
348348
.unwrap_or_else(|| {
@@ -353,7 +353,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
353353
// the `concrete_opaque_types` table.
354354
tcx.ty_error()
355355
} else {
356-
table.concrete_opaque_types.get(&def_id.to_def_id()).copied().unwrap_or_else(|| {
356+
table.concrete_opaque_types.get(&def_id).copied().unwrap_or_else(|| {
357357
// We failed to resolve the opaque type or it
358358
// resolves to itself. We interpret this as the
359359
// no values of the hidden type ever being constructed,
@@ -526,7 +526,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
526526
tcx: TyCtxt<'tcx>,
527527

528528
/// def_id of the opaque type whose defining uses are being checked
529-
def_id: DefId,
529+
def_id: LocalDefId,
530530

531531
/// as we walk the defining uses, we are checking that all of them
532532
/// define the same hidden type. This variable is set to `Some`
@@ -602,15 +602,15 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
602602
fn visit_item(&mut self, it: &'tcx Item<'tcx>) {
603603
trace!(?it.def_id);
604604
// The opaque type itself or its children are not within its reveal scope.
605-
if it.def_id.to_def_id() != self.def_id {
605+
if it.def_id != self.def_id {
606606
self.check(it.def_id);
607607
intravisit::walk_item(self, it);
608608
}
609609
}
610610
fn visit_impl_item(&mut self, it: &'tcx ImplItem<'tcx>) {
611611
trace!(?it.def_id);
612612
// The opaque type itself or its children are not within its reveal scope.
613-
if it.def_id.to_def_id() != self.def_id {
613+
if it.def_id != self.def_id {
614614
self.check(it.def_id);
615615
intravisit::walk_impl_item(self, it);
616616
}
@@ -624,7 +624,7 @@ fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Ty<'_> {
624624

625625
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
626626
let scope = tcx.hir().get_defining_scope(hir_id);
627-
let mut locator = ConstraintLocator { def_id: def_id.to_def_id(), tcx, found: None };
627+
let mut locator = ConstraintLocator { def_id: def_id, tcx, found: None };
628628

629629
debug!(?scope);
630630

0 commit comments

Comments
 (0)