diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2024-05-10 09:06:47 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2024-05-10 09:47:46 +1000 |
| commit | fe843feaabfd48fa5ed7e10f00a9d22bb64a20ef (patch) | |
| tree | cac7f74bc610e4da2445dfbcd130c2a9bb1ec03c /compiler/rustc_infer/src | |
| parent | 11f2ca340c427e0ce5e2e0288595ae7900a5e545 (diff) | |
| download | rust-fe843feaabfd48fa5ed7e10f00a9d22bb64a20ef.tar.gz rust-fe843feaabfd48fa5ed7e10f00a9d22bb64a20ef.zip | |
Use fewer origins when creating type variables.
`InferCtxt::next_{ty,const}_var*` all take an origin, but the
`param_def_id` is almost always `None`. This commit changes them to just
take a `Span` and build the origin within the method, and adds new
methods for the rare cases where `param_def_id` might not be `None`.
This avoids a lot of tedious origin building.
Specifically:
- next_ty_var{,_id_in_universe,_in_universe}: now take `Span` instead of
`TypeVariableOrigin`
- next_ty_var_with_origin: added
- next_const_var{,_in_universe}: takes Span instead of ConstVariableOrigin
- next_const_var_with_origin: added
- next_region_var, next_region_var_in_universe: these are unchanged,
still take RegionVariableOrigin
The API inconsistency (ty/const vs region) seems worth it for the
large conciseness improvements.
Diffstat (limited to 'compiler/rustc_infer/src')
| -rw-r--r-- | compiler/rustc_infer/src/infer/canonical/mod.rs | 20 | ||||
| -rw-r--r-- | compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs | 27 | ||||
| -rw-r--r-- | compiler/rustc_infer/src/infer/mod.rs | 49 | ||||
| -rw-r--r-- | compiler/rustc_infer/src/infer/opaque_types/mod.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_infer/src/infer/projection.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_infer/src/infer/relate/generalize.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_infer/src/infer/relate/lattice.rs | 7 | ||||
| -rw-r--r-- | compiler/rustc_infer/src/infer/snapshot/fudge.rs | 4 |
8 files changed, 45 insertions, 83 deletions
diff --git a/compiler/rustc_infer/src/infer/canonical/mod.rs b/compiler/rustc_infer/src/infer/canonical/mod.rs index 734fa919eb5..1abb8086d41 100644 --- a/compiler/rustc_infer/src/infer/canonical/mod.rs +++ b/compiler/rustc_infer/src/infer/canonical/mod.rs @@ -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 diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs index a2a38d1c507..fd516e735d6 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs @@ -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( diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index a10fcd4aef0..016402ac0ec 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -989,29 +989,34 @@ impl<'tcx> InferCtxt<'tcx> { self.inner.borrow_mut().type_variables().num_vars() } - pub fn next_ty_var(&self, origin: TypeVariableOrigin) -> Ty<'tcx> { + 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_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_ty_var_id_in_universe( - &self, - origin: TypeVariableOrigin, - universe: ty::UniverseIndex, - ) -> TyVid { + 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, - origin: TypeVariableOrigin, - universe: ty::UniverseIndex, - ) -> Ty<'tcx> { - let vid = self.next_ty_var_id_in_universe(origin, universe); + 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>, origin: ConstVariableOrigin) -> ty::Const<'tcx> { + 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() @@ -1024,9 +1029,10 @@ impl<'tcx> InferCtxt<'tcx> { 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() @@ -1457,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() } } diff --git a/compiler/rustc_infer/src/infer/opaque_types/mod.rs b/compiler/rustc_infer/src/infer/opaque_types/mod.rs index b7ee860cf07..703bd5ae90b 100644 --- a/compiler/rustc_infer/src/infer/opaque_types/mod.rs +++ b/compiler/rustc_infer/src/infer/opaque_types/mod.rs @@ -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, ); diff --git a/compiler/rustc_infer/src/infer/projection.rs b/compiler/rustc_infer/src/infer/projection.rs index e60efe37fd9..041838ffc16 100644 --- a/compiler/rustc_infer/src/infer/projection.rs +++ b/compiler/rustc_infer/src/infer/projection.rs @@ -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() }, ))); diff --git a/compiler/rustc_infer/src/infer/relate/generalize.rs b/compiler/rustc_infer/src/infer/relate/generalize.rs index fb9198b4c95..5880ca788bc 100644 --- a/compiler/rustc_infer/src/infer/relate/generalize.rs +++ b/compiler/rustc_infer/src/infer/relate/generalize.rs @@ -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)) } } }; diff --git a/compiler/rustc_infer/src/infer/relate/lattice.rs b/compiler/rustc_infer/src/infer/relate/lattice.rs index f9470c9b8f6..38e25b0d9b6 100644 --- a/compiler/rustc_infer/src/infer/relate/lattice.rs +++ b/compiler/rustc_infer/src/infer/relate/lattice.rs @@ -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) } diff --git a/compiler/rustc_infer/src/infer/snapshot/fudge.rs b/compiler/rustc_infer/src/infer/snapshot/fudge.rs index 83667f7276d..4408251c99d 100644 --- a/compiler/rustc_infer/src/infer/snapshot/fudge.rs +++ b/compiler/rustc_infer/src/infer/snapshot/fudge.rs @@ -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 } |
