Skip to content

Commit e70e92e

Browse files
oli-obkfee1-dead
authored andcommitted
Explicitly handle constness from parents
1 parent 0d5b724 commit e70e92e

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub struct GenericParamCount {
110110
/// with an item or method. Analogous to `hir::Generics`.
111111
///
112112
/// The ordering of parameters is the same as in `Subst` (excluding child generics):
113-
/// `Self` (optionally), `Lifetime` params..., `Type` params...
113+
/// `Self` (optionally), `Lifetime` params..., `Type` params..., `constness` (optional)
114114
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
115115
pub struct Generics {
116116
pub parent: Option<DefId>,
@@ -122,6 +122,7 @@ pub struct Generics {
122122
pub param_def_id_to_index: FxHashMap<DefId, u32>,
123123

124124
pub has_self: bool,
125+
pub has_constness: bool,
125126
pub has_late_bound_regions: Option<Span>,
126127
}
127128

@@ -240,7 +241,9 @@ impl<'tcx> Generics {
240241

241242
/// Returns the `ConstnessArg`
242243
pub fn has_constness_param(&'tcx self) -> bool {
243-
self.params.iter().any(|param| matches!(param.kind, ty::GenericParamDefKind::Constness))
244+
self.params
245+
.last()
246+
.map_or(false, |param| matches!(param.kind, ty::GenericParamDefKind::Constness))
244247
}
245248

246249
/// Returns `true` if `params` has `impl Trait`.

compiler/rustc_typeck/src/collect.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
15181518
params,
15191519
param_def_id_to_index,
15201520
has_self: generics.has_self,
1521+
has_constness: generics.has_constness,
15211522
has_late_bound_regions: generics.has_late_bound_regions,
15221523
};
15231524
}
@@ -1623,7 +1624,9 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
16231624
};
16241625

16251626
let has_self = opt_self.is_some();
1627+
let has_constness = tcx.has_attr(def_id, sym::const_trait);
16261628
let mut parent_has_self = false;
1629+
let mut parent_has_constness = false;
16271630
let mut own_start = has_self as u32;
16281631
let parent_count = parent_def_id.map_or(0, |def_id| {
16291632
let generics = tcx.generics_of(def_id);
@@ -1632,8 +1635,8 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
16321635
own_start = generics.count() as u32;
16331636
// if parent has constness param, we do not inherit it from the parent, and we
16341637
// get it in the end instead of the middle.
1635-
let parent_constness = parent_has_self as usize;
1636-
generics.parent_count + generics.params.len() - parent_constness
1638+
parent_has_constness = generics.has_constness;
1639+
generics.parent_count + generics.params.len() - parent_has_constness as usize
16371640
});
16381641

16391642
let mut params: Vec<_> = Vec::with_capacity(ast_generics.params.len() + has_self as usize);
@@ -1759,7 +1762,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
17591762
}
17601763
}
17611764

1762-
if (has_self || parent_has_self) && tcx.has_attr(def_id, sym::const_trait) {
1765+
if has_constness || parent_has_constness {
17631766
params.push(ty::GenericParamDef {
17641767
name: Symbol::intern("<constness>"),
17651768
index: type_start + i as u32,
@@ -1777,6 +1780,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
17771780
params,
17781781
param_def_id_to_index,
17791782
has_self: has_self || parent_has_self,
1783+
has_constness: has_constness || parent_has_constness,
17801784
has_late_bound_regions: has_late_bound_regions(tcx, node),
17811785
}
17821786
}

0 commit comments

Comments
 (0)