Skip to content

Unhelpful error message when missing the final path segment for a function call #120892

@Jarcho

Description

@Jarcho
Contributor

Given an invalid call such as:

fn main() {
  foo::("foo");
}

The following diagnostic is issued:

error: expected type, found `"foo"`
 --> src/main.rs:2:9
  |
2 |   foo::("foo");
  |         ^^^^^ expected type

This gets much worse on multiline calls:

fn main() {
  foo::(
    bar(x, y, z),
    bar(x, y, z),
    bar(x, y, z),
    bar(x, y, z),
    bar(x, y, z),
    bar(x, y, z),
    bar(x, y, z),
    baz("test"),
  ),
}

Which just prints out a diagnostic nowhere near the error:

error: expected type, found `"test"`
  --> src/main.rs:10:13
   |
10 |         baz("test"),
   |             ^^^^^^ expected type

Ideally this should mention something about either missing a missing turbofish (e.g. foo::<..>("foo")), or a missing identifier (e.g. foo::bar("foo")). This would at least get the error pointing to the correct spot.


Latest playground nightly (2024-02-09)

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Feb 10, 2024
added
A-diagnosticsArea: Messages for errors, warnings, and lints
A-parserArea: The lexing & parsing of Rust source code to an AST
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
D-confusingDiagnostics: Confusing error or lint that should be reworked.
D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.
and removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
A-parserArea: The lexing & parsing of Rust source code to an AST
on Feb 10, 2024
fmease

fmease commented on Feb 10, 2024

@fmease
Member

Oh, wow, it didn't even occur to me until now that the expression Option::(i32)::None::() is syntactically valid >.<

Edit: Wow, even let _ = Fn::() -> Option<i32>; is syntactically well-formed.

fmease

fmease commented on Feb 10, 2024

@fmease
Member

This should pretty easy to fix, basically if we fail to parse a type here

let (inputs, _) = self.parse_paren_comma_seq(|p| p.parse_ty())?;

we should first of all attach a label to the ::( saying something akin to while parsing this list of parenthesized type arguments starting here and we should suggest removing the trailing path separator if the PathStyle is Expr, the parenthesized generics aren't followed by :: or -> and if the list of non-types we encountered can be successfully parsed as a list of expressions (using a snapshot parser).

Furthermore, if the PathStyle is Pat we can try to reparse the list as a list of patterns and provide the same structured suggestion.

I will gladly mentor & review any PR for this.

added
E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
on Feb 10, 2024
SummerGram

SummerGram commented on Feb 12, 2024

@SummerGram

@rustbot claim

self-assigned this
on Mar 2, 2024

11 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-confusingDiagnostics: Confusing error or lint that should be reworked.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @Jarcho@fmease@wutchzone@SummerGram@rustbot

    Issue actions

      Unhelpful error message when missing the final path segment for a function call · Issue #120892 · rust-lang/rust