diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 3cfcac5c72915..fe9f9165160c5 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -1561,11 +1561,26 @@ fn const_param_default<'tcx>( "`const_param_default` expected a generic parameter with a constant" ), }; + + let param_def_id = def_id.to_def_id(); + let parent_def_id = tcx.parent(param_def_id); + let parent_generics = tcx.generics_of(parent_def_id); + let parent_identity_args = ty::GenericArgs::identity_for_item(tcx, parent_def_id); + + let lookup_index = parent_generics.param_def_id_to_index.get(¶m_def_id).copied(); + let param_index = lookup_index.unwrap_or_else(|| { + span_bug!( + tcx.def_span(def_id), + "missing const param index for `{param_def_id:?}` in `{parent_def_id:?}`" + ) + }) as usize; + let slice_index = parent_identity_args.len().min(param_index); + let params_in_scope = parent_identity_args.split_at(slice_index).0; + let icx = ItemCtxt::new(tcx, def_id); - let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id); let ct = icx .lowerer() - .lower_const_arg(default_ct, FeedConstTy::Param(def_id.to_def_id(), identity_args)); + .lower_const_arg(default_ct, FeedConstTy::Param(param_def_id, params_in_scope)); ty::EarlyBinder::bind(ct) } diff --git a/tests/ui/const-generics/generic_const_parameter_types/ice-142913-const-defaults-with-lifetimes.rs b/tests/ui/const-generics/generic_const_parameter_types/ice-142913-const-defaults-with-lifetimes.rs new file mode 100644 index 0000000000000..f49676447a5f1 --- /dev/null +++ b/tests/ui/const-generics/generic_const_parameter_types/ice-142913-const-defaults-with-lifetimes.rs @@ -0,0 +1,10 @@ +#![allow(incomplete_features)] +#![feature(generic_const_parameter_types)] +struct Variant; + +fn foo<'a, const N: &'a Variant = {}>() {} +//~^ ERROR: defaults for generic parameters are not allowed here +//~| ERROR: anonymous constants with lifetimes in their type are not yet supported +//~| ERROR: `&'a Variant` is forbidden as the type of a const generic parameter + +fn main() {} diff --git a/tests/ui/const-generics/generic_const_parameter_types/ice-142913-const-defaults-with-lifetimes.stderr b/tests/ui/const-generics/generic_const_parameter_types/ice-142913-const-defaults-with-lifetimes.stderr new file mode 100644 index 0000000000000..c7cdb8ffc3144 --- /dev/null +++ b/tests/ui/const-generics/generic_const_parameter_types/ice-142913-const-defaults-with-lifetimes.stderr @@ -0,0 +1,30 @@ +error: defaults for generic parameters are not allowed here + --> $DIR/ice-142913-const-defaults-with-lifetimes.rs:5:12 + | +LL | fn foo<'a, const N: &'a Variant = {}>() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: anonymous constants with lifetimes in their type are not yet supported + --> $DIR/ice-142913-const-defaults-with-lifetimes.rs:5:35 + | +LL | fn foo<'a, const N: &'a Variant = {}>() {} + | ^^ + +error: `&'a Variant` is forbidden as the type of a const generic parameter + --> $DIR/ice-142913-const-defaults-with-lifetimes.rs:5:21 + | +LL | fn foo<'a, const N: &'a Variant = {}>() {} + | ^^^^^^^^^^^ + | + = note: the only supported types are integers, `bool`, and `char` +help: add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types + | +LL + #![feature(adt_const_params)] + | +help: add `#![feature(unsized_const_params)]` to the crate attributes to enable references to implement the `ConstParamTy` trait + | +LL + #![feature(unsized_const_params)] + | + +error: aborting due to 3 previous errors +