Skip to content

Commit c59a58c

Browse files
oli-obkfee1-dead
authored andcommitted
Allow the constness argument anywhere in the generics list
1 parent 71bcd35 commit c59a58c

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ impl<'tcx> Generics {
241241

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

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

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -816,33 +816,32 @@ impl<'tcx> TraitRef<'tcx> {
816816
}
817817

818818
pub fn constness(self) -> ty::ConstnessArg {
819-
match self.substs.last().expect("constness").unpack() {
820-
ty::subst::GenericArgKind::Constness(constness) => constness,
821-
_ => ty::ConstnessArg::Not,
819+
for arg in self.substs.iter() {
820+
match arg.unpack() {
821+
ty::subst::GenericArgKind::Constness(constness) => return constness,
822+
_ => {}
823+
}
822824
}
825+
ty::ConstnessArg::Not
823826
}
824827

825828
pub fn without_const(mut self, tcx: TyCtxt<'tcx>) -> Self {
826829
if self.constness().is_const() {
827-
self.substs = tcx.mk_substs(
828-
self.substs
829-
.iter()
830-
.take(self.substs.len() - 1)
831-
.chain(Some(ty::ConstnessArg::Not.into())),
832-
);
830+
self.substs = tcx.mk_substs(self.substs.iter().map(|arg| match arg.unpack() {
831+
ty::subst::GenericArgKind::Constness(_) => ty::ConstnessArg::Not.into(),
832+
_ => arg,
833+
}));
833834
}
834835

835836
self
836837
}
837838

838839
pub fn with_const(mut self, tcx: TyCtxt<'tcx>) -> Self {
839840
if !self.constness().is_const() {
840-
self.substs = tcx.mk_substs(
841-
self.substs
842-
.iter()
843-
.take(self.substs.len() - 1)
844-
.chain(Some(ty::ConstnessArg::Required.into())),
845-
);
841+
self.substs = tcx.mk_substs(self.substs.iter().map(|arg| match arg.unpack() {
842+
ty::subst::GenericArgKind::Constness(_) => ty::ConstnessArg::Required.into(),
843+
_ => arg,
844+
}));
846845
}
847846

848847
self

compiler/rustc_typeck/src/check/method/probe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,9 +1784,9 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
17841784
assert_eq!(
17851785
substs.len(),
17861786
generics.parent_count as usize,
1787-
"{:#?} vs {:#?}",
1787+
"substs: {:#?}\ngenerics: {:#?}",
17881788
substs,
1789-
generics.params
1789+
generics
17901790
);
17911791

17921792
let xform_fn_sig = if generics.params.is_empty() {

compiler/rustc_typeck/src/collect.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,10 +1633,9 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
16331633
assert!(!has_self);
16341634
parent_has_self = generics.has_self;
16351635
own_start = generics.count() as u32;
1636-
// if parent has constness param, we do not inherit it from the parent, and we
1637-
// get it in the end instead of the middle.
1636+
// if the parent has a constness param, we inherit it from the parent
16381637
parent_has_constness = generics.has_constness;
1639-
generics.parent_count + generics.params.len() - parent_has_constness as usize
1638+
generics.parent_count + generics.params.len()
16401639
});
16411640

16421641
let mut params: Vec<_> = Vec::with_capacity(ast_generics.params.len() + has_self as usize);
@@ -1762,7 +1761,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
17621761
}
17631762
}
17641763

1765-
if has_constness || parent_has_constness {
1764+
if has_constness && !parent_has_constness {
17661765
trace!("adding constness param");
17671766
params.push(ty::GenericParamDef {
17681767
name: Symbol::intern("<constness>"),

0 commit comments

Comments
 (0)