Skip to content

Commit

Permalink
Avoid invalid suggestion by checking the snippet in const fn call
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Dec 10, 2019
1 parent 860c7e4 commit 94ab9ec
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
23 changes: 22 additions & 1 deletion src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,28 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
&obligation.cause.code,
) {
let generics = self.tcx.generics_of(*def_id);
if !generics.params.is_empty() {
if !generics.params.is_empty() && !snippet.ends_with('>'){
// FIXME: To avoid spurious suggestions in functions where type arguments
// where already supplied, we check the snippet to make sure it doesn't
// end with a turbofish. Ideally we would have access to a `PathSegment`
// instead. Otherwise we would produce the following output:
//
// error[E0283]: type annotations needed
// --> $DIR/issue-54954.rs:3:24
// |
// LL | const ARR_LEN: usize = Tt::const_val::<[i8; 123]>();
// | ^^^^^^^^^^^^^^^^^^^^^^^^^^
// | |
// | cannot infer type
// | help: consider specifying the type argument
// | in the function call:
// | `Tt::const_val::<[i8; 123]>::<T>`
// ...
// LL | const fn const_val<T: Sized>() -> usize {
// | --------- - required by this bound in `Tt::const_val`
// |
// = note: cannot resolve `_: Tt`

err.span_suggestion(
span,
&format!(
Expand Down
5 changes: 1 addition & 4 deletions src/test/ui/issues/issue-54954.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ error[E0283]: type annotations needed
--> $DIR/issue-54954.rs:3:24
|
LL | const ARR_LEN: usize = Tt::const_val::<[i8; 123]>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| cannot infer type
| help: consider specifying the type argument in the function call: `Tt::const_val::<[i8; 123]>::<T>`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
...
LL | const fn const_val<T: Sized>() -> usize {
| --------- - required by this bound in `Tt::const_val`
Expand Down

0 comments on commit 94ab9ec

Please sign in to comment.