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

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

Closed
rmustacc opened this issue Jul 1, 2020 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rmustacc
Copy link

rmustacc commented Jul 1, 2020

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.

@rmustacc rmustacc added the C-bug Category: This is a bug. label Jul 1, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Jul 1, 2020
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Jul 1, 2020
@estebank
Copy link
Contributor

Current output:

error: struct literal body without path
  --> src/lib.rs:16:45
   |
16 |   static RMOD_MODLINKAGE: IllumosModLinkage = {
   |  _____________________________________________^
17 | |         ml_rev: MODREV_1,
18 | | };
   | |_^
   |
help: you might have forgotten to add the struct literal inside the block
   |
16 ~ static RMOD_MODLINKAGE: IllumosModLinkage = { SomeStruct {
17 |         ml_rev: MODREV_1,
18 ~ } };
   |

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-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

3 participants