Skip to content

Commit 242ac57

Browse files
committed
Fix const-generics ICE related to binding
1 parent ce331ee commit 242ac57

File tree

5 files changed

+150
-1
lines changed

5 files changed

+150
-1
lines changed

compiler/rustc_ty_utils/src/instance.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ fn resolve_associated_item<'tcx>(
115115
);
116116

117117
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
118-
let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref, tcx)))?;
118+
let vtbl = if trait_item.kind == ty::AssocKind::Const {
119+
let bound_vars = tcx
120+
.mk_bound_variable_kinds(std::iter::once(ty::BoundVariableKind::Region(ty::BrAnon(0))));
121+
let bind = ty::Binder::bind_with_vars(trait_ref, bound_vars);
122+
tcx.codegen_fulfill_obligation((param_env, bind))?
123+
} else {
124+
tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref, tcx)))?
125+
};
119126

120127
// Now that we know which impl is being used, we can dispatch to
121128
// the actual function:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![feature(const_generics, const_evaluatable_checked)]
2+
#![allow(incomplete_features)]
3+
4+
trait TensorDimension {
5+
const DIM: usize;
6+
}
7+
8+
trait TensorSize: TensorDimension {
9+
fn size(&self) -> [usize; Self::DIM];
10+
}
11+
12+
trait Broadcastable: TensorSize + Sized {
13+
type Element;
14+
fn lazy_updim<const NEWDIM: usize>(&self, size: [usize; NEWDIM]) {}
15+
}
16+
17+
struct BMap<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> {
18+
reference: &'a T,
19+
closure: F,
20+
}
21+
22+
impl<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> TensorDimension
23+
for BMap<'a, R, T, F, DIM>
24+
{
25+
const DIM: usize = DIM;
26+
}
27+
impl<'a, R, T: Broadcastable, F: Fn(T::Element) -> R, const DIM: usize> TensorSize
28+
for BMap<'a, R, T, F, DIM>
29+
{
30+
fn size(&self) -> [usize; DIM] {
31+
//~^ ERROR: method not compatible with trait [E0308]
32+
self.reference.size()
33+
//~^ ERROR: unconstrained generic constant
34+
//~| ERROR: mismatched types
35+
}
36+
}
37+
38+
fn main() {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0308]: method not compatible with trait
2+
--> $DIR/issue-83765.rs:30:5
3+
|
4+
LL | fn size(&self) -> [usize; DIM] {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Self::DIM`, found `DIM`
6+
|
7+
= note: expected type `Self::DIM`
8+
found type `DIM`
9+
10+
error: unconstrained generic constant
11+
--> $DIR/issue-83765.rs:32:24
12+
|
13+
LL | self.reference.size()
14+
| ^^^^
15+
|
16+
= help: try adding a `where` bound using this expression: `where [(); Self::DIM]:`
17+
18+
error[E0308]: mismatched types
19+
--> $DIR/issue-83765.rs:32:9
20+
|
21+
LL | self.reference.size()
22+
| ^^^^^^^^^^^^^^^^^^^^^ expected `DIM`, found `Self::DIM`
23+
|
24+
= note: expected type `DIM`
25+
found type `Self::DIM`
26+
27+
error: aborting due to 3 previous errors
28+
29+
For more information about this error, try `rustc --explain E0308`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![feature(const_generics, const_fn_trait_bound, const_evaluatable_checked)]
2+
#![allow(incomplete_features)]
3+
4+
trait _Contains<T> {
5+
const does_contain: bool;
6+
}
7+
8+
trait Contains<T, const Satisfied: bool> {}
9+
10+
trait Delegates<T> {}
11+
12+
impl<T, U> Delegates<U> for T where T: Contains<U, true> {}
13+
14+
const fn contains<A, B>() -> bool
15+
where
16+
A: _Contains<B>,
17+
{
18+
A::does_contain
19+
}
20+
21+
impl<T, U> Contains<T, { contains::<T, U>() }> for U where T: _Contains<U> {}
22+
23+
fn writes_to_path<C>(cap: &C) {
24+
writes_to_specific_path(&cap);
25+
//~^ ERROR: the trait bound `(): _Contains<&C>` is not satisfied [E0277]
26+
//~| ERROR: unconstrained generic constant
27+
}
28+
29+
fn writes_to_specific_path<C: Delegates<()>>(cap: &C) {}
30+
31+
fn main() {}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error[E0277]: the trait bound `(): _Contains<&C>` is not satisfied
2+
--> $DIR/issue-85848.rs:24:5
3+
|
4+
LL | writes_to_specific_path(&cap);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `_Contains<&C>` is not implemented for `()`
6+
...
7+
LL | fn writes_to_specific_path<C: Delegates<()>>(cap: &C) {}
8+
| ------------- required by this bound in `writes_to_specific_path`
9+
|
10+
note: required because of the requirements on the impl of `Contains<(), true>` for `&C`
11+
--> $DIR/issue-85848.rs:21:12
12+
|
13+
LL | impl<T, U> Contains<T, { contains::<T, U>() }> for U where T: _Contains<U> {}
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
15+
note: required because of the requirements on the impl of `Delegates<()>` for `&C`
16+
--> $DIR/issue-85848.rs:12:12
17+
|
18+
LL | impl<T, U> Delegates<U> for T where T: Contains<U, true> {}
19+
| ^^^^^^^^^^^^ ^
20+
21+
error: unconstrained generic constant
22+
--> $DIR/issue-85848.rs:24:5
23+
|
24+
LL | writes_to_specific_path(&cap);
25+
| ^^^^^^^^^^^^^^^^^^^^^^^
26+
...
27+
LL | fn writes_to_specific_path<C: Delegates<()>>(cap: &C) {}
28+
| ------------- required by this bound in `writes_to_specific_path`
29+
|
30+
= help: try adding a `where` bound using this expression: `where [(); { contains::<T, U>() }]:`
31+
note: required because of the requirements on the impl of `Contains<(), true>` for `&C`
32+
--> $DIR/issue-85848.rs:21:12
33+
|
34+
LL | impl<T, U> Contains<T, { contains::<T, U>() }> for U where T: _Contains<U> {}
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^
36+
note: required because of the requirements on the impl of `Delegates<()>` for `&C`
37+
--> $DIR/issue-85848.rs:12:12
38+
|
39+
LL | impl<T, U> Delegates<U> for T where T: Contains<U, true> {}
40+
| ^^^^^^^^^^^^ ^
41+
42+
error: aborting due to 2 previous errors
43+
44+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)