Make param index generation a bit more robust

This commit is contained in:
Oli Scherer 2022-10-24 11:58:45 +00:00
parent 4b5fcae32d
commit b6824ba52a

View File

@ -249,6 +249,11 @@ enum Defaults {
// Now create the real type and const parameters.
let type_start = own_start - has_self as u32 + params.len() as u32;
let mut i = 0;
let mut next_index = || {
let prev = i;
i += 1;
prev as u32 + type_start
};
const TYPE_DEFAULT_NOT_ALLOWED: &'static str = "defaults for type parameters are only allowed in \
`struct`, `enum`, `type`, or `trait` definitions";
@ -278,15 +283,13 @@ enum Defaults {
let kind = ty::GenericParamDefKind::Type { has_default: default.is_some(), synthetic };
let param_def = ty::GenericParamDef {
index: type_start + i as u32,
Some(ty::GenericParamDef {
index: next_index(),
name: param.name.ident().name,
def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
pure_wrt_drop: param.pure_wrt_drop,
kind,
};
i += 1;
Some(param_def)
})
}
GenericParamKind::Const { default, .. } => {
if !matches!(allow_defaults, Defaults::Allowed) && default.is_some() {
@ -297,15 +300,13 @@ enum Defaults {
);
}
let param_def = ty::GenericParamDef {
index: type_start + i as u32,
Some(ty::GenericParamDef {
index: next_index(),
name: param.name.ident().name,
def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
pure_wrt_drop: param.pure_wrt_drop,
kind: ty::GenericParamDefKind::Const { has_default: default.is_some() },
};
i += 1;
Some(param_def)
})
}
}));
@ -323,8 +324,8 @@ enum Defaults {
&["<closure_kind>", "<closure_signature>", "<upvars>"][..]
};
params.extend(dummy_args.iter().enumerate().map(|(i, &arg)| ty::GenericParamDef {
index: type_start + i as u32,
params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {
index: next_index(),
name: Symbol::intern(arg),
def_id,
pure_wrt_drop: false,
@ -337,7 +338,7 @@ enum Defaults {
let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id));
if let Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) = parent_node {
params.push(ty::GenericParamDef {
index: type_start,
index: next_index(),
name: Symbol::intern("<const_ty>"),
def_id,
pure_wrt_drop: false,