Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 82ebbd7

Browse files
committed
add test for let-bindings
1 parent c7d16df commit 82ebbd7

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

compiler/rustc_typeck/src/collect.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,25 +1693,27 @@ pub fn const_evaluatable_predicates_of<'tcx>(
16931693
) -> impl Iterator<Item = (ty::Predicate<'tcx>, Span)> {
16941694
#[derive(Default)]
16951695
struct ConstCollector<'tcx> {
1696-
ct: SmallVec<[(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>); 4]>,
1696+
ct: SmallVec<[(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>, Span); 4]>,
1697+
curr_span: Span,
16971698
}
16981699

16991700
impl<'tcx> TypeVisitor<'tcx> for ConstCollector<'tcx> {
17001701
fn visit_const(&mut self, ct: &'tcx Const<'tcx>) -> bool {
17011702
if let ty::ConstKind::Unevaluated(def, substs, None) = ct.val {
1702-
self.ct.push((def, substs));
1703+
self.ct.push((def, substs, self.curr_span));
17031704
}
17041705
false
17051706
}
17061707
}
17071708

17081709
let mut collector = ConstCollector::default();
1709-
for (pred, _span) in predicates.predicates.iter() {
1710+
for &(pred, span) in predicates.predicates.iter() {
1711+
collector.curr_span = span;
17101712
pred.visit_with(&mut collector);
17111713
}
17121714
warn!("const_evaluatable_predicates_of({:?}) = {:?}", def_id, collector.ct);
1713-
collector.ct.into_iter().map(move |(def_id, subst)| {
1714-
(ty::PredicateAtom::ConstEvaluatable(def_id, subst).to_predicate(tcx), DUMMY_SP)
1715+
collector.ct.into_iter().map(move |(def_id, subst, span)| {
1716+
(ty::PredicateAtom::ConstEvaluatable(def_id, subst).to_predicate(tcx), span)
17151717
})
17161718
}
17171719

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(const_generics, const_evaluatable_checked)]
2+
#![allow(incomplete_features)]
3+
4+
// We do not yet want to support let-bindings in abstract consts,
5+
// so this test should keep failing for now.
6+
fn test<const N: usize>() -> [u8; { let x = N; N + 1 }] where [u8; { let x = N; N + 1 }]: Default {
7+
//~^ ERROR constant expression depends
8+
//~| ERROR constant expression depends
9+
Default::default()
10+
}
11+
12+
fn main() {
13+
let x = test::<31>();
14+
assert_eq!(x, [0; 32]);
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/let-bindings.rs:6:91
3+
|
4+
LL | fn test<const N: usize>() -> [u8; { let x = N; N + 1 }] where [u8; { let x = N; N + 1 }]: Default {
5+
| ^^^^^^^ required by this bound in `test::{{constant}}#0`
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: constant expression depends on a generic parameter
10+
--> $DIR/let-bindings.rs:6:30
11+
|
12+
LL | fn test<const N: usize>() -> [u8; { let x = N; N + 1 }] where [u8; { let x = N; N + 1 }]: Default {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: this may fail depending on what value the parameter takes
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)