From a4e20cf765364221946200c8e7f96c2041f827a5 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Tue, 22 Dec 2020 23:06:19 +0000 Subject: Shadowing rules are done as part of name resolution. When a new name is defined the name resolver freezes the previous declartion such that all new references point to the latest decl. This patch fixes a crash when we shadow and get a type mismatch and the combination of types fails. See the failure test case for the crash. --- gcc/rust/typecheck/rust-tyty-resolver.h | 10 ++++++++-- gcc/testsuite/rust.test/compilable/shadow1.rs | 5 +++++ gcc/testsuite/rust.test/fail_compilation/shadow1.rs | 6 ++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/rust.test/compilable/shadow1.rs create mode 100644 gcc/testsuite/rust.test/fail_compilation/shadow1.rs (limited to 'gcc') diff --git a/gcc/rust/typecheck/rust-tyty-resolver.h b/gcc/rust/typecheck/rust-tyty-resolver.h index 1ee533e..b971e66 100644 --- a/gcc/rust/typecheck/rust-tyty-resolver.h +++ b/gcc/rust/typecheck/rust-tyty-resolver.h @@ -87,12 +87,18 @@ public: auto resolved_tyty = resolved_type; for (auto it : gathered_types) - resolved_tyty = resolved_tyty->combine (it); + { + auto combined = resolved_tyty->combine (it); + if (combined == nullptr) + break; + + resolved_tyty = combined; + } // something is not inferred we need to look at all references now if (resolved_tyty == nullptr || resolved_tyty->is_unit ()) { - rust_error_at (decl->get_locus_slow (), "failed to resolve type"); + rust_fatal_error (decl->get_locus_slow (), "failed to resolve type"); return false; } diff --git a/gcc/testsuite/rust.test/compilable/shadow1.rs b/gcc/testsuite/rust.test/compilable/shadow1.rs new file mode 100644 index 0000000..3cb1ec5 --- /dev/null +++ b/gcc/testsuite/rust.test/compilable/shadow1.rs @@ -0,0 +1,5 @@ +fn main() { + let mut x = 5; + let mut x; + x = true; +} diff --git a/gcc/testsuite/rust.test/fail_compilation/shadow1.rs b/gcc/testsuite/rust.test/fail_compilation/shadow1.rs new file mode 100644 index 0000000..18c5c58 --- /dev/null +++ b/gcc/testsuite/rust.test/fail_compilation/shadow1.rs @@ -0,0 +1,6 @@ +fn main() { + let mut x = 5; + let mut x; + x = true; + x = x + 2; +} -- cgit v1.1