1
Fork 0

need_type_info: don't ICE when detected ty alias

fixes #97698
This commit is contained in:
lcnr 2022-06-03 20:26:20 +02:00
parent 2ea468e386
commit f8e73ede83
5 changed files with 93 additions and 6 deletions

View file

@ -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)

View file

@ -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>(T);
impl<T> Ty<T> {
fn new() {}
}
type IndirectAlias<T> = Ty<Box<T>>;
fn indirect_alias() {
IndirectAlias::new();
//~^ ERROR type annotations needed
}
fn main() {}

View file

@ -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`.

View file

@ -0,0 +1,36 @@
// Test the inference errors in case the relevant path
// uses a type alias.
//
// Regression test for #97698.
struct Ty<T>(T);
impl<T> Ty<T> {
fn new() {}
}
type DirectAlias<T> = Ty<T>;
fn direct_alias() {
DirectAlias::new()
//~^ ERROR type annotations needed
}
type IndirectAlias<T> = Ty<Box<T>>;
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 = u32>(T, U);
impl<T> TyDefault<T> {
fn new() {}
}
type DirectButWithDefaultAlias<T> = TyDefault<T>;
fn direct_but_with_default_alias() {
DirectButWithDefaultAlias::new();
//~^ ERROR type annotations needed
}
fn main() {}

View file

@ -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`.