Skip to content

Commit 87ced15

Browse files
committed
Pass the correct DefId when suggesting writing the aliased Self type out
1 parent fe61575 commit 87ced15

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10711071
err.span_suggestion_verbose(
10721072
*span,
10731073
"use the type name directly",
1074-
self.tcx.value_path_str_with_args(*alias_to, e_args),
1074+
self.tcx.value_path_str_with_args(e_def.did(), e_args),
10751075
Applicability::MaybeIncorrect,
10761076
);
10771077
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Checks that the following does not ICE when constructing type mismatch diagnostic involving
2+
// `Self` and const generics.
3+
// Issue: <https://github.com/rust-lang/rust/issues/122467>
4+
5+
pub struct GenericStruct<const N: usize, T> {
6+
thing: T,
7+
}
8+
9+
impl<T> GenericStruct<0, T> {
10+
pub fn new(thing: T) -> GenericStruct<1, T> {
11+
Self { thing }
12+
//~^ ERROR mismatched types
13+
}
14+
}
15+
16+
pub struct GenericStruct2<const M: usize, T>(T);
17+
18+
impl<T> GenericStruct2<0, T> {
19+
pub fn new(thing: T) -> GenericStruct2<1, T> {
20+
Self { 0: thing }
21+
//~^ ERROR mismatched types
22+
}
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/ice-self-mismatch-const-generics.rs:11:9
3+
|
4+
LL | impl<T> GenericStruct<0, T> {
5+
| ------------------- this is the type of the `Self` literal
6+
LL | pub fn new(thing: T) -> GenericStruct<1, T> {
7+
| ------------------- expected `GenericStruct<1, T>` because of return type
8+
LL | Self { thing }
9+
| ^^^^^^^^^^^^^^ expected `1`, found `0`
10+
|
11+
= note: expected struct `GenericStruct<_, 1>`
12+
found struct `GenericStruct<_, 0>`
13+
help: use the type name directly
14+
|
15+
LL | GenericStruct::<1, T> { thing }
16+
| ~~~~~~~~~~~~~~~~~~~~~
17+
18+
error[E0308]: mismatched types
19+
--> $DIR/ice-self-mismatch-const-generics.rs:20:9
20+
|
21+
LL | impl<T> GenericStruct2<0, T> {
22+
| -------------------- this is the type of the `Self` literal
23+
LL | pub fn new(thing: T) -> GenericStruct2<1, T> {
24+
| -------------------- expected `GenericStruct2<1, T>` because of return type
25+
LL | Self { 0: thing }
26+
| ^^^^^^^^^^^^^^^^^ expected `1`, found `0`
27+
|
28+
= note: expected struct `GenericStruct2<_, 1>`
29+
found struct `GenericStruct2<_, 0>`
30+
help: use the type name directly
31+
|
32+
LL | GenericStruct2::<1, T> { 0: thing }
33+
| ~~~~~~~~~~~~~~~~~~~~~~
34+
35+
error: aborting due to 2 previous errors
36+
37+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)