Description
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.