Skip to content

diagnostic about typoed path separator could be better #84566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
comex opened this issue Apr 25, 2021 · 3 comments
Closed

diagnostic about typoed path separator could be better #84566

comex opened this issue Apr 25, 2021 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. F-type_ascription `#![feature(type_ascription)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@comex
Copy link
Contributor

comex commented Apr 25, 2021

Given this code, where a path separator is typoed as a single instead of double colon:

struct Foo { a: () }

fn main() {
    Foo:new();        
}

the compiler currently outputs:

error[E0423]: expected value, found struct `Foo`
 --> src/main.rs:4:5
  |
1 | struct Foo { a: () }
  | -------------------- `Foo` defined here
...
4 |     Foo:new();        
  |     ^^^ help: use struct literal syntax instead: `Foo { a: val }`

error[E0412]: cannot find type `new` in this scope
 --> src/main.rs:4:9
  |
4 |     Foo:new();        
  |        -^^^ not found in this scope
  |        |
  |        help: maybe you meant to write a path separator here: `::`

This output is quite decent already: the second diagnostic correctly identifies that I meant to write a path separator and suggests a fix. However, before I read the second diagnostic, I naturally read the first diagnostic, which is entirely unhelpful. It would be better if the suggestion could be on the first diagnostic, or if the diagnostics could be combined in some way.

@comex comex added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 25, 2021
@estebank estebank added F-type_ascription `#![feature(type_ascription)]` D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. A-resolve Area: Name/path resolution done by `rustc_resolve` specifically labels Apr 27, 2021
@nsunderland1
Copy link
Contributor

Another example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=cb829e88ed7b85f4d0bc0b6fc2b6f388

#[allow(unused)]
use tokio;
#[allow(unused)]
use std::time::Duration;

fn main() {
    tokio::time::sleep(Duration:from_millis(200))
}

The full output is this:

Compiling playground v0.0.1 (/playground)
error: invalid `struct` delimiters or `fn` call arguments
 [--> src/main.rs:7:5
](https://play.rust-lang.org/#)  |
7 |     tokio::time::sleep(Duration:from_millis(200))
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
help: if `tokio::time::sleep` is a struct, use braces as delimiters
  |
7 |     tokio::time::sleep { Duration:from_millis(200) }
  |                        ~                           ~
help: if `tokio::time::sleep` is a function, use the arguments directly
  |
7 -     tokio::time::sleep(Duration:from_millis(200))
7 +     tokio::time::sleep(from_millis(200))
  | 

error: could not compile `playground` due to previous error

This case doesn't suggest fixing the path separator at all, which is super confusing.

@fmease
Copy link
Member

fmease commented Dec 28, 2024

Since the removal of the original type ascription syntax, the compiler output is:

error: path separator must be a double colon
 --> src/main.rs:4:8
  |
4 |     Foo:new();        
  |        ^
  |
help: use a double colon instead
  |
4 |     Foo::new();        
  |         +

error[E0599]: no function or associated item named `new` found for struct `Foo` in the current scope
 --> src/main.rs:4:9
  |
1 | struct Foo { a: () }
  | ---------- function or associated item `new` not found for this struct
...
4 |     Foo:new();        
  |         ^^^ function or associated item not found in `Foo`

Which I deem perfectly acceptable.

Similarly for #84566 (comment):

error: path separator must be a double colon
 --> src/main.rs:7:32
  |
7 |     tokio::time::sleep(Duration:from_millis(200))
  |                                ^
  |
help: use a double colon instead
  |
7 |     tokio::time::sleep(Duration::from_millis(200))
  |                                 +

error[E0308]: mismatched types
 --> src/main.rs:7:5
  |
6 | fn main() {
  |          - expected `()` because of default return type
7 |     tokio::time::sleep(Duration:from_millis(200))
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: consider using a semicolon here: `;`
  |     |
  |     expected `()`, found `Sleep`

@fmease
Copy link
Member

fmease commented Dec 28, 2024

Since we have a (regression) test for this already like tests/ui/suggestions/type-ascription-instead-of-path.rs I'll just close this as completed!

@fmease fmease closed this as completed Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name/path resolution done by `rustc_resolve` specifically D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. F-type_ascription `#![feature(type_ascription)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants