Skip to content

Weird type error after syntax error #44579

Closed
@bluss

Description

@bluss

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions