Auto merge of #138785 - lcnr:typing-mode-borrowck, r=compiler-errors,oli-obk
add `TypingMode::Borrowck` Shares the first commit with #138499, doesn't really matter which PR to land first 😊 😁 Introduces `TypingMode::Borrowck` which unlike `TypingMode::Analysis`, uses the hidden type computed by HIR typeck as the initial value of opaques instead of an unconstrained infer var. This is a part of https://github.com/rust-lang/types-team/issues/129. Using this new `TypingMode` is unfortunately a breaking change for now, see tests/ui/impl-trait/non-defining-uses/as-projection-term.rs. Using an inference variable as the initial value results in non-defining uses in the defining scope. We therefore only enable it if with `-Znext-solver=globally` or `-Ztyping-mode-borrowck` To do that the PR contains the following changes: - `TypeckResults::concrete_opaque_type` are already mapped to the definition of the opaque type - writeback now checks that the non-lifetime parameters of the opaque are universal - for this, `fn check_opaque_type_parameter_valid` is moved from `rustc_borrowck` to `rustc_trait_selection` - we add a new `query type_of_opaque_hir_typeck` which, using the same visitors as MIR typeck, attempts to merge the hidden types from HIR typeck from all defining scopes - done by adding a `DefiningScopeKind` flag to toggle between using borrowck and HIR typeck - the visitors stop checking that the MIR type matches the HIR type. This is trivial as the HIR type are now used as the initial hidden types of the opaque. This check is useful as a safeguard when not using `TypingMode::Borrowck`, but adding it to the new structure is annoying and it's not soundness critical, so I intend to not add it back. - add a `TypingMode::Borrowck` which behaves just like `TypingMode::Analysis` except when normalizing opaque types - it uses `type_of_opaque_hir_typeck(opaque)` as the initial value after replacing its regions with new inference vars - it uses structural lookup in the new solver fixes #112201, fixes #132335, fixes #137751 r? `@compiler-errors` `@oli-obk`
This commit is contained in:
commit
17ffbc81a3
136 changed files with 1069 additions and 993 deletions
|
@ -967,7 +967,8 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
pub fn can_define_opaque_ty(&self, id: impl Into<DefId>) -> bool {
|
||||
debug_assert!(!self.next_trait_solver());
|
||||
match self.typing_mode() {
|
||||
TypingMode::Analysis { defining_opaque_types } => {
|
||||
TypingMode::Analysis { defining_opaque_types }
|
||||
| TypingMode::Borrowck { defining_opaque_types } => {
|
||||
id.into().as_local().is_some_and(|def_id| defining_opaque_types.contains(&def_id))
|
||||
}
|
||||
// FIXME(#132279): This function is quite weird in post-analysis
|
||||
|
@ -1261,7 +1262,8 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
// to handle them without proper canonicalization. This means we may cause cycle
|
||||
// errors and fail to reveal opaques while inside of bodies. We should rename this
|
||||
// function and require explicit comments on all use-sites in the future.
|
||||
ty::TypingMode::Analysis { defining_opaque_types: _ } => {
|
||||
ty::TypingMode::Analysis { defining_opaque_types: _ }
|
||||
| ty::TypingMode::Borrowck { defining_opaque_types: _ } => {
|
||||
TypingMode::non_body_analysis()
|
||||
}
|
||||
mode @ (ty::TypingMode::Coherence
|
||||
|
|
|
@ -12,7 +12,7 @@ use rustc_middle::ty::{
|
|||
use rustc_span::Span;
|
||||
use tracing::{debug, instrument};
|
||||
|
||||
use super::DefineOpaqueTypes;
|
||||
use super::{DefineOpaqueTypes, RegionVariableOrigin};
|
||||
use crate::errors::OpaqueHiddenTypeDiag;
|
||||
use crate::infer::{InferCtxt, InferOk};
|
||||
use crate::traits::{self, Obligation, PredicateObligations};
|
||||
|
@ -221,6 +221,7 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
hidden_ty: Ty<'tcx>,
|
||||
goals: &mut Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
|
||||
) -> Result<(), TypeError<'tcx>> {
|
||||
let tcx = self.tcx;
|
||||
// Ideally, we'd get the span where *this specific `ty` came
|
||||
// from*, but right now we just use the span from the overall
|
||||
// value being folded. In simple cases like `-> impl Foo`,
|
||||
|
@ -231,7 +232,7 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
// During intercrate we do not define opaque types but instead always
|
||||
// force ambiguity unless the hidden type is known to not implement
|
||||
// our trait.
|
||||
goals.push(Goal::new(self.tcx, param_env, ty::PredicateKind::Ambiguous));
|
||||
goals.push(Goal::new(tcx, param_env, ty::PredicateKind::Ambiguous));
|
||||
}
|
||||
ty::TypingMode::Analysis { .. } => {
|
||||
let prev = self
|
||||
|
@ -249,6 +250,36 @@ impl<'tcx> InferCtxt<'tcx> {
|
|||
);
|
||||
}
|
||||
}
|
||||
ty::TypingMode::Borrowck { .. } => {
|
||||
let prev = self
|
||||
.inner
|
||||
.borrow_mut()
|
||||
.opaque_types()
|
||||
.register(opaque_type_key, OpaqueHiddenType { ty: hidden_ty, span });
|
||||
|
||||
// We either equate the new hidden type with the previous entry or with the type
|
||||
// inferred by HIR typeck.
|
||||
let actual = prev.unwrap_or_else(|| {
|
||||
let actual = tcx
|
||||
.type_of_opaque_hir_typeck(opaque_type_key.def_id)
|
||||
.instantiate(self.tcx, opaque_type_key.args);
|
||||
let actual = ty::fold_regions(tcx, actual, |re, _dbi| match re.kind() {
|
||||
ty::ReErased => {
|
||||
self.next_region_var(RegionVariableOrigin::MiscVariable(span))
|
||||
}
|
||||
_ => re,
|
||||
});
|
||||
actual
|
||||
});
|
||||
|
||||
goals.extend(
|
||||
self.at(&ObligationCause::dummy_with_span(span), param_env)
|
||||
.eq(DefineOpaqueTypes::Yes, hidden_ty, actual)?
|
||||
.obligations
|
||||
.into_iter()
|
||||
.map(|obligation| obligation.as_goal()),
|
||||
);
|
||||
}
|
||||
mode @ (ty::TypingMode::PostBorrowckAnalysis { .. } | ty::TypingMode::PostAnalysis) => {
|
||||
bug!("insert hidden type in {mode:?}")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue