1
Fork 0

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.
This commit is contained in:
Nicholas Nethercote 2024-05-10 09:06:47 +10:00
parent 11f2ca340c
commit fe843feaab
41 changed files with 119 additions and 312 deletions

View file

@ -11,7 +11,6 @@ use std::assert_matches::assert_matches;
use itertools::Itertools;
use rustc_hir as hir;
use rustc_infer::infer::type_variable::TypeVariableOrigin;
use rustc_infer::infer::{BoundRegionConversionTime, RegionVariableOrigin};
use rustc_middle::mir::*;
use rustc_middle::ty::{self, Ty};
@ -74,9 +73,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}),
);
let next_ty_var = || {
self.infcx.next_ty_var(TypeVariableOrigin { span: body.span, param_def_id: None })
};
let next_ty_var = || self.infcx.next_ty_var(body.span);
let output_ty = Ty::new_coroutine(
self.tcx(),
self.tcx().coroutine_for_closure(mir_def_id),

View file

@ -16,7 +16,6 @@ use rustc_index::{IndexSlice, IndexVec};
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_infer::infer::outlives::env::RegionBoundPairs;
use rustc_infer::infer::region_constraints::RegionConstraintData;
use rustc_infer::infer::type_variable::TypeVariableOrigin;
use rustc_infer::infer::{
BoundRegion, BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin,
};
@ -2356,10 +2355,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
// Types with regions are comparable if they have a common super-type.
ty::RawPtr(_, _) | ty::FnPtr(_) => {
let ty_right = right.ty(body, tcx);
let common_ty = self.infcx.next_ty_var(TypeVariableOrigin {
param_def_id: None,
span: body.source_info(location).span,
});
let common_ty = self.infcx.next_ty_var(body.source_info(location).span);
self.sub_types(
ty_left,
common_ty,

View file

@ -1,6 +1,5 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::ErrorGuaranteed;
use rustc_infer::infer::type_variable::TypeVariableOrigin;
use rustc_infer::infer::NllRegionVariableOrigin;
use rustc_infer::infer::{ObligationEmittingRelation, StructurallyRelateAliases};
use rustc_infer::traits::{Obligation, PredicateObligations};
@ -129,10 +128,7 @@ impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
// by using `ty_vid rel B` and then finally and end by equating `ty_vid` to
// the opaque.
let mut enable_subtyping = |ty, opaque_is_expected| {
let ty_vid = infcx.next_ty_var_id_in_universe(
TypeVariableOrigin { param_def_id: None, span: self.span() },
ty::UniverseIndex::ROOT,
);
let ty_vid = infcx.next_ty_var_id_in_universe(self.span(), ty::UniverseIndex::ROOT);
let variance = if opaque_is_expected {
self.ambient_variance