Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3dbfdb0

Browse files
committed
use the correct span when dealing with inference variables
1 parent 0abb1ab commit 3dbfdb0

File tree

9 files changed

+40
-11
lines changed

9 files changed

+40
-11
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
275275
(s, None, ty.prefix_string(), None, None)
276276
}
277277
GenericArgKind::Const(ct) => {
278-
if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
278+
let span = if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.val {
279279
let origin =
280280
self.inner.borrow_mut().const_unification_table().probe_value(vid).origin;
281281
if let ConstVariableOriginKind::ConstParameterDefinition(name, def_id) =
@@ -308,15 +308,19 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
308308
parent_descr,
309309
);
310310
}
311-
}
311+
312+
Some(origin.span).filter(|s| !s.is_dummy())
313+
} else {
314+
bug!("unexpect const: {:?}", ct);
315+
};
312316

313317
let mut s = String::new();
314-
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
318+
let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::ValueNS);
315319
if let Some(highlight) = highlight {
316320
printer.region_highlight_mode = highlight;
317321
}
318322
let _ = ct.print(printer);
319-
(s, None, "value".into(), None, None)
323+
(s, span, "the constant".into(), None, None)
320324
}
321325
GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
322326
}
@@ -705,7 +709,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
705709
"".to_string()
706710
};
707711

708-
let preposition = if "value" == kind_str { "of" } else { "for" };
712+
let preposition = if "the value" == kind_str { "of" } else { "for" };
709713
// For example: "cannot infer type for type parameter `T`"
710714
format!(
711715
"cannot infer {} {} {} `{}`{}",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/cannot-infer-const-args.rs:12:5
33
|
44
LL | foo();
5-
| ^^^ cannot infer the value for const parameter `X` declared on the function `foo`
5+
| ^^^ cannot infer the value of const parameter `X` declared on the function `foo`
66

77
error: aborting due to previous error
88

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0282]: type annotations needed
22
--> $DIR/cannot-infer-const-args.rs:12:5
33
|
44
LL | foo();
5-
| ^^^ cannot infer the value for const parameter `X` declared on the function `foo`
5+
| ^^^ cannot infer the value of const parameter `X` declared on the function `foo`
66

77
error: aborting due to previous error
88

Lines changed: 16 additions & 0 deletions
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+
}
Lines changed: 9 additions & 0 deletions
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

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

77
error: aborting due to previous error
88

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

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

77
error: aborting due to previous error
88

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

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

77
error: aborting due to previous error
88

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

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

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)