Skip to content
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

Weird type error after syntax error #44579

Closed
bluss opened this issue Sep 14, 2017 · 0 comments
Closed

Weird type error after syntax error #44579

bluss opened this issue Sep 14, 2017 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. WG-diagnostics Working group: Diagnostics

Comments

@bluss
Copy link
Member

bluss commented Sep 14, 2017

Steps to Reproduce

Given this code:

use std::env;

pub struct Foo {
    text: String
}

pub fn parse() -> Foo {
    let args: Vec<String> = env::args().collect();
    let text = args[1].clone();
    
    pub Foo { text }    // NB: Syntax error here
}

fn main() { }

Expected Output

The syntax error is diagnosed

Actual Output

  • There are 2 errors, one actual and one nonsensical
  • The nonsensical error uses more visual space (we can't really fault it for that) and it comes last, so it takes all the attention
error: unmatched visibility `pub`
  --> src/main.rs:11:5
   |
11 |     pub Foo { text }
   |     ^^^

error[E0308]: mismatched types
  --> src/main.rs:7:23
   |
7  |   pub fn parse() -> Foo {
   |  _______________________^
8  | |     let args: Vec<String> = env::args().collect();
9  | |     let text = args[1].clone();
10 | |     
11 | |     pub Foo { text }
12 | | }
   | |_^ expected struct `Foo`, found ()
   |
   = note: expected type `Foo`
              found type `()`

I'm sorry to be so harsh on the diagnostics; but we can be better at showing the user relevant diagnostics. (I remember g++ and having to learn to ignore everything after the first error. And those error outputs were long..)

This bug was reported after a discussion in #rust-beginners after a user was struggling with similar code and they came asking about error no. 2. Not about both errors, but about error no. 2.

@bluss bluss added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 14, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Sep 17, 2017
@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Oct 13, 2017
bors added a commit that referenced this issue Dec 18, 2017
Do not emit type errors on recovered blocks

When a parse error occurs on a block, the parser will recover and create
a block with the statements collected until that point. Now a flag
stating that a recovery has been performed in this block is propagated
so that the type checker knows that the type of the block (which will be
identified as `()`) shouldn't be checked against the expectation to
reduce the amount of irrelevant diagnostic errors shown to the user.

Fix #44579.
bors added a commit that referenced this issue Dec 21, 2017
Do not emit type errors on recovered blocks

When a parse error occurs on a block, the parser will recover and create
a block with the statements collected until that point. Now a flag
stating that a recovery has been performed in this block is propagated
so that the type checker knows that the type of the block (which will be
identified as `()`) shouldn't be checked against the expectation to
reduce the amount of irrelevant diagnostic errors shown to the user.

Fix #44579.
bors added a commit that referenced this issue Dec 22, 2017
Do not emit type errors on recovered blocks

When a parse error occurs on a block, the parser will recover and create
a block with the statements collected until that point. Now a flag
stating that a recovery has been performed in this block is propagated
so that the type checker knows that the type of the block (which will be
identified as `()`) shouldn't be checked against the expectation to
reduce the amount of irrelevant diagnostic errors shown to the user.

Fix #44579.
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 C-bug Category: This is a bug. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

3 participants