1
Fork 0

Remove TypeVariableOriginKind

This commit is contained in:
Michael Goulet 2024-03-24 12:47:01 -04:00
parent 99d0186b1d
commit 34bce07e8e
39 changed files with 154 additions and 307 deletions

View file

@ -22,7 +22,7 @@
//! [c]: https://rust-lang.github.io/chalk/book/canonical_queries/canonicalization.html
use crate::infer::{ConstVariableOrigin, ConstVariableOriginKind};
use crate::infer::{InferCtxt, RegionVariableOrigin, TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::{InferCtxt, RegionVariableOrigin, TypeVariableOrigin};
use rustc_index::IndexVec;
use rustc_middle::infer::unify_key::EffectVarValue;
use rustc_middle::ty::fold::TypeFoldable;
@ -115,7 +115,7 @@ impl<'tcx> InferCtxt<'tcx> {
CanonicalVarKind::Ty(ty_kind) => {
let ty = match ty_kind {
CanonicalTyVarKind::General(ui) => self.next_ty_var_in_universe(
TypeVariableOrigin { kind: TypeVariableOriginKind::MiscVariable, span },
TypeVariableOrigin { param_def_id: None, span },
universe_map(ui),
),

View file

@ -3,7 +3,7 @@ use crate::errors::{
SourceKindMultiSuggestion, SourceKindSubdiag,
};
use crate::infer::error_reporting::TypeErrCtxt;
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::type_variable::TypeVariableOrigin;
use crate::infer::InferCtxt;
use rustc_errors::{codes::*, Diag, IntoDiagArg};
use rustc_hir as hir;
@ -188,7 +188,8 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinte
let mut infcx_inner = infcx.inner.borrow_mut();
let ty_vars = infcx_inner.type_variables();
let var_origin = ty_vars.var_origin(ty_vid);
if let TypeVariableOriginKind::TypeParameterDefinition(name, def_id) = var_origin.kind
if let Some(def_id) = var_origin.param_def_id
&& let name = infcx.tcx.item_name(def_id)
&& name != kw::SelfUpper
&& !var_origin.span.from_expansion()
{
@ -302,9 +303,8 @@ impl<'tcx> InferCtxt<'tcx> {
let mut inner = self.inner.borrow_mut();
let ty_vars = &inner.type_variables();
let var_origin = ty_vars.var_origin(ty_vid);
if let TypeVariableOriginKind::TypeParameterDefinition(name, def_id) =
var_origin.kind
{
if let Some(def_id) = var_origin.param_def_id {
let name = self.tcx.item_name(def_id);
if name != kw::SelfUpper && !var_origin.span.from_expansion() {
return InferenceDiagnosticsData {
name: name.to_string(),
@ -549,7 +549,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
GenericArgKind::Type(_) => self
.next_ty_var(TypeVariableOrigin {
span: DUMMY_SP,
kind: TypeVariableOriginKind::MiscVariable,
param_def_id: None,
})
.into(),
GenericArgKind::Const(arg) => self
@ -576,10 +576,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}
InferSourceKind::FullyQualifiedMethodCall { receiver, successor, args, def_id } => {
let placeholder = Some(self.next_ty_var(TypeVariableOrigin {
span: DUMMY_SP,
kind: TypeVariableOriginKind::MiscVariable,
}));
let placeholder = Some(
self.next_ty_var(TypeVariableOrigin { span: DUMMY_SP, param_def_id: None }),
);
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();
@ -613,10 +612,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}
InferSourceKind::ClosureReturn { ty, data, should_wrap_expr } => {
let placeholder = Some(self.next_ty_var(TypeVariableOrigin {
span: DUMMY_SP,
kind: TypeVariableOriginKind::MiscVariable,
}));
let placeholder = Some(
self.next_ty_var(TypeVariableOrigin { span: DUMMY_SP, param_def_id: None }),
);
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

@ -48,7 +48,7 @@ use rustc_span::Span;
use snapshot::undo_log::InferCtxtUndoLogs;
use std::cell::{Cell, RefCell};
use std::fmt;
use type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use type_variable::TypeVariableOrigin;
pub mod at;
pub mod canonical;
@ -1111,13 +1111,7 @@ impl<'tcx> InferCtxt<'tcx> {
// as the generic parameters for the default, `(T, U)`.
let ty_var_id = self.inner.borrow_mut().type_variables().new_var(
self.universe(),
TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeParameterDefinition(
param.name,
param.def_id,
),
span,
},
TypeVariableOrigin { param_def_id: Some(param.def_id), span },
);
Ty::new_var(self.tcx, ty_var_id).into()
@ -1411,10 +1405,7 @@ impl<'tcx> InferCtxt<'tcx> {
.entry(bt.var)
.or_insert_with(|| {
self.infcx
.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span: self.span,
})
.next_ty_var(TypeVariableOrigin { param_def_id: None, span: self.span })
.into()
})
.expect_ty()

View file

@ -1,4 +1,4 @@
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use super::type_variable::TypeVariableOrigin;
use super::{DefineOpaqueTypes, InferResult};
use crate::errors::OpaqueHiddenTypeDiag;
use crate::infer::{InferCtxt, InferOk};
@ -68,10 +68,7 @@ impl<'tcx> InferCtxt<'tcx> {
// FIXME(compiler-errors): We probably should add a new TypeVariableOriginKind
// for opaque types, and then use that kind to fix the spans for type errors
// that we see later on.
let ty_var = self.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span,
});
let ty_var = self.next_ty_var(TypeVariableOrigin { param_def_id: None, span });
obligations.extend(
self.handle_opaque_type(ty, ty_var, &cause, param_env).unwrap().obligations,
);

View file

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

View file

@ -1,7 +1,7 @@
use std::mem;
use super::StructurallyRelateAliases;
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind, TypeVariableValue};
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableValue};
use crate::infer::{InferCtxt, ObligationEmittingRelation, RegionVariableOrigin};
use rustc_data_structures::sso::SsoHashMap;
use rustc_data_structures::stack::ensure_sufficient_stack;
@ -352,7 +352,7 @@ impl<'tcx> Generalizer<'_, 'tcx> {
) -> Result<Ty<'tcx>, TypeError<'tcx>> {
if self.infcx.next_trait_solver() && !alias.has_escaping_bound_vars() {
return Ok(self.infcx.next_ty_var_in_universe(
TypeVariableOrigin { kind: TypeVariableOriginKind::MiscVariable, span: self.span },
TypeVariableOrigin { param_def_id: None, span: self.span },
self.for_universe,
));
}
@ -375,10 +375,7 @@ impl<'tcx> Generalizer<'_, 'tcx> {
debug!("generalization failure in alias");
Ok(self.infcx.next_ty_var_in_universe(
TypeVariableOrigin {
kind: TypeVariableOriginKind::MiscVariable,
span: self.span,
},
TypeVariableOrigin { param_def_id: None, span: self.span },
self.for_universe,
))
}

View file

@ -18,7 +18,7 @@
//! [lattices]: https://en.wikipedia.org/wiki/Lattice_(order)
use super::combine::ObligationEmittingRelation;
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use crate::infer::type_variable::TypeVariableOrigin;
use crate::infer::{DefineOpaqueTypes, InferCtxt};
use crate::traits::ObligationCause;
@ -88,18 +88,14 @@ 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 {
kind: TypeVariableOriginKind::LatticeVariable,
span: this.cause().span,
});
let v = infcx
.next_ty_var(TypeVariableOrigin { param_def_id: None, span: this.cause().span });
this.relate_bound(v, b, a)?;
Ok(v)
}
(_, &ty::Infer(TyVar(..))) => {
let v = infcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::LatticeVariable,
span: this.cause().span,
});
let v = infcx
.next_ty_var(TypeVariableOrigin { param_def_id: None, span: this.cause().span });
this.relate_bound(v, a, b)?;
Ok(v)
}

View file

@ -2,7 +2,6 @@ use rustc_data_structures::undo_log::Rollback;
use rustc_hir::def_id::DefId;
use rustc_index::IndexVec;
use rustc_middle::ty::{self, Ty, TyVid};
use rustc_span::symbol::Symbol;
use rustc_span::Span;
use crate::infer::InferCtxtUndoLogs;
@ -37,30 +36,9 @@ pub struct TypeVariableTable<'a, 'tcx> {
#[derive(Copy, Clone, Debug)]
pub struct TypeVariableOrigin {
pub kind: TypeVariableOriginKind,
pub span: Span,
}
/// Reasons to create a type inference variable
#[derive(Copy, Clone, Debug)]
pub enum TypeVariableOriginKind {
MiscVariable,
NormalizeProjectionType,
TypeInference,
TypeParameterDefinition(Symbol, DefId),
/// One of the upvars or closure kind parameters in a `ClosureArgs`
/// (before it has been determined).
// FIXME(eddyb) distinguish upvar inference variables from the rest.
ClosureSynthetic,
AutoDeref,
AdjustmentType,
/// In type check, when we are type checking a function that
/// returns `-> dyn Foo`, we instantiate a type variable with the
/// return type for diagnostic purposes.
DynReturnFn,
LatticeVariable,
// `DefId` of the type parameter this was instantiated for, if any.
pub param_def_id: Option<DefId>,
}
#[derive(Clone)]