Skip to content

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

Closed
@ranweiler

Description

@ranweiler

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions