Skip to content

Commit ac8169d

Browse files
authored
Rollup merge of rust-lang#77093 - lcnr:const-generics-infer-warning, r=varkor
merge `need_type_info_err(_const)` I hoped that this would automatically solve rust-lang#76737 but it doesn't quite seem like it fixes rust-lang#77092 r? @varkor
2 parents 31fd0ad + 9a607c0 commit ac8169d

File tree

15 files changed

+254
-155
lines changed

15 files changed

+254
-155
lines changed

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

+174-103
Large diffs are not rendered by default.

compiler/rustc_infer/src/infer/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11631163
}
11641164
GenericParamDefKind::Const { .. } => {
11651165
let origin = ConstVariableOrigin {
1166-
kind: ConstVariableOriginKind::ConstParameterDefinition(param.name),
1166+
kind: ConstVariableOriginKind::ConstParameterDefinition(
1167+
param.name,
1168+
param.def_id,
1169+
),
11671170
span,
11681171
};
11691172
let const_var_id =

compiler/rustc_middle/src/infer/unify_key.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use rustc_data_structures::undo_log::UndoLogs;
44
use rustc_data_structures::unify::{
55
self, EqUnifyValue, InPlace, NoError, UnificationTable, UnifyKey, UnifyValue,
66
};
7+
use rustc_span::def_id::DefId;
78
use rustc_span::symbol::Symbol;
8-
use rustc_span::{Span, DUMMY_SP};
9+
use rustc_span::Span;
910

1011
use std::cmp;
1112
use std::marker::PhantomData;
@@ -124,8 +125,7 @@ pub struct ConstVariableOrigin {
124125
pub enum ConstVariableOriginKind {
125126
MiscVariable,
126127
ConstInference,
127-
// FIXME(const_generics): Consider storing the `DefId` of the param here.
128-
ConstParameterDefinition(Symbol),
128+
ConstParameterDefinition(Symbol, DefId),
129129
SubstitutionPlaceholder,
130130
}
131131

@@ -176,17 +176,17 @@ impl<'tcx> UnifyValue for ConstVarValue<'tcx> {
176176
type Error = (&'tcx ty::Const<'tcx>, &'tcx ty::Const<'tcx>);
177177

178178
fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
179-
let val = match (value1.val, value2.val) {
179+
let (val, span) = match (value1.val, value2.val) {
180180
(ConstVariableValue::Known { .. }, ConstVariableValue::Known { .. }) => {
181181
bug!("equating two const variables, both of which have known values")
182182
}
183183

184184
// If one side is known, prefer that one.
185185
(ConstVariableValue::Known { .. }, ConstVariableValue::Unknown { .. }) => {
186-
Ok(value1.val)
186+
(value1.val, value1.origin.span)
187187
}
188188
(ConstVariableValue::Unknown { .. }, ConstVariableValue::Known { .. }) => {
189-
Ok(value2.val)
189+
(value2.val, value2.origin.span)
190190
}
191191

192192
// If both sides are *unknown*, it hardly matters, does it?
@@ -200,14 +200,14 @@ impl<'tcx> UnifyValue for ConstVarValue<'tcx> {
200200
// universe is the minimum of the two universes, because that is
201201
// the one which contains the fewest names in scope.
202202
let universe = cmp::min(universe1, universe2);
203-
Ok(ConstVariableValue::Unknown { universe })
203+
(ConstVariableValue::Unknown { universe }, value1.origin.span)
204204
}
205-
}?;
205+
};
206206

207207
Ok(ConstVarValue {
208208
origin: ConstVariableOrigin {
209209
kind: ConstVariableOriginKind::ConstInference,
210-
span: DUMMY_SP,
210+
span: span,
211211
},
212212
val,
213213
})

compiler/rustc_mir/src/borrow_check/diagnostics/region_name.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,15 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
396396
) -> Option<RegionNameHighlight> {
397397
let mut highlight = RegionHighlightMode::default();
398398
highlight.highlighting_region_vid(needle_fr, counter);
399-
let type_name = self.infcx.extract_type_name(&ty, Some(highlight)).0;
399+
let type_name =
400+
self.infcx.extract_inference_diagnostics_data(ty.into(), Some(highlight)).name;
400401

401402
debug!(
402403
"highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
403404
type_name, needle_fr
404405
);
405406
if type_name.find(&format!("'{}", counter)).is_some() {
406407
// Only add a label if we can confirm that a region was labelled.
407-
408408
Some(RegionNameHighlight::CannotMatchHirTy(span, type_name))
409409
} else {
410410
None
@@ -646,7 +646,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
646646

647647
let mut highlight = RegionHighlightMode::default();
648648
highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap());
649-
let type_name = self.infcx.extract_type_name(&return_ty, Some(highlight)).0;
649+
let type_name =
650+
self.infcx.extract_inference_diagnostics_data(return_ty.into(), Some(highlight)).name;
650651

651652
let mir_hir_id = tcx.hir().local_def_id_to_hir_id(self.mir_def_id);
652653

@@ -698,7 +699,8 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
698699

699700
let mut highlight = RegionHighlightMode::default();
700701
highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap());
701-
let type_name = self.infcx.extract_type_name(&yield_ty, Some(highlight)).0;
702+
let type_name =
703+
self.infcx.extract_inference_diagnostics_data(yield_ty.into(), Some(highlight)).name;
702704

703705
let mir_hir_id = tcx.hir().local_def_id_to_hir_id(self.mir_def_id);
704706

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc_hir::Node;
2020
use rustc_middle::mir::interpret::ErrorHandled;
2121
use rustc_middle::ty::error::ExpectedFound;
2222
use rustc_middle::ty::fold::TypeFolder;
23-
use rustc_middle::ty::subst::GenericArgKind;
2423
use rustc_middle::ty::{
2524
self, fast_reject, AdtKind, SubtypePredicate, ToPolyTraitRef, ToPredicate, Ty, TyCtxt,
2625
TypeFoldable, WithConstness,
@@ -1513,10 +1512,21 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
15131512
// check upstream for type errors and don't add the obligations to
15141513
// begin with in those cases.
15151514
if self.tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
1516-
self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0282).emit();
1515+
self.emit_inference_failure_err(
1516+
body_id,
1517+
span,
1518+
self_ty.into(),
1519+
ErrorCode::E0282,
1520+
)
1521+
.emit();
15171522
return;
15181523
}
1519-
let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0283);
1524+
let mut err = self.emit_inference_failure_err(
1525+
body_id,
1526+
span,
1527+
self_ty.into(),
1528+
ErrorCode::E0283,
1529+
);
15201530
err.note(&format!("cannot satisfy `{}`", predicate));
15211531
if let ObligationCauseCode::ItemObligation(def_id) = obligation.cause.code {
15221532
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
@@ -1580,17 +1590,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
15801590
return;
15811591
}
15821592

1583-
match arg.unpack() {
1584-
GenericArgKind::Lifetime(lt) => {
1585-
span_bug!(span, "unexpected well formed predicate: {:?}", lt)
1586-
}
1587-
GenericArgKind::Type(ty) => {
1588-
self.need_type_info_err(body_id, span, ty, ErrorCode::E0282)
1589-
}
1590-
GenericArgKind::Const(ct) => {
1591-
self.need_type_info_err_const(body_id, span, ct, ErrorCode::E0282)
1592-
}
1593-
}
1593+
self.emit_inference_failure_err(body_id, span, arg, ErrorCode::E0282)
15941594
}
15951595

15961596
ty::PredicateAtom::Subtype(data) => {
@@ -1601,7 +1601,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
16011601
let SubtypePredicate { a_is_expected: _, a, b } = data;
16021602
// both must be type variables, or the other would've been instantiated
16031603
assert!(a.is_ty_var() && b.is_ty_var());
1604-
self.need_type_info_err(body_id, span, a, ErrorCode::E0282)
1604+
self.emit_inference_failure_err(body_id, span, a.into(), ErrorCode::E0282)
16051605
}
16061606
ty::PredicateAtom::Projection(data) => {
16071607
let trait_ref = ty::Binder::bind(data).to_poly_trait_ref(self.tcx);
@@ -1612,7 +1612,12 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
16121612
}
16131613
if self_ty.needs_infer() && ty.needs_infer() {
16141614
// We do this for the `foo.collect()?` case to produce a suggestion.
1615-
let mut err = self.need_type_info_err(body_id, span, self_ty, ErrorCode::E0284);
1615+
let mut err = self.emit_inference_failure_err(
1616+
body_id,
1617+
span,
1618+
self_ty.into(),
1619+
ErrorCode::E0284,
1620+
);
16161621
err.note(&format!("cannot satisfy `{}`", predicate));
16171622
err
16181623
} else {

compiler/rustc_typeck/src/check/fn_ctxt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2991,7 +2991,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29912991
ty
29922992
} else {
29932993
if !self.is_tainted_by_errors() {
2994-
self.need_type_info_err((**self).body_id, sp, ty, E0282)
2994+
self.emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282)
29952995
.note("type must be known at this point")
29962996
.emit();
29972997
}

compiler/rustc_typeck/src/check/writeback.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -653,18 +653,23 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
653653
fn report_type_error(&self, t: Ty<'tcx>) {
654654
if !self.tcx.sess.has_errors() {
655655
self.infcx
656-
.need_type_info_err(Some(self.body.id()), self.span.to_span(self.tcx), t, E0282)
656+
.emit_inference_failure_err(
657+
Some(self.body.id()),
658+
self.span.to_span(self.tcx),
659+
t.into(),
660+
E0282,
661+
)
657662
.emit();
658663
}
659664
}
660665

661666
fn report_const_error(&self, c: &'tcx ty::Const<'tcx>) {
662667
if !self.tcx.sess.has_errors() {
663668
self.infcx
664-
.need_type_info_err_const(
669+
.emit_inference_failure_err(
665670
Some(self.body.id()),
666671
self.span.to_span(self.tcx),
667-
c,
672+
c.into(),
668673
E0282,
669674
)
670675
.emit();

src/test/ui/const-generics/infer/cannot-infer-const-args.full.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/cannot-infer-const-args.rs:12:5
33
|
44
LL | foo();
5-
| ^^^
6-
|
7-
= note: cannot infer the value of the const parameter `X`
5+
| ^^^ cannot infer the value of const parameter `X` declared on the function `foo`
86

97
error: aborting due to previous error
108

src/test/ui/const-generics/infer/cannot-infer-const-args.min.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/cannot-infer-const-args.rs:12:5
33
|
44
LL | foo();
5-
| ^^^
6-
|
7-
= note: cannot infer the value of the const parameter `X`
5+
| ^^^ cannot infer the value of const parameter `X` declared on the function `foo`
86

97
error: aborting due to previous error
108

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(min_const_generics)]
2+
3+
use std::convert::TryInto;
4+
5+
fn take_array_from_mut<T, const N: usize>(data: &mut [T], start: usize) -> &mut [T; N] {
6+
(&mut data[start .. start + N]).try_into().unwrap()
7+
}
8+
9+
fn main() {
10+
let mut arr = [0, 1, 2, 3, 4, 5, 6, 7, 8];
11+
12+
for i in 1 .. 4 {
13+
println!("{:?}", take_array_from_mut(&mut arr, i));
14+
//~^ ERROR type annotations needed
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/issue-77092.rs:13:26
3+
|
4+
LL | println!("{:?}", take_array_from_mut(&mut arr, i));
5+
| ^^^^^^^^^^^^^^^^^^^ cannot infer the value of the constant `{_: usize}`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0282`.

src/test/ui/const-generics/infer/method-chain.full.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/method-chain.rs:21:33
33
|
44
LL | Foo.bar().bar().bar().bar().baz();
5-
| ^^^
6-
|
7-
= note: cannot infer the value of the const parameter `N`
5+
| ^^^ cannot infer the value of const parameter `N` declared on the associated function `baz`
86

97
error: aborting due to previous error
108

src/test/ui/const-generics/infer/method-chain.min.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/method-chain.rs:21:33
33
|
44
LL | Foo.bar().bar().bar().bar().baz();
5-
| ^^^
6-
|
7-
= note: cannot infer the value of the const parameter `N`
5+
| ^^^ cannot infer the value of const parameter `N` declared on the associated function `baz`
86

97
error: aborting due to previous error
108

src/test/ui/const-generics/infer/uninferred-consts.full.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/uninferred-consts.rs:14:9
33
|
44
LL | Foo.foo();
5-
| ^^^
6-
|
7-
= note: cannot infer the value of the const parameter `N`
5+
| ^^^ cannot infer the value of const parameter `N` declared on the associated function `foo`
86

97
error: aborting due to previous error
108

src/test/ui/const-generics/infer/uninferred-consts.min.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/uninferred-consts.rs:14:9
33
|
44
LL | Foo.foo();
5-
| ^^^
6-
|
7-
= note: cannot infer the value of the const parameter `N`
5+
| ^^^ cannot infer the value of const parameter `N` declared on the associated function `foo`
86

97
error: aborting due to previous error
108

0 commit comments

Comments
 (0)