diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs index 7c4477b6172..40b73eb670c 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs @@ -2,6 +2,7 @@ use crate::infer::type_variable::TypeVariableOriginKind; use crate::infer::InferCtxt; use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed}; use rustc_hir as hir; +use rustc_hir::def::Res; use rustc_hir::def::{CtorOf, DefKind, Namespace}; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, Visitor}; @@ -853,12 +854,20 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { hir::TyKind::Path(hir::QPath::Resolved(_self_ty, path)), ) => { if tcx.res_generics_def_id(path.res) != Some(def.did()) { - bug!( - "unexpected path: def={:?} substs={:?} path={:?}", - def, - substs, - path, - ); + match path.res { + Res::Def(DefKind::TyAlias, _) => { + // FIXME: Ideally we should support this. For that + // we have to map back from the self type to the + // type alias though. That's difficult. + // + // See the `need_type_info/type-alias.rs` test for + // some examples. + } + _ => warn!( + "unexpected path: def={:?} substs={:?} path={:?}", + def, substs, path, + ), + } } else { return Box::new( self.resolved_path_inferred_subst_iter(path, substs) diff --git a/src/test/ui/inference/need_type_info/type-alias-indirect.rs b/src/test/ui/inference/need_type_info/type-alias-indirect.rs new file mode 100644 index 00000000000..0ed02ddc5f3 --- /dev/null +++ b/src/test/ui/inference/need_type_info/type-alias-indirect.rs @@ -0,0 +1,18 @@ +// An addition to the `type-alias.rs` test, +// see the FIXME in that file for why this test +// exists. +// +// If there is none, feel free to remove this test +// again. +struct Ty(T); +impl Ty { + fn new() {} +} + +type IndirectAlias = Ty>; +fn indirect_alias() { + IndirectAlias::new(); + //~^ ERROR type annotations needed +} + +fn main() {} diff --git a/src/test/ui/inference/need_type_info/type-alias-indirect.stderr b/src/test/ui/inference/need_type_info/type-alias-indirect.stderr new file mode 100644 index 00000000000..6161690df50 --- /dev/null +++ b/src/test/ui/inference/need_type_info/type-alias-indirect.stderr @@ -0,0 +1,9 @@ +error[E0282]: type annotations needed + --> $DIR/type-alias-indirect.rs:14:5 + | +LL | IndirectAlias::new(); + | ^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the type alias `IndirectAlias` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0282`. diff --git a/src/test/ui/inference/need_type_info/type-alias.rs b/src/test/ui/inference/need_type_info/type-alias.rs new file mode 100644 index 00000000000..f921b046b6c --- /dev/null +++ b/src/test/ui/inference/need_type_info/type-alias.rs @@ -0,0 +1,36 @@ +// Test the inference errors in case the relevant path +// uses a type alias. +// +// Regression test for #97698. +struct Ty(T); +impl Ty { + fn new() {} +} + +type DirectAlias = Ty; +fn direct_alias() { + DirectAlias::new() + //~^ ERROR type annotations needed +} + +type IndirectAlias = Ty>; +fn indirect_alias() { + IndirectAlias::new(); + // FIXME: This should also emit an error. + // + // Added it separately as `type-alias-indirect.rs` + // where it does error. +} + +struct TyDefault(T, U); +impl TyDefault { + fn new() {} +} + +type DirectButWithDefaultAlias = TyDefault; +fn direct_but_with_default_alias() { + DirectButWithDefaultAlias::new(); + //~^ ERROR type annotations needed +} + +fn main() {} diff --git a/src/test/ui/inference/need_type_info/type-alias.stderr b/src/test/ui/inference/need_type_info/type-alias.stderr new file mode 100644 index 00000000000..a33f49baf54 --- /dev/null +++ b/src/test/ui/inference/need_type_info/type-alias.stderr @@ -0,0 +1,15 @@ +error[E0282]: type annotations needed + --> $DIR/type-alias.rs:12:5 + | +LL | DirectAlias::new() + | ^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` + +error[E0282]: type annotations needed + --> $DIR/type-alias.rs:32:5 + | +LL | DirectButWithDefaultAlias::new(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0282`.