Skip to content

Commit 21c5ffe

Browse files
committed
Clean up
Simplify match statement Add multiple tests - 1 test for checking `N + 1 + 1` does not unify with `N+1` - 2 tests for checking that a function that uses two parameters only returns the parameter that is actually used. - Check exact repeat predicates
1 parent 7c5cb73 commit 21c5ffe

File tree

8 files changed

+133
-25
lines changed

8 files changed

+133
-25
lines changed

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

+9-23
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,9 @@ fn satisfied_from_param_env<'tcx>(
186186
Some(Ok(o)) if o == c => Some(Ok(c)),
187187
Some(_) => Some(Err(())),
188188
};
189-
ControlFlow::CONTINUE
190-
} else if let ty::ConstKind::Expr(e) = c.kind() {
189+
}
190+
191+
if let ty::ConstKind::Expr(e) = c.kind() {
191192
e.visit_with(self)
192193
} else {
193194
// FIXME(generic_const_exprs): This doesn't recurse into `<T as Trait<U>>::ASSOC`'s substs.
@@ -208,35 +209,20 @@ fn satisfied_from_param_env<'tcx>(
208209
match pred.kind().skip_binder() {
209210
ty::PredicateKind::ConstEvaluatable(ce) => {
210211
let b_ct = tcx.expand_abstract_consts(ce);
211-
let mut v = Visitor { ct, infcx, param_env, single_match: None };
212+
let mut v = Visitor { ct, infcx, param_env, single_match };
212213
let _ = b_ct.visit_with(&mut v);
213214

214-
if let Some(inner) = v.single_match {
215-
single_match = if let Ok(inner) = inner {
216-
match single_match {
217-
None => Some(Ok(inner)),
218-
Some(Ok(prev)) if prev == inner => Some(Ok(prev)),
219-
Some(_) => Some(Err(())),
220-
}
221-
} else {
222-
Some(Err(()))
223-
};
224-
}
215+
single_match = v.single_match;
225216
}
226217
_ => {} // don't care
227218
}
228219
}
229220

230221
if let Some(Ok(c)) = single_match {
231-
let is_ok = infcx
232-
.commit_if_ok(|_| {
233-
let ocx = ObligationCtxt::new_in_snapshot(infcx);
234-
assert!(ocx.eq(&ObligationCause::dummy(), param_env, c.ty(), ct.ty()).is_ok());
235-
assert!(ocx.eq(&ObligationCause::dummy(), param_env, c, ct).is_ok());
236-
if ocx.select_all_or_error().is_empty() { Ok(()) } else { Err(()) }
237-
})
238-
.is_ok();
239-
assert!(is_ok);
222+
let ocx = ObligationCtxt::new(infcx);
223+
assert!(ocx.eq(&ObligationCause::dummy(), param_env, c.ty(), ct.ty()).is_ok());
224+
assert!(ocx.eq(&ObligationCause::dummy(), param_env, c, ct).is_ok());
225+
assert!(ocx.select_all_or_error().is_empty());
240226
return true;
241227
}
242228

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(generic_const_exprs)]
2+
#![allow(incomplete_features)]
3+
4+
const fn both(_: usize, b: usize) -> usize {
5+
b
6+
}
7+
8+
fn foo<const N: usize, const M: usize>() -> [(); N + 2]
9+
where
10+
[(); both(N + 1, M + 1)]:,
11+
{
12+
bar()
13+
//~^ ERROR: unconstrained generic constant
14+
}
15+
16+
fn bar<const N: usize>() -> [(); N]
17+
where
18+
[(); N + 1]:,
19+
{
20+
[(); N]
21+
}
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: unconstrained generic constant
2+
--> $DIR/fn_with_two_const_inputs.rs:12:5
3+
|
4+
LL | bar()
5+
| ^^^
6+
|
7+
= help: try adding a `where` bound using this expression: `where [(); N + 1]:`
8+
note: required by a bound in `bar`
9+
--> $DIR/fn_with_two_const_inputs.rs:18:10
10+
|
11+
LL | fn bar<const N: usize>() -> [(); N]
12+
| --- required by a bound in this
13+
LL | where
14+
LL | [(); N + 1]:,
15+
| ^^^^^ required by this bound in `bar`
16+
17+
error: aborting due to previous error
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// check-pass
2+
#![feature(generic_const_exprs)]
3+
#![allow(incomplete_features)]
4+
5+
const fn both(_: usize, b: usize) -> usize {
6+
b
7+
}
8+
9+
fn foo<const N: usize>()
10+
where
11+
[(); both(N + 1, N + 1)]:,
12+
{
13+
bar::<N>();
14+
}
15+
16+
fn bar<const N: usize>()
17+
where
18+
[(); N + 1]:,
19+
{
20+
}
21+
22+
fn main() {}

src/test/ui/const-generics/generic_const_exprs/const_kind_expr/wf_obligation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ where
1515
[(); (L - 1) + 1 + L]:,
1616
{
1717
foo::<_, L>([(); L + 1 + L]);
18-
//~^ ERROR: unconstrained generic constant
19-
//~| ERROR: mismatched types
18+
//~^ ERROR: mismatched types
19+
//~^^ ERROR: unconstrained generic constant
2020
}
2121

2222
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// check-pass
2+
#![feature(generic_const_exprs)]
3+
#![allow(incomplete_features)]
4+
5+
fn foo<const N: usize>()
6+
where
7+
[(); N + 1]:,
8+
[(); N + 1]:,
9+
{
10+
bar::<N>();
11+
}
12+
13+
fn bar<const N: usize>()
14+
where
15+
[(); N + 1]:,
16+
{
17+
}
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![feature(generic_const_exprs)]
2+
#![allow(incomplete_features)]
3+
4+
fn foo<const N: usize>()
5+
where
6+
[(); N + 1 + 1]:,
7+
{
8+
bar();
9+
//~^ ERROR: type annotations
10+
}
11+
12+
fn bar<const N: usize>()
13+
where
14+
[(); N + 1]:,
15+
{
16+
}
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0284]: type annotations needed
2+
--> $DIR/unify_with_nested_expr.rs:8:5
3+
|
4+
LL | bar();
5+
| ^^^ cannot infer the value of the const parameter `N` declared on the function `bar`
6+
|
7+
note: required by a bound in `bar`
8+
--> $DIR/unify_with_nested_expr.rs:14:10
9+
|
10+
LL | fn bar<const N: usize>()
11+
| --- required by a bound in this
12+
LL | where
13+
LL | [(); N + 1]:,
14+
| ^^^^^ required by this bound in `bar`
15+
help: consider specifying the generic argument
16+
|
17+
LL | bar::<N>();
18+
| +++++
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0284`.

0 commit comments

Comments
 (0)