Skip to content

Commit 0d5636c

Browse files
authored
Rollup merge of #98610 - lcnr:emit_inference_failure_err-ice, r=estebank
fix `emit_inference_failure_err` ICE fixes #98598 this fix doesn't make me too happy, but 🤷
2 parents acdcdfb + e043821 commit 0d5636c

File tree

8 files changed

+119
-7
lines changed

8 files changed

+119
-7
lines changed

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
334334
let mut local_visitor = FindInferSourceVisitor::new(&self, typeck_results, arg);
335335
if let Some(body_id) = body_id {
336336
let expr = self.tcx.hir().expect_expr(body_id.hir_id);
337-
debug!(?expr);
338337
local_visitor.visit_expr(expr);
339338
}
340339

@@ -550,6 +549,7 @@ impl<'tcx> InferSourceKind<'tcx> {
550549
}
551550
}
552551

552+
#[derive(Debug)]
553553
struct InsertableGenericArgs<'tcx> {
554554
insert_span: Span,
555555
substs: SubstsRef<'tcx>,
@@ -735,10 +735,20 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
735735
return self.path_inferred_subst_iter(expr.hir_id, substs, path);
736736
}
737737
}
738-
hir::ExprKind::Struct(path, _, _) => {
738+
// FIXME(#98711): Ideally we would also deal with type relative
739+
// paths here, even if that is quite rare.
740+
//
741+
// See the `need_type_info/expr-struct-type-relative-gat.rs` test
742+
// for an example where that would be needed.
743+
//
744+
// However, the `type_dependent_def_id` for `Self::Output` in an
745+
// impl is currently the `DefId` of `Output` in the trait definition
746+
// which makes this somewhat difficult and prevents us from just
747+
// using `self.path_inferred_subst_iter` here.
748+
hir::ExprKind::Struct(&hir::QPath::Resolved(_self_ty, path), _, _) => {
739749
if let Some(ty) = self.opt_node_type(expr.hir_id) {
740750
if let ty::Adt(_, substs) = ty.kind() {
741-
return self.path_inferred_subst_iter(expr.hir_id, substs, path);
751+
return Box::new(self.resolved_path_inferred_subst_iter(path, substs));
742752
}
743753
}
744754
}
@@ -945,6 +955,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
945955
intravisit::walk_body(self, body);
946956
}
947957

958+
#[instrument(level = "debug", skip(self))]
948959
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
949960
let tcx = self.infcx.tcx;
950961
match expr.kind {
@@ -959,9 +970,9 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
959970
_ => intravisit::walk_expr(self, expr),
960971
}
961972

962-
for InsertableGenericArgs { insert_span, substs, generics_def_id, def_id } in
963-
self.expr_inferred_subst_iter(expr)
964-
{
973+
for args in self.expr_inferred_subst_iter(expr) {
974+
debug!(?args);
975+
let InsertableGenericArgs { insert_span, substs, generics_def_id, def_id } = args;
965976
let generics = tcx.generics_of(generics_def_id);
966977
if let Some(argument_index) =
967978
generics.own_substs(substs).iter().position(|&arg| self.generic_arg_is_target(arg))

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
156156
self.typeck_results.borrow_mut().field_indices_mut().insert(hir_id, index);
157157
}
158158

159+
#[instrument(level = "debug", skip(self))]
159160
pub(in super::super) fn write_resolution(
160161
&self,
161162
hir_id: hir::HirId,
@@ -164,8 +165,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
164165
self.typeck_results.borrow_mut().type_dependent_defs_mut().insert(hir_id, r);
165166
}
166167

168+
#[instrument(level = "debug", skip(self))]
167169
pub fn write_method_call(&self, hir_id: hir::HirId, method: MethodCallee<'tcx>) {
168-
debug!("write_method_call(hir_id={:?}, method={:?})", hir_id, method);
169170
self.write_resolution(hir_id, Ok((DefKind::AssocFn, method.def_id)));
170171
self.write_substs(hir_id, method.substs);
171172

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
trait Foo {
2+
type Output;
3+
4+
fn baz() -> Self::Output;
5+
}
6+
7+
fn needs_infer<T>() {}
8+
9+
enum Bar {
10+
Variant {}
11+
}
12+
13+
impl Foo for u8 {
14+
type Output = Bar;
15+
fn baz() -> Self::Output {
16+
needs_infer(); //~ ERROR type annotations needed
17+
Self::Output::Variant {}
18+
}
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/expr-struct-type-relative-enum.rs:16:9
3+
|
4+
LL | needs_infer();
5+
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `needs_infer`
6+
|
7+
help: consider specifying the generic argument
8+
|
9+
LL | needs_infer::<T>();
10+
| +++++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0282`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(generic_associated_types)]
2+
3+
trait Foo {
4+
type Output<T>;
5+
6+
fn baz();
7+
}
8+
9+
enum Bar<T> {
10+
Simple {},
11+
Generic(T),
12+
}
13+
14+
impl Foo for u8 {
15+
type Output<T> = Bar<T>;
16+
fn baz() {
17+
Self::Output::Simple {}; //~ ERROR type annotations needed
18+
}
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/expr-struct-type-relative-gat.rs:17:9
3+
|
4+
LL | Self::Output::Simple {};
5+
| ^^^^^^^^^^^^ cannot infer type for type parameter `T` declared on the associated type `Output`
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,21 @@
1+
// regression test for #98598
2+
3+
trait Foo {
4+
type Output;
5+
6+
fn baz() -> Self::Output;
7+
}
8+
9+
fn needs_infer<T>() {}
10+
11+
struct Bar {}
12+
13+
impl Foo for u8 {
14+
type Output = Bar;
15+
fn baz() -> Self::Output {
16+
needs_infer(); //~ ERROR type annotations needed
17+
Self::Output {}
18+
}
19+
}
20+
21+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/expr-struct-type-relative.rs:16:9
3+
|
4+
LL | needs_infer();
5+
| ^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `needs_infer`
6+
|
7+
help: consider specifying the generic argument
8+
|
9+
LL | needs_infer::<T>();
10+
| +++++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)