Auto merge of #122077 - oli-obk:eager_opaque_checks4, r=lcnr · rust-lang/rust@b234e44 (original) (raw)

`@@ -34,7 +34,7 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin

`

34

34

`use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey};

`

35

35

`use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};

`

36

36

`use rustc_middle::mir::ConstraintCategory;

`

37

``

`-

use rustc_middle::traits::{select, DefiningAnchor};

`

``

37

`+

use rustc_middle::traits::select;

`

38

38

`use rustc_middle::ty::error::{ExpectedFound, TypeError};

`

39

39

`use rustc_middle::ty::fold::BoundVarReplacerDelegate;

`

40

40

`use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};

`

`@@ -243,18 +243,8 @@ impl<'tcx> InferCtxtInner<'tcx> {

`

243

243

`pub struct InferCtxt<'tcx> {

`

244

244

`pub tcx: TyCtxt<'tcx>,

`

245

245

``

246

``

`` -

/// The DefId of the item in whose context we are performing inference or typeck.

``

247

``

`-

/// It is used to check whether an opaque type use is a defining use.

`

248

``

`-

///

`

249

``

`` -

/// If it is DefiningAnchor::Bubble, we can't resolve opaque types here and need to bubble up

``

250

``

`-

/// the obligation. This frequently happens for

`

251

``

`-

/// short lived InferCtxt within queries. The opaque type obligations are forwarded

`

252

``

`` -

/// to the outside until the end up in an InferCtxt for typeck or borrowck.

``

253

``

`-

///

`

254

``

`` -

/// Its default value is DefiningAnchor::Bind(&[]), which means no opaque types may be defined.

``

255

``

`-

/// This way it is easier to catch errors that

`

256

``

`-

/// might come up during inference or typeck.

`

257

``

`-

pub defining_use_anchor: DefiningAnchor<'tcx>,

`

``

246

`` +

/// The DefIds of the opaque types that may have their hidden types constrained.

``

``

247

`+

defining_opaque_types: &'tcx ty::List,

`

258

248

``

259

249

`/// Whether this inference context should care about region obligations in

`

260

250

`/// the root universe. Most notably, this is used during hir typeck as region

`

`@@ -401,6 +391,10 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {

`

401

391

`fn probe_ct_var(&self, vid: ConstVid) -> Option<ty::Const<'tcx>> {

`

402

392

`self.probe_const_var(vid).ok()

`

403

393

`}

`

``

394

+

``

395

`+

fn defining_opaque_types(&self) -> &'tcx ty::List {

`

``

396

`+

self.defining_opaque_types

`

``

397

`+

}

`

404

398

`}

`

405

399

``

406

400

`` /// See the error_reporting module for more details.

``

`@@ -615,7 +609,7 @@ impl fmt::Display for FixupError {

`

615

609

`/// Used to configure inference contexts before their creation.

`

616

610

`pub struct InferCtxtBuilder<'tcx> {

`

617

611

`tcx: TyCtxt<'tcx>,

`

618

``

`-

defining_use_anchor: DefiningAnchor<'tcx>,

`

``

612

`+

defining_opaque_types: &'tcx ty::List,

`

619

613

`considering_regions: bool,

`

620

614

`skip_leak_check: bool,

`

621

615

`/// Whether we are in coherence mode.

`

`@@ -630,7 +624,7 @@ impl<'tcx> TyCtxt<'tcx> {

`

630

624

`fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {

`

631

625

`InferCtxtBuilder {

`

632

626

`tcx: self,

`

633

``

`-

defining_use_anchor: DefiningAnchor::Bind(ty::List::empty()),

`

``

627

`+

defining_opaque_types: ty::List::empty(),

`

634

628

`considering_regions: true,

`

635

629

`skip_leak_check: false,

`

636

630

`intercrate: false,

`

`@@ -646,8 +640,16 @@ impl<'tcx> InferCtxtBuilder<'tcx> {

`

646

640

`/// It is only meant to be called in two places, for typeck

`

647

641

`` /// (via Inherited::build) and for the inference context used

``

648

642

`/// in mir borrowck.

`

649

``

`-

pub fn with_opaque_type_inference(mut self, defining_use_anchor: DefiningAnchor<'tcx>) -> Self {

`

650

``

`-

self.defining_use_anchor = defining_use_anchor;

`

``

643

`+

pub fn with_opaque_type_inference(mut self, defining_anchor: LocalDefId) -> Self {

`

``

644

`+

self.defining_opaque_types = self.tcx.opaque_types_defined_by(defining_anchor);

`

``

645

`+

self

`

``

646

`+

}

`

``

647

+

``

648

`+

pub fn with_defining_opaque_types(

`

``

649

`+

mut self,

`

``

650

`+

defining_opaque_types: &'tcx ty::List,

`

``

651

`+

) -> Self {

`

``

652

`+

self.defining_opaque_types = defining_opaque_types;

`

651

653

`self

`

652

654

`}

`

653

655

``

`@@ -679,30 +681,30 @@ impl<'tcx> InferCtxtBuilder<'tcx> {

`

679

681

`` /// the bound values in C to their instantiated values in V

``

680

682

`` /// (in other words, S(C) = V).

``

681

683

`pub fn build_with_canonical(

`

682

``

`-

&mut self,

`

``

684

`+

self,

`

683

685

`span: Span,

`

684

686

`canonical: &Canonical<'tcx, T>,

`

685

687

`) -> (InferCtxt<'tcx>, T, CanonicalVarValues<'tcx>)

`

686

688

`where

`

687

689

`T: TypeFoldable<TyCtxt<'tcx>>,

`

688

690

`{

`

689

``

`-

let infcx = self.build();

`

``

691

`+

let infcx = self.with_defining_opaque_types(canonical.defining_opaque_types).build();

`

690

692

`let (value, args) = infcx.instantiate_canonical(span, canonical);

`

691

693

`(infcx, value, args)

`

692

694

`}

`

693

695

``

694

696

`pub fn build(&mut self) -> InferCtxt<'tcx> {

`

695

697

`let InferCtxtBuilder {

`

696

698

` tcx,

`

697

``

`-

defining_use_anchor,

`

``

699

`+

defining_opaque_types,

`

698

700

` considering_regions,

`

699

701

` skip_leak_check,

`

700

702

` intercrate,

`

701

703

` next_trait_solver,

`

702

704

`} = *self;

`

703

705

`InferCtxt {

`

704

706

` tcx,

`

705

``

`-

defining_use_anchor,

`

``

707

`+

defining_opaque_types,

`

706

708

` considering_regions,

`

707

709

` skip_leak_check,

`

708

710

`inner: RefCell::new(InferCtxtInner::new()),

`

`@@ -1230,6 +1232,12 @@ impl<'tcx> InferCtxt<'tcx> {

`

1230

1232

`self.inner.borrow().opaque_type_storage.opaque_types.clone()

`

1231

1233

`}

`

1232

1234

``

``

1235

`+

#[inline(always)]

`

``

1236

`+

pub fn can_define_opaque_ty(&self, id: impl Into) -> bool {

`

``

1237

`+

let Some(id) = id.into().as_local() else { return false };

`

``

1238

`+

self.defining_opaque_types.contains(&id)

`

``

1239

`+

}

`

``

1240

+

1233

1241

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

`

1234

1242

`self.resolve_vars_if_possible(t).to_string()

`

1235

1243

`}

`