Skip to content

Commit 94ab9ec

Browse files
committed
Avoid invalid suggestion by checking the snippet in const fn call
1 parent 860c7e4 commit 94ab9ec

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/librustc/traits/error_reporting.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,28 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
19991999
&obligation.cause.code,
20002000
) {
20012001
let generics = self.tcx.generics_of(*def_id);
2002-
if !generics.params.is_empty() {
2002+
if !generics.params.is_empty() && !snippet.ends_with('>'){
2003+
// FIXME: To avoid spurious suggestions in functions where type arguments
2004+
// where already supplied, we check the snippet to make sure it doesn't
2005+
// end with a turbofish. Ideally we would have access to a `PathSegment`
2006+
// instead. Otherwise we would produce the following output:
2007+
//
2008+
// error[E0283]: type annotations needed
2009+
// --> $DIR/issue-54954.rs:3:24
2010+
// |
2011+
// LL | const ARR_LEN: usize = Tt::const_val::<[i8; 123]>();
2012+
// | ^^^^^^^^^^^^^^^^^^^^^^^^^^
2013+
// | |
2014+
// | cannot infer type
2015+
// | help: consider specifying the type argument
2016+
// | in the function call:
2017+
// | `Tt::const_val::<[i8; 123]>::<T>`
2018+
// ...
2019+
// LL | const fn const_val<T: Sized>() -> usize {
2020+
// | --------- - required by this bound in `Tt::const_val`
2021+
// |
2022+
// = note: cannot resolve `_: Tt`
2023+
20032024
err.span_suggestion(
20042025
span,
20052026
&format!(

src/test/ui/issues/issue-54954.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ error[E0283]: type annotations needed
88
--> $DIR/issue-54954.rs:3:24
99
|
1010
LL | const ARR_LEN: usize = Tt::const_val::<[i8; 123]>();
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
| |
13-
| cannot infer type
14-
| help: consider specifying the type argument in the function call: `Tt::const_val::<[i8; 123]>::<T>`
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
1512
...
1613
LL | const fn const_val<T: Sized>() -> usize {
1714
| --------- - required by this bound in `Tt::const_val`

0 commit comments

Comments
 (0)