Skip to content

Commit f8e73ed

Browse files
committed
need_type_info: don't ICE when detected ty alias
fixes #97698
1 parent 2ea468e commit f8e73ed

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::infer::type_variable::TypeVariableOriginKind;
22
use crate::infer::InferCtxt;
33
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
44
use rustc_hir as hir;
5+
use rustc_hir::def::Res;
56
use rustc_hir::def::{CtorOf, DefKind, Namespace};
67
use rustc_hir::def_id::DefId;
78
use rustc_hir::intravisit::{self, Visitor};
@@ -853,12 +854,20 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
853854
hir::TyKind::Path(hir::QPath::Resolved(_self_ty, path)),
854855
) => {
855856
if tcx.res_generics_def_id(path.res) != Some(def.did()) {
856-
bug!(
857-
"unexpected path: def={:?} substs={:?} path={:?}",
858-
def,
859-
substs,
860-
path,
861-
);
857+
match path.res {
858+
Res::Def(DefKind::TyAlias, _) => {
859+
// FIXME: Ideally we should support this. For that
860+
// we have to map back from the self type to the
861+
// type alias though. That's difficult.
862+
//
863+
// See the `need_type_info/type-alias.rs` test for
864+
// some examples.
865+
}
866+
_ => warn!(
867+
"unexpected path: def={:?} substs={:?} path={:?}",
868+
def, substs, path,
869+
),
870+
}
862871
} else {
863872
return Box::new(
864873
self.resolved_path_inferred_subst_iter(path, substs)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// An addition to the `type-alias.rs` test,
2+
// see the FIXME in that file for why this test
3+
// exists.
4+
//
5+
// If there is none, feel free to remove this test
6+
// again.
7+
struct Ty<T>(T);
8+
impl<T> Ty<T> {
9+
fn new() {}
10+
}
11+
12+
type IndirectAlias<T> = Ty<Box<T>>;
13+
fn indirect_alias() {
14+
IndirectAlias::new();
15+
//~^ ERROR type annotations needed
16+
}
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/type-alias-indirect.rs:14:5
3+
|
4+
LL | IndirectAlias::new();
5+
| ^^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the type alias `IndirectAlias`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0282`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Test the inference errors in case the relevant path
2+
// uses a type alias.
3+
//
4+
// Regression test for #97698.
5+
struct Ty<T>(T);
6+
impl<T> Ty<T> {
7+
fn new() {}
8+
}
9+
10+
type DirectAlias<T> = Ty<T>;
11+
fn direct_alias() {
12+
DirectAlias::new()
13+
//~^ ERROR type annotations needed
14+
}
15+
16+
type IndirectAlias<T> = Ty<Box<T>>;
17+
fn indirect_alias() {
18+
IndirectAlias::new();
19+
// FIXME: This should also emit an error.
20+
//
21+
// Added it separately as `type-alias-indirect.rs`
22+
// where it does error.
23+
}
24+
25+
struct TyDefault<T, U = u32>(T, U);
26+
impl<T> TyDefault<T> {
27+
fn new() {}
28+
}
29+
30+
type DirectButWithDefaultAlias<T> = TyDefault<T>;
31+
fn direct_but_with_default_alias() {
32+
DirectButWithDefaultAlias::new();
33+
//~^ ERROR type annotations needed
34+
}
35+
36+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/type-alias.rs:12:5
3+
|
4+
LL | DirectAlias::new()
5+
| ^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
6+
7+
error[E0282]: type annotations needed
8+
--> $DIR/type-alias.rs:32:5
9+
|
10+
LL | DirectButWithDefaultAlias::new();
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)