Rollup merge of #103475 - oli-obk:generic_param_indices, r=lcnr

Make param index generation a bit more robust

r? ````@lcnr````

While not really necessary for closure and anon const ids, it's strictly more correct
This commit is contained in:
Matthias Krüger 2022-10-27 09:25:09 +02:00 committed by GitHub
commit d7ad6ad9ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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