From 8acf26603d97da62452eb5fec5526d9a8e6b72d3 Mon Sep 17 00:00:00 2001 From: Musab Kilic Date: Mon, 6 Feb 2023 16:13:23 +0100 Subject: [PATCH 1/4] - still in progress refactoring ParamTy to a trait TypeParameter - currently problems with: comparison with implementing struct and trait, return type of pattern-match and functions have to return the same concrete type, and cannot have impl trait in closure that takes Self as type parameter --- .../src/diagnostics/conflict_errors.rs | 2 +- .../src/transform/check_consts/ops.rs | 2 +- .../rustc_hir_analysis/src/check/check.rs | 2 +- compiler/rustc_hir_analysis/src/check/mod.rs | 2 +- .../rustc_hir_analysis/src/check/wfcheck.rs | 7 +- .../src/collect/lifetimes.rs | 2 +- .../src/collect/predicates_of.rs | 6 +- .../src/constrained_generic_params.rs | 2 +- .../rustc_hir_analysis/src/impl_wf_check.rs | 2 +- .../rustc_hir_analysis/src/outlives/utils.rs | 2 +- .../src/variance/constraints.rs | 2 +- compiler/rustc_hir_typeck/src/expr.rs | 2 +- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 12 +- .../src/fn_ctxt/suggestions.rs | 4 +- compiler/rustc_hir_typeck/src/method/probe.rs | 4 +- .../rustc_hir_typeck/src/method/suggest.rs | 12 +- .../src/infer/error_reporting/mod.rs | 10 +- .../src/infer/outlives/components.rs | 3 +- .../src/infer/outlives/obligations.rs | 4 +- .../rustc_infer/src/infer/outlives/verify.rs | 49 +------- .../src/infer/region_constraints/mod.rs | 5 +- compiler/rustc_infer/src/traits/util.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 10 +- compiler/rustc_middle/src/ty/diagnostics.rs | 3 +- compiler/rustc_middle/src/ty/error.rs | 20 +-- compiler/rustc_middle/src/ty/generics.rs | 6 +- compiler/rustc_middle/src/ty/mod.rs | 2 +- compiler/rustc_middle/src/ty/print/pretty.rs | 6 + compiler/rustc_middle/src/ty/relate.rs | 1 + .../rustc_middle/src/ty/structural_impls.rs | 16 +-- compiler/rustc_middle/src/ty/sty.rs | 114 +++++++++++++----- compiler/rustc_middle/src/ty/subst.rs | 7 +- compiler/rustc_middle/src/ty/util.rs | 9 +- .../rustc_monomorphize/src/polymorphize.rs | 2 +- .../src/traits/error_reporting/mod.rs | 10 +- .../src/traits/error_reporting/suggestions.rs | 13 +- .../rustc_trait_selection/src/traits/mod.rs | 4 +- .../src/traits/select/confirmation.rs | 4 +- .../rustc_ty_utils/src/representability.rs | 2 +- compiler/rustc_type_ir/src/lib.rs | 1 + compiler/rustc_type_ir/src/sty.rs | 5 +- 41 files changed, 190 insertions(+), 183 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 5e0b7b6d06a2..9bc1f13febc9 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -792,7 +792,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { PredicateKind::Clause(ty::Clause::Trait(predicate)) => { match predicate.self_ty().kind() { ty::Param(param_ty) => Ok(( - generics.type_param(param_ty, tcx), + generics.type_param(*param_ty, tcx), predicate.trait_ref.print_only_trait_path().to_string(), )), ty::HKT(_param_ty, ..) => { diff --git a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs index 4b86c13db798..fdf69adf3100 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/ops.rs @@ -13,7 +13,7 @@ use rustc_middle::mir; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::subst::{GenericArgKind, SubstsRef}; use rustc_middle::ty::{ - suggest_constraining_type_param, Adt, Closure, DefIdTree, FnDef, FnPtr, Param, Ty, HKT, + suggest_constraining_type_param, Adt, Closure, DefIdTree, FnDef, FnPtr, Param, Ty, HKT, TypeParameter }; use rustc_middle::ty::{Binder, TraitRef}; use rustc_session::parse::feature_err; diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 32b59002e1f0..c9289599b855 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -21,7 +21,7 @@ use rustc_middle::middle::stability::EvalResult; use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES}; use rustc_middle::ty::subst::GenericArgKind; use rustc_middle::ty::util::{Discr, IntTypeExt}; -use rustc_middle::ty::{self, AdtDef, ParamEnv, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable}; +use rustc_middle::ty::{self, AdtDef, ParamEnv, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, TypeParameter}; use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVENTIONS}; use rustc_span::symbol::sym; use rustc_span::{self, Span}; diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs index 04f7d31d650e..bce73772fa48 100644 --- a/compiler/rustc_hir_analysis/src/check/mod.rs +++ b/compiler/rustc_hir_analysis/src/check/mod.rs @@ -80,7 +80,7 @@ use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit::Visitor; use rustc_index::bit_set::BitSet; use rustc_middle::ty::query::Providers; -use rustc_middle::ty::{self, Ty, TyCtxt}; +use rustc_middle::ty::{self, Ty, TyCtxt, TypeParameter}; use rustc_middle::ty::{InternalSubsts, SubstsRef}; use rustc_session::parse::feature_err; use rustc_span::source_map::DUMMY_SP; diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index fd8ab9f37f1b..5515ea62ccdc 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -13,10 +13,7 @@ use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt}; use rustc_middle::mir::ConstraintCategory; use rustc_middle::ty::query::Providers; use rustc_middle::ty::trait_def::TraitSpecializationKind; -use rustc_middle::ty::{ - self, AdtKind, DefIdTree, GenericParamDefKind, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, - TypeVisitable, TypeVisitor, -}; +use rustc_middle::ty::{self, AdtKind, DefIdTree, GenericParamDefKind, Ty, TyCtxt, TypeFoldable, TypeParameter, TypeSuperVisitable, TypeVisitable, TypeVisitor}; use rustc_middle::ty::{GenericArgKind, InternalSubsts}; use rustc_session::parse::feature_err; use rustc_span::symbol::{sym, Ident, Symbol}; @@ -590,7 +587,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>( // `Self` in the GAT. let ty_param = gat_generics.param_at(*ty_idx, tcx); let ty_param = tcx - .mk_ty(ty::Param(ty::ParamTy::Param { index: ty_param.index, name: ty_param.name })); + .mk_ty(ty::Param(ty::ParamTy::new(ty_param.index, ty_param.name))); // Same for the region. In our example, 'a corresponds // to the 'me parameter. let region_param = gat_generics.param_at(*region_a_idx, tcx); diff --git a/compiler/rustc_hir_analysis/src/collect/lifetimes.rs b/compiler/rustc_hir_analysis/src/collect/lifetimes.rs index bf322cab47d2..d4745503706a 100644 --- a/compiler/rustc_hir_analysis/src/collect/lifetimes.rs +++ b/compiler/rustc_hir_analysis/src/collect/lifetimes.rs @@ -17,7 +17,7 @@ use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirIdMap, LifetimeNa use rustc_middle::bug; use rustc_middle::hir::nested_filter; use rustc_middle::middle::resolve_lifetime::*; -use rustc_middle::ty::{self, DefIdTree, TyCtxt, TypeSuperVisitable, TypeVisitor}; +use rustc_middle::ty::{self, DefIdTree, TyCtxt, TypeSuperVisitable, TypeVisitor, TypeParameter}; use rustc_span::def_id::DefId; use rustc_span::symbol::{sym, Ident}; use rustc_span::Span; diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 1f9f4ffedcd6..b06d73a16e6f 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -9,7 +9,7 @@ use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit::{self, Visitor}; use rustc_middle::ty::subst::InternalSubsts; -use rustc_middle::ty::ToPredicate; +use rustc_middle::ty::{ToPredicate, TypeParameter}; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::symbol::{sym, Ident}; use rustc_span::{Span, DUMMY_SP}; @@ -158,7 +158,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP GenericParamKind::Lifetime { .. } => (), GenericParamKind::Type { .. } => { let name = param.name.ident().name; - let param_ty = ty::ParamTy::new_param(index, name).to_ty(tcx); + let param_ty = ty::ParamTy::new(index, name).to_ty(tcx); index += 1; let mut bounds = Bounds::default(); @@ -181,7 +181,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP GenericParamKind::HKT(..) => { // TODO(hoch) let name = param.name.ident().name; - let param_ty = ty::ParamTy::new_hkt(index, name).to_ty(tcx); + let param_ty = ty::ParamTy::new(index, name).to_ty(tcx); index += 1; let mut bounds = Bounds::default(); diff --git a/compiler/rustc_hir_analysis/src/constrained_generic_params.rs b/compiler/rustc_hir_analysis/src/constrained_generic_params.rs index 9cde32294c75..cbc9b6470e41 100644 --- a/compiler/rustc_hir_analysis/src/constrained_generic_params.rs +++ b/compiler/rustc_hir_analysis/src/constrained_generic_params.rs @@ -1,6 +1,6 @@ use rustc_data_structures::fx::FxHashSet; use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor}; -use rustc_middle::ty::{self, Ty, TyCtxt}; +use rustc_middle::ty::{self, Ty, TyCtxt, TypeParameter}; use rustc_span::source_map::Span; use std::ops::ControlFlow; diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check.rs b/compiler/rustc_hir_analysis/src/impl_wf_check.rs index 1891d7f9d77e..c30d151965c2 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check.rs @@ -16,7 +16,7 @@ use rustc_errors::struct_span_err; use rustc_hir::def::DefKind; use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::query::Providers; -use rustc_middle::ty::{self, TyCtxt, TypeVisitable}; +use rustc_middle::ty::{self, TyCtxt, TypeParameter, TypeVisitable}; use rustc_span::{Span, Symbol}; mod min_specialization; diff --git a/compiler/rustc_hir_analysis/src/outlives/utils.rs b/compiler/rustc_hir_analysis/src/outlives/utils.rs index d8ae4efeea93..0668e9106993 100644 --- a/compiler/rustc_hir_analysis/src/outlives/utils.rs +++ b/compiler/rustc_hir_analysis/src/outlives/utils.rs @@ -1,6 +1,6 @@ use rustc_infer::infer::outlives::components::{push_outlives_components, Component}; use rustc_middle::ty::subst::{GenericArg, GenericArgKind}; -use rustc_middle::ty::{self, Region, Ty, TyCtxt}; +use rustc_middle::ty::{self, Region, Ty, TyCtxt, TypeParameter}; use rustc_span::Span; use smallvec::smallvec; use std::collections::BTreeMap; diff --git a/compiler/rustc_hir_analysis/src/variance/constraints.rs b/compiler/rustc_hir_analysis/src/variance/constraints.rs index 94b171857426..a9d681a3f22f 100644 --- a/compiler/rustc_hir_analysis/src/variance/constraints.rs +++ b/compiler/rustc_hir_analysis/src/variance/constraints.rs @@ -7,7 +7,7 @@ use hir::def_id::{DefId, LocalDefId}; use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_middle::ty::subst::{GenericArgKind, SubstsRef}; -use rustc_middle::ty::{self, Ty, TyCtxt}; +use rustc_middle::ty::{self, Ty, TyCtxt, TypeParameter}; use super::terms::VarianceTerm::*; use super::terms::*; diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 011c769a25b0..db08fb547258 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -2533,7 +2533,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { fn point_at_param_definition(&self, err: &mut Diagnostic, param: ty::ParamTy) { let generics = self.tcx.generics_of(self.body_id.owner.to_def_id()); - let generic_param = generics.type_param(¶m, self.tcx); + let generic_param = generics.type_param(param, self.tcx); if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param.kind { return; } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index cd83c4c2708d..84945a383e87 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -26,7 +26,7 @@ use rustc_infer::infer::InferOk; use rustc_infer::infer::TypeTrace; use rustc_middle::ty::adjustment::AllowTwoPhase; use rustc_middle::ty::visit::TypeVisitable; -use rustc_middle::ty::{self, DefIdTree, FnSig, GenericArgKind, IsSuggestable, ParamTy, SubstsRef, Ty, TyKind, TypeSuperVisitable, TypeVisitor}; +use rustc_middle::ty::{self, DefIdTree, FnSig, GenericArgKind, IsSuggestable, SubstsRef, Ty, TyKind, TypeParameter, TypeSuperVisitable, TypeVisitor}; use rustc_session::Session; use rustc_span::symbol::{kw, Ident}; use rustc_span::{self, sym, Span, Symbol}; @@ -189,9 +189,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { for (input, argument) in sig.inputs().iter().zip(arguments) { match input.kind() { - ty::HKT(ParamTy::HKT { index, .. }, substs) => { + ty::HKT(p, substs) => { let substs: &SubstsRef<'_> = substs; - let hkt_generics: &ty::Generics = self.tcx.generics_of(generics.params[*index as usize].def_id); + let hkt_generics: &ty::Generics = self.tcx.generics_of(generics.params[(*p).index() as usize].def_id); let res_type = self.ty_kind_substitution(argument.kind(), substs[0].expect_ty().kind(), hkt_generics.params[0].name); //todo!("{:#?}, {:#?}, {:#?}, {:#?}", argument, hkt_generics, substs, res_type) @@ -1860,12 +1860,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Prefer generics that are local to the fn item, since these are likely // to be the cause of the unsatisfied predicate. let mut param_to_point_at = find_param_matching(&|param_ty| { - self.tcx.parent(generics.type_param(param_ty, self.tcx).def_id) == def_id + self.tcx.parent(generics.type_param(*param_ty, self.tcx).def_id) == def_id }); // Fall back to generic that isn't local to the fn item. This will come // from a trait or impl, for example. let mut fallback_param_to_point_at = find_param_matching(&|param_ty| { - self.tcx.parent(generics.type_param(param_ty, self.tcx).def_id) != def_id + self.tcx.parent(generics.type_param(*param_ty, self.tcx).def_id) != def_id && param_ty.name() != rustc_span::symbol::kw::SelfUpper }); // Finally, the `Self` parameter is possibly the reason that the predicate @@ -2200,7 +2200,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } ty::Param(ref param) => { let param = - self.tcx.generics_of(self.body_id.owner).type_param(param, self.tcx); + self.tcx.generics_of(self.body_id.owner).type_param(*param, self.tcx); if param.kind.is_synthetic() { // if it's `impl Fn() -> ..` then just fall down to the def-id based logic def_id = param.def_id; diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index ab845d9752c5..175c04a3fd19 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -16,7 +16,7 @@ use rustc_infer::traits::{self, StatementAsExpression}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::{ self, suggest_constraining_type_params, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty, - TypeVisitable, + TypeVisitable, TypeParameter }; use rustc_session::errors::ExprParenthesesNeeded; use rustc_span::source_map::Spanned; @@ -214,7 +214,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }) } ty::Param(ref param) => { - let def_id = self.tcx.generics_of(self.body_id.owner).type_param(param, self.tcx).def_id; + let def_id = self.tcx.generics_of(self.body_id.owner).type_param(*param, self.tcx).def_id; self.tcx.predicates_of(self.body_id.owner).predicates.iter().find_map(|(pred, _)| { if let ty::PredicateKind::Clause(ty::Clause::Projection(proj)) = pred.kind().skip_binder() && Some(proj.projection_ty.def_id) == self.tcx.lang_items().fn_once_output() diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index eb0dec8e56b9..647012c70976 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -16,7 +16,7 @@ use rustc_infer::infer::{self, InferOk, TyCtxtInferExt}; use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind}; use rustc_middle::middle::stability; use rustc_middle::ty::fast_reject::{simplify_type, TreatParams}; -use rustc_middle::ty::AssocItem; +use rustc_middle::ty::{AssocItem, TypeParameter}; use rustc_middle::ty::GenericParamDefKind; use rustc_middle::ty::ToPredicate; use rustc_middle::ty::{self, ParamEnvAnd, Ty, TyCtxt, TypeFoldable, TypeVisitable}; @@ -812,7 +812,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { }); } - fn assemble_inherent_candidates_from_param(&mut self, param_ty: ty::ParamTy) { + fn assemble_inherent_candidates_from_param(&mut self, param_ty: impl TypeParameter<'tcx>) { // FIXME: do we want to commit to this behavior for param bounds? debug!("assemble_inherent_candidates_from_param(param_ty={:?})", param_ty); diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 78a6620af56c..7a6bcb3f06e0 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -26,7 +26,7 @@ use rustc_middle::traits::util::supertraits; use rustc_middle::ty::fast_reject::DeepRejectCtxt; use rustc_middle::ty::fast_reject::{simplify_type, TreatParams}; use rustc_middle::ty::print::{with_crate_prefix, with_forced_trimmed_paths}; -use rustc_middle::ty::{self, DefIdTree, GenericArgKind, Ty, TyCtxt, TypeVisitable}; +use rustc_middle::ty::{self, DefIdTree, GenericArgKind, Ty, TyCtxt, TypeVisitable, TypeParameter}; use rustc_middle::ty::{IsSuggestable, ToPolyTraitRef}; use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::Symbol; @@ -2376,12 +2376,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { candidates.sort_by(|a, b| a.cmp(b).reverse()); candidates.dedup(); - let param_type = match rcvr_ty.kind() { - ty::Param(param) => Some(param), - ty::HKT(param, ..) => Some(param), + let param_type: Option> = match rcvr_ty.kind() { + ty::Param(param) => Some(Box::new(param)), + ty::HKT(param, ..) => Some(Box::new(param)), ty::Ref(_, ty, _) => match ty.kind() { - ty::Param(param) => Some(param), - ty::HKT(param, ..) => Some(param), + ty::Param(param) => Some(Box::new(param)), + ty::HKT(param, ..) => Some(Box::new(param)), _ => None, }, _ => None, diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 23fb82427488..a431e9b24928 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -68,10 +68,7 @@ use rustc_hir::lang_items::LangItem; use rustc_hir::Node; use rustc_middle::dep_graph::DepContext; use rustc_middle::ty::relate::{self, RelateResult, TypeRelation}; -use rustc_middle::ty::{ - self, error::TypeError, List, Region, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, - TypeVisitable, -}; +use rustc_middle::ty::{self, error::TypeError, List, Region, Ty, TyCtxt, TypeFoldable, TypeParameter, TypeSuperVisitable, TypeVisitable}; use rustc_span::{sym, symbol::kw, BytePos, DesugaringKind, Pos, Span}; use rustc_target::spec::abi; use std::ops::{ControlFlow, Deref}; @@ -2134,7 +2131,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { // Account for the case where `param` corresponds to `Self`, // which doesn't have the expected type argument. if !(generics.has_self && param.index() == 0) { - let type_param = generics.type_param(param, self.tcx); + let type_param = generics.type_param(*param, self.tcx); type_param.def_id.as_local().map(|def_id| { // Get the `hir::Param` to verify whether it already has any bounds. // We do this to avoid suggesting code that ends up as `T: 'a'b`, @@ -2155,7 +2152,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { } else { None } - } + }, + GenericKind::HKT(_param) => todo!("hoch"), _ => None, }; diff --git a/compiler/rustc_infer/src/infer/outlives/components.rs b/compiler/rustc_infer/src/infer/outlives/components.rs index 77e7532426c4..f6fa5821f314 100644 --- a/compiler/rustc_infer/src/infer/outlives/components.rs +++ b/compiler/rustc_infer/src/infer/outlives/components.rs @@ -12,8 +12,7 @@ use smallvec::{smallvec, SmallVec}; pub enum Component<'tcx> { Region(ty::Region<'tcx>), Param(ty::ParamTy), - // TODO muki change to HKTTy - HKT(ty::ParamTy), + HKT(ty::HKTTy), UnresolvedInferenceVariable(ty::InferTy), // Projections like `T::Foo` are tricky because a constraint like diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs index cfa668116037..445d8d81bc3c 100644 --- a/compiler/rustc_infer/src/infer/outlives/obligations.rs +++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs @@ -330,7 +330,7 @@ where &mut self, origin: infer::SubregionOrigin<'tcx>, region: ty::Region<'tcx>, - hkt_ty: ty::ParamTy, + hkt_ty: ty::HKTTy, ) { debug!( "hkt_ty_must_outlive(region={:?}, hkt_ty={:?}, origin={:?})", @@ -338,7 +338,7 @@ where ); let generic = GenericKind::HKT(hkt_ty.clone()); - let verify_bound = self.verify_bound.hkt_bound(hkt_ty); + let verify_bound = self.verify_bound.param_bound(hkt_ty); self.delegate.push_verify(origin, generic, region, verify_bound); } diff --git a/compiler/rustc_infer/src/infer/outlives/verify.rs b/compiler/rustc_infer/src/infer/outlives/verify.rs index a1d6d5629ea0..34f7d1b3eef8 100644 --- a/compiler/rustc_infer/src/infer/outlives/verify.rs +++ b/compiler/rustc_infer/src/infer/outlives/verify.rs @@ -4,7 +4,7 @@ use crate::infer::region_constraints::VerifyIfEq; use crate::infer::{GenericKind, VerifyBound}; use rustc_data_structures::sso::SsoHashSet; use rustc_hir::def_id::DefId; -use rustc_middle::ty::GenericArg; +use rustc_middle::ty::{GenericArg, TypeParameter}; use rustc_middle::ty::{self, OutlivesPredicate, SubstsRef, Ty, TyCtxt}; use smallvec::smallvec; @@ -38,7 +38,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> { } #[instrument(level = "debug", skip(self))] - pub fn param_bound(&self, param_ty: ty::ParamTy) -> VerifyBound<'tcx> { + pub fn param_bound(&self, param_ty: impl TypeParameter<'tcx>) -> VerifyBound<'tcx> { // Start with anything like `T: 'a` we can scrape from the // environment. If the environment contains something like // `for<'a> T: 'a`, then we know that `T` outlives everything. @@ -79,49 +79,6 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> { } } - #[instrument(level = "debug", skip(self))] - pub fn hkt_bound(&self, hkt_ty: ty::ParamTy) -> VerifyBound<'tcx> { - // TODO muki ParamTy change - // Start with anything like `T: 'a` we can scrape from the - // environment. If the environment contains something like - // `for<'a> T: 'a`, then we know that `T` outlives everything. - let declared_bounds_from_env = self.declared_generic_bounds_from_env(hkt_ty.clone()); - debug!(?declared_bounds_from_env); - let mut hkt_bounds = vec![]; - for declared_bound in declared_bounds_from_env { - let bound_region = declared_bound.map_bound(|outlives| outlives.1); - if let Some(region) = bound_region.no_bound_vars() { - // This is `T: 'a` for some free region `'a`. - hkt_bounds.push(VerifyBound::OutlivedBy(region)); - } else { - // This is `for<'a> T: 'a`. This means that `T` outlives everything! All done here. - debug!("found that {hkt_ty:?} outlives any lifetime, returning empty vector"); - return VerifyBound::AllBounds(vec![]); - } - } - - // Add in the default bound of fn body that applies to all in - // scope type parameters: - if let Some(r) = self.implicit_region_bound { - debug!("adding implicit region bound of {r:?}"); - hkt_bounds.push(VerifyBound::OutlivedBy(r)); - } - - if hkt_bounds.is_empty() { - // We know that all types `T` outlive `'empty`, so if we - // can find no other bound, then check that the region - // being tested is `'empty`. - VerifyBound::IsEmpty - } else if hkt_bounds.len() == 1 { - // Micro-opt: no need to store the vector if it's just len 1 - hkt_bounds.pop().unwrap() - } else { - // If we can find any other bound `R` such that `T: R`, then - // we don't need to check for `'empty`, because `R: 'empty`. - VerifyBound::AnyBound(hkt_bounds) - } - } - /// Given a projection like `T::Item`, searches the environment /// for where-clauses like `T::Item: 'a`. Returns the set of /// regions `'a` that it finds. @@ -253,7 +210,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> { /// bounds, but all the bounds it returns can be relied upon. fn declared_generic_bounds_from_env( &self, - param_ty: ty::ParamTy, + param_ty: impl TypeParameter<'tcx>, ) -> Vec, ty::Region<'tcx>>>> { // TODO muki make copy with HKTTy let generic_ty = param_ty.to_ty(self.tcx); diff --git a/compiler/rustc_infer/src/infer/region_constraints/mod.rs b/compiler/rustc_infer/src/infer/region_constraints/mod.rs index f4c058f08c23..8bac5c056812 100644 --- a/compiler/rustc_infer/src/infer/region_constraints/mod.rs +++ b/compiler/rustc_infer/src/infer/region_constraints/mod.rs @@ -16,7 +16,7 @@ use rustc_hir::def_id::DefId; use rustc_index::vec::IndexVec; use rustc_middle::infer::unify_key::{RegionVidKey, UnifiedRegion}; use rustc_middle::ty::subst::SubstsRef; -use rustc_middle::ty::ReStatic; +use rustc_middle::ty::{ReStatic, TypeParameter}; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{ReLateBound, ReVar}; use rustc_middle::ty::{Region, RegionVid}; @@ -169,8 +169,7 @@ pub struct Verify<'tcx> { #[derive(Clone, PartialEq, Eq, Hash, TypeFoldable, TypeVisitable)] pub enum GenericKind<'tcx> { Param(ty::ParamTy), - // TODO muki replace ParamTy - HKT(ty::ParamTy), + HKT(ty::HKTTy), Projection(ty::AliasTy<'tcx>), Opaque(DefId, SubstsRef<'tcx>), } diff --git a/compiler/rustc_infer/src/traits/util.rs b/compiler/rustc_infer/src/traits/util.rs index da7be1b7a359..6a6053336b1c 100644 --- a/compiler/rustc_infer/src/traits/util.rs +++ b/compiler/rustc_infer/src/traits/util.rs @@ -3,7 +3,7 @@ use smallvec::smallvec; use crate::infer::outlives::components::{push_outlives_components, Component}; use crate::traits::{Obligation, ObligationCause, PredicateObligation}; use rustc_data_structures::fx::{FxHashSet, FxIndexSet}; -use rustc_middle::ty::{self, ToPredicate, TyCtxt}; +use rustc_middle::ty::{self, ToPredicate, TyCtxt, TypeParameter}; use rustc_span::symbol::Ident; use rustc_span::Span; diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 9f52a1f68ec1..090e86df5a82 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -75,6 +75,7 @@ use std::iter; use std::mem; use std::ops::{Bound, Deref}; use std::sync::Arc; +use rustc_middle::ty::{HKTTy, TypeParameter}; pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync { /// Creates a new `OnDiskCache` instance from the serialized data in `data`. @@ -109,6 +110,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { type ListTy = &'tcx List>; type AliasTy = ty::AliasTy<'tcx>; type ParamTy = ParamTy; + type HKTTy = HKTTy; type BoundTy = ty::BoundTy; type PlaceholderType = ty::PlaceholderType; type InferTy = InferTy; @@ -304,8 +306,8 @@ impl<'tcx> CommonTypes<'tcx> { f32: mk(Float(ty::FloatTy::F32)), f64: mk(Float(ty::FloatTy::F64)), str_: mk(Str), - self_param: mk(ty::Param(ty::ParamTy::Param { index: 0, name: kw::SelfUpper })), - + self_param: mk(ty::Param(ty::ParamTy::new(0,kw::SelfUpper ))), + //TODO: hoch, make one for HKT? trait_object_dummy_self: mk(Infer(ty::FreshTy(0))), } } @@ -2000,12 +2002,12 @@ impl<'tcx> TyCtxt<'tcx> { #[inline] pub fn mk_ty_param(self, index: u32, name: Symbol) -> Ty<'tcx> { - self.mk_ty(Param(ParamTy::Param { index, name })) + self.mk_ty(Param(ParamTy::new(index, name))) } #[inline] pub fn mk_hkt_param(self, index: u32, name: Symbol, subst: SubstsRef<'tcx>) -> Ty<'tcx> { - self.mk_ty(HKT(ParamTy::HKT { index, name }, subst)) + self.mk_ty(HKT(HKTTy::new(index, name), subst)) } pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> GenericArg<'tcx> { diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index 450be263125d..ec3b788da1a7 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -14,6 +14,7 @@ use rustc_hir::def_id::DefId; use rustc_hir::WherePredicate; use rustc_span::Span; use rustc_type_ir::sty::TyKind::*; +use rustc_middle::ty::TypeParameter; impl<'tcx> IntoDiagnosticArg for Ty<'tcx> { fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { @@ -481,7 +482,7 @@ impl<'tcx> TypeVisitor<'tcx> for IsSuggestableVisitor<'tcx> { // sufficient info to determine if it is synthetic, and we don't // always have a convenient way of getting `ty::Generics` at the call // sites we invoke `IsSuggestable::is_suggestable`. - if param.name().as_str().starts_with("impl ") { + if (*param).name().as_str().starts_with("impl ") { return ControlFlow::Break(()); } } diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs index 09b3ee7b1128..62971c72dcdc 100644 --- a/compiler/rustc_middle/src/ty/error.rs +++ b/compiler/rustc_middle/src/ty/error.rs @@ -339,8 +339,8 @@ impl<'tcx> Ty<'tcx> { ty::Infer(ty::FreshIntTy(_)) => "fresh integral type".into(), ty::Infer(ty::FreshFloatTy(_)) => "fresh floating-point type".into(), ty::Alias(ty::Projection, _) => "associated type".into(), - ty::Param(ref p) => format!("type parameter `{}`", p).into(), - ty::HKT(ref p, _) => format!("hkt parameter `{}`", p).into(), + ty::Param(ref p) => format!("type parameter `{}`", *p).into(), + ty::HKT(ref p, _) => format!("hkt parameter `{}`", *p).into(), ty::Alias(ty::Opaque, ..) => "opaque type".into(), ty::Error(_) => "type error".into(), } @@ -428,11 +428,11 @@ impl<'tcx> TyCtxt<'tcx> { } (ty::Param(expected), ty::Param(found)) => { let generics = self.generics_of(body_owner_def_id); - let e_span = self.def_span(generics.type_param(expected, self).def_id); + let e_span = self.def_span(generics.type_param(*expected, self).def_id); if !sp.contains(e_span) { diag.span_label(e_span, "expected type parameter"); } - let f_span = self.def_span(generics.type_param(found, self).def_id); + let f_span = self.def_span(generics.type_param(*found, self).def_id); if !sp.contains(f_span) { diag.span_label(f_span, "found type parameter"); } @@ -454,14 +454,14 @@ impl<'tcx> TyCtxt<'tcx> { if self.def_kind(proj.def_id) != DefKind::ImplTraitPlaceholder => { let generics = self.generics_of(body_owner_def_id); - let p_span = self.def_span(generics.type_param(p, self).def_id); + let p_span = self.def_span(generics.type_param(*p, self).def_id); if !sp.contains(p_span) { diag.span_label(p_span, "this type parameter"); } let hir = self.hir(); let mut note = true; if let Some(generics) = generics - .type_param(p, self) + .type_param(*p, self) .def_id .as_local() .map(|id| hir.local_def_id_to_hir_id(id)) @@ -504,7 +504,7 @@ impl<'tcx> TyCtxt<'tcx> { (ty::Param(p), ty::Dynamic(..) | ty::Alias(ty::Opaque, ..)) | (ty::Dynamic(..) | ty::Alias(ty::Opaque, ..), ty::Param(p)) => { let generics = self.generics_of(body_owner_def_id); - let p_span = self.def_span(generics.type_param(p, self).def_id); + let p_span = self.def_span(generics.type_param(*p, self).def_id); if !sp.contains(p_span) { diag.span_label(p_span, "this type parameter"); } @@ -544,7 +544,7 @@ impl Trait for X { } (ty::Param(p), ty::Closure(..) | ty::Generator(..)) => { let generics = self.generics_of(body_owner_def_id); - let p_span = self.def_span(generics.type_param(p, self).def_id); + let p_span = self.def_span(generics.type_param(*p, self).def_id); if !sp.contains(p_span) { diag.span_label(p_span, "this type parameter"); } @@ -559,7 +559,7 @@ impl Trait for X { } (ty::Param(p), _) | (_, ty::Param(p)) => { let generics = self.generics_of(body_owner_def_id); - let p_span = self.def_span(generics.type_param(p, self).def_id); + let p_span = self.def_span(generics.type_param(*p, self).def_id); if !sp.contains(p_span) { diag.span_label(p_span, "this type parameter"); } @@ -646,7 +646,7 @@ impl Trait for X { // This will also work for `impl Trait`. let def_id = if let ty::Param(param_ty) = proj_ty.self_ty().kind() { let generics = self.generics_of(body_owner_def_id); - generics.type_param(param_ty, self).def_id + generics.type_param(*param_ty, self).def_id } else if let ty::HKT(..) = proj_ty.self_ty().kind() { //let generics = self.generics_of(body_owner_def_id); //generics.type_param(param_ty, self).def_id diff --git a/compiler/rustc_middle/src/ty/generics.rs b/compiler/rustc_middle/src/ty/generics.rs index ccd50c5cfa92..da6806d85c8e 100644 --- a/compiler/rustc_middle/src/ty/generics.rs +++ b/compiler/rustc_middle/src/ty/generics.rs @@ -3,10 +3,11 @@ use crate::ty::{EarlyBinder, SubstsRef}; use rustc_ast as ast; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::DefId; +use rustc_middle::ty::sty::TypeParameter; use rustc_span::symbol::{kw, Symbol}; use rustc_span::Span; -use super::{EarlyBoundRegion, InstantiatedPredicates, ParamConst, ParamTy, Predicate, TyCtxt}; +use super::{EarlyBoundRegion, InstantiatedPredicates, ParamConst, Predicate, TyCtxt}; #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)] pub enum GenericParamDefKind { @@ -268,10 +269,11 @@ impl<'tcx> Generics { } /// Returns the `GenericParamDef` associated with this `ParamTy`. - pub fn type_param(&'tcx self, param: &ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef { + pub fn type_param(&'tcx self, param: impl TypeParameter<'tcx>, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef { let param = self.param_at(param.index() as usize, tcx); match param.kind { GenericParamDefKind::Type { .. } => param, + GenericParamDefKind::HKT { .. } => todo!("hoch"), _ => bug!("expected type parameter, but found another generic parameter"), } } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index f01d74539a12..04b261d25335 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -96,7 +96,7 @@ pub use self::sty::{ BoundVariableKind, CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstVid, EarlyBoundRegion, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FnSig, FreeRegion, GenSig, GeneratorSubsts, GeneratorSubstsParts, InlineConstSubsts, - InlineConstSubstsParts, ParamConst, ParamTy, PolyExistentialPredicate, + InlineConstSubstsParts, ParamConst, TypeParameter, ParamTy, HKTTy, PolyExistentialPredicate, PolyExistentialProjection, PolyExistentialTraitRef, PolyFnSig, PolyGenSig, PolyTraitRef, Region, RegionKind, RegionVid, TraitRef, TyKind, TypeAndMut, UpvarSubsts, VarianceDiagInfo, }; diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 96dda38457cd..f994aeca5ce4 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -28,6 +28,8 @@ use std::fmt::{self, Write as _}; use std::iter; use std::ops::{ControlFlow, Deref, DerefMut}; +use rustc_middle::ty::TypeParameter; + // `pretty` is a separate module only for organization. use super::*; @@ -2721,6 +2723,10 @@ define_print_and_forward_display! { p!(write("{}", self.name())) } + ty::HKTTy { + p!(write("{}", self.name())) + } + ty::ParamConst { p!(write("{}", self.name)) } diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index ac14becf21e2..3e4f12a94550 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -11,6 +11,7 @@ use rustc_hir as ast; use rustc_hir::def_id::DefId; use rustc_span::DUMMY_SP; use rustc_target::spec::abi; +use rustc_middle::ty::TypeParameter; use std::iter; pub type RelateResult<'tcx, T> = Result>; diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 31afe73a5ced..344a2c8748e2 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -7,7 +7,7 @@ use crate::mir::{Field, ProjectionKind}; use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeSuperFoldable}; use crate::ty::print::{with_no_trimmed_paths, FmtPrinter, Printer}; use crate::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor}; -use crate::ty::{self, InferConst, Lift, ParamTy, Term, TermKind, Ty, TyCtxt}; +use crate::ty::{self, InferConst, Lift, Term, TermKind, Ty, TyCtxt}; use rustc_data_structures::functor::IdFunctor; use rustc_hir::def::Namespace; use rustc_index::vec::{Idx, IndexVec}; @@ -112,19 +112,6 @@ impl<'tcx> fmt::Debug for Ty<'tcx> { } } -impl fmt::Debug for ty::ParamTy { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - ParamTy::Param { .. } => { - write!(f, "{}/#{}", self.name(), self.index()) - } - ParamTy::HKT { .. } => { - write!(f, "{}/{}", self.name(), self.index()) - } - } - } -} - impl fmt::Debug for ty::ParamConst { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}/#{}", self.name, self.index) @@ -242,6 +229,7 @@ TrivialTypeTraversalAndLiftImpls! { crate::ty::IntVarValue, crate::ty::ParamConst, crate::ty::ParamTy, + crate::ty::HKTTy, crate::ty::adjustment::PointerCast, crate::ty::RegionVid, crate::ty::UniverseIndex, diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 56b505a47688..6344ad872152 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -24,6 +24,8 @@ use rustc_target::spec::abi; use std::borrow::Cow; use std::cmp::Ordering; use std::fmt; +use std::fmt::{Debug, Formatter}; +use std::hash::Hash; use std::marker::PhantomData; use std::ops::{ControlFlow, Deref, Range}; use ty::util::IntTypeExt; @@ -1289,6 +1291,7 @@ impl<'tcx> PolyFnSig<'tcx> { pub type CanonicalPolyFnSig<'tcx> = Canonical<'tcx, Binder<'tcx, FnSig<'tcx>>>; +/* #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)] #[derive(HashStable)] pub enum ParamTy { @@ -1301,65 +1304,112 @@ pub enum ParamTy { name: Symbol, } } +*/ -impl<'tcx> ParamTy { - pub fn new_param(index: u32, name: Symbol) -> ParamTy { - ParamTy::Param { index, name } +pub trait TypeParameter<'tcx>: Clone + Copy + PartialEq + Eq + PartialOrd + Ord + Hash + Debug { + fn new(index: u32, name: Symbol) -> Self; + fn for_def(def: &ty::GenericParamDef) -> Self; + fn to_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>; + fn index(&self) -> u32; + fn name(&self) -> Symbol; + fn span_from_generics(&self, tcx: TyCtxt<'tcx>, item_with_generics: DefId) -> Span { + let generics = tcx.generics_of(item_with_generics); + let type_param = generics.type_param(*self, tcx); + tcx.def_span(type_param.def_id) } +} + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)] +#[derive(HashStable)] +pub struct ParamTy { + index: u32, + name: Symbol, +} - pub fn new_hkt(index: u32, name: Symbol) -> ParamTy { - ParamTy::HKT { index, name } +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)] +#[derive(HashStable)] +pub struct HKTTy { + index: u32, + name: Symbol, +} + +impl Debug for ParamTy { + fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { + todo!("hoch"); } +} - pub fn for_def(def: &ty::GenericParamDef) -> ParamTy { +impl Debug for HKTTy { + fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { + todo!("hoch"); + } +} + +impl<'tcx> TypeParameter<'tcx> for ParamTy { + fn new(index: u32, name: Symbol) -> ParamTy { + Self { index, name } + } + + fn for_def(def: &ty::GenericParamDef) -> ParamTy { match def.kind { GenericParamDefKind::Lifetime | GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => { - ParamTy::new_param(def.index, def.name) + ParamTy::new(def.index, def.name) } GenericParamDefKind::HKT => { - ParamTy::new_hkt(def.index, def.name) + bug!("Cannot convert Lifetime, Type, or Const to a HKT"); } } } #[inline] - pub fn to_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { - match self { - ParamTy::Param { index, name } => tcx.mk_ty_param(*index, *name), - ParamTy::HKT { index, name} => tcx.mk_hkt_param(*index, *name, tcx.intern_substs(&[])) - } + fn to_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { + tcx.mk_ty_param(self.index, self.name) } #[inline] - pub fn index(&self) -> u32 { - match *self { - ParamTy::Param { index, .. } => { - index - } - ParamTy::HKT { index, .. } => { - index - } - } + fn index(&self) -> u32 { + self.index } #[inline] - pub fn name(&self) -> Symbol { - match *self { - ParamTy::Param { name, .. } => { - name + fn name(&self) -> Symbol { + self.name + } +} + +impl<'tcx> TypeParameter<'tcx> for HKTTy { + fn new(index: u32, name: Symbol) -> HKTTy { + Self { index, name } + } + + fn for_def(def: &ty::GenericParamDef) -> HKTTy { + match def.kind { + GenericParamDefKind::Lifetime | + GenericParamDefKind::Type { .. } | + GenericParamDefKind::Const { .. } => { + bug!("Cannot convert HKT to Lifetime, Type, or Const"); } - ParamTy::HKT { name, .. } => { - name + GenericParamDefKind::HKT => { + HKTTy::new(def.index, def.name) } } } - pub fn span_from_generics(&self, tcx: TyCtxt<'tcx>, item_with_generics: DefId) -> Span { - let generics = tcx.generics_of(item_with_generics); - let type_param = generics.type_param(self, tcx); - tcx.def_span(type_param.def_id) + #[inline] + fn to_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { + tcx.mk_ty_param(self.index, self.name) + } + + #[inline] + fn index(&self) -> u32 { + self.index + } + + #[inline] + fn name(&self) -> Symbol { + self.name } } diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index 2e873b8318c3..bcec84476002 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -21,6 +21,7 @@ use std::mem; use std::num::NonZeroUsize; use std::ops::{ControlFlow, Deref}; use std::slice; +use rustc_middle::ty::TypeParameter; use rustc_span::Symbol; /// An entity in the Rust type system, which can be one of @@ -808,7 +809,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> { } impl<'a, 'tcx> SubstFolder<'a, 'tcx> { - fn ty_for_param(&self, p: ty::ParamTy, source_ty: Ty<'tcx>) -> Ty<'tcx> { + fn ty_for_param(&self, p: impl TypeParameter<'tcx>, source_ty: Ty<'tcx>) -> Ty<'tcx> { // Look up the type in the substitutions. It really should be in there. let opt_ty = self.substs.get(p.index() as usize).map(|k| k.unpack()); let ty = match opt_ty { @@ -822,7 +823,7 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> { #[cold] #[inline(never)] - fn type_param_expected(&self, p: ty::ParamTy, ty: Ty<'tcx>, kind: GenericArgKind<'tcx>) -> ! { + fn type_param_expected(&self, p: impl TypeParameter<'tcx>, ty: Ty<'tcx>, kind: GenericArgKind<'tcx>) -> ! { bug!( "expected type for `{:?}` ({:?}/{}) but found {:?} when substituting, substs={:?}", p, @@ -835,7 +836,7 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> { #[cold] #[inline(never)] - fn type_param_out_of_range(&self, p: ty::ParamTy, ty: Ty<'tcx>) -> ! { + fn type_param_out_of_range(&self, p: impl TypeParameter<'tcx>, ty: Ty<'tcx>) -> ! { bug!( "type parameter `{:?}` ({:?}/{}) out of range when substituting, substs={:?}", p, diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index a36a2b4d7b2b..bba3689444ce 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -22,6 +22,7 @@ use rustc_macros::HashStable; use rustc_span::{sym, DUMMY_SP}; use rustc_target::abi::{Integer, IntegerType, Size, TargetDataLayout}; use rustc_target::spec::abi::Abi; +use rustc_middle::ty::TypeParameter; use smallvec::SmallVec; use std::{fmt, iter}; @@ -437,8 +438,8 @@ impl<'tcx> TyCtxt<'tcx> { _ => false, }, GenericArgKind::Type(ty) => match ty.kind() { - ty::Param(ref pt) => !impl_generics.type_param(pt, self).pure_wrt_drop, - ty::HKT(ref pt, ..) => !impl_generics.type_param(pt, self).pure_wrt_drop, + ty::Param(ref pt) => !impl_generics.type_param(*pt, self).pure_wrt_drop, + ty::HKT(ref pt, ..) => !impl_generics.type_param(*pt, self).pure_wrt_drop, // Error: not a type param _ => false, }, @@ -478,12 +479,12 @@ impl<'tcx> TyCtxt<'tcx> { } GenericArgKind::Type(t) => match t.kind() { ty::Param(p) => { - if !seen.insert(p.index()) { + if !seen.insert((*p).index()) { return Err(NotUniqueParam::DuplicateParam(t.into())); } } ty::HKT(p, ..) => { - if !seen.insert(p.index()) { + if !seen.insert((*p).index()) { return Err(NotUniqueParam::DuplicateParam(t.into())); } } diff --git a/compiler/rustc_monomorphize/src/polymorphize.rs b/compiler/rustc_monomorphize/src/polymorphize.rs index f5e0cac6650c..a244a43e3554 100644 --- a/compiler/rustc_monomorphize/src/polymorphize.rs +++ b/compiler/rustc_monomorphize/src/polymorphize.rs @@ -17,7 +17,7 @@ use rustc_middle::ty::{ query::Providers, subst::SubstsRef, visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor}, - Const, Ty, TyCtxt, + Const, Ty, TyCtxt, TypeParameter }; use rustc_span::symbol::sym; use std::ops::ControlFlow; diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 3e31d86388ec..163afaf9cf3c 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -38,7 +38,7 @@ use rustc_middle::ty::fold::{TypeFolder, TypeSuperFoldable}; use rustc_middle::ty::print::{with_forced_trimmed_paths, FmtPrinter, Print}; use rustc_middle::ty::{ self, SubtypePredicate, ToPolyTraitRef, ToPredicate, TraitRef, Ty, TyCtxt, TypeFoldable, - TypeVisitable, + TypeVisitable, TypeParameter }; use rustc_session::config::TraitSolver; use rustc_session::Limit; @@ -2584,20 +2584,20 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { } fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { - if let ty::Param(ty::ParamTy::Param { name, .. }) = *ty.kind() { + if let ty::Param(p) = *ty.kind() { let infcx = self.infcx; *self.var_map.entry(ty).or_insert_with(|| { infcx.next_ty_var(TypeVariableOrigin { - kind: TypeVariableOriginKind::TypeParameterDefinition(name, None), + kind: TypeVariableOriginKind::TypeParameterDefinition(p.name(), None), span: DUMMY_SP, }) }) - } else if let ty::HKT(ty::ParamTy::HKT { name, .. }, ..) = *ty.kind() { + } else if let ty::HKT(p, ..) = *ty.kind() { // TODO(hoch) let infcx = self.infcx; *self.var_map.entry(ty).or_insert_with(|| { infcx.next_ty_var(TypeVariableOrigin { - kind: TypeVariableOriginKind::TypeParameterDefinition(name, None), + kind: TypeVariableOriginKind::TypeParameterDefinition(p.name(), None), span: DUMMY_SP, }) }) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index fe066eae518f..21ed1f2d7075 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -29,7 +29,7 @@ use rustc_infer::infer::{InferOk, LateBoundRegionConversionTime}; use rustc_middle::hir::map; use rustc_middle::ty::error::TypeError::{self, Sorts}; use rustc_middle::ty::relate::TypeRelation; -use rustc_middle::ty::{self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, DefIdTree, GeneratorDiagnosticData, GeneratorInteriorTypeCause, Infer, InferTy, InternalSubsts, IsSuggestable, ToPredicate, Ty, TyCtxt, TypeAndMut, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable, TypeckResults, GenericParamDefKind}; +use rustc_middle::ty::{self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, DefIdTree, GeneratorDiagnosticData, GeneratorInteriorTypeCause, Infer, InferTy, InternalSubsts, IsSuggestable, ToPredicate, Ty, TyCtxt, TypeAndMut, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable, TypeckResults, GenericParamDefKind, TypeParameter}; use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::{BytePos, DesugaringKind, ExpnKind, Span, DUMMY_SP}; use rustc_target::spec::abi; @@ -405,14 +405,15 @@ fn suggest_restriction<'tcx>( fn_sig.zip(projection).and_then(|(sig, p)| match p.self_ty().kind() { // Shenanigans to get the `Trait` from the `impl Trait`. ty::Param(param) => { - let param_def = generics.type_param(param, tcx); + let param_def = generics.type_param(*param, tcx); if param_def.kind.is_synthetic() { let bound_str = param_def.name.as_str().strip_prefix("impl ")?.trim_start().to_string(); return Some((param_def, bound_str, sig)); } None - } + }, + ty::HKT(_param, ..) => todo!("hoch"), _ => None, }) { @@ -420,7 +421,7 @@ fn suggest_restriction<'tcx>( let trait_pred = trait_pred.fold_with(&mut ReplaceImplTraitFolder { tcx, param, - replace_ty: ty::ParamTy::new_param(generics.count() as u32, Symbol::intern(&type_param_name)) + replace_ty: ty::ParamTy::new(generics.count() as u32, Symbol::intern(&type_param_name)) .to_ty(tcx), }); // TODO: new_param or new_hkt @@ -3766,8 +3767,8 @@ struct ReplaceImplTraitFolder<'tcx> { impl<'tcx> TypeFolder<'tcx> for ReplaceImplTraitFolder<'tcx> { fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { - if let ty::Param(ty::ParamTy::Param { index, .. }) = t.kind() { - if self.param.index == *index { + if let ty::Param(p) = t.kind() { + if self.param.index == (*p).index() { return self.replace_ty; } } diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 2c832e796a08..cbde6547f9b7 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -495,13 +495,13 @@ fn is_impossible_method(tcx: TyCtxt<'_>, (impl_def_id, trait_item_def_id): (DefI fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow { // If this is a parameter from the trait item's own generics, then bail if let ty::Param(param) = t.kind() - && let param_def_id = self.generics.type_param(param, self.tcx).def_id + && let param_def_id = self.generics.type_param(*param, self.tcx).def_id && self.tcx.parent(param_def_id) == self.trait_item_def_id { return ControlFlow::BREAK; } if let ty::HKT(param, ..) = t.kind() - && let param_def_id = self.generics.type_param(param, self.tcx).def_id + && let param_def_id = self.generics.type_param(*param, self.tcx).def_id && self.tcx.parent(param_def_id) == self.trait_item_def_id { return ControlFlow::BREAK; diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 7dc45ea3e28b..c98ad69b7c8e 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -13,7 +13,7 @@ use rustc_infer::infer::InferOk; use rustc_infer::infer::LateBoundRegionConversionTime::HigherRankedType; use rustc_middle::ty::{ self, Binder, GenericArg, GenericArgKind, GenericParamDefKind, InternalSubsts, SubstsRef, - ToPolyTraitRef, ToPredicate, TraitRef, Ty, TyCtxt, + ToPolyTraitRef, ToPredicate, TraitRef, Ty, TyCtxt, TypeParameter }; use rustc_session::config::TraitSolver; use rustc_span::def_id::DefId; @@ -1069,7 +1069,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { (&ty::Adt(def, substs_a), &ty::Adt(_, substs_b)) => { let maybe_unsizing_param_idx = |arg: GenericArg<'tcx>| match arg.unpack() { GenericArgKind::Type(ty) => match ty.kind() { - ty::Param(p) => Some(p.index()), + ty::Param(p) => Some((*p).index()), ty::HKT(..) => todo!("hoch"), _ => None, }, diff --git a/compiler/rustc_ty_utils/src/representability.rs b/compiler/rustc_ty_utils/src/representability.rs index caf2745402ea..4cd1b74165ad 100644 --- a/compiler/rustc_ty_utils/src/representability.rs +++ b/compiler/rustc_ty_utils/src/representability.rs @@ -3,7 +3,7 @@ use rustc_hir::def::DefKind; use rustc_index::bit_set::BitSet; use rustc_middle::ty::query::Providers; -use rustc_middle::ty::{self, Representability, Ty, TyCtxt}; +use rustc_middle::ty::{self, Representability, Ty, TyCtxt, TypeParameter}; use rustc_span::def_id::{DefId, LocalDefId}; pub fn provide(providers: &mut Providers) { diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 954469484bcc..8fcc6c04c4da 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -45,6 +45,7 @@ pub trait Interner { type ListTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; type AliasTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; type ParamTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type HKTTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; type BoundTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; type PlaceholderType: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; type InferTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; diff --git a/compiler/rustc_type_ir/src/sty.rs b/compiler/rustc_type_ir/src/sty.rs index 779223c8f8da..2f1144443655 100644 --- a/compiler/rustc_type_ir/src/sty.rs +++ b/compiler/rustc_type_ir/src/sty.rs @@ -178,7 +178,7 @@ pub enum TyKind { /// expected to be provided. /// Furthermore it contains a substsref, which contains the corresponding /// values that are substituted with the parameter names. - HKT(I::ParamTy, I::SubstsRef), + HKT(I::HKTTy, I::SubstsRef), /// Bound type variable, used to represent the `'a` in `for<'a> fn(&'a ())`. /// @@ -518,6 +518,7 @@ where I::ListTy: Encodable, I::AliasTy: Encodable, I::ParamTy: Encodable, + I::HKTTy: Encodable, I::BoundTy: Encodable, I::PlaceholderType: Encodable, I::InferTy: Encodable, @@ -640,6 +641,7 @@ where I::ListTy: Decodable, I::AliasTy: Decodable, I::ParamTy: Decodable, + I::HKTTy: Decodable, I::AliasTy: Decodable, I::BoundTy: Decodable, I::PlaceholderType: Decodable, @@ -709,6 +711,7 @@ where I::AliasTy: HashStable, I::BoundTy: HashStable, I::ParamTy: HashStable, + I::HKTTy: HashStable, I::PlaceholderType: HashStable, I::InferTy: HashStable, I::ErrorGuaranteed: HashStable, From bfc0a87f7d49eec3d61fe9c522e294ac5aa8f01b Mon Sep 17 00:00:00 2001 From: Musab Kilic Date: Tue, 7 Feb 2023 11:33:23 +0100 Subject: [PATCH 2/4] - still in progress refactoring ParamTy to a trait TypeParameter - currently problems with the compiler tests failing due to: asserts, not implemented, and something else --- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 23 +- compiler/rustc_hir_typeck/src/method/probe.rs | 21 +- .../rustc_hir_typeck/src/method/suggest.rs | 203 ++++++++++-------- compiler/rustc_middle/src/ty/mod.rs | 2 +- compiler/rustc_middle/src/ty/sty.rs | 37 +++- 5 files changed, 178 insertions(+), 108 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 84945a383e87..1051479f0eea 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -26,7 +26,7 @@ use rustc_infer::infer::InferOk; use rustc_infer::infer::TypeTrace; use rustc_middle::ty::adjustment::AllowTwoPhase; use rustc_middle::ty::visit::TypeVisitable; -use rustc_middle::ty::{self, DefIdTree, FnSig, GenericArgKind, IsSuggestable, SubstsRef, Ty, TyKind, TypeParameter, TypeSuperVisitable, TypeVisitor}; +use rustc_middle::ty::{self, DefIdTree, FnSig, GenericArgKind, IsSuggestable, SubstsRef, Ty, TyKind, TypeParameter, TypeParamResult, TypeSuperVisitable, TypeVisitor}; use rustc_session::Session; use rustc_span::symbol::{kw, Ident}; use rustc_span::{self, sym, Span, Symbol}; @@ -1835,18 +1835,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { _ => ty::List::empty(), }; - let find_param_matching = |matches: &dyn Fn(&ty::ParamTy) -> bool| { + let find_param_matching = |matches: &dyn Fn(&TypeParamResult) -> bool| { predicate_substs.types().find_map(|ty| { ty.walk().find_map(|arg| { // TODO(hoch) if let ty::GenericArgKind::Type(ty) = arg.unpack() && let ty::Param(param_ty) = ty.kind() - && matches(param_ty) + && matches(&TypeParamResult::Param(*param_ty)) { Some(arg) } else if let ty::GenericArgKind::Type(ty) = arg.unpack() && let ty::HKT(param_ty, ..) = ty.kind() - && matches(param_ty) + && matches(&TypeParamResult::HKT(*param_ty)) { todo!("hoch") } else { @@ -1860,13 +1860,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Prefer generics that are local to the fn item, since these are likely // to be the cause of the unsatisfied predicate. let mut param_to_point_at = find_param_matching(&|param_ty| { - self.tcx.parent(generics.type_param(*param_ty, self.tcx).def_id) == def_id + match param_ty { + TypeParamResult::Param(p) => self.tcx.parent(generics.type_param(*p, self.tcx).def_id) == def_id, + TypeParamResult::HKT(h) => self.tcx.parent(generics.type_param(*h, self.tcx).def_id) == def_id + } + }); // Fall back to generic that isn't local to the fn item. This will come // from a trait or impl, for example. let mut fallback_param_to_point_at = find_param_matching(&|param_ty| { - self.tcx.parent(generics.type_param(*param_ty, self.tcx).def_id) != def_id - && param_ty.name() != rustc_span::symbol::kw::SelfUpper + match param_ty { + TypeParamResult::Param(p) => self.tcx.parent(generics.type_param(*p, self.tcx).def_id) != def_id + && p.name() != rustc_span::symbol::kw::SelfUpper, + TypeParamResult::HKT(h) => self.tcx.parent(generics.type_param(*h, self.tcx).def_id) != def_id + && h.name() != rustc_span::symbol::kw::SelfUpper + } + }); // Finally, the `Self` parameter is possibly the reason that the predicate // is unsatisfied. This is less likely to be true for methods, because diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 647012c70976..466c1de1c998 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -16,7 +16,7 @@ use rustc_infer::infer::{self, InferOk, TyCtxtInferExt}; use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind}; use rustc_middle::middle::stability; use rustc_middle::ty::fast_reject::{simplify_type, TreatParams}; -use rustc_middle::ty::{AssocItem, TypeParameter}; +use rustc_middle::ty::{AssocItem, TypeParamResult}; use rustc_middle::ty::GenericParamDefKind; use rustc_middle::ty::ToPredicate; use rustc_middle::ty::{self, ParamEnvAnd, Ty, TyCtxt, TypeFoldable, TypeVisitable}; @@ -680,10 +680,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } } ty::Param(ref p) => { - self.assemble_inherent_candidates_from_param(p.clone()); + self.assemble_inherent_candidates_from_param(TypeParamResult::Param(p.clone())); } ty::HKT(ref p, ..) => { - self.assemble_inherent_candidates_from_param(p.clone()); + self.assemble_inherent_candidates_from_param(TypeParamResult::HKT(p.clone())); } ty::Bool | ty::Char @@ -812,19 +812,26 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { }); } - fn assemble_inherent_candidates_from_param(&mut self, param_ty: impl TypeParameter<'tcx>) { + fn assemble_inherent_candidates_from_param(&mut self, param_ty: TypeParamResult) { // FIXME: do we want to commit to this behavior for param bounds? - debug!("assemble_inherent_candidates_from_param(param_ty={:?})", param_ty); + match param_ty { + TypeParamResult::Param(p) => { + debug!("assemble_inherent_candidates_from_param(param_ty={:?})", p); + } + TypeParamResult::HKT(h) => { + debug!("assemble_inherent_candidates_from_param(param_ty={:?})", h); + } + } let bounds = self.param_env.caller_bounds().iter().filter_map(|predicate| { let bound_predicate = predicate.kind(); match bound_predicate.skip_binder() { ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) => { match *trait_predicate.trait_ref.self_ty().kind() { - ty::Param(ref p) if p.clone() == param_ty => { + ty::Param(ref p) if let TypeParamResult::Param(param) = param_ty && p.clone() == param => { Some(bound_predicate.rebind(trait_predicate.trait_ref)) } - ty::HKT(ref p, ..) if p.clone() == param_ty => { + ty::HKT(ref p, ..) if let TypeParamResult::HKT(param) = param_ty && p.clone() == param => { // TODO(hoch) Some(bound_predicate.rebind(trait_predicate.trait_ref)) } diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 7a6bcb3f06e0..f87d36be0e1d 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -26,7 +26,7 @@ use rustc_middle::traits::util::supertraits; use rustc_middle::ty::fast_reject::DeepRejectCtxt; use rustc_middle::ty::fast_reject::{simplify_type, TreatParams}; use rustc_middle::ty::print::{with_crate_prefix, with_forced_trimmed_paths}; -use rustc_middle::ty::{self, DefIdTree, GenericArgKind, Ty, TyCtxt, TypeVisitable, TypeParameter}; +use rustc_middle::ty::{self, DefIdTree, GenericArgKind, Ty, TyCtxt, TypeVisitable, TypeParameter, TypeParamResult}; use rustc_middle::ty::{IsSuggestable, ToPolyTraitRef}; use rustc_span::symbol::{kw, sym, Ident}; use rustc_span::Symbol; @@ -2376,12 +2376,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { candidates.sort_by(|a, b| a.cmp(b).reverse()); candidates.dedup(); - let param_type: Option> = match rcvr_ty.kind() { - ty::Param(param) => Some(Box::new(param)), - ty::HKT(param, ..) => Some(Box::new(param)), + let param_type = match rcvr_ty.kind() { + ty::Param(param) => Some(TypeParamResult::Param(*param)), + ty::HKT(param, ..) => Some(TypeParamResult::HKT(*param)), ty::Ref(_, ty, _) => match ty.kind() { - ty::Param(param) => Some(Box::new(param)), - ty::HKT(param, ..) => Some(Box::new(param)), + ty::Param(param) => Some(TypeParamResult::Param(*param)), + ty::HKT(param, ..) => Some(TypeParamResult::HKT(*param)), _ => None, }, _ => None, @@ -2404,87 +2404,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) }; // Obtain the span for `param` and use it for a structured suggestion. - if let Some(param) = param_type { - let generics = self.tcx.generics_of(self.body_id.owner.to_def_id()); - let type_param = generics.type_param(param, self.tcx); - let hir = self.tcx.hir(); - if let Some(def_id) = type_param.def_id.as_local() { - let id = hir.local_def_id_to_hir_id(def_id); - // Get the `hir::Param` to verify whether it already has any bounds. - // We do this to avoid suggesting code that ends up as `T: FooBar`, - // instead we suggest `T: Foo + Bar` in that case. - match hir.get(id) { - Node::GenericParam(param) => { - enum Introducer { - Plus, - Colon, - Nothing, - } - let ast_generics = hir.get_generics(id.owner.def_id).unwrap(); - let (sp, mut introducer) = if let Some(span) = - ast_generics.bounds_span_for_suggestions(def_id) - { - (span, Introducer::Plus) - } else if let Some(colon_span) = param.colon_span { - (colon_span.shrink_to_hi(), Introducer::Nothing) - } else { - (param.span.shrink_to_hi(), Introducer::Colon) - }; - if matches!( - param.kind, - hir::GenericParamKind::Type { synthetic: true, .. }, - ) { - introducer = Introducer::Plus - } - let trait_def_ids: FxHashSet = ast_generics - .bounds_for_param(def_id) - .flat_map(|bp| bp.bounds.iter()) - .filter_map(|bound| bound.trait_ref()?.trait_def_id()) - .collect(); - if !candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) { - err.span_suggestions( - sp, - &message(format!( - "restrict type parameter `{}` with", - param.name.ident(), - )), - candidates.iter().map(|t| { - format!( - "{} {}", - match introducer { - Introducer::Plus => " +", - Introducer::Colon => ":", - Introducer::Nothing => "", - }, - self.tcx.def_path_str(t.def_id), - ) - }), - Applicability::MaybeIncorrect, - ); - } - return; - } - Node::Item(hir::Item { - kind: hir::ItemKind::Trait(.., bounds, _), - ident, - .. - }) => { - let (sp, sep, article) = if bounds.is_empty() { - (ident.span.shrink_to_hi(), ":", "a") - } else { - (bounds.last().unwrap().span().shrink_to_hi(), " +", "another") - }; - err.span_suggestions( - sp, - &message(format!("add {} supertrait for", article)), - candidates.iter().map(|t| { - format!("{} {}", sep, self.tcx.def_path_str(t.def_id),) - }), - Applicability::MaybeIncorrect, - ); - return; - } - _ => {} + if let Some(param) = ¶m_type { + + match param { + TypeParamResult::Param(param_ty) => { + self.use_param_span_for_structured_suggestion(err, &mut candidates, &message, param_ty) + } + TypeParamResult::HKT(hkt_ty) => { + self.use_param_span_for_structured_suggestion(err, &mut candidates, &message, hkt_ty) } } } @@ -2524,8 +2451,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { (candidates, Vec::new()) }; - let action = if let Some(param) = param_type { - format!("restrict type parameter `{}` with", param) + let action = if let Some(param) = ¶m_type { + match param { + TypeParamResult::Param(param_ty) => { + format!("restrict type parameter `{}` with", *param_ty) + } + TypeParamResult::HKT(hkt_ty) => { + format!("restrict type parameter `{}` with", *hkt_ty) + } + } } else { // FIXME: it might only need to be imported into scope, not implemented. "implement".to_string() @@ -2579,6 +2513,97 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } + fn use_param_span_for_structured_suggestion( + &self, + err: &mut Diagnostic, + candidates: &mut Vec, + message: &impl Fn(String) -> String, + param: &impl TypeParameter<'tcx> + ) { + let generics = self.tcx.generics_of(self.body_id.owner.to_def_id()); + let type_param = generics.type_param(*param, self.tcx); + let hir = self.tcx.hir(); + if let Some(def_id) = type_param.def_id.as_local() { + let id = hir.local_def_id_to_hir_id(def_id); + // Get the `hir::Param` to verify whether it already has any bounds. + // We do this to avoid suggesting code that ends up as `T: FooBar`, + // instead we suggest `T: Foo + Bar` in that case. + match hir.get(id) { + Node::GenericParam(param) => { + enum Introducer { + Plus, + Colon, + Nothing, + } + let ast_generics = hir.get_generics(id.owner.def_id).unwrap(); + let (sp, mut introducer) = if let Some(span) = + ast_generics.bounds_span_for_suggestions(def_id) + { + (span, Introducer::Plus) + } else if let Some(colon_span) = param.colon_span { + (colon_span.shrink_to_hi(), Introducer::Nothing) + } else { + (param.span.shrink_to_hi(), Introducer::Colon) + }; + if matches!( + param.kind, + hir::GenericParamKind::Type { synthetic: true, .. }, + ) { + introducer = Introducer::Plus + } + let trait_def_ids: FxHashSet = ast_generics + .bounds_for_param(def_id) + .flat_map(|bp| bp.bounds.iter()) + .filter_map(|bound| bound.trait_ref()?.trait_def_id()) + .collect(); + if !candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) { + err.span_suggestions( + sp, + &message(format!( + "restrict type parameter `{}` with", + param.name.ident(), + )), + candidates.iter().map(|t| { + format!( + "{} {}", + match introducer { + Introducer::Plus => " +", + Introducer::Colon => ":", + Introducer::Nothing => "", + }, + self.tcx.def_path_str(t.def_id), + ) + }), + Applicability::MaybeIncorrect, + ); + } + return; + } + Node::Item(hir::Item { + kind: hir::ItemKind::Trait(.., bounds, _), + ident, + .. + }) => { + let (sp, sep, article) = if bounds.is_empty() { + (ident.span.shrink_to_hi(), ":", "a") + } else { + (bounds.last().unwrap().span().shrink_to_hi(), " +", "another") + }; + err.span_suggestions( + sp, + &message(format!("add {} supertrait for", article)), + candidates.iter().map(|t| { + format!("{} {}", sep, self.tcx.def_path_str(t.def_id), ) + }), + Applicability::MaybeIncorrect, + ); + return; + } + _ => {} + } + } + } + /// issue #102320, for `unwrap_or` with closure as argument, suggest `unwrap_or_else` /// FIXME: currently not working for suggesting `map_or_else`, see #102408 pub(crate) fn suggest_else_fn_with_closure( diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 04b261d25335..bdd983ea8c0c 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -96,7 +96,7 @@ pub use self::sty::{ BoundVariableKind, CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstVid, EarlyBoundRegion, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FnSig, FreeRegion, GenSig, GeneratorSubsts, GeneratorSubstsParts, InlineConstSubsts, - InlineConstSubstsParts, ParamConst, TypeParameter, ParamTy, HKTTy, PolyExistentialPredicate, + InlineConstSubstsParts, ParamConst, TypeParameter, TypeParamResult, ParamTy, HKTTy, PolyExistentialPredicate, PolyExistentialProjection, PolyExistentialTraitRef, PolyFnSig, PolyGenSig, PolyTraitRef, Region, RegionKind, RegionVid, TraitRef, TyKind, TypeAndMut, UpvarSubsts, VarianceDiagInfo, }; diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 6344ad872152..36adac4b6a05 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1319,6 +1319,27 @@ pub trait TypeParameter<'tcx>: Clone + Copy + PartialEq + Eq + PartialOrd + Ord } } +pub enum TypeParamResult { + Param(ParamTy), + HKT(HKTTy) +} + +impl TypeParamResult { + pub fn name(&self) -> Symbol { + match self { + TypeParamResult::Param(p) => p.name, + TypeParamResult::HKT(h) => h.name + } + } + + pub fn index(&self) -> u32 { + match self { + TypeParamResult::Param(p) => p.index, + TypeParamResult::HKT(h) => h.index + } + } +} + #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)] #[derive(HashStable)] pub struct ParamTy { @@ -1334,14 +1355,22 @@ pub struct HKTTy { } impl Debug for ParamTy { - fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { - todo!("hoch"); + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("ParamTy") + .field("name", &self.name) + .field("index", &self.index) + .finish() + //TODO: hoch, is this implemented correctly in the correct place? } } impl Debug for HKTTy { - fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { - todo!("hoch"); + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + f.debug_struct("HKTTy") + .field("name", &self.name) + .field("index", &self.index) + .finish() + //TODO: hoch, is this implemented correctly in the correct place? } } From f04abfd2522e80013033eb0664212fa9a5eebd55 Mon Sep 17 00:00:00 2001 From: Musab Kilic Date: Wed, 8 Feb 2023 12:38:52 +0100 Subject: [PATCH 3/4] - still in progress refactoring ParamTy to a trait TypeParameter - currently hkt.rs fails with the expected errors --- .../src/collect/predicates_of.rs | 2 +- compiler/rustc_middle/src/ty/sty.rs | 17 +---------------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index b06d73a16e6f..ccb4b12bc70b 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -181,7 +181,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP GenericParamKind::HKT(..) => { // TODO(hoch) let name = param.name.ident().name; - let param_ty = ty::ParamTy::new(index, name).to_ty(tcx); + let param_ty = ty::HKTTy::new(index, name).to_ty(tcx); index += 1; let mut bounds = Bounds::default(); diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 36adac4b6a05..b2e3afc8a091 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1291,21 +1291,6 @@ impl<'tcx> PolyFnSig<'tcx> { pub type CanonicalPolyFnSig<'tcx> = Canonical<'tcx, Binder<'tcx, FnSig<'tcx>>>; -/* -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)] -#[derive(HashStable)] -pub enum ParamTy { - Param { - index: u32, - name: Symbol, - }, - HKT { - index: u32, - name: Symbol, - } -} -*/ - pub trait TypeParameter<'tcx>: Clone + Copy + PartialEq + Eq + PartialOrd + Ord + Hash + Debug { fn new(index: u32, name: Symbol) -> Self; fn for_def(def: &ty::GenericParamDef) -> Self; @@ -1428,7 +1413,7 @@ impl<'tcx> TypeParameter<'tcx> for HKTTy { #[inline] fn to_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { - tcx.mk_ty_param(self.index, self.name) + tcx.mk_hkt_param(self.index, self.name, tcx.intern_substs(&[])) } #[inline] From 98d115a8b5e94126925a3075780c445551cd123d Mon Sep 17 00:00:00 2001 From: Musab Kilic Date: Fri, 17 Feb 2023 12:03:37 +0100 Subject: [PATCH 4/4] - fixed some merge errors between branches 'refactor_ParamTy' and 'master' --- .../rustc_hir_typeck/src/fn_ctxt/checks.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 2 +- compiler/rustc_middle/src/ty/generics.rs | 1 - .../rustc_middle/src/ty/structural_impls.rs | 21 +++++++ compiler/rustc_middle/src/ty/sty.rs | 61 ++++++++----------- compiler/rustc_middle/src/ty/subst.rs | 2 +- 6 files changed, 50 insertions(+), 39 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 024af38695e3..cc5960564706 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -26,7 +26,7 @@ use rustc_infer::infer::InferOk; use rustc_infer::infer::TypeTrace; use rustc_middle::ty::adjustment::AllowTwoPhase; use rustc_middle::ty::visit::TypeVisitable; -use rustc_middle::ty::{self, DefIdTree, IsSuggestable, SubstsRef, Ty, TyKind, TypeParameter, TypeParamResult, TypeSuperVisitable, TypeVisitor}; +use rustc_middle::ty::{self, DefIdTree, IsSuggestable, Ty, TypeParameter, TypeParamResult, TypeSuperVisitable, TypeVisitor}; use rustc_session::Session; use rustc_span::symbol::{kw, Ident}; use rustc_span::{self, sym, Span}; diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 7f866c4c21d5..7e3596ae7bd5 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -75,7 +75,7 @@ use std::iter; use std::mem; use std::ops::{Bound, Deref}; use std::sync::Arc; -use rustc_middle::ty::{HKTTy, TypeParameter, Generics}; +use rustc_middle::ty::{HKTTy, Generics}; pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync { /// Creates a new `OnDiskCache` instance from the serialized data in `data`. diff --git a/compiler/rustc_middle/src/ty/generics.rs b/compiler/rustc_middle/src/ty/generics.rs index 5e277332be80..49f813ef5d4a 100644 --- a/compiler/rustc_middle/src/ty/generics.rs +++ b/compiler/rustc_middle/src/ty/generics.rs @@ -273,7 +273,6 @@ impl<'tcx> Generics { let param = self.param_at(param.index() as usize, tcx); match param.kind { GenericParamDefKind::Type { .. } => param, - GenericParamDefKind::HKT { .. } => todo!("hoch"), _ => bug!("expected type parameter, but found another generic parameter"), } } diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 7957d311252e..77ec770adcad 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -17,6 +17,7 @@ use std::mem::ManuallyDrop; use std::ops::ControlFlow; use std::rc::Rc; use std::sync::Arc; +use rustc_middle::ty::{HKTTy, ParamTy, TypeParameter}; impl fmt::Debug for ty::TraitDef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -112,6 +113,26 @@ impl<'tcx> fmt::Debug for Ty<'tcx> { } } +impl fmt::Debug for ParamTy { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("ParamTy") + .field("name", &self.name()) + .field("index", &self.index()) + .finish() + //TODO: hoch, is this implemented correctly in the correct place? + } +} + +impl fmt::Debug for HKTTy { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("HKTTy") + .field("name", &self.name()) + .field("index", &self.index()) + .finish() + //TODO: hoch, is this implemented correctly in the correct place? + } +} + impl fmt::Debug for ty::ArgumentDef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}/#{}", self.name, self.index) diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 5f7503ab9a49..66c022035112 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -24,7 +24,7 @@ use rustc_target::spec::abi; use std::borrow::Cow; use std::cmp::Ordering; use std::fmt; -use std::fmt::{Debug, Formatter}; +use std::fmt::{Debug}; use std::hash::Hash; use std::marker::PhantomData; use std::ops::{ControlFlow, Deref, Range}; @@ -1312,11 +1312,11 @@ impl PartialEq for ArgumentDef { } pub trait TypeParameter<'tcx>: Clone + Copy + PartialEq + Eq + PartialOrd + Ord + Hash + Debug { - fn new(index: u32, name: Symbol) -> Self; fn for_def(def: &ty::GenericParamDef) -> Self; fn to_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>; fn index(&self) -> u32; fn name(&self) -> Symbol; + fn def_id(&self) -> DefId; fn span_from_generics(&self, tcx: TyCtxt<'tcx>, item_with_generics: DefId) -> Span { let generics = tcx.generics_of(item_with_generics); let type_param = generics.type_param(*self, tcx); @@ -1360,30 +1360,20 @@ pub struct HKTTy { name: Symbol, } -impl Debug for ParamTy { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - f.debug_struct("ParamTy") - .field("name", &self.name) - .field("index", &self.index) - .finish() - //TODO: hoch, is this implemented correctly in the correct place? +impl ParamTy { + pub fn new(index: u32, name: Symbol) -> ParamTy { + Self { index, name } } } -impl Debug for HKTTy { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - f.debug_struct("HKTTy") - .field("name", &self.name) - .field("index", &self.index) - .finish() - //TODO: hoch, is this implemented correctly in the correct place? +impl HKTTy { + pub fn new(def_id: DefId, index: u32, name: Symbol) -> HKTTy { + Self {def_id, index, name } } } + impl<'tcx> TypeParameter<'tcx> for ParamTy { - fn new(index: u32, name: Symbol) -> ParamTy { - Self { index, name } - } fn for_def(def: &ty::GenericParamDef) -> ParamTy { match def.kind { @@ -1408,28 +1398,16 @@ impl<'tcx> TypeParameter<'tcx> for ParamTy { self.index } - #[inline] - pub fn def_id(&self) -> DefId { - match *self { - ParamTy::Param { .. } => { - todo!("What to do here") - } - ParamTy::HKT { def_id, .. } => { - def_id - } - } - } - #[inline] fn name(&self) -> Symbol { self.name } + + #[inline] + fn def_id(&self) -> DefId { todo!("What to do here") } } impl<'tcx> TypeParameter<'tcx> for HKTTy { - fn new(def_id: DefId, index: u32, name: Symbol) -> HKTTy { - Self {def_id, index, name } - } fn for_def(def: &ty::GenericParamDef) -> HKTTy { match def.kind { @@ -1446,7 +1424,17 @@ impl<'tcx> TypeParameter<'tcx> for HKTTy { #[inline] fn to_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { - tcx.mk_hkt_param(self.index, self.name, tcx.intern_substs(&[])) + let generics: &ty::Generics = tcx.generics_of(self.def_id); + + let generics = generics.params.iter().map(|param| { + tcx.mk_ty(ty::Argument(ArgumentDef { + def_id: self.def_id, + index: param.index, + name: param.name, + })).into() + }).collect::>(); + + tcx.mk_hkt_param(self.def_id, self.index, self.name, tcx.intern_substs(&generics)).into() } #[inline] @@ -1458,6 +1446,9 @@ impl<'tcx> TypeParameter<'tcx> for HKTTy { fn name(&self) -> Symbol { self.name } + + #[inline] + fn def_id(&self) -> DefId { self.def_id } } #[derive(Copy, Clone, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)] diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index ffb61453bab1..8571b5a622a0 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -825,7 +825,7 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> { self.shift_vars_through_binders(ty) } - fn hkt_for_param(&self, p: ty::ParamTy, substs: SubstsRef<'tcx>, source_ty: Ty<'tcx>) -> Ty<'tcx> { + fn hkt_for_param(&self, p: impl TypeParameter<'tcx>, substs: SubstsRef<'tcx>, source_ty: Ty<'tcx>) -> Ty<'tcx> { // Look up the type in the substitutions. It really should be in there. let opt_ty = self.substs.get(p.index() as usize).map(|k| k.unpack());