Skip to content

missing struct declaration type leads to confusing type ascription diagnosticΒ #73941

Closed
@rmustacc

Description

@rmustacc

While trying to get started with rust, I was attempting to declare a global instance of a struct for various reasons (integrating into a device driver framework). This was based on the nightly toolchain because of some other nightly features. Having written a lot of C, but not a lot of rust, I mistakenly declared a structure leaving out the type on the right hand side of the equals sign. This led to a relatively confusing message about type ascription. Here's a reduced version of the code that generates this as of version rustc 1.46.0-nightly (16957bd4d 2020-06-30) though I first encountered this on rustc 1.44.0-nightly (b77aefb76 2020-04-14).

#![crate_type="staticlib"]
#![feature(extern_types)]
#![feature(lang_items)]
#![no_std]

use core::panic::PanicInfo;

const MODREV_1: i32 = 1;

#[repr(C)]
struct IllumosModLinkage {
    ml_rev: i32,
}

#[no_mangle]
static RMOD_MODLINKAGE: IllumosModLinkage = {
        ml_rev: MODREV_1,
};

#[lang = "eh_personality"]
extern fn eh_personality() {}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

When building this with a nightly rustc you receive the following error message:

$ rustc example.rs 
error: expected one of `!`, `(`, `.`, `::`, `;`, `<`, `?`, or `}`, found `,`
  --> example.rs:17:25
   |
17 |         ml_rev: MODREV_1,
   |               -         ^ expected one of 8 possible tokens
   |               |
   |               tried to parse a type due to this type ascription
   |
   = note: `#![feature(type_ascription)]` lets you annotate an expression with a type: `<expr>: <type>`
   = note: see issue #23416 <https://github.com/rust-lang/rust/issues/23416> for more information

error: aborting due to previous error

While I now understand that this was trying to use type ascription incidentally, this sent me initially down the path of trying to figure out what type ascription was and a number of wrong paths. I know at one point I did try to add the feature there, as it seems to be suggesting that, but that didn't change anything. Upon better understanding the problem and what I did wrong, I wouldn't expect adding the feature to have changed anything, but when getting started these messages are usually pretty helpful in indicating what to change or what's going on which is what led me to try it.

If there's some way to indicate that someone might have left a type declaration out in this case, that might be useful and help save someone else who's learning some time in the future.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.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

    No branches or pull requests

    Issue actions