Skip to content

Commit 4f50485

Browse files
committed
More attempts to fix ICE
1 parent 25be7a4 commit 4f50485

File tree

13 files changed

+53
-19
lines changed

13 files changed

+53
-19
lines changed

compiler/rustc_infer/src/infer/equate.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
166166
a: ty::ConstnessArg,
167167
b: ty::ConstnessArg,
168168
) -> RelateResult<'tcx, ty::ConstnessArg> {
169-
// TODO handle infer
170-
relate::super_relate_constness(self, a, b)
169+
match (a, b) {
170+
(ty::ConstnessArg::Infer, x) | (x, ty::ConstnessArg::Infer) => Ok(x),
171+
_ => relate::super_relate_constness(self, a, b),
172+
}
171173
}
172174
}
173175

compiler/rustc_middle/src/ty/context.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2964,6 +2964,15 @@ impl<'tcx> TyCtxt<'tcx> {
29642964
})
29652965
)
29662966
}
2967+
2968+
pub fn should_have_constness(self, def_id: DefId) -> bool {
2969+
match self.def_kind(def_id) {
2970+
DefKind::Trait => true,
2971+
DefKind::Impl if self.constness(def_id) == hir::Constness::Const => true,
2972+
DefKind::AssocFn => self.should_have_constness(self.parent(def_id)),
2973+
_ => false,
2974+
}
2975+
}
29672976
}
29682977

29692978
impl<'tcx> TyCtxtAt<'tcx> {

compiler/rustc_middle/src/ty/generics.rs

+11
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ pub struct Generics {
123123

124124
pub has_self: bool,
125125
pub has_constness: bool,
126+
pub parent_has_constness: bool,
126127
pub has_late_bound_regions: Option<Span>,
127128
}
128129

@@ -240,6 +241,7 @@ impl<'tcx> Generics {
240241
}
241242

242243
/// Returns the `ConstnessArg`
244+
// TODO just use self.has_constness?
243245
pub fn has_constness_param(&'tcx self) -> bool {
244246
self.params.iter().any(|param| matches!(param.kind, ty::GenericParamDefKind::Constness))
245247
}
@@ -295,6 +297,15 @@ impl<'tcx> Generics {
295297
let own = &substs[self.parent_count..][..self.params.len()];
296298
if self.has_self && self.parent.is_none() { &own[1..] } else { &own }
297299
}
300+
301+
/*pub fn expected_arg_count(&self) -> std::ops::RangeInclusive<usize> {
302+
self.count()
303+
todo!()
304+
}*/
305+
306+
pub fn expected_parent_count(&self) -> std::ops::RangeInclusive<usize> {
307+
self.parent_count..=self.parent_count + self.parent_has_constness as usize
308+
}
298309
}
299310

300311
/// Bounds on generics.

compiler/rustc_middle/src/ty/print/pretty.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1824,11 +1824,15 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
18241824
GenericArgKind::Lifetime(r) => !r.is_erased(),
18251825
_ => false,
18261826
});
1827-
let mut args = args.iter().cloned().filter(|arg| match arg.unpack() {
1828-
GenericArgKind::Lifetime(_) => print_regions,
1829-
GenericArgKind::Constness(_) => false,
1830-
_ => true,
1831-
}).peekable();
1827+
let mut args = args
1828+
.iter()
1829+
.cloned()
1830+
.filter(|arg| match arg.unpack() {
1831+
GenericArgKind::Lifetime(_) => print_regions,
1832+
GenericArgKind::Constness(_) => false,
1833+
_ => true,
1834+
})
1835+
.peekable();
18321836

18331837
if args.peek().is_some() {
18341838
if self.in_value {

compiler/rustc_middle/src/ty/subst.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,8 @@ impl<'a, 'tcx> TypeFolder<'tcx> for SubstFolder<'a, 'tcx> {
580580
_ => {
581581
let msg = format!(
582582
"Region parameter out of range \
583-
when substituting in region {} (index={})",
584-
data.name, data.index
583+
when substituting in region {} (index={}, substs={:?})",
584+
data.name, data.index, self.substs
585585
);
586586
span_bug!(DUMMY_SP, "{}", msg);
587587
}

compiler/rustc_typeck/src/astconv/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
376376
// non-`Self` generic parameters.
377377
if generics.params.is_empty() {
378378
return (
379-
tcx.intern_substs(&constness.map(Into::into).into_iter().collect::<Vec<_>>()),
379+
tcx.intern_substs(&if let Some(ty::ConstnessArg::Const) = constness {
380+
vec![ty::ConstnessArg::Const.into()]
381+
} else {
382+
vec![]
383+
}),
380384
arg_count,
381385
);
382386
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
345345

346346
// Create subst for early-bound lifetime parameters, combining
347347
// parameters from the type and those from the method.
348-
assert_eq!(generics.parent_count, parent_substs.len());
348+
assert!(generics.expected_parent_count().contains(&parent_substs.len()));
349349

350350
struct MethodSubstsCtxt<'a, 'tcx> {
351351
cfcx: &'a ConfirmContext<'a, 'tcx>,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
454454
};
455455
let def_id = method_item.def_id;
456456
let generics = tcx.generics_of(def_id);
457-
assert_eq!(generics.params.len(), generics.has_constness as usize); // no generics except for constness
457+
assert!(generics.params.len() <= generics.has_constness as usize); // no generics except for constness
458458

459459
debug!("lookup_in_trait_adjusted: method_item={:?}", method_item);
460460
let mut obligations = vec![];

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1783,9 +1783,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
17831783
// method yet. So create fresh variables here for those too,
17841784
// if there are any.
17851785
let generics = self.tcx.generics_of(method);
1786-
assert_eq!(
1787-
substs.len(),
1788-
generics.parent_count as usize,
1786+
assert!(
1787+
generics.expected_parent_count().contains(&substs.len()),
17891788
"substs: {:#?}\ngenerics: {:#?}",
17901789
substs,
17911790
generics

compiler/rustc_typeck/src/check/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,10 @@ fn has_expected_num_generic_args<'tcx>(
963963
) -> bool {
964964
trait_did.map_or(true, |trait_did| {
965965
let generics = tcx.generics_of(trait_did);
966-
generics.count() == expected + if generics.has_self { 1 } else { 0 } + if generics.has_constness { 1 } else { 0 }
966+
generics.count()
967+
== expected
968+
+ if generics.has_self { 1 } else { 0 }
969+
+ if generics.has_constness { 1 } else { 0 }
967970
})
968971
}
969972

compiler/rustc_typeck/src/collect.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
15191519
param_def_id_to_index,
15201520
has_self: generics.has_self,
15211521
has_constness: generics.has_constness,
1522+
parent_has_constness: generics.parent_has_constness,
15221523
has_late_bound_regions: generics.has_late_bound_regions,
15231524
};
15241525
}
@@ -1624,9 +1625,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
16241625
};
16251626

16261627
let has_self = opt_self.is_some();
1627-
let has_constness = tcx.has_attr(def_id, sym::const_trait)
1628-
|| (tcx.def_kind(def_id) == DefKind::Impl
1629-
&& tcx.impl_constness(def_id) == hir::Constness::Const);
1628+
let has_constness = tcx.should_have_constness(def_id);
16301629
let mut parent_has_self = false;
16311630
let mut parent_has_constness = false;
16321631
let mut own_start = has_self as u32;
@@ -1783,6 +1782,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
17831782
param_def_id_to_index,
17841783
has_self: has_self || parent_has_self,
17851784
has_constness: has_constness || parent_has_constness,
1785+
parent_has_constness: parent_has_constness,
17861786
has_late_bound_regions: has_late_bound_regions(tcx, node),
17871787
};
17881788
trace!("{:#?}", generics);

library/core/src/ops/function.rs

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ pub trait FnMut<Args>: FnOnce<Args> {
237237
)]
238238
#[fundamental] // so that regex can rely that `&str: !FnMut`
239239
#[must_use = "closures are lazy and do nothing unless called"]
240+
#[cfg_attr(not(bootstrap), const_trait)]
240241
pub trait FnOnce<Args> {
241242
/// The returned type after the call operator is used.
242243
#[lang = "fn_once_output"]

library/core/src/slice/index.rs

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ mod private_slice_index {
152152
message = "the type `{T}` cannot be indexed by `{Self}`",
153153
label = "slice indices are of type `usize` or ranges of `usize`"
154154
)]
155+
#[cfg_attr(not(bootstrap), const_trait)]
155156
pub unsafe trait SliceIndex<T: ?Sized>: private_slice_index::Sealed {
156157
/// The output type returned by methods.
157158
#[stable(feature = "slice_get_slice", since = "1.28.0")]

0 commit comments

Comments
 (0)