From 87ced1561ff66f7e19c8e564f9d85383543c8999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9D=B0=E5=8F=8B=20Jieyou=20Xu=20=28Joe=29?= Date: Thu, 14 Mar 2024 19:37:41 +0000 Subject: [PATCH] Pass the correct DefId when suggesting writing the aliased Self type out --- compiler/rustc_hir_typeck/src/demand.rs | 2 +- .../ice-self-mismatch-const-generics.rs | 25 +++++++++++++ .../ice-self-mismatch-const-generics.stderr | 37 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/ui/typeck/ice-self-mismatch-const-generics.rs create mode 100644 tests/ui/typeck/ice-self-mismatch-const-generics.stderr diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index 71da6554340..67ff412651c 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -1071,7 +1071,7 @@ fn explain_self_literal( err.span_suggestion_verbose( *span, "use the type name directly", - self.tcx.value_path_str_with_args(*alias_to, e_args), + self.tcx.value_path_str_with_args(e_def.did(), e_args), Applicability::MaybeIncorrect, ); } diff --git a/tests/ui/typeck/ice-self-mismatch-const-generics.rs b/tests/ui/typeck/ice-self-mismatch-const-generics.rs new file mode 100644 index 00000000000..43f435ba4cf --- /dev/null +++ b/tests/ui/typeck/ice-self-mismatch-const-generics.rs @@ -0,0 +1,25 @@ +// Checks that the following does not ICE when constructing type mismatch diagnostic involving +// `Self` and const generics. +// Issue: + +pub struct GenericStruct { + thing: T, +} + +impl GenericStruct<0, T> { + pub fn new(thing: T) -> GenericStruct<1, T> { + Self { thing } + //~^ ERROR mismatched types + } +} + +pub struct GenericStruct2(T); + +impl GenericStruct2<0, T> { + pub fn new(thing: T) -> GenericStruct2<1, T> { + Self { 0: thing } + //~^ ERROR mismatched types + } +} + +fn main() {} diff --git a/tests/ui/typeck/ice-self-mismatch-const-generics.stderr b/tests/ui/typeck/ice-self-mismatch-const-generics.stderr new file mode 100644 index 00000000000..c502ea4565f --- /dev/null +++ b/tests/ui/typeck/ice-self-mismatch-const-generics.stderr @@ -0,0 +1,37 @@ +error[E0308]: mismatched types + --> $DIR/ice-self-mismatch-const-generics.rs:11:9 + | +LL | impl GenericStruct<0, T> { + | ------------------- this is the type of the `Self` literal +LL | pub fn new(thing: T) -> GenericStruct<1, T> { + | ------------------- expected `GenericStruct<1, T>` because of return type +LL | Self { thing } + | ^^^^^^^^^^^^^^ expected `1`, found `0` + | + = note: expected struct `GenericStruct<_, 1>` + found struct `GenericStruct<_, 0>` +help: use the type name directly + | +LL | GenericStruct::<1, T> { thing } + | ~~~~~~~~~~~~~~~~~~~~~ + +error[E0308]: mismatched types + --> $DIR/ice-self-mismatch-const-generics.rs:20:9 + | +LL | impl GenericStruct2<0, T> { + | -------------------- this is the type of the `Self` literal +LL | pub fn new(thing: T) -> GenericStruct2<1, T> { + | -------------------- expected `GenericStruct2<1, T>` because of return type +LL | Self { 0: thing } + | ^^^^^^^^^^^^^^^^^ expected `1`, found `0` + | + = note: expected struct `GenericStruct2<_, 1>` + found struct `GenericStruct2<_, 0>` +help: use the type name directly + | +LL | GenericStruct2::<1, T> { 0: thing } + | ~~~~~~~~~~~~~~~~~~~~~~ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`.