diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 266d653b46d..1d279706278 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -497,7 +497,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.tcx.hir().def_key(self.local_def_id(node_id)), ); - let def_id = self.tcx.create_def(parent, data).def_id; + let def_id = self.tcx.create_def(parent, data).def_id(); debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id); self.resolver.node_id_to_def_id.insert(node_id, def_id); diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 60e600f22a2..62c00db2da1 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1010,12 +1010,19 @@ pub struct FreeRegionInfo { pub is_impl_item: bool, } +/// This struct should only be created by `create_def`. #[derive(Copy, Clone)] pub struct TyCtxtFeed<'tcx> { pub tcx: TyCtxt<'tcx>, - pub def_id: LocalDefId, - /// This struct should only be created by `create_def`. - _priv: (), + // Do not allow direct access, as downstream code must not mutate this field. + def_id: LocalDefId, +} + +impl<'tcx> TyCtxtFeed<'tcx> { + #[inline(always)] + pub fn def_id(&self) -> LocalDefId { + self.def_id + } } /// The central data structure of the compiler. It stores references @@ -1507,7 +1514,7 @@ impl<'tcx> TyCtxt<'tcx> { // - this write will have happened before these queries are called. let def_id = self.definitions.write().create_def(parent, data); - TyCtxtFeed { tcx: self, def_id, _priv: () } + TyCtxtFeed { tcx: self, def_id } } pub fn iter_local_def_id(self) -> impl Iterator + 'tcx { diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index ae246c81959..47c1379b308 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -334,7 +334,7 @@ macro_rules! define_feedable { $($(#[$attr])* #[inline(always)] pub fn $name(self, value: $V) -> query_stored::$name<'tcx> { - let key = self.def_id.into_query_param(); + let key = self.def_id().into_query_param(); opt_remap_env_constness!([$($modifiers)*][key]); let tcx = self.tcx;