Skip to content

Commit 686cb11

Browse files
authored
Rollup merge of #99890 - compiler-errors:issue-99828, r=lcnr
Do not allow bad projection term to leak into the type checker Fixes #99828
2 parents 451349a + 77f7a83 commit 686cb11

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12341234
}
12351235

12361236
match binding.kind {
1237-
ConvertedBindingKind::Equality(term) => {
1237+
ConvertedBindingKind::Equality(mut term) => {
12381238
// "Desugar" a constraint like `T: Iterator<Item = u32>` this to
12391239
// the "projection predicate" for:
12401240
//
@@ -1245,18 +1245,28 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12451245
(hir::def::DefKind::AssocTy, ty::Term::Ty(_))
12461246
| (hir::def::DefKind::AssocConst, ty::Term::Const(_)) => (),
12471247
(_, _) => {
1248-
let got = if let ty::Term::Ty(_) = term { "type" } else { "const" };
1248+
let got = if let ty::Term::Ty(_) = term { "type" } else { "constant" };
12491249
let expected = def_kind.descr(assoc_item_def_id);
12501250
tcx.sess
12511251
.struct_span_err(
12521252
binding.span,
1253-
&format!("mismatch in bind of {expected}, got {got}"),
1253+
&format!("expected {expected} bound, found {got}"),
12541254
)
12551255
.span_note(
12561256
tcx.def_span(assoc_item_def_id),
1257-
&format!("{expected} defined here does not match {got}"),
1257+
&format!("{expected} defined here"),
12581258
)
12591259
.emit();
1260+
term = match def_kind {
1261+
hir::def::DefKind::AssocTy => tcx.ty_error().into(),
1262+
hir::def::DefKind::AssocConst => tcx
1263+
.const_error(
1264+
tcx.bound_type_of(assoc_item_def_id)
1265+
.subst(tcx, projection_ty.skip_binder().substs),
1266+
)
1267+
.into(),
1268+
_ => unreachable!(),
1269+
};
12601270
}
12611271
}
12621272
bounds.projection_bounds.push((

src/test/ui/associated-consts/assoc-const-ty-mismatch.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ impl FooTy for Bar {
2121

2222

2323
fn foo<F: Foo<N=usize>>() {}
24-
//~^ ERROR mismatch in
24+
//~^ ERROR expected associated constant bound, found type
2525
fn foo2<F: FooTy<T=3usize>>() {}
26-
//~^ ERROR mismatch in
26+
//~^ ERROR expected associated type bound, found constant
2727

2828
fn main() {
2929
foo::<Bar>();

src/test/ui/associated-consts/assoc-const-ty-mismatch.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
error: mismatch in bind of associated constant, got type
1+
error: expected associated constant bound, found type
22
--> $DIR/assoc-const-ty-mismatch.rs:23:15
33
|
44
LL | fn foo<F: Foo<N=usize>>() {}
55
| ^^^^^^^
66
|
7-
note: associated constant defined here does not match type
7+
note: associated constant defined here
88
--> $DIR/assoc-const-ty-mismatch.rs:5:3
99
|
1010
LL | const N: usize;
1111
| ^^^^^^^^^^^^^^
1212

13-
error: mismatch in bind of associated type, got const
13+
error: expected associated type bound, found constant
1414
--> $DIR/assoc-const-ty-mismatch.rs:25:18
1515
|
1616
LL | fn foo2<F: FooTy<T=3usize>>() {}
1717
| ^^^^^^^^
1818
|
19-
note: associated type defined here does not match const
19+
note: associated type defined here
2020
--> $DIR/assoc-const-ty-mismatch.rs:9:3
2121
|
2222
LL | type T;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn get_iter(vec: &[i32]) -> impl Iterator<Item = {}> + '_ {
2+
//~^ ERROR expected associated type bound, found constant
3+
//~| ERROR associated const equality is incomplete
4+
vec.iter()
5+
}
6+
7+
fn main() {
8+
let vec = Vec::new();
9+
let mut iter = get_iter(&vec);
10+
iter.next();
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0658]: associated const equality is incomplete
2+
--> $DIR/issue-99828.rs:1:43
3+
|
4+
LL | fn get_iter(vec: &[i32]) -> impl Iterator<Item = {}> + '_ {
5+
| ^^^^^^^^^
6+
|
7+
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
8+
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
9+
10+
error: expected associated type bound, found constant
11+
--> $DIR/issue-99828.rs:1:43
12+
|
13+
LL | fn get_iter(vec: &[i32]) -> impl Iterator<Item = {}> + '_ {
14+
| ^^^^^^^^^
15+
|
16+
note: associated type defined here
17+
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
18+
|
19+
LL | type Item;
20+
| ^^^^^^^^^
21+
22+
error: aborting due to 2 previous errors
23+
24+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)