Rollup merge of #124955 - nnethercote:next_ty_var, r=lcnr

Use fewer origins when creating type variables.

To reduce lots of repetitive boilerplate code. Details in the individual commit messages.

r? ``@lcnr``
This commit is contained in:
Matthias Krüger 2024-05-10 07:30:22 +02:00 committed by GitHub
commit 0ee258009c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 135 additions and 339 deletions

View file

@ -21,8 +21,7 @@
//!
//! [c]: https://rust-lang.github.io/chalk/book/canonical_queries/canonicalization.html
use crate::infer::ConstVariableOrigin;
use crate::infer::{InferCtxt, RegionVariableOrigin, TypeVariableOrigin};
use crate::infer::{InferCtxt, RegionVariableOrigin};
use rustc_index::IndexVec;
use rustc_middle::infer::unify_key::EffectVarValue;
use rustc_middle::ty::fold::TypeFoldable;
@ -114,10 +113,9 @@ impl<'tcx> InferCtxt<'tcx> {
match cv_info.kind {
CanonicalVarKind::Ty(ty_kind) => {
let ty = match ty_kind {
CanonicalTyVarKind::General(ui) => self.next_ty_var_in_universe(
TypeVariableOrigin { param_def_id: None, span },
universe_map(ui),
),
CanonicalTyVarKind::General(ui) => {
self.next_ty_var_in_universe(span, universe_map(ui))
}
CanonicalTyVarKind::Int => self.next_int_var(),
@ -145,13 +143,9 @@ impl<'tcx> InferCtxt<'tcx> {
ty::Region::new_placeholder(self.tcx, placeholder_mapped).into()
}
CanonicalVarKind::Const(ui, ty) => self
.next_const_var_in_universe(
ty,
ConstVariableOrigin { param_def_id: None, span },
universe_map(ui),
)
.into(),
CanonicalVarKind::Const(ui, ty) => {
self.next_const_var_in_universe(ty, span, universe_map(ui)).into()
}
CanonicalVarKind::Effect => {
let vid = self
.inner

View file

@ -3,7 +3,6 @@ use crate::errors::{
SourceKindMultiSuggestion, SourceKindSubdiag,
};
use crate::infer::error_reporting::TypeErrCtxt;
use crate::infer::type_variable::TypeVariableOrigin;
use crate::infer::InferCtxt;
use rustc_errors::{codes::*, Diag, IntoDiagArg};
use rustc_hir as hir;
@ -13,7 +12,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, LetStmt, LocalSource};
use rustc_middle::hir::nested_filter;
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableValue};
use rustc_middle::infer::unify_key::ConstVariableValue;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
use rustc_middle::ty::{
@ -542,18 +541,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
match arg.unpack() {
GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
GenericArgKind::Type(_) => self
.next_ty_var(TypeVariableOrigin {
span: DUMMY_SP,
param_def_id: None,
})
.into(),
GenericArgKind::Const(arg) => self
.next_const_var(
arg.ty(),
ConstVariableOrigin { span: DUMMY_SP, param_def_id: None },
)
.into(),
GenericArgKind::Type(_) => self.next_ty_var(DUMMY_SP).into(),
GenericArgKind::Const(arg) => {
self.next_const_var(arg.ty(), DUMMY_SP).into()
}
}
}))
.unwrap();
@ -569,9 +560,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}
InferSourceKind::FullyQualifiedMethodCall { receiver, successor, args, def_id } => {
let placeholder = Some(
self.next_ty_var(TypeVariableOrigin { span: DUMMY_SP, param_def_id: None }),
);
let placeholder = Some(self.next_ty_var(DUMMY_SP));
if let Some(args) = args.make_suggestable(self.infcx.tcx, true, placeholder) {
let mut printer = fmt_printer(self, Namespace::ValueNS);
printer.print_def_path(def_id, args).unwrap();
@ -605,9 +594,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}
InferSourceKind::ClosureReturn { ty, data, should_wrap_expr } => {
let placeholder = Some(
self.next_ty_var(TypeVariableOrigin { span: DUMMY_SP, param_def_id: None }),
);
let placeholder = Some(self.next_ty_var(DUMMY_SP));
if let Some(ty) = ty.make_suggestable(self.infcx.tcx, true, placeholder) {
let ty_info = ty_to_string(self, ty, None);
multi_suggestions.push(SourceKindMultiSuggestion::new_closure_return(

View file

@ -989,41 +989,50 @@ impl<'tcx> InferCtxt<'tcx> {
self.inner.borrow_mut().type_variables().num_vars()
}
pub fn next_ty_var_id(&self, origin: TypeVariableOrigin) -> TyVid {
self.inner.borrow_mut().type_variables().new_var(self.universe(), origin)
pub fn next_ty_var(&self, span: Span) -> Ty<'tcx> {
self.next_ty_var_with_origin(TypeVariableOrigin { span, param_def_id: None })
}
pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
Ty::new_var(self.tcx, self.next_ty_var_id(origin))
}
pub fn next_ty_var_id_in_universe(
&self,
origin: TypeVariableOrigin,
universe: ty::UniverseIndex,
) -> TyVid {
self.inner.borrow_mut().type_variables().new_var(universe, origin)
}
pub fn next_ty_var_in_universe(
&self,
origin: TypeVariableOrigin,
universe: ty::UniverseIndex,
) -> Ty<'tcx> {
let vid = self.next_ty_var_id_in_universe(origin, universe);
pub fn next_ty_var_with_origin(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
let vid = self.inner.borrow_mut().type_variables().new_var(self.universe(), origin);
Ty::new_var(self.tcx, vid)
}
pub fn next_const_var(&self, ty: Ty<'tcx>, origin: ConstVariableOrigin) -> ty::Const<'tcx> {
ty::Const::new_var(self.tcx, self.next_const_var_id(origin), ty)
pub fn next_ty_var_id_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> TyVid {
let origin = TypeVariableOrigin { span, param_def_id: None };
self.inner.borrow_mut().type_variables().new_var(universe, origin)
}
pub fn next_ty_var_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> Ty<'tcx> {
let vid = self.next_ty_var_id_in_universe(span, universe);
Ty::new_var(self.tcx, vid)
}
pub fn next_const_var(&self, ty: Ty<'tcx>, span: Span) -> ty::Const<'tcx> {
self.next_const_var_with_origin(ty, ConstVariableOrigin { span, param_def_id: None })
}
pub fn next_const_var_with_origin(
&self,
ty: Ty<'tcx>,
origin: ConstVariableOrigin,
) -> ty::Const<'tcx> {
let vid = self
.inner
.borrow_mut()
.const_unification_table()
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
.vid;
ty::Const::new_var(self.tcx, vid, ty)
}
pub fn next_const_var_in_universe(
&self,
ty: Ty<'tcx>,
origin: ConstVariableOrigin,
span: Span,
universe: ty::UniverseIndex,
) -> ty::Const<'tcx> {
let origin = ConstVariableOrigin { span, param_def_id: None };
let vid = self
.inner
.borrow_mut()
@ -1033,28 +1042,14 @@ impl<'tcx> InferCtxt<'tcx> {
ty::Const::new_var(self.tcx, vid, ty)
}
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid {
self.inner
.borrow_mut()
.const_unification_table()
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
.vid
}
fn next_int_var_id(&self) -> IntVid {
self.inner.borrow_mut().int_unification_table().new_key(None)
}
pub fn next_int_var(&self) -> Ty<'tcx> {
Ty::new_int_var(self.tcx, self.next_int_var_id())
}
fn next_float_var_id(&self) -> FloatVid {
self.inner.borrow_mut().float_unification_table().new_key(None)
let vid = self.inner.borrow_mut().int_unification_table().new_key(None);
Ty::new_int_var(self.tcx, vid)
}
pub fn next_float_var(&self) -> Ty<'tcx> {
Ty::new_float_var(self.tcx, self.next_float_var_id())
let vid = self.inner.borrow_mut().float_unification_table().new_key(None);
Ty::new_float_var(self.tcx, vid)
}
/// Creates a fresh region variable with the next available index.
@ -1468,24 +1463,13 @@ impl<'tcx> InferCtxt<'tcx> {
fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
self.map
.entry(bt.var)
.or_insert_with(|| {
self.infcx
.next_ty_var(TypeVariableOrigin { param_def_id: None, span: self.span })
.into()
})
.or_insert_with(|| self.infcx.next_ty_var(self.span).into())
.expect_ty()
}
fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx> {
self.map
.entry(bv)
.or_insert_with(|| {
self.infcx
.next_const_var(
ty,
ConstVariableOrigin { param_def_id: None, span: self.span },
)
.into()
})
.or_insert_with(|| self.infcx.next_const_var(ty, self.span).into())
.expect_const()
}
}

View file

@ -1,4 +1,3 @@
use super::type_variable::TypeVariableOrigin;
use super::{DefineOpaqueTypes, InferResult};
use crate::errors::OpaqueHiddenTypeDiag;
use crate::infer::{InferCtxt, InferOk};
@ -65,7 +64,7 @@ impl<'tcx> InferCtxt<'tcx> {
let span = if span.contains(def_span) { def_span } else { span };
let code = traits::ObligationCauseCode::OpaqueReturnType(None);
let cause = ObligationCause::new(span, body_id, code);
let ty_var = self.next_ty_var(TypeVariableOrigin { param_def_id: None, span });
let ty_var = self.next_ty_var(span);
obligations.extend(
self.handle_opaque_type(ty, ty_var, &cause, param_env).unwrap().obligations,
);

View file

@ -3,7 +3,6 @@ use rustc_middle::ty::{self, Ty};
use crate::traits::{Obligation, PredicateObligation};
use super::type_variable::TypeVariableOrigin;
use super::InferCtxt;
impl<'tcx> InferCtxt<'tcx> {
@ -23,10 +22,7 @@ impl<'tcx> InferCtxt<'tcx> {
) -> Ty<'tcx> {
debug_assert!(!self.next_trait_solver());
let def_id = projection_ty.def_id;
let ty_var = self.next_ty_var(TypeVariableOrigin {
param_def_id: None,
span: self.tcx.def_span(def_id),
});
let ty_var = self.next_ty_var(self.tcx.def_span(def_id));
let projection = ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::Projection(
ty::ProjectionPredicate { projection_ty, term: ty_var.into() },
)));

View file

@ -1,7 +1,7 @@
use std::mem;
use super::StructurallyRelateAliases;
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableValue};
use crate::infer::type_variable::TypeVariableValue;
use crate::infer::{InferCtxt, ObligationEmittingRelation, RegionVariableOrigin};
use rustc_data_structures::sso::SsoHashMap;
use rustc_data_structures::stack::ensure_sufficient_stack;
@ -357,10 +357,7 @@ impl<'tcx> Generalizer<'_, 'tcx> {
//
// cc trait-system-refactor-initiative#110
if self.infcx.next_trait_solver() && !alias.has_escaping_bound_vars() && !self.in_alias {
return Ok(self.infcx.next_ty_var_in_universe(
TypeVariableOrigin { param_def_id: None, span: self.span },
self.for_universe,
));
return Ok(self.infcx.next_ty_var_in_universe(self.span, self.for_universe));
}
let is_nested_alias = mem::replace(&mut self.in_alias, true);
@ -380,10 +377,7 @@ impl<'tcx> Generalizer<'_, 'tcx> {
}
debug!("generalization failure in alias");
Ok(self.infcx.next_ty_var_in_universe(
TypeVariableOrigin { param_def_id: None, span: self.span },
self.for_universe,
))
Ok(self.infcx.next_ty_var_in_universe(self.span, self.for_universe))
}
}
};

View file

@ -18,7 +18,6 @@
//! [lattices]: https://en.wikipedia.org/wiki/Lattice_(order)
use super::combine::ObligationEmittingRelation;
use crate::infer::type_variable::TypeVariableOrigin;
use crate::infer::{DefineOpaqueTypes, InferCtxt};
use crate::traits::ObligationCause;
@ -88,14 +87,12 @@ where
// iterate on the subtype obligations that are returned, but I
// think this suffices. -nmatsakis
(&ty::Infer(TyVar(..)), _) => {
let v = infcx
.next_ty_var(TypeVariableOrigin { param_def_id: None, span: this.cause().span });
let v = infcx.next_ty_var(this.cause().span);
this.relate_bound(v, b, a)?;
Ok(v)
}
(_, &ty::Infer(TyVar(..))) => {
let v = infcx
.next_ty_var(TypeVariableOrigin { param_def_id: None, span: this.cause().span });
let v = infcx.next_ty_var(this.cause().span);
this.relate_bound(v, a, b)?;
Ok(v)
}

View file

@ -195,7 +195,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
// Recreate it with a fresh variable here.
let idx = vid.as_usize() - self.type_vars.0.start.as_usize();
let origin = self.type_vars.1[idx];
self.infcx.next_ty_var(origin)
self.infcx.next_ty_var_with_origin(origin)
} else {
// This variable was created before the
// "fudging". Since we refresh all type
@ -244,7 +244,7 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for InferenceFudger<'a, 'tcx> {
// Recreate it with a fresh variable here.
let idx = vid.index() - self.const_vars.0.start.index();
let origin = self.const_vars.1[idx];
self.infcx.next_const_var(ct.ty(), origin)
self.infcx.next_const_var_with_origin(ct.ty(), origin)
} else {
ct
}