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

[E0026] Non-Existent Field Extraction in Struct Pattern #2326

Merged
merged 1 commit into from
Jun 27, 2023

Conversation

MahadMuhammad
Copy link
Contributor

Non-Existent Field Extraction in Struct Pattern
variant Foo::D does not have a field named b

Code Tested from E0026

// https://doc.rust-lang.org/error_codes/E0026.html
fn main() {
    struct Thing {
        x: u32,
        y: u32,
    }

    let thing = Thing { x: 0, y: 0 };

    match thing {
        Thing { x, z } => {} // error: `Thing::z` field doesn't exist
    }
}

Output:

mahad@linux:~/Desktop/mahad/gccrs-build$ gcc/crab1 ../mahad-testsuite/E0026.rs -frust-incomplete-and-experimental-compiler-do-not-use
../mahad-testsuite/E0026.rs:11:20: error: variant Thing does not have a field named z [E0026]
   11 |         Thing { x, z } => {} // error: `Thing::z` field doesn't exist
      |                    ^
../mahad-testsuite/E0026.rs:11:9: error: pattern does not mention fields y
   11 |         Thing { x, z } => {} // error: `Thing::z` field doesn't exist
      |         ^~~~~

Analyzing compilation unit

Time variable                                   usr           sys          wall           GGC
 TOTAL                              :   0.00          0.00          0.00          146k
Extra diagnostic checks enabled; compiler may run slowly.
Configure with --enable-checking=release to disable checks.

Running testcases:

/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/match3.rs:13:18: error: variant D does not have a field named z [E0026]
/home/mahad/Desktop/mahad/gccrs/gcc/testsuite/rust/compile/match3.rs:13:9: error: pattern does not mention fields x, y
compiler exited with status 1

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): called rust_error_at

Non-Existent Field Extraction in Struct Pattern
variant `Foo::D` does not have a field named `b`

gcc/rust/ChangeLog:
	* typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): called rust_error_at

Signed-off-by: Muhammad Mahad <mahadtxt@gmail.com>
Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM nice work

@philberty philberty added this pull request to the merge queue Jun 27, 2023
@philberty philberty self-assigned this Jun 27, 2023
Merged via the queue into Rust-GCC:master with commit f56fd7e Jun 27, 2023
9 checks passed
@@ -204,7 +204,7 @@ TypeCheckPattern::visit (HIR::StructPattern &pattern)
if (!variant->lookup_field (ident.get_identifier (), &field,
nullptr))
{
rust_error_at (ident.get_locus (),
rust_error_at (ident.get_locus (), ErrorCode ("E0026"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the lines you modified have the exact same error message and pattern. I think it would be worth opening an issue to factor them into one function (emit_invalid_field_error or w/ever) in this file, and it's something you can work on if you like. This will make it easier for you to rework the Error emitting code once we get to that point in your GSoC project @MahadMuhammad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diagnostic diagnostic static analysis enhancement
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants