Skip to content

Commit e2b0de5

Browse files
committed
Skip expected_projection if proj_ty is forbidden
1 parent 21e6de7 commit e2b0de5

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ impl<T> Trait<T> for X {
242242
}
243243
}
244244
(ty::Alias(ty::Projection | ty::Inherent, proj_ty), _)
245-
if !tcx.is_impl_trait_in_trait(proj_ty.def_id) =>
245+
if !tcx.is_impl_trait_in_trait(proj_ty.def_id)
246+
// proj ty is forbidden as the type of a const generic parameter, skip it
247+
&& !matches!(cause.code(), ObligationCauseCode::WhereClause { .. }) =>
246248
{
247249
self.expected_projection(
248250
diag,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(specialization)]
2+
#![allow(incomplete_features)]
3+
4+
trait Trait {
5+
type Type;
6+
}
7+
8+
impl Trait for i32 {
9+
default type Type = i32;
10+
}
11+
12+
struct Wrapper<const C: <i32 as Trait>::Type> {}
13+
//~^ ERROR `<i32 as Trait>::Type` is forbidden as the type of a const generic parameter
14+
15+
impl<const C: usize> Wrapper<C> {}
16+
//~^ ERROR the constant `C` is not of type `<i32 as Trait>::Type`
17+
//~^^ ERROR mismatched types
18+
19+
fn main() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: `<i32 as Trait>::Type` is forbidden as the type of a const generic parameter
2+
--> $DIR/skip-expect-proj-if-forbidden-issue-125757.rs:12:25
3+
|
4+
LL | struct Wrapper<const C: <i32 as Trait>::Type> {}
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: the only supported types are integers, `bool` and `char`
8+
9+
error: the constant `C` is not of type `<i32 as Trait>::Type`
10+
--> $DIR/skip-expect-proj-if-forbidden-issue-125757.rs:15:22
11+
|
12+
LL | impl<const C: usize> Wrapper<C> {}
13+
| ^^^^^^^^^^ expected associated type, found `usize`
14+
|
15+
note: required by a bound in `Wrapper`
16+
--> $DIR/skip-expect-proj-if-forbidden-issue-125757.rs:12:16
17+
|
18+
LL | struct Wrapper<const C: <i32 as Trait>::Type> {}
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Wrapper`
20+
21+
error[E0308]: mismatched types
22+
--> $DIR/skip-expect-proj-if-forbidden-issue-125757.rs:15:30
23+
|
24+
LL | impl<const C: usize> Wrapper<C> {}
25+
| ^ expected associated type, found `usize`
26+
|
27+
= note: expected associated type `<i32 as Trait>::Type`
28+
found type `usize`
29+
= help: consider constraining the associated type `<i32 as Trait>::Type` to `usize`
30+
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
31+
32+
error: aborting due to 3 previous errors
33+
34+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)