Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&param_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)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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

Loading