Skip to content

Commit 6ffb696

Browse files
committed
Forbid simultaneously defining *and* opaquely using TAITs within bodies
1 parent 6c26336 commit 6ffb696

21 files changed

+104
-111
lines changed

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,16 @@ impl TaitConstraintLocator<'_> {
229229
return;
230230
}
231231

232-
let opaques = self.tcx.opaque_types_defined_by(item_def_id);
233-
let opaque_type_must_be_defined = opaques.in_signature.contains(&self.def_id);
234-
let opaque_type_may_be_defined = opaques.in_body.contains(&self.def_id);
232+
let opaque_types_defined_by = self.tcx.opaque_types_defined_by(item_def_id);
235233

236234
let mut constrained = false;
237235
for (&opaque_type_key, &hidden_type) in &tables.concrete_opaque_types {
238236
if opaque_type_key.def_id != self.def_id {
239237
continue;
240238
}
241239
constrained = true;
242-
if !opaque_type_must_be_defined && !opaque_type_may_be_defined {
240+
241+
if !opaque_types_defined_by.contains(&self.def_id) {
243242
self.tcx.dcx().emit_err(TaitForwardCompat {
244243
span: hidden_type.span,
245244
item_span: self
@@ -261,7 +260,7 @@ impl TaitConstraintLocator<'_> {
261260

262261
if !constrained {
263262
debug!("no constraints in typeck results");
264-
if opaque_type_must_be_defined {
263+
if opaque_types_defined_by.contains(&self.def_id) {
265264
self.tcx.dcx().emit_err(TaitForwardCompat2 {
266265
span: self
267266
.tcx

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,9 @@ impl<T> Trait<T> for X {
362362
| DefKind::AssocConst
363363
)
364364
&& tcx.is_type_alias_impl_trait(opaque_ty.def_id)
365-
&& let opaques =
366-
tcx.opaque_types_defined_by(body_owner_def_id.expect_local())
367-
&& !opaques.in_body.contains(&opaque_ty.def_id.expect_local())
368-
&& !opaques.in_signature.contains(&opaque_ty.def_id.expect_local())
365+
&& !tcx
366+
.opaque_types_defined_by(body_owner_def_id.expect_local())
367+
.contains(&opaque_ty.def_id.expect_local())
369368
{
370369
let sp = tcx
371370
.def_ident_span(body_owner_def_id)
@@ -433,7 +432,6 @@ impl<T> Trait<T> for X {
433432
}
434433
}
435434
}
436-
437435
(ty::FnPtr(sig), ty::FnDef(def_id, _))
438436
| (ty::FnDef(def_id, _), ty::FnPtr(sig)) => {
439437
if tcx.fn_sig(def_id).skip_binder().safety() < sig.safety() {

compiler/rustc_infer/src/infer/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ pub struct InferCtxt<'tcx> {
243243
pub tcx: TyCtxt<'tcx>,
244244

245245
/// The `DefIds` of the opaque types that may have their hidden types constrained.
246-
defining_opaque_types: ty::OpaqueTypes<'tcx>,
246+
defining_opaque_types: &'tcx ty::List<LocalDefId>,
247247

248248
/// Whether this inference context should care about region obligations in
249249
/// the root universe. Most notably, this is used during hir typeck as region
@@ -370,7 +370,7 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
370370
self.inner.borrow_mut().unwrap_region_constraints().opportunistic_resolve_var(self.tcx, vid)
371371
}
372372

373-
fn defining_opaque_types(&self) -> ty::OpaqueTypes<'tcx> {
373+
fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> {
374374
self.defining_opaque_types
375375
}
376376

@@ -620,7 +620,7 @@ impl fmt::Display for FixupError {
620620
/// Used to configure inference contexts before their creation.
621621
pub struct InferCtxtBuilder<'tcx> {
622622
tcx: TyCtxt<'tcx>,
623-
defining_opaque_types: ty::OpaqueTypes<'tcx>,
623+
defining_opaque_types: &'tcx ty::List<LocalDefId>,
624624
considering_regions: bool,
625625
skip_leak_check: bool,
626626
/// Whether we are in coherence mode.
@@ -635,7 +635,7 @@ impl<'tcx> TyCtxt<'tcx> {
635635
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
636636
InferCtxtBuilder {
637637
tcx: self,
638-
defining_opaque_types: Default::default(),
638+
defining_opaque_types: ty::List::empty(),
639639
considering_regions: true,
640640
skip_leak_check: false,
641641
intercrate: false,
@@ -651,14 +651,14 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
651651
/// It is only meant to be called in two places, for typeck
652652
/// (via `Inherited::build`) and for the inference context used
653653
/// in mir borrowck.
654-
pub fn with_opaque_type_inference(self, defining_anchor: LocalDefId) -> Self {
655-
let defining_opaque_types = self.tcx.opaque_types_defined_by(defining_anchor);
656-
self.with_defining_opaque_types(defining_opaque_types)
654+
pub fn with_opaque_type_inference(mut self, defining_anchor: LocalDefId) -> Self {
655+
self.defining_opaque_types = self.tcx.opaque_types_defined_by(defining_anchor);
656+
self
657657
}
658658

659659
pub fn with_defining_opaque_types(
660660
mut self,
661-
defining_opaque_types: ty::OpaqueTypes<'tcx>,
661+
defining_opaque_types: &'tcx ty::List<LocalDefId>,
662662
) -> Self {
663663
self.defining_opaque_types = defining_opaque_types;
664664
self
@@ -1213,8 +1213,7 @@ impl<'tcx> InferCtxt<'tcx> {
12131213
#[inline(always)]
12141214
pub fn can_define_opaque_ty(&self, id: impl Into<DefId>) -> bool {
12151215
let Some(id) = id.into().as_local() else { return false };
1216-
self.defining_opaque_types.in_signature.contains(&id)
1217-
|| self.defining_opaque_types.in_body.contains(&id)
1216+
self.defining_opaque_types.contains(&id)
12181217
}
12191218

12201219
pub fn ty_to_string(&self, t: Ty<'tcx>) -> String {

compiler/rustc_middle/src/infer/canonical.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl<'tcx> CanonicalParamEnvCache<'tcx> {
182182
max_universe: ty::UniverseIndex::ROOT,
183183
variables: List::empty(),
184184
value: key,
185-
defining_opaque_types: Default::default(),
185+
defining_opaque_types: ty::List::empty(),
186186
};
187187
}
188188

compiler/rustc_middle/src/query/erase.rs

-4
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,6 @@ impl EraseType for Option<mir::DestructuredConstant<'_>> {
183183
type Result = [u8; size_of::<Option<mir::DestructuredConstant<'static>>>()];
184184
}
185185

186-
impl EraseType for ty::OpaqueTypes<'_> {
187-
type Result = [u8; size_of::<ty::OpaqueTypes<'static>>()];
188-
}
189-
190186
impl EraseType for Option<ty::ImplTraitHeader<'_>> {
191187
type Result = [u8; size_of::<Option<ty::ImplTraitHeader<'static>>>()];
192188
}

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ rustc_queries! {
337337

338338
query opaque_types_defined_by(
339339
key: LocalDefId
340-
) -> ty::OpaqueTypes<'tcx> {
340+
) -> &'tcx ty::List<LocalDefId> {
341341
desc {
342342
|tcx| "computing the opaque types defined by `{}`",
343343
tcx.def_path_str(key.to_def_id())

compiler/rustc_middle/src/query/plumbing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ macro_rules! define_callbacks {
325325
// Increase this limit if necessary, but do try to keep the size low if possible
326326
#[cfg(target_pointer_width = "64")]
327327
const _: () = {
328-
if mem::size_of::<Key<'static>>() > 80 {
328+
if mem::size_of::<Key<'static>>() > 72 {
329329
panic!("{}", concat!(
330330
"the query `",
331331
stringify!($name),
@@ -340,7 +340,7 @@ macro_rules! define_callbacks {
340340
// Increase this limit if necessary, but do try to keep the size low if possible
341341
#[cfg(target_pointer_width = "64")]
342342
const _: () = {
343-
if mem::size_of::<Value<'static>>() > 72 {
343+
if mem::size_of::<Value<'static>>() > 64 {
344344
panic!("{}", concat!(
345345
"the query `",
346346
stringify!($name),

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
100100

101101
type CanonicalVars = CanonicalVarInfos<'tcx>;
102102
type PredefinedOpaques = solve::PredefinedOpaques<'tcx>;
103-
type DefiningOpaqueTypes = ty::OpaqueTypes<'tcx>;
103+
type DefiningOpaqueTypes = &'tcx ty::List<LocalDefId>;
104104
type ExternalConstraints = ExternalConstraints<'tcx>;
105105
type CanonicalGoalEvaluationStepRef =
106106
&'tcx solve::inspect::CanonicalGoalEvaluationStep<TyCtxt<'tcx>>;

compiler/rustc_middle/src/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub use assoc::*;
3131
pub use generic_args::{GenericArgKind, TermKind, *};
3232
pub use generics::*;
3333
pub use intrinsic::IntrinsicDef;
34-
pub use opaque_types::OpaqueTypes;
3534
use rustc_ast as ast;
3635
use rustc_ast::expand::StrippedCfgItem;
3736
use rustc_ast::node_id::NodeMap;

compiler/rustc_middle/src/ty/opaque_types.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use crate::ty::fold::{TypeFolder, TypeSuperFoldable};
33
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
44
use crate::ty::{GenericArg, GenericArgKind};
55
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeVisitable};
7-
use rustc_span::def_id::{DefId, LocalDefId};
6+
use rustc_span::def_id::DefId;
87
use rustc_span::Span;
98
use tracing::{debug, instrument, trace};
109

@@ -226,14 +225,3 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReverseMapper<'tcx> {
226225
}
227226
}
228227
}
229-
230-
/// The opaque types defined by an item.
231-
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Default)]
232-
#[derive(HashStable, TypeVisitable, TypeFoldable, TyDecodable, TyEncodable)]
233-
pub struct OpaqueTypes<'tcx> {
234-
/// Opaque types in the signature *must* be defined by the item itself.
235-
pub in_signature: &'tcx ty::List<LocalDefId>,
236-
/// Opaque types declared in the body are not required to be defined by
237-
/// the item itself, they could be defined by other nested items.
238-
pub in_body: &'tcx ty::List<LocalDefId>,
239-
}

compiler/rustc_ty_utils/src/opaque_types.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
119119
let id = id.owner_id.def_id;
120120
if let DefKind::TyAlias = self.collector.tcx.def_kind(id) {
121121
let items = self.collector.tcx.opaque_types_defined_by(id);
122-
assert_eq!(&items.in_body[..], []);
123-
self.collector.opaques.extend(items.in_signature);
122+
self.collector.opaques.extend(items);
124123
}
125124
}
126125
#[instrument(level = "trace", skip(self))]
@@ -313,20 +312,22 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
313312
}
314313
}
315314

316-
fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> ty::OpaqueTypes<'tcx> {
315+
fn opaque_types_defined_by<'tcx>(
316+
tcx: TyCtxt<'tcx>,
317+
item: LocalDefId,
318+
) -> &'tcx ty::List<LocalDefId> {
317319
let kind = tcx.def_kind(item);
318320
trace!(?kind);
319-
let mut in_signature = OpaqueTypeCollector::new(tcx, item);
320-
let mut in_body = OpaqueTypeCollector::new(tcx, item);
321-
super::sig_types::walk_types(tcx, item, &mut in_signature);
321+
let mut collector = OpaqueTypeCollector::new(tcx, item);
322+
super::sig_types::walk_types(tcx, item, &mut collector);
322323
match kind {
323324
DefKind::AssocFn
324325
| DefKind::Fn
325326
| DefKind::Static { .. }
326327
| DefKind::Const
327328
| DefKind::AssocConst
328329
| DefKind::AnonConst => {
329-
in_body.collect_taits_declared_in_body();
330+
collector.collect_taits_declared_in_body();
330331
}
331332
DefKind::OpaqueTy
332333
| DefKind::TyAlias
@@ -353,15 +354,10 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> ty::Opa
353354
// Closures and coroutines are type checked with their parent, so we need to allow all
354355
// opaques from the closure signature *and* from the parent body.
355356
DefKind::Closure | DefKind::InlineConst => {
356-
let nested = tcx.opaque_types_defined_by(tcx.local_parent(item));
357-
in_signature.opaques.extend(nested.in_signature);
358-
in_body.opaques.extend(nested.in_body);
357+
collector.opaques.extend(tcx.opaque_types_defined_by(tcx.local_parent(item)));
359358
}
360359
}
361-
ty::OpaqueTypes {
362-
in_signature: tcx.mk_local_def_ids(&in_signature.opaques),
363-
in_body: tcx.mk_local_def_ids(&in_body.opaques),
364-
}
360+
tcx.mk_local_def_ids(&collector.opaques)
365361
}
366362

367363
pub(super) fn provide(providers: &mut Providers) {

0 commit comments

Comments
 (0)