|
1 | 1 | use crate::infer::type_variable::TypeVariableOriginKind;
|
2 | 2 | use crate::infer::InferCtxt;
|
| 3 | +use crate::rustc_middle::ty::TypeFoldable; |
3 | 4 | use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
|
4 | 5 | use rustc_hir as hir;
|
5 | 6 | use rustc_hir::def::{DefKind, Namespace};
|
@@ -390,36 +391,75 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
390 | 391 | }
|
391 | 392 | }
|
392 | 393 | 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, |
401 | 425 | 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 | + } |
405 | 429 | }
|
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 | + ); |
413 | 459 | }
|
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); |
420 | 462 | }
|
421 |
| - } else { |
422 |
| - bug!("unexpect const: {:?}", ct); |
423 | 463 | }
|
424 | 464 | }
|
425 | 465 | GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
|
|
0 commit comments