From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id 6FAD23858012; Tue, 16 Jan 2024 18:08:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6FAD23858012 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705428481; bh=TW1L+ZOowJt4nlQrqwwBbZrDhpSZiw7njWnjfSxMprA=; h=From:To:Subject:Date:From; b=Iq64dPodT/GNcHchNd+Rc7+4nlaRfRpgprV4j9l1S24jhWf+Cq7GMLpyNIh3LsItg /FmAGDocStxrKqbOJc8OostoknIq1fntcE1II8qpLx/anJh6WwpaynXIrMY+NNSAFO Tg//5IyR3/jSXFMBCuw60ICtLV4Or/3LoPeSqsNY= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-7927] gccrs: [E0164] Neither tuple struct nor tuple variant used as a pattern X-Act-Checkin: gcc X-Git-Author: Muhammad Mahad X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 87ccd32da00e82584215c759e80bd092003c9efb X-Git-Newrev: cdddf7baff74af1542788868d1cfab6dc435f2ef Message-Id: <20240116180801.6FAD23858012@sourceware.org> Date: Tue, 16 Jan 2024 18:08:01 +0000 (GMT) List-Id: https://gcc.gnu.org/g:cdddf7baff74af1542788868d1cfab6dc435f2ef commit r14-7927-gcdddf7baff74af1542788868d1cfab6dc435f2ef Author: Muhammad Mahad Date: Wed Aug 16 17:40:09 2023 +0500 gccrs: [E0164] Neither tuple struct nor tuple variant used as a pattern Checking if pattern has items, before returing solves ICE. Added error code and rich location. Also, fixes https://github.com/Rust-GCC/gccrs/issues/2430 gcc/rust/ChangeLog: * ast/rust-pattern.h: No need of assertion, we are handling it. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Added check which emits error instead of using assertion. * typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Added rich location and error code. gcc/testsuite/ChangeLog: * rust/compile/match5.rs: Updated comment for dejagnu. * rust/compile/pattern-struct.rs: New test for ICE. Signed-off-by: Muhammad Mahad Diff: --- gcc/rust/ast/rust-pattern.h | 7 +------ gcc/rust/resolve/rust-early-name-resolver.cc | 10 ++++++++++ gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 6 ++++-- gcc/testsuite/rust/compile/match5.rs | 2 +- gcc/testsuite/rust/compile/pattern-struct.rs | 18 ++++++++++++++++++ 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/gcc/rust/ast/rust-pattern.h b/gcc/rust/ast/rust-pattern.h index 95655f5c097..18fe9995790 100644 --- a/gcc/rust/ast/rust-pattern.h +++ b/gcc/rust/ast/rust-pattern.h @@ -1176,12 +1176,7 @@ public: void accept_vis (ASTVisitor &vis) override; - // TODO: seems kinda dodgy. Think of better way. - std::unique_ptr &get_items () - { - rust_assert (has_items ()); - return items; - } + std::unique_ptr &get_items () { return items; } PathInExpression &get_path () { return path; } const PathInExpression &get_path () const { return path; } diff --git a/gcc/rust/resolve/rust-early-name-resolver.cc b/gcc/rust/resolve/rust-early-name-resolver.cc index 47d4e1b7bd1..5b701f5c0c1 100644 --- a/gcc/rust/resolve/rust-early-name-resolver.cc +++ b/gcc/rust/resolve/rust-early-name-resolver.cc @@ -1105,6 +1105,16 @@ EarlyNameResolver::visit (AST::TupleStructItemsRange &tuple_items) void EarlyNameResolver::visit (AST::TupleStructPattern &pattern) { + if (!pattern.has_items ()) + { + rich_location rich_locus (line_table, pattern.get_locus ()); + rich_locus.add_fixit_replace ( + "function calls are not allowed in patterns"); + rust_error_at ( + rich_locus, ErrorCode::E0164, + "expected tuple struct or tuple variant, found associated function"); + return; + } pattern.get_items ()->accept_vis (*this); } diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc index 97370454bbd..18e9b3442ac 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc @@ -76,14 +76,16 @@ TypeCheckPattern::visit (HIR::TupleStructPattern &pattern) } // error[E0532]: expected tuple struct or tuple variant, found struct variant - // `Foo::D` + // `Foo::D`, E0532 by rustc 1.49.0 , E0164 by rustc 1.71.0 if (variant->get_variant_type () != TyTy::VariantDef::VariantType::TUPLE) { std::string variant_type = TyTy::VariantDef::variant_type_string (variant->get_variant_type ()); + rich_location rich_locus (line_table, pattern.get_locus ()); + rich_locus.add_fixit_replace ("not a tuple struct or tuple variant"); rust_error_at ( - pattern.get_locus (), + rich_locus, ErrorCode::E0164, "expected tuple struct or tuple variant, found %s variant %<%s::%s%>", variant_type.c_str (), adt->get_name ().c_str (), variant->get_identifier ().c_str ()); diff --git a/gcc/testsuite/rust/compile/match5.rs b/gcc/testsuite/rust/compile/match5.rs index a5f934d6aeb..6741ee8664f 100644 --- a/gcc/testsuite/rust/compile/match5.rs +++ b/gcc/testsuite/rust/compile/match5.rs @@ -10,6 +10,6 @@ fn inspect(f: Foo) { Foo::A => {} Foo::B => {} Foo::C(a) => {} - Foo::D(x, y) => {} // { dg-error "expected tuple struct or tuple variant, found struct variant 'Foo::D'" } + Foo::D(x, y) => {} // { dg-error "expected tuple struct or tuple variant, found struct variant .Foo::D." } } } diff --git a/gcc/testsuite/rust/compile/pattern-struct.rs b/gcc/testsuite/rust/compile/pattern-struct.rs new file mode 100644 index 00000000000..17275098cd2 --- /dev/null +++ b/gcc/testsuite/rust/compile/pattern-struct.rs @@ -0,0 +1,18 @@ +fn main() { + enum A { + B, + C, + } + + impl A { + fn new() {} + } + + fn bar(foo: A) { + match foo { + A::new() => (), + // { dg-error "expected tuple struct or tuple variant, found associated function" "" { target *-*-* } .-1 } + _ => {} + } + } +}