Skip to content

Commit d286313

Browse files
authored
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
2 parents b9a8b5d + b6824ba commit d286313

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
249249
// Now create the real type and const parameters.
250250
let type_start = own_start - has_self as u32 + params.len() as u32;
251251
let mut i = 0;
252+
let mut next_index = || {
253+
let prev = i;
254+
i += 1;
255+
prev as u32 + type_start
256+
};
252257

253258
const TYPE_DEFAULT_NOT_ALLOWED: &'static str = "defaults for type parameters are only allowed in \
254259
`struct`, `enum`, `type`, or `trait` definitions";
@@ -278,15 +283,13 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
278283

279284
let kind = ty::GenericParamDefKind::Type { has_default: default.is_some(), synthetic };
280285

281-
let param_def = ty::GenericParamDef {
282-
index: type_start + i as u32,
286+
Some(ty::GenericParamDef {
287+
index: next_index(),
283288
name: param.name.ident().name,
284289
def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
285290
pure_wrt_drop: param.pure_wrt_drop,
286291
kind,
287-
};
288-
i += 1;
289-
Some(param_def)
292+
})
290293
}
291294
GenericParamKind::Const { default, .. } => {
292295
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 {
297300
);
298301
}
299302

300-
let param_def = ty::GenericParamDef {
301-
index: type_start + i as u32,
303+
Some(ty::GenericParamDef {
304+
index: next_index(),
302305
name: param.name.ident().name,
303306
def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
304307
pure_wrt_drop: param.pure_wrt_drop,
305308
kind: ty::GenericParamDefKind::Const { has_default: default.is_some() },
306-
};
307-
i += 1;
308-
Some(param_def)
309+
})
309310
}
310311
}));
311312

@@ -323,8 +324,8 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
323324
&["<closure_kind>", "<closure_signature>", "<upvars>"][..]
324325
};
325326

326-
params.extend(dummy_args.iter().enumerate().map(|(i, &arg)| ty::GenericParamDef {
327-
index: type_start + i as u32,
327+
params.extend(dummy_args.iter().map(|&arg| ty::GenericParamDef {
328+
index: next_index(),
328329
name: Symbol::intern(arg),
329330
def_id,
330331
pure_wrt_drop: false,
@@ -337,7 +338,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
337338
let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id));
338339
if let Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) = parent_node {
339340
params.push(ty::GenericParamDef {
340-
index: type_start,
341+
index: next_index(),
341342
name: Symbol::intern("<const_ty>"),
342343
def_id,
343344
pure_wrt_drop: false,

0 commit comments

Comments
 (0)