Skip to content

Commit d6ca388

Browse files
authored
Rollup merge of rust-lang#94235 - Dirbaio:fix-chalk-opaque-debrujin, r=jackh726
chalk: Fix wrong debrujin index in opaque type handling. A folder in opaque type lowering was substituting all opaque type references with a variable with debrujin index 0 ignoring how many binders deep we are. This caused an ICE with `Not enough bound vars: ^0 not found in []` ([full logs](https://gist.github.com/Dirbaio/2b9374ff4fce37afb9d665dc9f0000df)) with the following code. ```rust fn main() -> () {} async fn foo(x: u32) -> u32 { x } ``` With the fix, it no longer ICEs. It still doesn't typecheck due to generator issues. I've added a "known-bug" test so that at least it doesn't regress back to ICEing. r? ``@jackh726``
2 parents 734b924 + a1d8ce4 commit d6ca388

File tree

5 files changed

+94
-18
lines changed

5 files changed

+94
-18
lines changed

compiler/rustc_traits/src/chalk/db.rs

+40-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
use rustc_middle::traits::ChalkRustInterner as RustInterner;
1010
use rustc_middle::ty::subst::{InternalSubsts, Subst, SubstsRef};
11-
use rustc_middle::ty::{self, AssocItemContainer, AssocKind, TyCtxt, TypeFoldable};
11+
use rustc_middle::ty::{self, AssocItemContainer, AssocKind, Ty, TyCtxt, TypeFoldable};
1212

1313
use rustc_ast::ast;
1414
use rustc_attr as attr;
@@ -482,21 +482,11 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
482482
.iter()
483483
.map(|(bound, _)| bound.subst(self.interner.tcx, &bound_vars))
484484
.map(|bound| {
485-
bound.fold_with(&mut ty::fold::BottomUpFolder {
485+
bound.fold_with(&mut ReplaceOpaqueTyFolder {
486486
tcx: self.interner.tcx,
487-
ty_op: |ty| {
488-
if let ty::Opaque(def_id, substs) = *ty.kind() {
489-
if def_id == opaque_ty_id.0 && substs == identity_substs {
490-
return self.interner.tcx.mk_ty(ty::Bound(
491-
ty::INNERMOST,
492-
ty::BoundTy::from(ty::BoundVar::from_u32(0)),
493-
));
494-
}
495-
}
496-
ty
497-
},
498-
lt_op: |lt| lt,
499-
ct_op: |ct| ct,
487+
opaque_ty_id,
488+
identity_substs,
489+
binder_index: ty::INNERMOST,
500490
})
501491
})
502492
.filter_map(|bound| {
@@ -739,3 +729,38 @@ fn binders_for<'tcx>(
739729
}),
740730
)
741731
}
732+
733+
struct ReplaceOpaqueTyFolder<'tcx> {
734+
tcx: TyCtxt<'tcx>,
735+
opaque_ty_id: chalk_ir::OpaqueTyId<RustInterner<'tcx>>,
736+
identity_substs: SubstsRef<'tcx>,
737+
binder_index: ty::DebruijnIndex,
738+
}
739+
740+
impl<'tcx> ty::TypeFolder<'tcx> for ReplaceOpaqueTyFolder<'tcx> {
741+
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
742+
self.tcx
743+
}
744+
745+
fn fold_binder<T: TypeFoldable<'tcx>>(
746+
&mut self,
747+
t: ty::Binder<'tcx, T>,
748+
) -> ty::Binder<'tcx, T> {
749+
self.binder_index.shift_in(1);
750+
let t = t.super_fold_with(self);
751+
self.binder_index.shift_out(1);
752+
t
753+
}
754+
755+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
756+
if let ty::Opaque(def_id, substs) = *ty.kind() {
757+
if def_id == self.opaque_ty_id.0 && substs == self.identity_substs {
758+
return self.tcx.mk_ty(ty::Bound(
759+
self.binder_index,
760+
ty::BoundTy::from(ty::BoundVar::from_u32(0)),
761+
));
762+
}
763+
}
764+
ty
765+
}
766+
}

compiler/rustc_traits/src/chalk/lowering.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,10 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
323323
ty::Closure(def_id, substs) => {
324324
chalk_ir::TyKind::Closure(chalk_ir::ClosureId(def_id), substs.lower_into(interner))
325325
}
326-
ty::Generator(_def_id, _substs, _) => unimplemented!(),
326+
ty::Generator(def_id, substs, _) => chalk_ir::TyKind::Generator(
327+
chalk_ir::GeneratorId(def_id),
328+
substs.lower_into(interner),
329+
),
327330
ty::GeneratorWitness(_) => unimplemented!(),
328331
ty::Never => chalk_ir::TyKind::Never,
329332
ty::Tuple(types) => {

src/test/ui/chalkify/bugs/async.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-fail
2+
// known-bug
3+
// compile-flags: -Z chalk --edition=2021
4+
5+
fn main() -> () {}
6+
7+
async fn foo(x: u32) -> u32 {
8+
x
9+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0277]: the trait bound `[static generator@$DIR/async.rs:7:29: 9:2]: Generator<ResumeTy>` is not satisfied
2+
--> $DIR/async.rs:7:29
3+
|
4+
LL | async fn foo(x: u32) -> u32 {
5+
| _____________________________^
6+
LL | | x
7+
LL | | }
8+
| |_^ the trait `Generator<ResumeTy>` is not implemented for `[static generator@$DIR/async.rs:7:29: 9:2]`
9+
|
10+
note: required by a bound in `from_generator`
11+
--> $SRC_DIR/core/src/future/mod.rs:LL:COL
12+
|
13+
LL | T: Generator<ResumeTy, Yield = ()>,
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `from_generator`
15+
16+
error[E0280]: the requirement `<[static generator@$DIR/async.rs:7:29: 9:2] as Generator<ResumeTy>>::Yield == ()` is not satisfied
17+
--> $DIR/async.rs:7:29
18+
|
19+
LL | async fn foo(x: u32) -> u32 {
20+
| _____________________________^
21+
LL | | x
22+
LL | | }
23+
| |_^
24+
|
25+
note: required by a bound in `from_generator`
26+
--> $SRC_DIR/core/src/future/mod.rs:LL:COL
27+
|
28+
LL | T: Generator<ResumeTy, Yield = ()>,
29+
| ^^^^^^^^^^ required by this bound in `from_generator`
30+
31+
error[E0280]: the requirement `<impl Future<Output = [async output]> as Future>::Output == u32` is not satisfied
32+
--> $DIR/async.rs:7:25
33+
|
34+
LL | async fn foo(x: u32) -> u32 {
35+
| ^^^
36+
37+
error: aborting due to 3 previous errors
38+
39+
For more information about this error, try `rustc --explain E0277`.

src/tools/tidy/src/error_codes_check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use regex::Regex;
1010

1111
// A few of those error codes can't be tested but all the others can and *should* be tested!
1212
const EXEMPTED_FROM_TEST: &[&str] = &[
13-
"E0279", "E0280", "E0313", "E0377", "E0461", "E0462", "E0465", "E0476", "E0514", "E0519",
14-
"E0523", "E0554", "E0640", "E0717", "E0729",
13+
"E0279", "E0313", "E0377", "E0461", "E0462", "E0465", "E0476", "E0514", "E0519", "E0523",
14+
"E0554", "E0640", "E0717", "E0729",
1515
];
1616

1717
// Some error codes don't have any tests apparently...

0 commit comments

Comments
 (0)