Skip to content

Commit

Permalink
Rollup merge of rust-lang#103475 - oli-obk:generic_param_indices, r=lcnr
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dylan-DPC authored Oct 25, 2022
2 parents d0ce4d5 + b6824ba commit 646e13e
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
// 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";
Expand Down Expand Up @@ -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 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() {
Expand All @@ -297,15 +300,13 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
);
}

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)
})
}
}));

Expand All @@ -323,8 +324,8 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
&["<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,
Expand All @@ -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));
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,
Expand Down

0 comments on commit 646e13e

Please sign in to comment.