From 61813afed6ff9156970276908fbd4ac1fc721a56 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sat, 9 Dec 2023 16:26:42 +0000 Subject: [PATCH 1/3] Add an `is_effect` flag to `GenericArgKind` --- .../src/diagnostics/region_name.rs | 4 +- .../src/region_infer/opaque_types.rs | 2 +- .../src/type_check/constraint_conversion.rs | 2 +- .../src/debuginfo/type_names.rs | 2 +- .../rustc_hir_analysis/src/astconv/bounds.rs | 17 +++-- .../rustc_hir_analysis/src/astconv/mod.rs | 61 +++++++++------- compiler/rustc_hir_analysis/src/bounds.rs | 6 +- .../src/check/compare_impl_item.rs | 20 ++--- .../rustc_hir_analysis/src/check/wfcheck.rs | 8 +- .../src/collect/predicates_of.rs | 2 +- .../src/outlives/implicit_infer.rs | 2 +- .../rustc_hir_analysis/src/outlives/mod.rs | 2 +- .../rustc_hir_analysis/src/outlives/utils.rs | 2 +- .../src/variance/constraints.rs | 4 +- .../rustc_hir_typeck/src/fn_ctxt/_impl.rs | 38 ++++++---- .../src/fn_ctxt/adjust_fulfillment_errors.rs | 10 ++- .../rustc_hir_typeck/src/method/confirm.rs | 20 +++-- .../rustc_hir_typeck/src/method/suggest.rs | 9 ++- compiler/rustc_hir_typeck/src/writeback.rs | 2 +- compiler/rustc_infer/src/infer/at.rs | 8 +- .../src/infer/canonical/canonicalizer.rs | 2 +- .../rustc_infer/src/infer/canonical/mod.rs | 21 ++++-- .../src/infer/canonical/query_response.rs | 6 +- .../src/infer/canonical/substitute.rs | 2 +- .../infer/error_reporting/need_type_info.rs | 20 +++-- compiler/rustc_infer/src/infer/mod.rs | 29 ++++---- .../rustc_infer/src/infer/opaque_types.rs | 2 +- .../src/infer/outlives/components.rs | 6 +- .../src/infer/outlives/obligations.rs | 2 +- compiler/rustc_middle/src/infer/canonical.rs | 34 +++++---- compiler/rustc_middle/src/ty/context.rs | 18 +++-- compiler/rustc_middle/src/ty/fast_reject.rs | 7 +- compiler/rustc_middle/src/ty/flags.rs | 2 +- compiler/rustc_middle/src/ty/generic_args.rs | 73 ++++++++++++------- compiler/rustc_middle/src/ty/generics.rs | 21 +++--- compiler/rustc_middle/src/ty/instance.rs | 4 +- compiler/rustc_middle/src/ty/mod.rs | 7 -- .../src/ty/normalize_erasing_regions.rs | 7 +- compiler/rustc_middle/src/ty/opaque_types.rs | 4 +- compiler/rustc_middle/src/ty/print/pretty.rs | 19 +---- compiler/rustc_middle/src/ty/relate.rs | 9 ++- .../rustc_middle/src/ty/structural_impls.rs | 6 +- .../rustc_middle/src/ty/typeck_results.rs | 2 +- compiler/rustc_middle/src/ty/util.rs | 34 +++++---- compiler/rustc_middle/src/ty/walk.rs | 22 +++--- compiler/rustc_monomorphize/src/collector.rs | 8 +- .../src/canonicalizer.rs | 4 +- .../rustc_smir/src/rustc_internal/internal.rs | 4 +- .../rustc_smir/src/rustc_smir/convert/ty.rs | 2 +- .../src/typeid/typeid_itanium_cxx_abi.rs | 2 +- compiler/rustc_symbol_mangling/src/v0.rs | 4 +- .../src/solve/eval_ctxt/canonical.rs | 4 +- .../error_reporting/type_err_ctxt_ext.rs | 6 +- .../query/type_op/implied_outlives_bounds.rs | 2 +- .../src/traits/select/confirmation.rs | 22 +++--- .../rustc_trait_selection/src/traits/util.rs | 9 ++- .../rustc_trait_selection/src/traits/wf.rs | 38 +++++----- compiler/rustc_ty_utils/src/ty.rs | 2 +- src/librustdoc/clean/utils.rs | 6 +- .../call-generic-method-nonconst.rs | 3 +- .../call-generic-method-nonconst.stderr | 4 +- 61 files changed, 395 insertions(+), 305 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/region_name.rs b/compiler/rustc_borrowck/src/diagnostics/region_name.rs index a17c3bc3a78c5..8c564c1f8d8c4 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_name.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_name.rs @@ -607,7 +607,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { search_stack.push((ty, hir_ty)); } - (GenericArgKind::Const(_ct), hir::GenericArg::Const(_hir_ct)) => { + (GenericArgKind::Const(_ct, _), hir::GenericArg::Const(_hir_ct)) => { // Lifetimes cannot be found in consts, so we don't need // to search anything here. } @@ -615,7 +615,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> { ( GenericArgKind::Lifetime(_) | GenericArgKind::Type(_) - | GenericArgKind::Const(_), + | GenericArgKind::Const(_, _), _, ) => { // HIR lowering sometimes doesn't catch this in erroneous diff --git a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs index c93cfa78832a2..f86ea917404f6 100644 --- a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs +++ b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs @@ -392,7 +392,7 @@ fn check_opaque_type_parameter_valid( // see that issue for more. We will also have to ignore unused lifetime // params for RPIT, but that's comparatively trivial ✨ GenericArgKind::Lifetime(_) => continue, - GenericArgKind::Const(ct) => matches!(ct.kind(), ty::ConstKind::Param(_)), + GenericArgKind::Const(ct, _) => matches!(ct.kind(), ty::ConstKind::Param(_)), }; if arg_is_param { diff --git a/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs b/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs index 21d8026e17089..36f8419a6e2ac 100644 --- a/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs +++ b/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs @@ -162,7 +162,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> { .type_must_outlive(origin, t1, r2, constraint_category); } - GenericArgKind::Const(_) => unreachable!(), + GenericArgKind::Const(_, _) => unreachable!(), } } diff --git a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs index dda30046bfbad..54e2dc3779840 100644 --- a/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs +++ b/compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs @@ -644,7 +644,7 @@ fn push_generic_params_internal<'tcx>( GenericArgKind::Type(type_parameter) => { push_debuginfo_type_name(tcx, type_parameter, true, output, visited); } - GenericArgKind::Const(ct) => { + GenericArgKind::Const(ct, false) => { push_const_param(tcx, ct, output); } other => bug!("Unexpected non-erasable generic: {:?}", other), diff --git a/compiler/rustc_hir_analysis/src/astconv/bounds.rs b/compiler/rustc_hir_analysis/src/astconv/bounds.rs index dfec3c5e829a6..e087fa176043e 100644 --- a/compiler/rustc_hir_analysis/src/astconv/bounds.rs +++ b/compiler/rustc_hir_analysis/src/astconv/bounds.rs @@ -361,7 +361,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ { ) .into() } - ty::GenericParamDefKind::Const { .. } => { + ty::GenericParamDefKind::Const { is_host_effect, .. } => { if !emitted_bad_param_err { tcx.sess.emit_err( crate::errors::ReturnTypeNotationIllegalParam::Const { @@ -375,13 +375,16 @@ impl<'tcx> dyn AstConv<'tcx> + '_ { .type_of(param.def_id) .no_bound_vars() .expect("ct params cannot have early bound vars"); - ty::Const::new_bound( - tcx, - ty::INNERMOST, - ty::BoundVar::from_usize(num_bound_vars), - ty, + // FIXME(fee1-dead) this seems like something that's very commonly written + ty::GenericArg::new_const( + ty::Const::new_bound( + tcx, + ty::INNERMOST, + ty::BoundVar::from_usize(num_bound_vars), + ty, + ), + is_host_effect, ) - .into() } }; num_bound_vars += 1; diff --git a/compiler/rustc_hir_analysis/src/astconv/mod.rs b/compiler/rustc_hir_analysis/src/astconv/mod.rs index 20d36a1b0690c..59b350678e782 100644 --- a/compiler/rustc_hir_analysis/src/astconv/mod.rs +++ b/compiler/rustc_hir_analysis/src/astconv/mod.rs @@ -449,33 +449,40 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } }; - match (¶m.kind, arg) { + match (param.kind, arg) { (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => { self.astconv.ast_region_to_region(lt, Some(param)).into() } - (&GenericParamDefKind::Type { has_default, .. }, GenericArg::Type(ty)) => { + (GenericParamDefKind::Type { has_default, .. }, GenericArg::Type(ty)) => { handle_ty_args(has_default, ty) } - (&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => { + (GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => { handle_ty_args(has_default, &inf.to_ty()) } - (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => { + (GenericParamDefKind::Const { is_host_effect, .. }, GenericArg::Const(ct)) => { let did = ct.value.def_id; tcx.feed_anon_const_type(did, tcx.type_of(param.def_id)); - ty::Const::from_anon_const(tcx, did).into() + ty::GenericArg::new_const( + ty::Const::from_anon_const(tcx, did), + is_host_effect, + ) } - (&GenericParamDefKind::Const { .. }, hir::GenericArg::Infer(inf)) => { + ( + GenericParamDefKind::Const { is_host_effect, .. }, + hir::GenericArg::Infer(inf), + ) => { let ty = tcx .at(self.span) .type_of(param.def_id) .no_bound_vars() .expect("const parameter types cannot be generic"); - if self.astconv.allow_ty_infer() { - self.astconv.ct_infer(ty, Some(param), inf.span).into() + let ct = if self.astconv.allow_ty_infer() { + self.astconv.ct_infer(ty, Some(param), inf.span) } else { self.inferred_params.push(inf.span); - ty::Const::new_misc_error(tcx, ty).into() - } + ty::Const::new_misc_error(tcx, ty) + }; + ty::GenericArg::new_const(ct, is_host_effect) } _ => unreachable!(), } @@ -522,28 +529,31 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { Ty::new_misc_error(tcx).into() } } - GenericParamDefKind::Const { has_default, .. } => { + GenericParamDefKind::Const { has_default, is_host_effect } => { let ty = tcx .at(self.span) .type_of(param.def_id) .no_bound_vars() .expect("const parameter types cannot be generic"); if let Err(guar) = ty.error_reported() { - return ty::Const::new_error(tcx, guar, ty).into(); + return ty::GenericArg::new_const( + ty::Const::new_error(tcx, guar, ty), + is_host_effect, + ); } // FIXME(effects) see if we should special case effect params here - if !infer_args && has_default { - tcx.const_param_default(param.def_id) - .instantiate(tcx, args.unwrap()) - .into() + let ct = if !infer_args && has_default { + tcx.const_param_default(param.def_id).instantiate(tcx, args.unwrap()) } else { if infer_args { - self.astconv.ct_infer(ty, Some(param), self.span).into() + self.astconv.ct_infer(ty, Some(param), self.span) } else { // We've already errored above about the mismatch. - ty::Const::new_misc_error(tcx, ty).into() + ty::Const::new_misc_error(tcx, ty) } - } + }; + + ty::GenericArg::new_const(ct, is_host_effect) } } } @@ -1131,12 +1141,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { (bound, bound2) = (bound2, bound); } - let unconsted_args = bound - .skip_binder() - .args - .iter() - .enumerate() - .map(|(n, arg)| if host_index == n { tcx.consts.true_.into() } else { arg }); + let unconsted_args = bound.skip_binder().args.iter().enumerate().map(|(n, arg)| { + if host_index == n { + ty::GenericArg::effect_const_arg(tcx.consts.true_) + } else { + arg + } + }); if unconsted_args.eq(bound2.skip_binder().args.iter()) { next_cand = matching_candidates.next(); diff --git a/compiler/rustc_hir_analysis/src/bounds.rs b/compiler/rustc_hir_analysis/src/bounds.rs index b6688e0ce29e0..282a7c0e20ad5 100644 --- a/compiler/rustc_hir_analysis/src/bounds.rs +++ b/compiler/rustc_hir_analysis/src/bounds.rs @@ -56,7 +56,11 @@ impl<'tcx> Bounds<'tcx> { let trait_ref = trait_ref.map_bound(|mut trait_ref| { trait_ref.args = tcx.mk_args_from_iter(trait_ref.args.iter().enumerate().map(|(n, arg)| { - if host_index == n { tcx.consts.true_.into() } else { arg } + if host_index == n { + ty::GenericArg::effect_const_arg(tcx.consts.true_) + } else { + arg + } })); trait_ref }); diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 1412fd1a987cb..e46944925828f 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -2357,18 +2357,20 @@ fn param_env_with_gat_bounds<'tcx>( ) .into() } - GenericParamDefKind::Const { .. } => { + GenericParamDefKind::Const { is_host_effect, .. } => { let bound_var = ty::BoundVariableKind::Const; bound_vars.push(bound_var); - ty::Const::new_bound( - tcx, - ty::INNERMOST, - ty::BoundVar::from_usize(bound_vars.len() - 1), - tcx.type_of(param.def_id) - .no_bound_vars() - .expect("const parameter types cannot be generic"), + ty::GenericArg::new_const( + ty::Const::new_bound( + tcx, + ty::INNERMOST, + ty::BoundVar::from_usize(bound_vars.len() - 1), + tcx.type_of(param.def_id) + .no_bound_vars() + .expect("const parameter types cannot be generic"), + ), + is_host_effect, ) - .into() } }); // When checking something like diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 3e805ab00b9c7..62631ca0fd067 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -1328,7 +1328,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id } } } - GenericParamDefKind::Const { .. } => { + GenericParamDefKind::Const { is_host_effect, .. } => { if is_our_default(param) { // FIXME(const_generics_defaults): This // is incorrect when dealing with unused args, for example @@ -1339,7 +1339,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id wfcx.register_wf_obligation( tcx.def_span(param.def_id), None, - default_ct.into(), + ty::GenericArg::new_const(default_ct, is_host_effect), ); } } @@ -1377,14 +1377,14 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id tcx.mk_param_from_def(param) } - GenericParamDefKind::Const { .. } => { + GenericParamDefKind::Const { is_host_effect, .. } => { // If the param has a default, ... if is_our_default(param) { let default_ct = tcx.const_param_default(param.def_id).instantiate_identity(); // ... and it's not a dependent default, ... if !default_ct.has_param() { // ... then substitute it with the default. - return default_ct.into(); + return ty::GenericArg::new_const(default_ct, is_host_effect); } } diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index ca9443225e294..02bf2d9cdb7ac 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -47,7 +47,7 @@ pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredic def_id, ty::GenericArgs::for_item(tcx, def_id, |param, _| { if param.is_host_effect() { - tcx.consts.true_.into() + ty::GenericArg::effect_const_arg(tcx.consts.true_) } else { tcx.mk_param_from_def(param) } diff --git a/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs b/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs index c17925471d983..3cae10ab9893d 100644 --- a/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs +++ b/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs @@ -100,7 +100,7 @@ fn insert_required_predicates_to_be_wf<'tcx>( // No predicates from lifetimes or constants, except potentially // constants' types, but `walk` will get to them as well. - GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => continue, + GenericArgKind::Lifetime(_) | GenericArgKind::Const(_, _) => continue, }; match *ty.kind() { diff --git a/compiler/rustc_hir_analysis/src/outlives/mod.rs b/compiler/rustc_hir_analysis/src/outlives/mod.rs index c065f6e7e881e..b00b94c5bee5a 100644 --- a/compiler/rustc_hir_analysis/src/outlives/mod.rs +++ b/compiler/rustc_hir_analysis/src/outlives/mod.rs @@ -115,7 +115,7 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, (): ()) -> CratePredicatesMap<'_> { .to_predicate(tcx), span, )), - GenericArgKind::Const(_) => { + GenericArgKind::Const(_, _) => { // Generic consts don't impose any constraints. None } diff --git a/compiler/rustc_hir_analysis/src/outlives/utils.rs b/compiler/rustc_hir_analysis/src/outlives/utils.rs index 8077acea52e44..6fa263108cd7c 100644 --- a/compiler/rustc_hir_analysis/src/outlives/utils.rs +++ b/compiler/rustc_hir_analysis/src/outlives/utils.rs @@ -138,7 +138,7 @@ pub(crate) fn insert_outlives_predicate<'tcx>( required_predicates.entry(ty::OutlivesPredicate(kind, outlived_region)).or_insert(span); } - GenericArgKind::Const(_) => { + GenericArgKind::Const(_, _) => { // Generic consts don't impose any constraints. } } diff --git a/compiler/rustc_hir_analysis/src/variance/constraints.rs b/compiler/rustc_hir_analysis/src/variance/constraints.rs index f09594cbbc667..a3e6c4346667f 100644 --- a/compiler/rustc_hir_analysis/src/variance/constraints.rs +++ b/compiler/rustc_hir_analysis/src/variance/constraints.rs @@ -205,7 +205,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { self.add_constraints_from_region(current, lt, variance_i) } GenericArgKind::Type(ty) => self.add_constraints_from_ty(current, ty, variance_i), - GenericArgKind::Const(val) => { + GenericArgKind::Const(val, _) => { self.add_constraints_from_const(current, val, variance_i) } } @@ -364,7 +364,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { self.add_constraints_from_region(current, lt, variance_i) } GenericArgKind::Type(ty) => self.add_constraints_from_ty(current, ty, variance_i), - GenericArgKind::Const(val) => { + GenericArgKind::Const(val, _) => { self.add_constraints_from_const(current, val, variance) } } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index df840aaa57884..32bdfb274ad1b 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -413,7 +413,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { hir::ArrayLen::Body(anon_const) => { let span = self.tcx.def_span(anon_const.def_id); let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id); - self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None)); + self.register_wf_obligation( + ty::GenericArg::normal_const_arg(c), + span, + ObligationCauseCode::WellFormed(None), + ); self.normalize(span, c) } } @@ -428,7 +432,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id)); let c = ty::Const::from_anon_const(self.tcx, did); self.register_wf_obligation( - c.into(), + ty::GenericArg::normal_const_arg(c), self.tcx.hir().span(ast_c.hir_id), ObligationCauseCode::WellFormed(None), ); @@ -1296,37 +1300,41 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { param: &ty::GenericParamDef, arg: &GenericArg<'_>, ) -> ty::GenericArg<'tcx> { - match (¶m.kind, arg) { + match (param.kind, arg) { (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => { self.fcx.astconv().ast_region_to_region(lt, Some(param)).into() } (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => { self.fcx.to_ty(ty).raw.into() } - (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => { - self.fcx.const_arg_to_const(&ct.value, param.def_id).into() + (GenericParamDefKind::Const { is_host_effect, .. }, GenericArg::Const(ct)) => { + ty::GenericArg::new_const( + self.fcx.const_arg_to_const(&ct.value, param.def_id), + is_host_effect, + ) } (GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => { self.fcx.ty_infer(Some(param), inf.span).into() } ( - &GenericParamDefKind::Const { has_default, is_host_effect }, + GenericParamDefKind::Const { has_default, is_host_effect }, GenericArg::Infer(inf), ) => { let tcx = self.fcx.tcx(); - if has_default && is_host_effect { + if is_host_effect { + assert!(has_default); self.fcx.var_for_effect(param) } else { - self.fcx - .ct_infer( + ty::GenericArg::normal_const_arg( + self.fcx.ct_infer( tcx.type_of(param.def_id) .no_bound_vars() .expect("const parameter types cannot be generic"), Some(param), inf.span, - ) - .into() + ), + ) } } _ => unreachable!(), @@ -1372,10 +1380,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if is_host_effect { return self.fcx.var_for_effect(param); } else if !infer_args { - return tcx - .const_param_default(param.def_id) - .instantiate(tcx, args.unwrap()) - .into(); + return ty::GenericArg::normal_const_arg( + tcx.const_param_default(param.def_id) + .instantiate(tcx, args.unwrap()), + ); } } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs index e3aca11fd03aa..d4a9117d4f4e3 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs @@ -39,8 +39,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { (pred.trait_ref.args.to_vec(), Some(pred.self_ty().into())) } ty::ClauseKind::Projection(pred) => (pred.projection_ty.args.to_vec(), None), - ty::ClauseKind::ConstArgHasType(arg, ty) => (vec![ty.into(), arg.into()], None), - ty::ClauseKind::ConstEvaluatable(e) => (vec![e.into()], None), + ty::ClauseKind::ConstArgHasType(arg, ty) => { + (vec![ty.into(), ty::GenericArg::normal_const_arg(arg)], None) + } + ty::ClauseKind::ConstEvaluatable(e) => { + (vec![ty::GenericArg::normal_const_arg(e)], None) + } _ => return false, }; @@ -52,7 +56,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { && matches(ty::ParamTerm::Ty(param_ty)) { Some(arg) - } else if let ty::GenericArgKind::Const(ct) = arg.unpack() + } else if let ty::GenericArgKind::Const(ct, _) = arg.unpack() && let ty::ConstKind::Param(param_ct) = ct.kind() && matches(ty::ParamTerm::Const(param_ct)) { diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs index e7af7da205c61..47a61d762a07a 100644 --- a/compiler/rustc_hir_typeck/src/method/confirm.rs +++ b/compiler/rustc_hir_typeck/src/method/confirm.rs @@ -390,30 +390,34 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { param: &ty::GenericParamDef, arg: &GenericArg<'_>, ) -> ty::GenericArg<'tcx> { - match (¶m.kind, arg) { + match (param.kind, arg) { (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => { self.cfcx.fcx.astconv().ast_region_to_region(lt, Some(param)).into() } (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => { self.cfcx.to_ty(ty).raw.into() } - (GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => { - self.cfcx.const_arg_to_const(&ct.value, param.def_id).into() + (GenericParamDefKind::Const { is_host_effect, .. }, GenericArg::Const(ct)) => { + ty::GenericArg::new_const( + self.cfcx.const_arg_to_const(&ct.value, param.def_id), + is_host_effect, + ) } (GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => { self.cfcx.ty_infer(Some(param), inf.span).into() } - (GenericParamDefKind::Const { .. }, GenericArg::Infer(inf)) => { + (GenericParamDefKind::Const { is_host_effect, .. }, GenericArg::Infer(inf)) => { let tcx = self.cfcx.tcx(); - self.cfcx - .ct_infer( + ty::GenericArg::new_const( + self.cfcx.ct_infer( tcx.type_of(param.def_id) .no_bound_vars() .expect("const parameter types cannot be generic"), Some(param), inf.span, - ) - .into() + ), + is_host_effect, + ) } _ => unreachable!(), } diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index b38a6ebd501e3..9c8f859ce6c38 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -1562,15 +1562,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { kind: TypeVariableOriginKind::MiscVariable, }) .into(), - GenericArgKind::Const(arg) => self - .next_const_var( + GenericArgKind::Const(arg, is_effect) => ty::GenericArg::new_const( + self.next_const_var( arg.ty(), ConstVariableOrigin { span: rustc_span::DUMMY_SP, kind: ConstVariableOriginKind::MiscVariable, }, - ) - .into(), + ), + is_effect, + ), } } else { arg diff --git a/compiler/rustc_hir_typeck/src/writeback.rs b/compiler/rustc_hir_typeck/src/writeback.rs index cc617fd2d0be6..c3748b0b6e42f 100644 --- a/compiler/rustc_hir_typeck/src/writeback.rs +++ b/compiler/rustc_hir_typeck/src/writeback.rs @@ -814,7 +814,7 @@ impl<'cx, 'tcx> TypeFolder> for Resolver<'cx, 'tcx> { Ok(ct) => self.fcx.tcx.erase_regions(ct), Err(_) => { debug!("Resolver::fold_const: input const `{:?}` not fully resolvable", ct); - let e = self.report_error(ct); + let e = self.report_error(ty::GenericArg::normal_const_arg(ct)); self.replaced_with_error = Some(e); ty::Const::new_error(self.fcx.tcx, e, ct.ty()) } diff --git a/compiler/rustc_infer/src/infer/at.rs b/compiler/rustc_infer/src/infer/at.rs index 09313cd9738c2..8745b7168d232 100644 --- a/compiler/rustc_infer/src/infer/at.rs +++ b/compiler/rustc_infer/src/infer/at.rs @@ -414,13 +414,13 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> { values: match (a.unpack(), b.unpack()) { (Lifetime(a), Lifetime(b)) => Regions(ExpectedFound::new(a_is_expected, a, b)), (Type(a), Type(b)) => Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())), - (Const(a), Const(b)) => { + (Const(a, _), Const(b, _)) => { Terms(ExpectedFound::new(a_is_expected, a.into(), b.into())) } - (Lifetime(_), Type(_) | Const(_)) - | (Type(_), Lifetime(_) | Const(_)) - | (Const(_), Lifetime(_) | Type(_)) => { + (Lifetime(_), Type(_) | Const(_, _)) + | (Type(_), Lifetime(_) | Const(_, _)) + | (Const(_, _), Lifetime(_) | Type(_)) => { bug!("relating different kinds: {a:?} {b:?}") } }, diff --git a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs index 473a3965885fe..d1e2bc9094d42 100644 --- a/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs +++ b/compiler/rustc_infer/src/infer/canonical/canonicalizer.rs @@ -807,7 +807,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> { if bound_to != const_var { self.fold_const(bound_to) } else { - let var = self.canonical_var(info, const_var.into()); + let var = self.canonical_var(info, ty::GenericArg::normal_const_arg(const_var)); ty::Const::new_bound(self.tcx, self.binder_index, var, self.fold_ty(const_var.ty())) } } diff --git a/compiler/rustc_infer/src/infer/canonical/mod.rs b/compiler/rustc_infer/src/infer/canonical/mod.rs index 3c4c4644fe6ef..2f32686c52dca 100644 --- a/compiler/rustc_infer/src/infer/canonical/mod.rs +++ b/compiler/rustc_infer/src/infer/canonical/mod.rs @@ -144,22 +144,29 @@ impl<'tcx> InferCtxt<'tcx> { ty::Region::new_placeholder(self.tcx, placeholder_mapped).into() } - CanonicalVarKind::Const(ui, ty) => self - .next_const_var_in_universe( + CanonicalVarKind::Const(ui, ty) => { + ty::GenericArg::normal_const_arg(self.next_const_var_in_universe( ty, ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span }, universe_map(ui), - ) - .into(), + )) + } CanonicalVarKind::Effect => { let vid = self.inner.borrow_mut().effect_unification_table().new_key(None).vid; - ty::Const::new_infer(self.tcx, ty::InferConst::EffectVar(vid), self.tcx.types.bool) - .into() + ty::GenericArg::effect_const_arg(ty::Const::new_infer( + self.tcx, + ty::InferConst::EffectVar(vid), + self.tcx.types.bool, + )) } CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { universe, bound }, ty) => { let universe_mapped = universe_map(universe); let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, bound }; - ty::Const::new_placeholder(self.tcx, placeholder_mapped, ty).into() + ty::GenericArg::normal_const_arg(ty::Const::new_placeholder( + self.tcx, + placeholder_mapped, + ty, + )) } } } diff --git a/compiler/rustc_infer/src/infer/canonical/query_response.rs b/compiler/rustc_infer/src/infer/canonical/query_response.rs index 6860d0de179ea..8da794b8e2952 100644 --- a/compiler/rustc_infer/src/infer/canonical/query_response.rs +++ b/compiler/rustc_infer/src/infer/canonical/query_response.rs @@ -301,7 +301,7 @@ impl<'tcx> InferCtxt<'tcx> { .relate(v1, v2)?; } - (GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => { + (GenericArgKind::Const(v1, _), GenericArgKind::Const(v2, _)) => { TypeRelating::new( self, QueryTypeRelatingDelegate { @@ -468,7 +468,7 @@ impl<'tcx> InferCtxt<'tcx> { opt_values[br.var] = Some(*original_value); } } - GenericArgKind::Const(result_value) => { + GenericArgKind::Const(result_value, _) => { if let ty::ConstKind::Bound(debruijn, b) = result_value.kind() { // ...in which case we would set `canonical_vars[0]` to `Some(const X)`. @@ -635,7 +635,7 @@ impl<'tcx> InferCtxt<'tcx> { .into_obligations(), ); } - (GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => { + (GenericArgKind::Const(v1, _), GenericArgKind::Const(v2, _)) => { let ok = self.at(cause, param_env).eq(DefineOpaqueTypes::Yes, v1, v2)?; obligations.extend(ok.into_obligations()); } diff --git a/compiler/rustc_infer/src/infer/canonical/substitute.rs b/compiler/rustc_infer/src/infer/canonical/substitute.rs index f368b30fbd193..a9dfc7adc3a34 100644 --- a/compiler/rustc_infer/src/infer/canonical/substitute.rs +++ b/compiler/rustc_infer/src/infer/canonical/substitute.rs @@ -83,7 +83,7 @@ where r => bug!("{:?} is a type but value is {:?}", bound_ty, r), }, consts: &mut |bound_ct: ty::BoundVar, _| match var_values[bound_ct].unpack() { - GenericArgKind::Const(ct) => ct, + GenericArgKind::Const(ct, _) => ct, c => bug!("{:?} is a const but value is {:?}", bound_ct, c), }, }; diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs index 8fe6c1b0d86fd..e760369dc752f 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs @@ -299,7 +299,7 @@ impl<'tcx> InferCtxt<'tcx> { parent: None, } } - GenericArgKind::Const(ct) => { + GenericArgKind::Const(ct, _) => { if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.kind() { let origin = self.inner.borrow_mut().const_unification_table().probe_value(vid).origin; @@ -511,15 +511,16 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { kind: TypeVariableOriginKind::MiscVariable, }) .into(), - GenericArgKind::Const(arg) => self - .next_const_var( + GenericArgKind::Const(arg, is_effect) => ty::GenericArg::new_const( + self.next_const_var( arg.ty(), ConstVariableOrigin { span: rustc_span::DUMMY_SP, kind: ConstVariableOriginKind::MiscVariable, }, - ) - .into(), + ), + is_effect, + ), } })) .unwrap(); @@ -744,7 +745,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { match arg.unpack() { GenericArgKind::Lifetime(_) => 0, // erased GenericArgKind::Type(ty) => self.ty_cost(ty), - GenericArgKind::Const(_) => 3, // some non-zero value + GenericArgKind::Const(_, _) => 3, // some non-zero value } } fn ty_cost(self, ty: Ty<'tcx>) -> usize { @@ -846,7 +847,10 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { _ => false, } } - (GenericArgKind::Const(inner_ct), GenericArgKind::Const(target_ct)) => { + ( + GenericArgKind::Const(inner_ct, is_effect_a), + GenericArgKind::Const(target_ct, is_effect_b), + ) if is_effect_a == is_effect_b => { use ty::InferConst::*; match (inner_ct.kind(), target_ct.kind()) { (ty::ConstKind::Infer(Var(a_vid)), ty::ConstKind::Infer(Var(b_vid))) => self @@ -890,7 +894,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { walker.skip_current_subtree(); } } - GenericArgKind::Const(ct) => { + GenericArgKind::Const(ct, _) => { if matches!(ct.kind(), ty::ConstKind::Unevaluated(..)) { // You can't write the generic arguments for // unevaluated constants. diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 3a71251e73da4..589773ab34e7b 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -1237,15 +1237,14 @@ impl<'tcx> InferCtxt<'tcx> { val: ConstVariableValue::Unknown { universe: self.universe() }, }) .vid; - ty::Const::new_var( + ty::GenericArg::normal_const_arg(ty::Const::new_var( self.tcx, const_var_id, self.tcx .type_of(param.def_id) .no_bound_vars() .expect("const parameter types cannot be generic"), - ) - .into() + )) } } } @@ -1258,7 +1257,11 @@ impl<'tcx> InferCtxt<'tcx> { .no_bound_vars() .expect("const parameter types cannot be generic"); debug_assert_eq!(self.tcx.types.bool, ty); - ty::Const::new_infer(self.tcx, ty::InferConst::EffectVar(effect_vid), ty).into() + ty::GenericArg::effect_const_arg(ty::Const::new_infer( + self.tcx, + ty::InferConst::EffectVar(effect_vid), + ty, + )) } /// Given a set of generics defined on a type or impl, returns a substitution mapping each @@ -1528,15 +1531,13 @@ impl<'tcx> InferCtxt<'tcx> { self.map .entry(bv) .or_insert_with(|| { - self.infcx - .next_const_var( - ty, - ConstVariableOrigin { - kind: ConstVariableOriginKind::MiscVariable, - span: self.span, - }, - ) - .into() + ty::GenericArg::normal_const_arg(self.infcx.next_const_var( + ty, + ConstVariableOrigin { + kind: ConstVariableOriginKind::MiscVariable, + span: self.span, + }, + )) }) .expect_const() } @@ -1830,7 +1831,7 @@ impl<'tcx> TyOrConstInferVar { pub fn maybe_from_generic_arg(arg: GenericArg<'tcx>) -> Option { match arg.unpack() { GenericArgKind::Type(ty) => Self::maybe_from_ty(ty), - GenericArgKind::Const(ct) => Self::maybe_from_const(ct), + GenericArgKind::Const(ct, _) => Self::maybe_from_const(ct), GenericArgKind::Lifetime(_) => None, } } diff --git a/compiler/rustc_infer/src/infer/opaque_types.rs b/compiler/rustc_infer/src/infer/opaque_types.rs index 82ab19550536c..eca750e450a67 100644 --- a/compiler/rustc_infer/src/infer/opaque_types.rs +++ b/compiler/rustc_infer/src/infer/opaque_types.rs @@ -356,7 +356,7 @@ impl<'tcx> InferCtxt<'tcx> { .filter(|(i, _)| variances[*i] == ty::Variance::Invariant) .filter_map(|(_, arg)| match arg.unpack() { GenericArgKind::Lifetime(r) => Some(r), - GenericArgKind::Type(_) | GenericArgKind::Const(_) => None, + GenericArgKind::Type(_) | GenericArgKind::Const(_, _) => None, }) .chain(std::iter::once(self.tcx.lifetimes.re_static)) .collect(), diff --git a/compiler/rustc_infer/src/infer/outlives/components.rs b/compiler/rustc_infer/src/infer/outlives/components.rs index 45df22d44e810..635a38ca7d183 100644 --- a/compiler/rustc_infer/src/infer/outlives/components.rs +++ b/compiler/rustc_infer/src/infer/outlives/components.rs @@ -86,7 +86,7 @@ fn compute_components<'tcx>( compute_components(tcx, ty, out, visited); } GenericArgKind::Lifetime(_) => {} - GenericArgKind::Const(_) => { + GenericArgKind::Const(_, _) => { compute_components_recursive(tcx, child, out, visited); } } @@ -222,7 +222,7 @@ pub(super) fn compute_alias_components_recursive<'tcx>( out.push(Component::Region(lt)); } } - GenericArgKind::Const(_) => { + GenericArgKind::Const(_, _) => { compute_components_recursive(tcx, child, out, visited); } } @@ -250,7 +250,7 @@ fn compute_components_recursive<'tcx>( out.push(Component::Region(lt)); } } - GenericArgKind::Const(_) => { + GenericArgKind::Const(_, _) => { compute_components_recursive(tcx, child, out, visited); } } diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs index 395830d937b80..0eca1e3d66ebc 100644 --- a/compiler/rustc_infer/src/infer/outlives/obligations.rs +++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs @@ -444,7 +444,7 @@ where GenericArgKind::Type(ty) => { self.type_must_outlive(origin.clone(), ty, region, constraint); } - GenericArgKind::Const(_) => { + GenericArgKind::Const(_, _) => { // Const parameters don't impose constraints. } } diff --git a/compiler/rustc_middle/src/infer/canonical.rs b/compiler/rustc_middle/src/infer/canonical.rs index ef5a1caadb7bd..d4a21154bacf9 100644 --- a/compiler/rustc_middle/src/infer/canonical.rs +++ b/compiler/rustc_middle/src/infer/canonical.rs @@ -72,7 +72,7 @@ impl CanonicalVarValues<'_> { ty::GenericArgKind::Type(ty) => { matches!(*ty.kind(), ty::Bound(ty::INNERMOST, bt) if bt.var.as_usize() == bv) } - ty::GenericArgKind::Const(ct) => { + ty::GenericArgKind::Const(ct, _) => { matches!(ct.kind(), ty::ConstKind::Bound(ty::INNERMOST, bc) if bc.as_usize() == bv) } }) @@ -100,7 +100,7 @@ impl CanonicalVarValues<'_> { return false; } } - ty::GenericArgKind::Const(ct) => { + ty::GenericArgKind::Const(ct, _) => { if let ty::ConstKind::Bound(ty::INNERMOST, bc) = ct.kind() && var == bc { @@ -242,21 +242,23 @@ impl<'tcx> CanonicalVarValues<'tcx> { }; ty::Region::new_bound(tcx, ty::INNERMOST, br).into() } - CanonicalVarKind::Effect => ty::Const::new_bound( - tcx, - ty::INNERMOST, - ty::BoundVar::from_usize(i), - tcx.types.bool, - ) - .into(), + CanonicalVarKind::Effect => { + ty::GenericArg::effect_const_arg(ty::Const::new_bound( + tcx, + ty::INNERMOST, + ty::BoundVar::from_usize(i), + tcx.types.bool, + )) + } CanonicalVarKind::Const(_, ty) - | CanonicalVarKind::PlaceholderConst(_, ty) => ty::Const::new_bound( - tcx, - ty::INNERMOST, - ty::BoundVar::from_usize(i), - ty, - ) - .into(), + | CanonicalVarKind::PlaceholderConst(_, ty) => { + ty::GenericArg::normal_const_arg(ty::Const::new_bound( + tcx, + ty::INNERMOST, + ty::BoundVar::from_usize(i), + ty, + )) + } } }, )), diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index eb6fde83fcc8f..4498840df6ccb 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1826,14 +1826,16 @@ impl<'tcx> TyCtxt<'tcx> { ty::Region::new_early_param(self, param.to_early_bound_region_data()).into() } GenericParamDefKind::Type { .. } => Ty::new_param(self, param.index, param.name).into(), - GenericParamDefKind::Const { .. } => ty::Const::new_param( - self, - ParamConst { index: param.index, name: param.name }, - self.type_of(param.def_id) - .no_bound_vars() - .expect("const parameter types cannot be generic"), - ) - .into(), + GenericParamDefKind::Const { is_host_effect, .. } => GenericArg::new_const( + ty::Const::new_param( + self, + ParamConst { index: param.index, name: param.name }, + self.type_of(param.def_id) + .no_bound_vars() + .expect("const parameter types cannot be generic"), + ), + is_host_effect, + ), } } diff --git a/compiler/rustc_middle/src/ty/fast_reject.rs b/compiler/rustc_middle/src/ty/fast_reject.rs index f95ceeff1507a..15d07ce663410 100644 --- a/compiler/rustc_middle/src/ty/fast_reject.rs +++ b/compiler/rustc_middle/src/ty/fast_reject.rs @@ -201,9 +201,10 @@ impl DeepRejectCtxt { (GenericArgKind::Type(obl), GenericArgKind::Type(imp)) => { self.types_may_unify(obl, imp) } - (GenericArgKind::Const(obl), GenericArgKind::Const(imp)) => { - self.consts_may_unify(obl, imp) - } + ( + GenericArgKind::Const(obl, is_effect_a), + GenericArgKind::Const(imp, is_effect_b), + ) if is_effect_a == is_effect_b => self.consts_may_unify(obl, imp), _ => bug!("kind mismatch: {obl} {imp}"), } }) diff --git a/compiler/rustc_middle/src/ty/flags.rs b/compiler/rustc_middle/src/ty/flags.rs index f9a2385b10005..595210dd619a1 100644 --- a/compiler/rustc_middle/src/ty/flags.rs +++ b/compiler/rustc_middle/src/ty/flags.rs @@ -375,7 +375,7 @@ impl FlagComputation { match kind.unpack() { GenericArgKind::Type(ty) => self.add_ty(ty), GenericArgKind::Lifetime(lt) => self.add_region(lt), - GenericArgKind::Const(ct) => self.add_const(ct), + GenericArgKind::Const(ct, _) => self.add_const(ct), } } } diff --git a/compiler/rustc_middle/src/ty/generic_args.rs b/compiler/rustc_middle/src/ty/generic_args.rs index fa68923b2c126..819293355a055 100644 --- a/compiler/rustc_middle/src/ty/generic_args.rs +++ b/compiler/rustc_middle/src/ty/generic_args.rs @@ -45,12 +45,13 @@ const TAG_MASK: usize = 0b11; const TYPE_TAG: usize = 0b00; const REGION_TAG: usize = 0b01; const CONST_TAG: usize = 0b10; +const CONST_EFFECT_TAG: usize = 0b11; #[derive(Debug, TyEncodable, TyDecodable, PartialEq, Eq, PartialOrd, Ord, HashStable)] pub enum GenericArgKind<'tcx> { Lifetime(ty::Region<'tcx>), Type(Ty<'tcx>), - Const(ty::Const<'tcx>), + Const(ty::Const<'tcx>, /* is_effect */ bool), } impl<'tcx> GenericArgKind<'tcx> { @@ -67,11 +68,19 @@ impl<'tcx> GenericArgKind<'tcx> { assert_eq!(mem::align_of_val(&*ty.0.0) & TAG_MASK, 0); (TYPE_TAG, ty.0.0 as *const WithCachedTypeInfo> as usize) } - GenericArgKind::Const(ct) => { + GenericArgKind::Const(ct, false) => { // Ensure we can use the tag bits. assert_eq!(mem::align_of_val(&*ct.0.0) & TAG_MASK, 0); (CONST_TAG, ct.0.0 as *const WithCachedTypeInfo> as usize) } + GenericArgKind::Const(ct, true) => { + // Ensure we can use the tag bits. + assert_eq!(mem::align_of_val(&*ct.0.0) & TAG_MASK, 0); + ( + CONST_EFFECT_TAG, + ct.0.0 as *const WithCachedTypeInfo> as usize, + ) + } }; GenericArg { ptr: unsafe { NonZeroUsize::new_unchecked(ptr | tag) }, marker: PhantomData } @@ -104,22 +113,6 @@ impl<'tcx> From> for GenericArg<'tcx> { } } -impl<'tcx> From> for GenericArg<'tcx> { - #[inline] - fn from(c: ty::Const<'tcx>) -> GenericArg<'tcx> { - GenericArgKind::Const(c).pack() - } -} - -impl<'tcx> From> for GenericArg<'tcx> { - fn from(value: ty::Term<'tcx>) -> Self { - match value.unpack() { - ty::TermKind::Ty(t) => t.into(), - ty::TermKind::Const(c) => c.into(), - } - } -} - impl<'tcx> GenericArg<'tcx> { #[inline] pub fn unpack(self) -> GenericArgKind<'tcx> { @@ -135,14 +128,35 @@ impl<'tcx> GenericArg<'tcx> { TYPE_TAG => GenericArgKind::Type(Ty(Interned::new_unchecked( &*((ptr & !TAG_MASK) as *const WithCachedTypeInfo>), ))), - CONST_TAG => GenericArgKind::Const(ty::Const(Interned::new_unchecked( - &*((ptr & !TAG_MASK) as *const WithCachedTypeInfo>), - ))), + CONST_TAG => GenericArgKind::Const( + ty::Const(Interned::new_unchecked( + &*((ptr & !TAG_MASK) as *const WithCachedTypeInfo>), + )), + false, + ), + CONST_EFFECT_TAG => GenericArgKind::Const( + ty::Const(Interned::new_unchecked( + &*((ptr & !TAG_MASK) as *const WithCachedTypeInfo>), + )), + true, + ), _ => intrinsics::unreachable(), } } } + pub fn new_const(ct: ty::Const<'tcx>, is_effect: bool) -> GenericArg<'tcx> { + GenericArgKind::Const(ct, is_effect).pack() + } + + pub fn normal_const_arg(ct: ty::Const<'tcx>) -> GenericArg<'tcx> { + Self::new_const(ct, false) + } + + pub fn effect_const_arg(ct: ty::Const<'tcx>) -> GenericArg<'tcx> { + Self::new_const(ct, true) + } + #[inline] pub fn as_type(self) -> Option> { match self.unpack() { @@ -162,7 +176,7 @@ impl<'tcx> GenericArg<'tcx> { #[inline] pub fn as_const(self) -> Option> { match self.unpack() { - GenericArgKind::Const(ct) => Some(ct), + GenericArgKind::Const(ct, _) => Some(ct), _ => None, } } @@ -188,7 +202,7 @@ impl<'tcx> GenericArg<'tcx> { match self.unpack() { GenericArgKind::Lifetime(_) => false, GenericArgKind::Type(ty) => ty.is_ty_or_numeric_infer(), - GenericArgKind::Const(ct) => ct.is_ct_infer(), + GenericArgKind::Const(ct, _) => ct.is_ct_infer(), } } } @@ -200,7 +214,9 @@ impl<'a, 'tcx> Lift<'tcx> for GenericArg<'a> { match self.unpack() { GenericArgKind::Lifetime(lt) => tcx.lift(lt).map(|lt| lt.into()), GenericArgKind::Type(ty) => tcx.lift(ty).map(|ty| ty.into()), - GenericArgKind::Const(ct) => tcx.lift(ct).map(|ct| ct.into()), + GenericArgKind::Const(ct, is_effect) => { + tcx.lift(ct).map(|ct| GenericArgKind::Const(ct, is_effect).pack()) + } } } } @@ -213,7 +229,9 @@ impl<'tcx> TypeFoldable> for GenericArg<'tcx> { match self.unpack() { GenericArgKind::Lifetime(lt) => lt.try_fold_with(folder).map(Into::into), GenericArgKind::Type(ty) => ty.try_fold_with(folder).map(Into::into), - GenericArgKind::Const(ct) => ct.try_fold_with(folder).map(Into::into), + GenericArgKind::Const(ct, is_effect) => { + ct.try_fold_with(folder).map(|ct| GenericArg::new_const(ct, is_effect)) + } } } } @@ -223,7 +241,7 @@ impl<'tcx> TypeVisitable> for GenericArg<'tcx> { match self.unpack() { GenericArgKind::Lifetime(lt) => lt.visit_with(visitor), GenericArgKind::Type(ty) => ty.visit_with(visitor), - GenericArgKind::Const(ct) => ct.visit_with(visitor), + GenericArgKind::Const(ct, _) => ct.visit_with(visitor), } } } @@ -379,6 +397,7 @@ impl<'tcx> GenericArgs<'tcx> { } /// Returns generic arguments that are not lifetimes or host effect params. + // FIXME(effects) make this more efficient with the new `GenericArg` addition #[inline] pub fn non_erasable_generics( &'tcx self, @@ -908,7 +927,7 @@ impl<'a, 'tcx> ArgFolder<'a, 'tcx> { // Look up the const in the args. It really should be in there. let opt_ct = self.args.get(p.index as usize).map(|k| k.unpack()); let ct = match opt_ct { - Some(GenericArgKind::Const(ct)) => ct, + Some(GenericArgKind::Const(ct, _)) => ct, Some(kind) => self.const_param_expected(p, source_ct, kind), None => self.const_param_out_of_range(p, source_ct), }; diff --git a/compiler/rustc_middle/src/ty/generics.rs b/compiler/rustc_middle/src/ty/generics.rs index 8316f9c30587f..2fde492057ec8 100644 --- a/compiler/rustc_middle/src/ty/generics.rs +++ b/compiler/rustc_middle/src/ty/generics.rs @@ -8,7 +8,7 @@ use rustc_span::Span; use super::{Clause, InstantiatedPredicates, ParamConst, ParamTy, Ty, TyCtxt}; -#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)] +#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)] pub enum GenericParamDefKind { Lifetime, Type { has_default: bool, synthetic: bool }, @@ -91,9 +91,10 @@ impl GenericParamDef { GenericParamDefKind::Type { has_default, .. } if has_default => { Some(tcx.type_of(self.def_id).map_bound(|t| t.into())) } - GenericParamDefKind::Const { has_default, .. } if has_default => { - Some(tcx.const_param_default(self.def_id).map_bound(|c| c.into())) - } + GenericParamDefKind::Const { has_default, is_host_effect } if has_default => Some( + tcx.const_param_default(self.def_id) + .map_bound(|c| ty::GenericArg::new_const(c, is_host_effect)), + ), _ => None, } } @@ -106,11 +107,13 @@ impl GenericParamDef { match &self.kind { ty::GenericParamDefKind::Lifetime => ty::Region::new_error_misc(tcx).into(), ty::GenericParamDefKind::Type { .. } => Ty::new_misc_error(tcx).into(), - ty::GenericParamDefKind::Const { .. } => ty::Const::new_misc_error( - tcx, - tcx.type_of(self.def_id).instantiate(tcx, preceding_args), - ) - .into(), + ty::GenericParamDefKind::Const { is_host_effect, .. } => ty::GenericArg::new_const( + ty::Const::new_misc_error( + tcx, + tcx.type_of(self.def_id).instantiate(tcx, preceding_args), + ), + *is_host_effect, + ), } } } diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index 1c7a7545e2b0e..93c20dc015396 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -346,7 +346,9 @@ impl<'tcx> Instance<'tcx> { pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> { let args = GenericArgs::for_item(tcx, def_id, |param, _| match param.kind { ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(), - ty::GenericParamDefKind::Const { is_host_effect: true, .. } => tcx.consts.true_.into(), + ty::GenericParamDefKind::Const { is_host_effect: true, .. } => { + ty::GenericArg::effect_const_arg(tcx.consts.true_) + } ty::GenericParamDefKind::Type { .. } => { bug!("Instance::mono: {:?} has type parameters", def_id) } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 71ff7021ca5de..0a4723b9980a1 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -934,13 +934,6 @@ impl<'tcx> Term<'tcx> { if let TermKind::Const(c) = self.unpack() { Some(c) } else { None } } - pub fn into_arg(self) -> GenericArg<'tcx> { - match self.unpack() { - TermKind::Ty(ty) => ty.into(), - TermKind::Const(c) => c.into(), - } - } - /// This function returns the inner `AliasTy` for a `ty::Alias` or `ConstKind::Unevaluated`. pub fn to_alias_ty(&self, tcx: TyCtxt<'tcx>) -> Option> { match self.unpack() { diff --git a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs index 27c436c82f53e..e2672e68cac80 100644 --- a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs +++ b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs @@ -201,7 +201,8 @@ impl<'tcx> TypeFolder> for NormalizeAfterErasingRegionsFolder<'tcx> } fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> { - self.normalize_generic_arg_after_erasing_regions(c.into()).expect_const() + self.normalize_generic_arg_after_erasing_regions(ty::GenericArg::normal_const_arg(c)) + .expect_const() } } @@ -242,7 +243,9 @@ impl<'tcx> FallibleTypeFolder> for TryNormalizeAfterErasingRegionsF } fn try_fold_const(&mut self, c: ty::Const<'tcx>) -> Result, Self::Error> { - match self.try_normalize_generic_arg_after_erasing_regions(c.into()) { + match self + .try_normalize_generic_arg_after_erasing_regions(ty::GenericArg::normal_const_arg(c)) + { Ok(t) => Ok(t.expect_const()), Err(_) => Err(NormalizationError::Const(c)), } diff --git a/compiler/rustc_middle/src/ty/opaque_types.rs b/compiler/rustc_middle/src/ty/opaque_types.rs index 1305f63bdbc2f..97eca91a3713b 100644 --- a/compiler/rustc_middle/src/ty/opaque_types.rs +++ b/compiler/rustc_middle/src/ty/opaque_types.rs @@ -200,10 +200,10 @@ impl<'tcx> TypeFolder> for ReverseMapper<'tcx> { match ct.kind() { ty::ConstKind::Param(..) => { // Look it up in the substitution list. - match self.map.get(&ct.into()).map(|k| k.unpack()) { + match self.map.get(&GenericArg::normal_const_arg(ct)).map(|k| k.unpack()) { // Found it in the substitution list, replace with the parameter from the // opaque type. - Some(GenericArgKind::Const(c1)) => c1, + Some(GenericArgKind::Const(c1, _)) => c1, Some(u) => panic!("const mapped to unexpected kind: {u:?}"), None => { let guar = self diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index bd9f0fd3ea9c5..45be3bc248ac2 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -19,7 +19,6 @@ use rustc_hir::LangItem; use rustc_session::config::TrimmedDefPaths; use rustc_session::cstore::{ExternCrate, ExternCrateSource}; use rustc_session::Limit; -use rustc_span::sym; use rustc_span::symbol::{kw, Ident, Symbol}; use rustc_span::FileNameDisplayPreference; use rustc_target::abi::Size; @@ -2040,20 +2039,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { let args: Vec<_> = if !tcx.sess.verbose() { // skip host param as those are printed as `~const` - args.filter(|arg| match arg.unpack() { - // FIXME(effects) there should be a better way than just matching the name - GenericArgKind::Const(c) - if tcx.features().effects - && matches!( - c.kind(), - ty::ConstKind::Param(ty::ParamConst { name: sym::host, .. }) - ) => - { - false - } - _ => true, - }) - .collect() + args.filter(|arg| !matches!(arg.unpack(), GenericArgKind::Const(_, true))).collect() } else { // If -Zverbose is passed, we should print the host parameter instead // of eating it. @@ -2983,7 +2969,8 @@ define_print_and_forward_display! { match self.unpack() { GenericArgKind::Lifetime(lt) => p!(print(lt)), GenericArgKind::Type(ty) => p!(print(ty)), - GenericArgKind::Const(ct) => p!(print(ct)), + // FIXME(effects) + GenericArgKind::Const(ct, _) => p!(print(ct)), } } } diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index d7d9afc30e7ea..dfb73dcd595cd 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -774,8 +774,11 @@ impl<'tcx> Relate<'tcx> for GenericArg<'tcx> { (GenericArgKind::Type(a_ty), GenericArgKind::Type(b_ty)) => { Ok(relation.relate(a_ty, b_ty)?.into()) } - (GenericArgKind::Const(a_ct), GenericArgKind::Const(b_ct)) => { - Ok(relation.relate(a_ct, b_ct)?.into()) + ( + GenericArgKind::Const(a_ct, is_effect_a), + GenericArgKind::Const(b_ct, is_effect_b), + ) if is_effect_a == is_effect_b => { + Ok(GenericArg::new_const(relation.relate(a_ct, b_ct)?, is_effect_a)) } (GenericArgKind::Lifetime(unpacked), x) => { bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x) @@ -783,7 +786,7 @@ impl<'tcx> Relate<'tcx> for GenericArg<'tcx> { (GenericArgKind::Type(unpacked), x) => { bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x) } - (GenericArgKind::Const(unpacked), x) => { + (GenericArgKind::Const(unpacked, _), x) => { bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x) } } diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 0cff6b77eb606..99f83efb0c94b 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -316,7 +316,7 @@ impl<'tcx> fmt::Debug for GenericArg<'tcx> { match self.unpack() { GenericArgKind::Lifetime(lt) => lt.fmt(f), GenericArgKind::Type(ty) => ty.fmt(f), - GenericArgKind::Const(ct) => ct.fmt(f), + GenericArgKind::Const(ct, _) => ct.fmt(f), } } } @@ -327,7 +327,9 @@ impl<'tcx> DebugWithInfcx> for GenericArg<'tcx> { ) -> core::fmt::Result { match this.data.unpack() { GenericArgKind::Lifetime(lt) => write!(f, "{:?}", &this.wrap(lt)), - GenericArgKind::Const(ct) => write!(f, "{:?}", &this.wrap(ct)), + GenericArgKind::Const(ct, is_effect) => { + write!(f, "{:?}{}", &this.wrap(ct), if is_effect { " (effect arg)" } else { "" }) + } GenericArgKind::Type(ty) => write!(f, "{:?}", &this.wrap(ty)), } } diff --git a/compiler/rustc_middle/src/ty/typeck_results.rs b/compiler/rustc_middle/src/ty/typeck_results.rs index d372c1cd60496..2caef955238b7 100644 --- a/compiler/rustc_middle/src/ty/typeck_results.rs +++ b/compiler/rustc_middle/src/ty/typeck_results.rs @@ -647,7 +647,7 @@ impl<'tcx> IsIdentity for CanonicalUserType<'tcx> { _ => false, }, - GenericArgKind::Const(ct) => match ct.kind() { + GenericArgKind::Const(ct, _) => match ct.kind() { ty::ConstKind::Bound(debruijn, b) => { // We only allow a `ty::INNERMOST` index in substitutions. assert_eq!(debruijn, ty::INNERMOST); diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index b7c3edee9e59e..64e784c04d53e 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -444,7 +444,7 @@ impl<'tcx> TyCtxt<'tcx> { // Error: not a type param _ => false, }, - GenericArgKind::Const(ct) => match ct.kind() { + GenericArgKind::Const(ct, _) => match ct.kind() { ty::ConstKind::Param(ref pc) => { !impl_generics.const_param(pc, self).pure_wrt_drop } @@ -467,39 +467,39 @@ impl<'tcx> TyCtxt<'tcx> { ) -> Result<(), NotUniqueParam<'tcx>> { let mut seen = GrowableBitSet::default(); let mut seen_late = FxHashSet::default(); - for arg in args { + for &arg in args { match arg.unpack() { GenericArgKind::Lifetime(lt) => match (ignore_regions, lt.kind()) { (CheckRegions::FromFunction, ty::ReBound(di, reg)) => { if !seen_late.insert((di, reg)) { - return Err(NotUniqueParam::DuplicateParam(lt.into())); + return Err(NotUniqueParam::DuplicateParam(arg)); } } (CheckRegions::OnlyParam | CheckRegions::FromFunction, ty::ReEarlyParam(p)) => { if !seen.insert(p.index) { - return Err(NotUniqueParam::DuplicateParam(lt.into())); + return Err(NotUniqueParam::DuplicateParam(arg)); } } (CheckRegions::OnlyParam | CheckRegions::FromFunction, _) => { - return Err(NotUniqueParam::NotParam(lt.into())); + return Err(NotUniqueParam::NotParam(arg)); } (CheckRegions::No, _) => {} }, GenericArgKind::Type(t) => match t.kind() { ty::Param(p) => { if !seen.insert(p.index) { - return Err(NotUniqueParam::DuplicateParam(t.into())); + return Err(NotUniqueParam::DuplicateParam(arg)); } } - _ => return Err(NotUniqueParam::NotParam(t.into())), + _ => return Err(NotUniqueParam::NotParam(arg)), }, - GenericArgKind::Const(c) => match c.kind() { + GenericArgKind::Const(c, _) => match c.kind() { ty::ConstKind::Param(p) => { if !seen.insert(p.index) { - return Err(NotUniqueParam::DuplicateParam(c.into())); + return Err(NotUniqueParam::DuplicateParam(arg)); } } - _ => return Err(NotUniqueParam::NotParam(c.into())), + _ => return Err(NotUniqueParam::NotParam(arg)), }, } } @@ -524,18 +524,18 @@ impl<'tcx> TyCtxt<'tcx> { GenericArgKind::Type(t) => match t.kind() { ty::Placeholder(p) => { if !seen.insert(p.bound.var) { - return Err(NotUniqueParam::DuplicateParam(t.into())); + return Err(NotUniqueParam::DuplicateParam(arg)); } } - _ => return Err(NotUniqueParam::NotParam(t.into())), + _ => return Err(NotUniqueParam::NotParam(arg)), }, - GenericArgKind::Const(c) => match c.kind() { + GenericArgKind::Const(c, _) => match c.kind() { ty::ConstKind::Placeholder(p) => { if !seen.insert(p.bound) { - return Err(NotUniqueParam::DuplicateParam(c.into())); + return Err(NotUniqueParam::DuplicateParam(arg)); } } - _ => return Err(NotUniqueParam::NotParam(c.into())), + _ => return Err(NotUniqueParam::NotParam(arg)), }, } } @@ -827,7 +827,9 @@ impl<'tcx> TyCtxt<'tcx> { assert_eq!(generics.parent, None); let opt_const_param = generics.host_effect_index.is_some().then(|| { - ty::GenericArg::from(self.expected_const_effect_param_for_body(caller_def_id)) + ty::GenericArg::effect_const_arg( + self.expected_const_effect_param_for_body(caller_def_id), + ) }); self.mk_args_from_iter(args.into_iter().map(|arg| arg.into()).chain(opt_const_param)) diff --git a/compiler/rustc_middle/src/ty/walk.rs b/compiler/rustc_middle/src/ty/walk.rs index 20bdbcb5b7bb4..4103aa01440cf 100644 --- a/compiler/rustc_middle/src/ty/walk.rs +++ b/compiler/rustc_middle/src/ty/walk.rs @@ -124,7 +124,7 @@ impl<'tcx> ty::Const<'tcx> { /// [isize] => { [isize], isize } /// ``` pub fn walk(self) -> TypeWalker<'tcx> { - TypeWalker::new(self.into()) + TypeWalker::new(GenericArg::normal_const_arg(self)) } } @@ -152,7 +152,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>) | ty::Foreign(..) => {} ty::Array(ty, len) => { - stack.push(len.into()); + stack.push(GenericArg::normal_const_arg(len)); stack.push(ty.into()); } ty::Slice(ty) => { @@ -183,7 +183,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>) args.iter().rev().chain(opt_ty.map(|term| match term.unpack() { ty::TermKind::Ty(ty) => ty.into(), - ty::TermKind::Const(ct) => ct.into(), + ty::TermKind::Const(ct) => ty::GenericArg::normal_const_arg(ct), })) })); } @@ -201,7 +201,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>) } }, GenericArgKind::Lifetime(_) => {} - GenericArgKind::Const(parent_ct) => { + GenericArgKind::Const(parent_ct, is_effect) => { stack.push(parent_ct.ty().into()); match parent_ct.kind() { ty::ConstKind::Infer(_) @@ -212,20 +212,22 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>) | ty::ConstKind::Error(_) => {} ty::ConstKind::Expr(expr) => match expr { - ty::Expr::UnOp(_, v) => push_inner(stack, v.into()), + ty::Expr::UnOp(_, v) => { + push_inner(stack, ty::GenericArg::new_const(v, is_effect)) + } ty::Expr::Binop(_, l, r) => { - push_inner(stack, r.into()); - push_inner(stack, l.into()) + push_inner(stack, ty::GenericArg::new_const(r, is_effect)); + push_inner(stack, ty::GenericArg::new_const(l, is_effect)) } ty::Expr::FunctionCall(func, args) => { for a in args.iter().rev() { - push_inner(stack, a.into()); + push_inner(stack, ty::GenericArg::new_const(a, is_effect)); } - push_inner(stack, func.into()); + push_inner(stack, ty::GenericArg::new_const(func, is_effect)); } ty::Expr::Cast(_, c, t) => { push_inner(stack, t.into()); - push_inner(stack, c.into()); + push_inner(stack, ty::GenericArg::new_const(c, is_effect)); } }, diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 71b3754fac870..2890bfbda35ec 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -561,8 +561,8 @@ fn check_type_length_limit<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) { .iter() .flat_map(|arg| arg.walk()) .filter(|arg| match arg.unpack() { - GenericArgKind::Type(_) | GenericArgKind::Const(_) => true, - GenericArgKind::Lifetime(_) => false, + GenericArgKind::Type(_) | GenericArgKind::Const(_, false) => true, + GenericArgKind::Lifetime(_) | GenericArgKind::Const(_, true) => false, }) .count(); debug!(" => type length={}", type_length); @@ -1305,7 +1305,9 @@ fn create_mono_items_for_default_impls<'tcx>( // it, to validate whether or not the impl is legal to instantiate at all. let only_region_params = |param: &ty::GenericParamDef, _: &_| match param.kind { GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(), - GenericParamDefKind::Const { is_host_effect: true, .. } => tcx.consts.true_.into(), + GenericParamDefKind::Const { is_host_effect: true, .. } => { + ty::GenericArg::effect_const_arg(tcx.consts.true_) + } GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => { unreachable!( "`own_requires_monomorphization` check means that \ diff --git a/compiler/rustc_next_trait_solver/src/canonicalizer.rs b/compiler/rustc_next_trait_solver/src/canonicalizer.rs index cb1f328577d02..1d44d2a2c1fa3 100644 --- a/compiler/rustc_next_trait_solver/src/canonicalizer.rs +++ b/compiler/rustc_next_trait_solver/src/canonicalizer.rs @@ -409,9 +409,9 @@ impl, I: Interner> TypeFolder }; let var = ty::BoundVar::from( - self.variables.iter().position(|&v| v == c.into()).unwrap_or_else(|| { + self.variables.iter().position(|&v| v.as_const() == Some(c)).unwrap_or_else(|| { let var = self.variables.len(); - self.variables.push(c.into()); + self.variables.push(ty::GenericArg::new_const(c, kind == CanonicalVarKind::Effect)); self.primitive_var_infos.push(CanonicalVarInfo { kind }); var }), diff --git a/compiler/rustc_smir/src/rustc_internal/internal.rs b/compiler/rustc_smir/src/rustc_internal/internal.rs index 632072003539f..60ba88d59f297 100644 --- a/compiler/rustc_smir/src/rustc_internal/internal.rs +++ b/compiler/rustc_smir/src/rustc_internal/internal.rs @@ -47,7 +47,9 @@ impl<'tcx> RustcInternal<'tcx> for GenericArgKind { match self { GenericArgKind::Lifetime(reg) => reg.internal(tables).into(), GenericArgKind::Type(ty) => ty.internal(tables).into(), - GenericArgKind::Const(cnst) => ty_const(cnst, tables).into(), + GenericArgKind::Const(cnst) => { + rustc_ty::GenericArg::normal_const_arg(ty_const(cnst, tables)) + } } } } diff --git a/compiler/rustc_smir/src/rustc_smir/convert/ty.rs b/compiler/rustc_smir/src/rustc_smir/convert/ty.rs index cbdddc3007273..b8c7371ec1df5 100644 --- a/compiler/rustc_smir/src/rustc_smir/convert/ty.rs +++ b/compiler/rustc_smir/src/rustc_smir/convert/ty.rs @@ -163,7 +163,7 @@ impl<'tcx> Stable<'tcx> for ty::GenericArgKind<'tcx> { match self { ty::GenericArgKind::Lifetime(region) => GenericArgKind::Lifetime(region.stable(tables)), ty::GenericArgKind::Type(ty) => GenericArgKind::Type(ty.stable(tables)), - ty::GenericArgKind::Const(cnst) => GenericArgKind::Const(cnst.stable(tables)), + ty::GenericArgKind::Const(cnst, _) => GenericArgKind::Const(cnst.stable(tables)), } } } diff --git a/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs b/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs index e49d134659ec6..bb88f353f5901 100644 --- a/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs +++ b/compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs @@ -319,7 +319,7 @@ fn encode_args<'tcx>( GenericArgKind::Type(ty) => { s.push_str(&encode_ty(tcx, ty, dict, options)); } - GenericArgKind::Const(c) => { + GenericArgKind::Const(c, _) => { s.push_str(&encode_const(tcx, c, dict, options)); } } diff --git a/compiler/rustc_symbol_mangling/src/v0.rs b/compiler/rustc_symbol_mangling/src/v0.rs index e002e345ae689..47919b753fe4f 100644 --- a/compiler/rustc_symbol_mangling/src/v0.rs +++ b/compiler/rustc_symbol_mangling/src/v0.rs @@ -808,6 +808,8 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> { }); let args = args.iter().cloned().filter(|arg| match arg.unpack() { GenericArgKind::Lifetime(_) => print_regions, + // don't print effect parameters + GenericArgKind::Const(_, true) => false, _ => true, }); @@ -825,7 +827,7 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> { GenericArgKind::Type(ty) => { ty.print(self)?; } - GenericArgKind::Const(c) => { + GenericArgKind::Const(c, _) => { self.push("K"); c.print(self)?; } diff --git a/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs b/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs index 7457ba837f505..9c018dc96a304 100644 --- a/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs +++ b/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs @@ -265,7 +265,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { opt_values[br.var] = Some(*original_value); } } - GenericArgKind::Const(c) => { + GenericArgKind::Const(c, _) => { if let ty::ConstKind::Bound(debruijn, b) = c.kind() { assert_eq!(debruijn, ty::INNERMOST); opt_values[b] = Some(*original_value); @@ -340,7 +340,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { match lhs.unpack() { GenericArgKind::Lifetime(lhs) => self.register_region_outlives(lhs, rhs), GenericArgKind::Type(lhs) => self.register_ty_outlives(lhs, rhs), - GenericArgKind::Const(_) => bug!("const outlives: {lhs:?}: {rhs:?}"), + GenericArgKind::Const(_, _) => bug!("const outlives: {lhs:?}: {rhs:?}"), } } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs index 8fa0dceda8742..cba9756a932f8 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs @@ -2611,7 +2611,11 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { .projection_ty .args .iter() - .chain(Some(data.term.into_arg())) + .chain(Some(match data.term.unpack() { + ty::TermKind::Ty(ty) => ty.into(), + // FIXME(effects) is this correct + ty::TermKind::Const(ct) => ty::GenericArg::normal_const_arg(ct), + })) .find(|g| g.has_non_region_infer()); if let Some(subst) = subst { let mut err = self.emit_inference_failure_err( diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs index f9b8ea32d8926..bc19d64de7e26 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/implied_outlives_bounds.rs @@ -184,7 +184,7 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>( push_outlives_components(tcx, ty_a, &mut components); implied_bounds.extend(implied_bounds_from_components(r_b, components)) } - ty::GenericArgKind::Const(_) => unreachable!(), + ty::GenericArgKind::Const(_, _) => unreachable!(), } } diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 4a342a7f6b146..b3bf6f222490e 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -304,7 +304,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { ty::GenericArg::from(to_ty), ty::GenericArg::from(from_ty), ty::GenericArg::from(scope), - ty::GenericArg::from(assume_const), + ty::GenericArg::normal_const_arg(assume_const), ], ); Obligation::with_depth( @@ -608,18 +608,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { ) .into() } - GenericParamDefKind::Const { .. } => { + GenericParamDefKind::Const { is_host_effect, .. } => { let bound_var = ty::BoundVariableKind::Const; bound_vars.push(bound_var); - ty::Const::new_bound( - tcx, - ty::INNERMOST, - ty::BoundVar::from_usize(bound_vars.len() - 1), - tcx.type_of(param.def_id) - .no_bound_vars() - .expect("const parameter types cannot be generic"), + ty::GenericArg::new_const( + ty::Const::new_bound( + tcx, + ty::INNERMOST, + ty::BoundVar::from_usize(bound_vars.len() - 1), + tcx.type_of(param.def_id) + .no_bound_vars() + .expect("const parameter types cannot be generic"), + ), + is_host_effect, ) - .into() } }); let bound_vars = tcx.mk_bound_variable_kinds(&bound_vars); diff --git a/compiler/rustc_trait_selection/src/traits/util.rs b/compiler/rustc_trait_selection/src/traits/util.rs index 98da3bc2fe9fa..4b2cae4c9bd11 100644 --- a/compiler/rustc_trait_selection/src/traits/util.rs +++ b/compiler/rustc_trait_selection/src/traits/util.rs @@ -354,10 +354,13 @@ pub fn check_args_compatible<'tcx>( } for (param, arg) in std::iter::zip(&generics.params, own_args) { - match (¶m.kind, arg.unpack()) { + match (param.kind, arg.unpack()) { (ty::GenericParamDefKind::Type { .. }, ty::GenericArgKind::Type(_)) - | (ty::GenericParamDefKind::Lifetime, ty::GenericArgKind::Lifetime(_)) - | (ty::GenericParamDefKind::Const { .. }, ty::GenericArgKind::Const(_)) => {} + | (ty::GenericParamDefKind::Lifetime, ty::GenericArgKind::Lifetime(_)) => {} + ( + ty::GenericParamDefKind::Const { is_host_effect, .. }, + ty::GenericArgKind::Const(_, is_effect), + ) if is_host_effect == is_effect => {} _ => return false, } } diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index 0f8d9c6bf4bdb..5be05e07b6f05 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -39,20 +39,22 @@ pub fn obligations<'tcx>( } .into() } - GenericArgKind::Const(ct) => { - match ct.kind() { - ty::ConstKind::Infer(_) => { - let resolved = infcx.shallow_resolve(ct); - if resolved == ct { - // No progress. - return None; - } else { - resolved + GenericArgKind::Const(ct, is_effect) => { + ty::GenericArg::new_const( + match ct.kind() { + ty::ConstKind::Infer(_) => { + let resolved = infcx.shallow_resolve(ct); + if resolved == ct { + // No progress. + return None; + } else { + resolved + } } - } - _ => ct, - } - .into() + _ => ct, + }, + is_effect, + ) } // There is nothing we have to do for lifetimes. GenericArgKind::Lifetime(..) => return Some(Vec::new()), @@ -165,11 +167,11 @@ pub fn clause_obligations<'tcx>( wf.compute_projection(t.projection_ty); wf.compute(match t.term.unpack() { ty::TermKind::Ty(ty) => ty.into(), - ty::TermKind::Const(c) => c.into(), + ty::TermKind::Const(c) => ty::GenericArg::normal_const_arg(c), }) } ty::ClauseKind::ConstArgHasType(ct, ty) => { - wf.compute(ct.into()); + wf.compute(ty::GenericArg::normal_const_arg(ct)); wf.compute(ty.into()); } ty::ClauseKind::WellFormed(arg) => { @@ -177,7 +179,7 @@ pub fn clause_obligations<'tcx>( } ty::ClauseKind::ConstEvaluatable(ct) => { - wf.compute(ct.into()); + wf.compute(ty::GenericArg::normal_const_arg(ct)); } } @@ -522,7 +524,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { // obligations are handled by the parent (e.g. `ty::Ref`). GenericArgKind::Lifetime(_) => continue, - GenericArgKind::Const(ct) => { + GenericArgKind::Const(ct, _) => { match ct.kind() { ty::ConstKind::Unevaluated(uv) => { if !ct.has_escaping_bound_vars() { @@ -551,7 +553,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> { self.recursion_depth, self.param_env, ty::Binder::dummy(ty::PredicateKind::Clause( - ty::ClauseKind::WellFormed(ct.into()), + ty::ClauseKind::WellFormed(arg), )), )); } diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 56e84b6015d46..c33dd43f42436 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -320,7 +320,7 @@ fn unsizing_params_for_adt<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> BitSet None, - ty::GenericArgKind::Const(ct) => match ct.kind() { + ty::GenericArgKind::Const(ct, _) => match ct.kind() { ty::ConstKind::Param(p) => Some(p.index), _ => None, }, diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 82f4a38384dd1..de4cf0686b75e 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -123,12 +123,16 @@ pub(crate) fn ty_args_to_args<'tcx>( }), ))) } - GenericArgKind::Const(ct) => { + GenericArgKind::Const(ct, is_effect) => { if let ty::GenericParamDefKind::Const { is_host_effect: true, .. } = params[index].kind { return None; } + if is_effect { + return None; + } + if !elision_has_failed_once_before && let Some(default) = params[index].default_value(cx.tcx) { diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/call-generic-method-nonconst.rs b/tests/ui/rfcs/rfc-2632-const-trait-impl/call-generic-method-nonconst.rs index b9331caaf8efb..76bc738123d39 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/call-generic-method-nonconst.rs +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/call-generic-method-nonconst.rs @@ -21,7 +21,6 @@ const fn equals_self(t: &T) -> bool { // it not using the impl. pub const EQ: bool = equals_self(&S); -//~^ ERROR -// FIXME(effects) the diagnostics here isn't ideal, we shouldn't get `` +//~^ ERROR: the trait bound `S: ~const Foo` is not satisfied fn main() {} diff --git a/tests/ui/rfcs/rfc-2632-const-trait-impl/call-generic-method-nonconst.stderr b/tests/ui/rfcs/rfc-2632-const-trait-impl/call-generic-method-nonconst.stderr index 4fe296d4d3fa9..a2ff4dd6e77b5 100644 --- a/tests/ui/rfcs/rfc-2632-const-trait-impl/call-generic-method-nonconst.stderr +++ b/tests/ui/rfcs/rfc-2632-const-trait-impl/call-generic-method-nonconst.stderr @@ -1,8 +1,8 @@ -error[E0277]: the trait bound `S: ~const Foo` is not satisfied +error[E0277]: the trait bound `S: ~const Foo` is not satisfied --> $DIR/call-generic-method-nonconst.rs:23:34 | LL | pub const EQ: bool = equals_self(&S); - | ----------- ^^ the trait `Foo` is not implemented for `S` + | ----------- ^^ the trait `Foo` is not implemented for `S` | | | required by a bound introduced by this call | From 7a3559d962c2115308d948e2fd9982e5ba20a7e3 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sat, 9 Dec 2023 16:30:46 +0000 Subject: [PATCH 2/3] fix clippy --- src/tools/clippy/clippy_lints/src/derive.rs | 2 +- src/tools/clippy/clippy_lints/src/eta_reduction.rs | 2 +- src/tools/clippy/clippy_lints/src/let_underscore.rs | 2 +- .../clippy/clippy_lints/src/non_send_fields_in_send_ty.rs | 2 +- .../clippy/clippy_lints/src/only_used_in_recursion.rs | 2 +- src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs | 2 +- src/tools/clippy/clippy_utils/src/ty.rs | 8 ++++---- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/tools/clippy/clippy_lints/src/derive.rs b/src/tools/clippy/clippy_lints/src/derive.rs index 61faaa10b8a43..3065010cd6392 100644 --- a/src/tools/clippy/clippy_lints/src/derive.rs +++ b/src/tools/clippy/clippy_lints/src/derive.rs @@ -349,7 +349,7 @@ fn check_copy_clone<'tcx>(cx: &LateContext<'tcx>, item: &Item<'_>, trait_ref: &h if ty_adt.repr().packed() && ty_subs .iter() - .any(|arg| matches!(arg.unpack(), GenericArgKind::Type(_) | GenericArgKind::Const(_))) + .any(|arg| matches!(arg.unpack(), GenericArgKind::Type(_) | GenericArgKind::Const(..))) { return; } diff --git a/src/tools/clippy/clippy_lints/src/eta_reduction.rs b/src/tools/clippy/clippy_lints/src/eta_reduction.rs index 450cee4007c72..f54bf614cbdc8 100644 --- a/src/tools/clippy/clippy_lints/src/eta_reduction.rs +++ b/src/tools/clippy/clippy_lints/src/eta_reduction.rs @@ -267,7 +267,7 @@ fn has_late_bound_to_non_late_bound_regions(from_sig: FnSig<'_>, to_sig: FnSig<' return true; } }, - (GenericArgKind::Const(_), GenericArgKind::Const(_)) => (), + (GenericArgKind::Const(..), GenericArgKind::Const(..)) => (), _ => return true, } } diff --git a/src/tools/clippy/clippy_lints/src/let_underscore.rs b/src/tools/clippy/clippy_lints/src/let_underscore.rs index 606c2ed72becc..420392f4a6f27 100644 --- a/src/tools/clippy/clippy_lints/src/let_underscore.rs +++ b/src/tools/clippy/clippy_lints/src/let_underscore.rs @@ -146,7 +146,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore { let init_ty = cx.typeck_results().expr_ty(init); let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() { GenericArgKind::Type(inner_ty) => SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path)), - GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, + GenericArgKind::Lifetime(_) | GenericArgKind::Const(..) => false, }); if contains_sync_guard { span_lint_and_help( diff --git a/src/tools/clippy/clippy_lints/src/non_send_fields_in_send_ty.rs b/src/tools/clippy/clippy_lints/src/non_send_fields_in_send_ty.rs index 352540d70b52c..d9f15902796ba 100644 --- a/src/tools/clippy/clippy_lints/src/non_send_fields_in_send_ty.rs +++ b/src/tools/clippy/clippy_lints/src/non_send_fields_in_send_ty.rs @@ -213,7 +213,7 @@ fn ty_allowed_with_raw_pointer_heuristic<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'t args.iter().all(|generic_arg| match generic_arg.unpack() { GenericArgKind::Type(ty) => ty_allowed_with_raw_pointer_heuristic(cx, ty, send_trait), // Lifetimes and const generics are not solid part of ADT and ignored - GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => true, + GenericArgKind::Lifetime(_) | GenericArgKind::Const(..) => true, }) } else { false diff --git a/src/tools/clippy/clippy_lints/src/only_used_in_recursion.rs b/src/tools/clippy/clippy_lints/src/only_used_in_recursion.rs index d621051ef1653..01a23865dabc5 100644 --- a/src/tools/clippy/clippy_lints/src/only_used_in_recursion.rs +++ b/src/tools/clippy/clippy_lints/src/only_used_in_recursion.rs @@ -388,7 +388,7 @@ fn has_matching_args(kind: FnKind, args: GenericArgsRef<'_>) -> bool { FnKind::TraitFn => args.iter().enumerate().all(|(idx, subst)| match subst.unpack() { GenericArgKind::Lifetime(_) => true, GenericArgKind::Type(ty) => matches!(*ty.kind(), ty::Param(ty) if ty.index as usize == idx), - GenericArgKind::Const(c) => matches!(c.kind(), ConstKind::Param(c) if c.index as usize == idx), + GenericArgKind::Const(c, _) => matches!(c.kind(), ConstKind::Param(c) if c.index as usize == idx), }), #[allow(trivial_casts)] FnKind::ImplTraitFn(expected_args) => args as *const _ as usize == expected_args, diff --git a/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs b/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs index 668ea9fcf3b4b..eefa896017802 100644 --- a/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs +++ b/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs @@ -55,7 +55,7 @@ fn check_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) -> McfResult { // No constraints on lifetimes or constants, except potentially // constants' types, but `walk` will get to them as well. - GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => continue, + GenericArgKind::Lifetime(_) | GenericArgKind::Const(_, _) => continue, }; match ty.kind() { diff --git a/src/tools/clippy/clippy_utils/src/ty.rs b/src/tools/clippy/clippy_utils/src/ty.rs index 1e748a46922cb..af7f32d792bd5 100644 --- a/src/tools/clippy/clippy_utils/src/ty.rs +++ b/src/tools/clippy/clippy_utils/src/ty.rs @@ -65,7 +65,7 @@ pub fn can_partially_move_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool pub fn contains_adt_constructor<'tcx>(ty: Ty<'tcx>, adt: AdtDef<'tcx>) -> bool { ty.walk().any(|inner| match inner.unpack() { GenericArgKind::Type(inner_ty) => inner_ty.ty_adt_def() == Some(adt), - GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, + GenericArgKind::Lifetime(_) | GenericArgKind::Const(_, _) => false, }) } @@ -127,7 +127,7 @@ pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<' false }, - GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false, + GenericArgKind::Lifetime(_) | GenericArgKind::Const(_, _) => false, }) } @@ -562,7 +562,7 @@ pub fn same_type_and_consts<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool { .iter() .zip(args_b.iter()) .all(|(arg_a, arg_b)| match (arg_a.unpack(), arg_b.unpack()) { - (GenericArgKind::Const(inner_a), GenericArgKind::Const(inner_b)) => inner_a == inner_b, + (GenericArgKind::Const(inner_a, _), GenericArgKind::Const(inner_b, _)) => inner_a == inner_b, (GenericArgKind::Type(type_a), GenericArgKind::Type(type_b)) => { same_type_and_consts(type_a, type_b) }, @@ -1080,7 +1080,7 @@ fn assert_generic_args_match<'tcx>(tcx: TyCtxt<'tcx>, did: DefId, args: &[Generi .find(|(_, (param, arg))| match (param, arg) { (GenericParamDefKind::Lifetime, GenericArgKind::Lifetime(_)) | (GenericParamDefKind::Type { .. }, GenericArgKind::Type(_)) - | (GenericParamDefKind::Const { .. }, GenericArgKind::Const(_)) => false, + | (GenericParamDefKind::Const { .. }, GenericArgKind::Const(_, _)) => false, ( GenericParamDefKind::Lifetime | GenericParamDefKind::Type { .. } From 9950f54b5b7ab47fe2c309c9aa8c815140033131 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sat, 9 Dec 2023 16:55:30 +0000 Subject: [PATCH 3/3] fix IR changes after rebase --- .../src/transform/check_consts/qualifs.rs | 4 +++- compiler/rustc_middle/src/ty/context.rs | 4 ++++ compiler/rustc_middle/src/ty/generic_args.rs | 9 +++++++++ .../rustc_next_trait_solver/src/canonicalizer.rs | 16 ++++++++++------ .../src/traits/select/confirmation.rs | 5 +++-- compiler/rustc_type_ir/src/interner.rs | 3 ++- 6 files changed, 31 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs b/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs index d5f418e1710e3..94f53c76e62ee 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs @@ -172,7 +172,9 @@ impl Qualif for NeedsNonConstDrop { destruct_def_id, [ ty::GenericArg::from(ty), - ty::GenericArg::from(cx.tcx.expected_const_effect_param_for_body(cx.def_id())), + ty::GenericArg::effect_const_arg( + cx.tcx.expected_const_effect_param_for_body(cx.def_id()), + ), ], ), ); diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 4498840df6ccb..a28266fca4c4a 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -158,6 +158,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> { ) -> Self::Const { Const::new_bound(self, debruijn, var, ty) } + + fn mk_const_arg(self, ct: Self::Const, is_effect: bool) -> Self::GenericArg { + GenericArg::new_const(ct, is_effect) + } } type InternedSet<'tcx, T> = ShardedHashMap, ()>; diff --git a/compiler/rustc_middle/src/ty/generic_args.rs b/compiler/rustc_middle/src/ty/generic_args.rs index 819293355a055..8e8d87d62664a 100644 --- a/compiler/rustc_middle/src/ty/generic_args.rs +++ b/compiler/rustc_middle/src/ty/generic_args.rs @@ -113,6 +113,15 @@ impl<'tcx> From> for GenericArg<'tcx> { } } +impl<'tcx> From> for GenericArg<'tcx> { + fn from(value: ty::Term<'tcx>) -> Self { + match value.unpack() { + ty::TermKind::Ty(t) => t.into(), + ty::TermKind::Const(c) => Self::new_const(c, false), + } + } +} + impl<'tcx> GenericArg<'tcx> { #[inline] pub fn unpack(self) -> GenericArgKind<'tcx> { diff --git a/compiler/rustc_next_trait_solver/src/canonicalizer.rs b/compiler/rustc_next_trait_solver/src/canonicalizer.rs index 1d44d2a2c1fa3..0523a05cc6c4c 100644 --- a/compiler/rustc_next_trait_solver/src/canonicalizer.rs +++ b/compiler/rustc_next_trait_solver/src/canonicalizer.rs @@ -409,12 +409,16 @@ impl, I: Interner> TypeFolder }; let var = ty::BoundVar::from( - self.variables.iter().position(|&v| v.as_const() == Some(c)).unwrap_or_else(|| { - let var = self.variables.len(); - self.variables.push(ty::GenericArg::new_const(c, kind == CanonicalVarKind::Effect)); - self.primitive_var_infos.push(CanonicalVarInfo { kind }); - var - }), + self.variables + .iter() + .position(|&v| self.interner().mk_const_arg(c, false) == v) + .unwrap_or_else(|| { + let var = self.variables.len(); + self.variables + .push(self.interner().mk_const_arg(c, kind == CanonicalVarKind::Effect)); + self.primitive_var_infos.push(CanonicalVarInfo { kind }); + var + }), ); self.interner().mk_bound_const(self.binder_index, var, c.ty()) diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index b3bf6f222490e..2d724cf9bd871 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -1213,8 +1213,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { else { bug!() }; - let host_effect_param: ty::GenericArg<'tcx> = - obligation.predicate.skip_binder().trait_ref.args.const_at(host_effect_index).into(); + let host_effect_param = ty::GenericArg::effect_const_arg( + obligation.predicate.skip_binder().trait_ref.args.const_at(host_effect_index), + ); let drop_trait = self.tcx().require_lang_item(LangItem::Drop, None); diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs index c262302133bee..95dc3ce4dcaad 100644 --- a/compiler/rustc_type_ir/src/interner.rs +++ b/compiler/rustc_type_ir/src/interner.rs @@ -47,7 +47,6 @@ pub trait Interner: Sized { + DebugWithInfcx + Hash + Ord - + Into + IntoKind> + ConstTy; type AliasConst: Copy + DebugWithInfcx + Hash + Ord; @@ -89,6 +88,8 @@ pub trait Interner: Sized { fn mk_bound_ty(self, debruijn: DebruijnIndex, var: BoundVar) -> Self::Ty; fn mk_bound_region(self, debruijn: DebruijnIndex, var: BoundVar) -> Self::Region; fn mk_bound_const(self, debruijn: DebruijnIndex, var: BoundVar, ty: Self::Ty) -> Self::Const; + + fn mk_const_arg(self, ct: Self::Const, is_effect: bool) -> Self::GenericArg; } /// Common capabilities of placeholder kinds