Skip to content

Add a compiler error number, explanation for attempts to initialize a struct with nonexistent field #34914

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

Closed
ranweiler opened this issue Jul 19, 2016 · 2 comments
Assignees

Comments

@ranweiler
Copy link
Contributor

Summary

When a struct literal is initialized with a field name that does not belong to the struct, the result is a compiler error. This error does not have a code which can be looked up with --explain.

Repro

To reproduce, attempt to compile the following:

struct S {}

fn f() {
    let s = S { x: 0 };
}

I would expect an error like:

src/main.rs:12:17: 12:18 error: structure `S` has no field named `x` [EXXXX]
src/main.rs:12     let s = S { x: 0 };
src/main.rs:12:20: 12:24 help: run `rustc --explain EXXXX` to see a detailed explanation

...where XXXX is an appropriate error code. Then, when I invoke rustc --explain EXXXX, I expect to see a verbose explanation of the error, which should also appear in the Rust Compiler Error Index.

Instead, the error has only a brief description with no code, and looks like:

src/main.rs:12:17: 12:18 error: structure `S` has no field named `x`
src/main.rs:12     let s = S { x: 0 };

Meta

The existing compiler error is located in the Rust source in librustc_typeck/check/mod.rs.

rustc version info:

rustc 1.10.0 (cfcb716cf 2016-07-03)
binary: rustc
commit-hash: cfcb716cf0961a7e3a4eceac828d94805cf8140b
commit-date: 2016-07-03
host: x86_64-apple-darwin
release: 1.10.0
@GuillaumeGomez
Copy link
Member

Ok, sounds like a nice idea. If no one is against it, I'll start in the next days to come.

@Mark-Simulacrum
Copy link
Member

This has since been done, and there are numerous compile-fail tests already which mention this. Closing.

error[E0560]: struct `S` has no field named `x`
 --> test.rs:4:17
  |
4 |     let s = S { x: 0 };
  |                 ^^ `S` does not have this field

error: aborting due to previous error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants