Skip to content

Commit 37ed2db

Browse files
committed
consider unevaluated consts in extract_inference_diagnostics_data
1 parent dae2407 commit 37ed2db

File tree

4 files changed

+95
-41
lines changed

4 files changed

+95
-41
lines changed

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

+66-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::infer::type_variable::TypeVariableOriginKind;
22
use crate::infer::InferCtxt;
3+
use crate::rustc_middle::ty::TypeFoldable;
34
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
45
use rustc_hir as hir;
56
use rustc_hir::def::{DefKind, Namespace};
@@ -390,36 +391,75 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
390391
}
391392
}
392393
GenericArgKind::Const(ct) => {
393-
if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
394-
let origin =
395-
self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
396-
if let ConstVariableOriginKind::ConstParameterDefinition(name, def_id) =
397-
origin.kind
398-
{
399-
return InferenceDiagnosticsData {
400-
name: name.to_string(),
394+
match ct.val {
395+
ty::ConstKind::Infer(InferConst::Var(vid)) => {
396+
let origin = self
397+
.inner
398+
.borrow_mut()
399+
.const_unification_table()
400+
.probe_value(vid)
401+
.origin;
402+
if let ConstVariableOriginKind::ConstParameterDefinition(name, def_id) =
403+
origin.kind
404+
{
405+
return InferenceDiagnosticsData {
406+
name: name.to_string(),
407+
span: Some(origin.span),
408+
kind: UnderspecifiedArgKind::Const { is_parameter: true },
409+
parent: InferenceDiagnosticsParentData::for_def_id(
410+
self.tcx, def_id,
411+
),
412+
};
413+
}
414+
415+
debug_assert!(!origin.span.is_dummy());
416+
let mut s = String::new();
417+
let mut printer =
418+
ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::ValueNS);
419+
if let Some(highlight) = highlight {
420+
printer.region_highlight_mode = highlight;
421+
}
422+
let _ = ct.print(printer);
423+
InferenceDiagnosticsData {
424+
name: s,
401425
span: Some(origin.span),
402-
kind: UnderspecifiedArgKind::Const { is_parameter: true },
403-
parent: InferenceDiagnosticsParentData::for_def_id(self.tcx, def_id),
404-
};
426+
kind: UnderspecifiedArgKind::Const { is_parameter: false },
427+
parent: None,
428+
}
405429
}
406-
407-
debug_assert!(!origin.span.is_dummy());
408-
let mut s = String::new();
409-
let mut printer =
410-
ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::ValueNS);
411-
if let Some(highlight) = highlight {
412-
printer.region_highlight_mode = highlight;
430+
ty::ConstKind::Unevaluated(ty::Unevaluated {
431+
substs_: Some(substs), ..
432+
}) => {
433+
assert!(substs.has_infer_types_or_consts());
434+
435+
// FIXME: We only use the first inference variable we encounter in
436+
// `substs` here, this gives insufficiently informative diagnostics
437+
// in case there are multiple inference variables
438+
for s in substs.iter() {
439+
match s.unpack() {
440+
GenericArgKind::Type(t) => match t.kind() {
441+
ty::Infer(_) => {
442+
return self.extract_inference_diagnostics_data(s, None);
443+
}
444+
_ => {}
445+
},
446+
GenericArgKind::Const(c) => match c.val {
447+
ty::ConstKind::Infer(InferConst::Var(_)) => {
448+
return self.extract_inference_diagnostics_data(s, None);
449+
}
450+
_ => {}
451+
},
452+
_ => {}
453+
}
454+
}
455+
bug!(
456+
"expected an inference variable in substs of unevaluated const {:?}",
457+
ct
458+
);
413459
}
414-
let _ = ct.print(printer);
415-
InferenceDiagnosticsData {
416-
name: s,
417-
span: Some(origin.span),
418-
kind: UnderspecifiedArgKind::Const { is_parameter: false },
419-
parent: None,
460+
_ => {
461+
bug!("unexpect const: {:?}", ct);
420462
}
421-
} else {
422-
bug!("unexpect const: {:?}", ct);
423463
}
424464
}
425465
GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),

src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ fn main() {
2727
//~^ ERROR type annotations needed
2828

2929
_q = foo::<_, 2>(_q);
30+
//~^ ERROR type annotations needed
3031
}

src/test/ui/const-generics/generic_const_exprs/const_eval_resolve_canonical.stderr

+26-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ error[E0282]: type annotations needed
44
LL | let mut _q = Default::default();
55
| ^^^^^^ consider giving `_q` a type
66

7-
error: aborting due to previous error
7+
error[E0283]: type annotations needed
8+
--> $DIR/const_eval_resolve_canonical.rs:29:10
9+
|
10+
LL | _q = foo::<_, 2>(_q);
11+
| ^^^^^^^^^^^ cannot infer type
12+
|
13+
note: multiple `impl`s satisfying `(): Foo<{ N + 1 }>` found
14+
--> $DIR/const_eval_resolve_canonical.rs:8:1
15+
|
16+
LL | impl Foo<0> for () {
17+
| ^^^^^^^^^^^^^^^^^^
18+
...
19+
LL | impl Foo<3> for () {
20+
| ^^^^^^^^^^^^^^^^^^
21+
note: required by a bound in `foo`
22+
--> $DIR/const_eval_resolve_canonical.rs:18:9
23+
|
24+
LL | fn foo<T, const N: usize>(_: T) -> <() as Foo<{ N + 1 }>>::Assoc
25+
| --- required by a bound in this
26+
LL | where
27+
LL | (): Foo<{ N + 1 }>,
28+
| ^^^^^^^^^^^^^^ required by this bound in `foo`
29+
30+
error: aborting due to 2 previous errors
831

9-
For more information about this error, try `rustc --explain E0282`.
32+
Some errors have detailed explanations: E0282, E0283.
33+
For more information about an error, try `rustc --explain E0282`.
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
error[E0283]: type annotations needed
1+
error[E0282]: type annotations needed
22
--> $DIR/issue-83249.rs:19:13
33
|
44
LL | let _ = foo([0; 1]);
55
| - ^^^ cannot infer type for type parameter `T` declared on the function `foo`
66
| |
77
| consider giving this pattern a type
8-
|
9-
= note: cannot satisfy `_: Foo`
10-
note: required by a bound in `foo`
11-
--> $DIR/issue-83249.rs:12:11
12-
|
13-
LL | fn foo<T: Foo>(_: [u8; T::N]) -> T {
14-
| ^^^ required by this bound in `foo`
15-
help: consider specifying the type argument in the function call
16-
|
17-
LL | let _ = foo::<T>([0; 1]);
18-
| +++++
198

209
error: aborting due to previous error
2110

22-
For more information about this error, try `rustc --explain E0283`.
11+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)