-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[draft] Rework the way we treat opaques in check_opaque_meets_bounds
#124598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ pub use relate::combine::CombineFields; | |
pub use relate::combine::ObligationEmittingRelation; | ||
pub use relate::StructurallyRelateAliases; | ||
pub use rustc_macros::{TypeFoldable, TypeVisitable}; | ||
use rustc_middle::traits::Reveal; | ||
use rustc_middle::traits::TreatOpaque; | ||
pub use rustc_middle::ty::IntVarValue; | ||
pub use BoundRegionConversionTime::*; | ||
pub use RegionVariableOrigin::*; | ||
|
@@ -246,7 +248,7 @@ pub struct InferCtxt<'tcx> { | |
pub tcx: TyCtxt<'tcx>, | ||
|
||
/// The `DefIds` of the opaque types that may have their hidden types constrained. | ||
defining_opaque_types: &'tcx ty::List<LocalDefId>, | ||
opaque_type_mode: ty::OpaqueTypeMode<TyCtxt<'tcx>>, | ||
|
||
/// Whether this inference context should care about region obligations in | ||
/// the root universe. Most notably, this is used during hir typeck as region | ||
|
@@ -373,8 +375,8 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> { | |
self.inner.borrow_mut().unwrap_region_constraints().opportunistic_resolve_var(self.tcx, vid) | ||
} | ||
|
||
fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> { | ||
self.defining_opaque_types | ||
fn opaque_type_mode(&self) -> ty::OpaqueTypeMode<TyCtxt<'tcx>> { | ||
self.opaque_type_mode | ||
} | ||
|
||
fn opportunistic_resolve_ty_var(&self, vid: TyVid) -> Ty<'tcx> { | ||
|
@@ -621,7 +623,7 @@ impl fmt::Display for FixupError { | |
/// Used to configure inference contexts before their creation. | ||
pub struct InferCtxtBuilder<'tcx> { | ||
tcx: TyCtxt<'tcx>, | ||
defining_opaque_types: &'tcx ty::List<LocalDefId>, | ||
opaque_type_mode: ty::OpaqueTypeMode<TyCtxt<'tcx>>, | ||
considering_regions: bool, | ||
skip_leak_check: bool, | ||
/// Whether we are in coherence mode. | ||
|
@@ -636,7 +638,7 @@ impl<'tcx> TyCtxt<'tcx> { | |
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> { | ||
InferCtxtBuilder { | ||
tcx: self, | ||
defining_opaque_types: ty::List::empty(), | ||
opaque_type_mode: Default::default(), | ||
considering_regions: true, | ||
skip_leak_check: false, | ||
intercrate: false, | ||
|
@@ -646,22 +648,23 @@ impl<'tcx> TyCtxt<'tcx> { | |
} | ||
|
||
impl<'tcx> InferCtxtBuilder<'tcx> { | ||
pub fn with_opaque_type_mode( | ||
mut self, | ||
opaque_type_mode: ty::OpaqueTypeMode<TyCtxt<'tcx>>, | ||
) -> Self { | ||
self.opaque_type_mode = opaque_type_mode; | ||
self | ||
} | ||
|
||
/// Whenever the `InferCtxt` should be able to handle defining uses of opaque types, | ||
/// you need to call this function. Otherwise the opaque type will be treated opaquely. | ||
/// | ||
/// It is only meant to be called in two places, for typeck | ||
/// (via `Inherited::build`) and for the inference context used | ||
/// in mir borrowck. | ||
pub fn with_opaque_type_inference(mut self, defining_anchor: LocalDefId) -> Self { | ||
self.defining_opaque_types = self.tcx.opaque_types_defined_by(defining_anchor); | ||
self | ||
} | ||
|
||
pub fn with_defining_opaque_types( | ||
mut self, | ||
defining_opaque_types: &'tcx ty::List<LocalDefId>, | ||
) -> Self { | ||
self.defining_opaque_types = defining_opaque_types; | ||
let types = self.tcx.opaque_types_defined_by(defining_anchor); | ||
self.opaque_type_mode = ty::OpaqueTypeMode::Define(types); | ||
self | ||
} | ||
|
||
|
@@ -700,23 +703,23 @@ impl<'tcx> InferCtxtBuilder<'tcx> { | |
where | ||
T: TypeFoldable<TyCtxt<'tcx>>, | ||
{ | ||
let infcx = self.with_defining_opaque_types(canonical.defining_opaque_types).build(); | ||
let infcx = self.with_opaque_type_mode(canonical.opaque_type_mode).build(); | ||
let (value, args) = infcx.instantiate_canonical(span, canonical); | ||
(infcx, value, args) | ||
} | ||
|
||
pub fn build(&mut self) -> InferCtxt<'tcx> { | ||
let InferCtxtBuilder { | ||
tcx, | ||
defining_opaque_types, | ||
opaque_type_mode: defining_opaque_types, | ||
considering_regions, | ||
skip_leak_check, | ||
intercrate, | ||
next_trait_solver, | ||
} = *self; | ||
InferCtxt { | ||
tcx, | ||
defining_opaque_types, | ||
opaque_type_mode: defining_opaque_types, | ||
considering_regions, | ||
skip_leak_check, | ||
inner: RefCell::new(InferCtxtInner::new()), | ||
|
@@ -1240,10 +1243,30 @@ impl<'tcx> InferCtxt<'tcx> { | |
self.inner.borrow().opaque_type_storage.opaque_types.clone() | ||
} | ||
|
||
#[inline(always)] | ||
pub fn can_define_opaque_ty(&self, id: impl Into<DefId>) -> bool { | ||
let Some(id) = id.into().as_local() else { return false }; | ||
self.defining_opaque_types.contains(&id) | ||
pub fn treat_opaque_ty(&self, reveal: Reveal, def_id: DefId) -> TreatOpaque { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😊 |
||
if self.intercrate { | ||
return TreatOpaque::Ambiguous; | ||
} | ||
|
||
match reveal { | ||
Reveal::All => return TreatOpaque::Reveal, | ||
Reveal::UserFacing => {} | ||
} | ||
|
||
match self.opaque_type_mode { | ||
ty::OpaqueTypeMode::Define(list) => { | ||
if def_id.as_local().is_some_and(|def_id| list.contains(&def_id)) { | ||
return TreatOpaque::Define; | ||
} | ||
} | ||
ty::OpaqueTypeMode::Reveal(list) => { | ||
if def_id.as_local().is_some_and(|def_id| list.contains(&def_id)) { | ||
return TreatOpaque::Reveal; | ||
} | ||
} | ||
} | ||
|
||
TreatOpaque::Rigid | ||
} | ||
|
||
pub fn ty_to_string(&self, t: Ty<'tcx>) -> String { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,7 +6,7 @@ use hir::def_id::{DefId, LocalDefId}; | |||||||||||
use rustc_data_structures::fx::FxIndexMap; | ||||||||||||
use rustc_data_structures::sync::Lrc; | ||||||||||||
use rustc_hir as hir; | ||||||||||||
use rustc_middle::traits::ObligationCause; | ||||||||||||
use rustc_middle::traits::{ObligationCause, TreatOpaque}; | ||||||||||||
use rustc_middle::ty::error::{ExpectedFound, TypeError}; | ||||||||||||
use rustc_middle::ty::fold::BottomUpFolder; | ||||||||||||
use rustc_middle::ty::GenericArgKind; | ||||||||||||
|
@@ -58,7 +58,10 @@ impl<'tcx> InferCtxt<'tcx> { | |||||||||||
ct_op: |ct| ct, | ||||||||||||
ty_op: |ty| match *ty.kind() { | ||||||||||||
ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) | ||||||||||||
if self.can_define_opaque_ty(def_id) && !ty.has_escaping_bound_vars() => | ||||||||||||
if matches!( | ||||||||||||
self.treat_opaque_ty(param_env.reveal(), def_id), | ||||||||||||
TreatOpaque::Define | ||||||||||||
) && !ty.has_escaping_bound_vars() => | ||||||||||||
{ | ||||||||||||
let def_span = self.tcx.def_span(def_id); | ||||||||||||
let span = if span.contains(def_span) { def_span } else { span }; | ||||||||||||
|
@@ -85,16 +88,6 @@ impl<'tcx> InferCtxt<'tcx> { | |||||||||||
) -> InferResult<'tcx, ()> { | ||||||||||||
let process = |a: Ty<'tcx>, b: Ty<'tcx>| match *a.kind() { | ||||||||||||
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) if def_id.is_local() => { | ||||||||||||
let def_id = def_id.expect_local(); | ||||||||||||
if self.intercrate { | ||||||||||||
// See comment on `insert_hidden_type` for why this is sufficient in coherence | ||||||||||||
return Some(self.register_hidden_type( | ||||||||||||
OpaqueTypeKey { def_id, args }, | ||||||||||||
cause.clone(), | ||||||||||||
param_env, | ||||||||||||
b, | ||||||||||||
)); | ||||||||||||
} | ||||||||||||
// Check that this is `impl Trait` type is | ||||||||||||
// declared by `parent_def_id` -- i.e., one whose | ||||||||||||
// value we are inferring. At present, this is | ||||||||||||
|
@@ -129,8 +122,18 @@ impl<'tcx> InferCtxt<'tcx> { | |||||||||||
// let x = || foo(); // returns the Opaque assoc with `foo` | ||||||||||||
// } | ||||||||||||
// ``` | ||||||||||||
if !self.can_define_opaque_ty(def_id) { | ||||||||||||
return None; | ||||||||||||
match self.treat_opaque_ty(param_env.reveal(), def_id) { | ||||||||||||
TreatOpaque::Define => {} | ||||||||||||
TreatOpaque::Reveal | TreatOpaque::Rigid => return None, | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
TreatOpaque::Ambiguous => { | ||||||||||||
// See comment on `insert_hidden_type` for why this is sufficient in coherence | ||||||||||||
return Some(self.register_hidden_type( | ||||||||||||
OpaqueTypeKey { def_id: def_id.expect_local(), args }, | ||||||||||||
cause.clone(), | ||||||||||||
param_env, | ||||||||||||
b, | ||||||||||||
)); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }) = *b.kind() { | ||||||||||||
|
@@ -139,8 +142,10 @@ impl<'tcx> InferCtxt<'tcx> { | |||||||||||
// no one encounters it in practice. | ||||||||||||
// It does occur however in `fn fut() -> impl Future<Output = i32> { async { 42 } }`, | ||||||||||||
// where it is of no concern, so we only check for TAITs. | ||||||||||||
if self.can_define_opaque_ty(b_def_id) | ||||||||||||
&& self.tcx.is_type_alias_impl_trait(b_def_id) | ||||||||||||
if matches!( | ||||||||||||
self.treat_opaque_ty(param_env.reveal(), b_def_id), | ||||||||||||
TreatOpaque::Define | ||||||||||||
) && self.tcx.is_type_alias_impl_trait(b_def_id) | ||||||||||||
{ | ||||||||||||
self.tcx.dcx().emit_err(OpaqueHiddenTypeDiag { | ||||||||||||
span: cause.span, | ||||||||||||
|
@@ -150,7 +155,7 @@ impl<'tcx> InferCtxt<'tcx> { | |||||||||||
} | ||||||||||||
} | ||||||||||||
Some(self.register_hidden_type( | ||||||||||||
OpaqueTypeKey { def_id, args }, | ||||||||||||
OpaqueTypeKey { def_id: def_id.expect_local(), args }, | ||||||||||||
cause.clone(), | ||||||||||||
param_env, | ||||||||||||
b, | ||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,13 @@ pub enum Reveal { | |
All, | ||
} | ||
|
||
pub enum TreatOpaque { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs comment |
||
Reveal, | ||
Define, | ||
Rigid, | ||
Ambiguous, | ||
} | ||
|
||
/// The reason why we incurred this obligation; used for error reporting. | ||
/// | ||
/// Non-misc `ObligationCauseCode`s are stored on the heap. This gives the | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.